Add support for different spinners

This commit is contained in:
Patrik Svensson
2020-12-05 10:52:51 +01:00
committed by Patrik Svensson
parent 3c504155bc
commit cbed41e637
20 changed files with 3618 additions and 45 deletions

View File

@ -0,0 +1,22 @@
##########################################################
# Script that generates progress spinners.
##########################################################
$Output = Join-Path $PSScriptRoot "Temp"
$Source = Join-Path $PSScriptRoot "/../../src/Spectre.Console"
if(!(Test-Path $Output -PathType Container)) {
New-Item -ItemType Directory -Path $Output | Out-Null
}
# Generate the files
Push-Location Generator
&dotnet run -- spinners "$Output" --input $Output
if(!$?) {
Pop-Location
Throw "An error occured when generating code."
}
Pop-Location
# Copy the files to the correct location
Copy-Item (Join-Path "$Output" "ProgressSpinner.Generated.cs") -Destination "$Source/Progress/ProgressSpinner.Generated.cs"

View File

@ -7,7 +7,7 @@ using Spectre.IO;
namespace Generator.Commands
{
public sealed class ColorGeneratorCommand : Command<GeneratorCommandSettings>
public sealed class ColorGeneratorCommand : Command<ColorGeneratorCommand.Settings>
{
private readonly IFileSystem _fileSystem;
@ -16,7 +16,13 @@ namespace Generator.Commands
_fileSystem = new FileSystem();
}
public override int Execute(CommandContext context, GeneratorCommandSettings settings)
public sealed class Settings : GeneratorSettings
{
[CommandOption("-i|--input <PATH>")]
public string Input { get; set; }
}
public override int Execute(CommandContext context, Settings settings)
{
var templates = new FilePath[]
{
@ -50,13 +56,4 @@ namespace Generator.Commands
return 0;
}
}
public sealed class GeneratorCommandSettings : CommandSettings
{
[CommandArgument(0, "<OUTPUT>")]
public string Output { get; set; }
[CommandOption("-i|--input <PATH>")]
public string Input { get; set; }
}
}

View File

@ -15,7 +15,7 @@ using SpectreEnvironment = Spectre.IO.Environment;
namespace Generator.Commands
{
public sealed class EmojiGeneratorCommand : AsyncCommand<GeneratorCommandSettings>
public sealed class EmojiGeneratorCommand : AsyncCommand<EmojiGeneratorCommand.Settings>
{
private readonly IFileSystem _fileSystem;
private readonly IEnvironment _environment;
@ -24,9 +24,15 @@ namespace Generator.Commands
private readonly Dictionary<string, string> _templates = new Dictionary<string, string>
{
{ "Templates/Emoji.Generated.template", "Emoji.Generated.cs" },
{ "Templates/Emoji.Json.template", "emojis.json" },
{ "Templates/Emoji.Json.template", "emojis.json" }, // For documentation
};
public sealed class Settings : GeneratorSettings
{
[CommandOption("-i|--input <PATH>")]
public string Input { get; set; }
}
public EmojiGeneratorCommand()
{
_fileSystem = new FileSystem();
@ -34,7 +40,7 @@ namespace Generator.Commands
_parser = new HtmlParser();
}
public override async Task<int> ExecuteAsync(CommandContext context, GeneratorCommandSettings settings)
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{
var output = new DirectoryPath(settings.Output);
if (!_fileSystem.Directory.Exists(settings.Output))
@ -60,7 +66,7 @@ namespace Generator.Commands
return 0;
}
private async Task<Stream> FetchEmojis(GeneratorCommandSettings settings)
private async Task<Stream> FetchEmojis(Settings settings)
{
var input = string.IsNullOrEmpty(settings.Input)
? _environment.WorkingDirectory

View File

@ -0,0 +1,10 @@
using Spectre.Cli;
namespace Generator.Commands
{
public class GeneratorSettings : CommandSettings
{
[CommandArgument(0, "<OUTPUT>")]
public string Output { get; set; }
}
}

View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.IO;
using Generator.Models;
using Scriban;
using Spectre.Cli;
using Spectre.IO;
namespace Generator.Commands
{
public sealed class SpinnerGeneratorCommand : Command<GeneratorSettings>
{
private readonly IFileSystem _fileSystem;
public SpinnerGeneratorCommand()
{
_fileSystem = new FileSystem();
}
public override int Execute(CommandContext context, GeneratorSettings settings)
{
// Read the spinner model.
var spinners = new List<Spinner>();
spinners.AddRange(Spinner.Parse(File.ReadAllText("Data/spinners.json")));
spinners.AddRange(Spinner.Parse(File.ReadAllText("Data/spinners_default.json")));
var output = new DirectoryPath(settings.Output);
if (!_fileSystem.Directory.Exists(settings.Output))
{
_fileSystem.Directory.Create(settings.Output);
}
// Parse the Scriban template.
var templatePath = new FilePath("Templates/ProgressSpinner.Generated.template");
var template = Template.Parse(File.ReadAllText(templatePath.FullPath));
// Render the template with the model.
var result = template.Render(new { Spinners = spinners });
// Write output to file
var file = output.CombineWithFilePath(templatePath.GetFilename().ChangeExtension(".cs"));
File.WriteAllText(file.FullPath, result);
return 0;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,30 @@
{
"Default": {
"interval": 100,
"unicode": true,
"frames": [
"⣷",
"⣯",
"⣟",
"⡿",
"⢿",
"⣻",
"⣽",
"⣾"
]
},
"Ascii": {
"interval": 100,
"unicode": true,
"frames": [
"-",
"\\",
"|",
"/",
"-",
"\\",
"|",
"/"
]
}
}

View File

@ -15,6 +15,9 @@
<None Update="Data\colors.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\spinners.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\ColorTable.Generated.template">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
@ -24,6 +27,9 @@
<None Update="Templates\ColorPalette.Generated.template">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\ProgressSpinner.Generated.template">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\Emoji.Json.template">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

View File

@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Linq;
using Humanizer;
using Newtonsoft.Json;
namespace Generator.Models
{
public sealed class Spinner
{
public string Name { get; set; }
public string NormalizedName { get; set; }
public int Interval { get; set; }
public bool Unicode { get; set; }
public List<string> Frames { get; set; }
public static IEnumerable<Spinner> Parse(string json)
{
var data = JsonConvert.DeserializeObject<Dictionary<string, Spinner>>(json);
foreach (var item in data)
{
item.Value.Name = item.Key;
item.Value.NormalizedName = item.Value.Name.Pascalize();
var frames = item.Value.Frames;
item.Value.Frames = frames.Select(f => f.Replace("\\", "\\\\")).ToList();
}
return data.Values;
}
}
}

View File

@ -12,6 +12,7 @@ namespace Generator
{
config.AddCommand<ColorGeneratorCommand>("colors");
config.AddCommand<EmojiGeneratorCommand>("emoji");
config.AddCommand<SpinnerGeneratorCommand>("spinners");
});
return app.Run(args);

View File

@ -2,6 +2,7 @@
// <auto-generated>
// This code was generated by a tool.
// Generated {{ date.now | date.to_string `%F %R` }}
// Generated from https://github.com/sindresorhus/cli-spinners/blob/master/spinners.json
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.

View File

@ -0,0 +1,45 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Generated {{ date.now | date.to_string `%F %R` }}
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace Spectre.Console
{
public abstract partial class ProgressSpinner
{
{{~ for spinner in spinners ~}}
private sealed class {{ spinner.normalized_name }}Spinner : ProgressSpinner
{
public override TimeSpan Interval => TimeSpan.FromMilliseconds({{ spinner.interval }});
public override bool IsUnicode => {{ spinner.unicode }};
public override IReadOnlyList<string> Frames => new List<string>
{
{{~ for frame in spinner.frames ~}}
"{{ frame }}",
{{~ end ~}}
};
}
{{~ end ~}}
/// <summary>
/// Contains all predefined spinners.
/// </summary>
public static class Known
{
{{~ for spinner in spinners ~}}
/// <summary>
/// Gets the "{{ spinner.name }}" spinner.
/// </summary>
public static ProgressSpinner {{ spinner.normalized_name }} { get; } = new {{ spinner.normalized_name }}Spinner();
{{~ end ~}}
}
}
}