Restructure and update example image

This commit is contained in:
Patrik Svensson
2020-08-25 11:19:58 +02:00
parent 695327ec72
commit c111c7d463
20 changed files with 3 additions and 2 deletions

View File

@ -0,0 +1,59 @@
using System;
using System.IO;
using Generator.Models;
using Scriban;
using Spectre.Cli;
using Spectre.IO;
namespace Generator.Commands
{
public sealed class ColorGeneratorCommand : Command<GeneratorCommandSettings>
{
private readonly IFileSystem _fileSystem;
public ColorGeneratorCommand()
{
_fileSystem = new FileSystem();
}
public override int Execute(CommandContext context, GeneratorCommandSettings settings)
{
var templates = new FilePath[]
{
"Templates/ColorPalette.Generated.template",
"Templates/Color.Generated.template",
"Templates/ColorTable.Generated.template"
};
// Read the color model.
var model = Color.Parse(File.ReadAllText("Data/colors.json"));
var output = new DirectoryPath(settings.Output);
if (!_fileSystem.Directory.Exists(settings.Output))
{
_fileSystem.Directory.Create(settings.Output);
}
foreach (var templatePath in templates)
{
// Parse the Scriban template.
var template = Template.Parse(File.ReadAllText(templatePath.FullPath));
// Render the template with the model.
var result = template.Render(new { Colors = model });
// Write output to file
var file = output.CombineWithFilePath(templatePath.GetFilename().ChangeExtension(".cs"));
File.WriteAllText(file.FullPath, result);
}
return 0;
}
}
public sealed class GeneratorCommandSettings : CommandSettings
{
[CommandArgument(0, "<OUTPUT>")]
public string Output { get; set; }
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Remove="out\**" />
<EmbeddedResource Remove="out\**" />
<None Remove="out\**" />
</ItemGroup>
<ItemGroup>
<None Update="Data\colors.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\ColorTable.Generated.template">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\Color.Generated.template">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\ColorPalette.Generated.template">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Scriban" Version="2.1.3" />
<PackageReference Include="Spectre.Cli" Version="0.36.1-preview.0.6" />
<PackageReference Include="Spectre.IO" Version="0.1.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30320.27
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generator", "Generator.csproj", "{5668D267-53E3-4B99-97AE-59AA597D22ED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5668D267-53E3-4B99-97AE-59AA597D22ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5668D267-53E3-4B99-97AE-59AA597D22ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5668D267-53E3-4B99-97AE-59AA597D22ED}.Debug|x64.ActiveCfg = Debug|Any CPU
{5668D267-53E3-4B99-97AE-59AA597D22ED}.Debug|x64.Build.0 = Debug|Any CPU
{5668D267-53E3-4B99-97AE-59AA597D22ED}.Debug|x86.ActiveCfg = Debug|Any CPU
{5668D267-53E3-4B99-97AE-59AA597D22ED}.Debug|x86.Build.0 = Debug|Any CPU
{5668D267-53E3-4B99-97AE-59AA597D22ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5668D267-53E3-4B99-97AE-59AA597D22ED}.Release|Any CPU.Build.0 = Release|Any CPU
{5668D267-53E3-4B99-97AE-59AA597D22ED}.Release|x64.ActiveCfg = Release|Any CPU
{5668D267-53E3-4B99-97AE-59AA597D22ED}.Release|x64.Build.0 = Release|Any CPU
{5668D267-53E3-4B99-97AE-59AA597D22ED}.Release|x86.ActiveCfg = Release|Any CPU
{5668D267-53E3-4B99-97AE-59AA597D22ED}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5F37FDE3-D591-4D43-8DDE-2ED6BAB0A7B4}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace Generator.Models
{
public sealed class Color
{
public int Number { get; set; }
public string Hex { get; set; }
public string Name { get; set; }
public Rgb Rgb { get; set; }
public int R => Rgb.R;
public int G => Rgb.G;
public int B => Rgb.B;
public static IEnumerable<Color> Parse(string json)
{
var source = JsonConvert.DeserializeObject<List<Color>>(json);
var check = new Dictionary<string, Color>(StringComparer.OrdinalIgnoreCase);
foreach (var color in source.OrderBy(c => c.Number))
{
if (!check.ContainsKey(color.Name))
{
check.Add(color.Name, color);
}
else
{
var newName = (string)null;
for (int i = 1; i < 100; i++)
{
if (!check.ContainsKey($"{color.Name}_{i}"))
{
newName = $"{color.Name}_{i}";
break;
}
}
if (newName == null)
{
throw new InvalidOperationException("Impossible!");
}
check.Add(newName, color);
color.Name = newName;
}
}
return source;
}
}
public sealed class Rgb
{
public int R { get; set; }
public int G { get; set; }
public int B { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
namespace Generator.Models
{
public sealed class ColorModel
{
public List<Color> Colors { get; set; }
public ColorModel(IEnumerable<Color> colors)
{
Colors = new List<Color>(colors);
}
}
}

View File

@ -0,0 +1,6 @@
namespace Generator.Models
{
public sealed class Palette
{
}
}

View File

@ -0,0 +1,19 @@
using Generator.Commands;
using Spectre.Cli;
namespace Generator
{
public static class Program
{
public static int Main(string[] args)
{
var app = new CommandApp();
app.Configure(config =>
{
config.AddCommand<ColorGeneratorCommand>("colors");
});
return app.Run(args);
}
}
}

View File

@ -0,0 +1,36 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Generated {{ date.now | date.to_string `%Y-%m-%d %k:%M` }}
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Diagnostics.CodeAnalysis;
namespace Spectre.Console
{
/// <summary>
/// Represents a color.
/// </summary>
public partial struct Color
{
internal Color(byte number, byte red, byte green, byte blue, bool isDefault = false)
: this(red, green, blue)
{
Number = number;
IsDefault = isDefault;
}
{{~ for color in colors }}
/// <summary>
/// Gets the color "{{ color.name }}" (RGB {{ color.r }},{{ color.g }},{{ color.b }}).
/// </summary>
{{- if string.contains color.name "_" }}
[SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores")]
{{- end}}
public static Color {{ color.name }} { get; } = new Color({{ color.number }}, {{ color.r }}, {{ color.g }}, {{ color.b }});
{{~ end ~}}
}
}

View File

@ -0,0 +1,47 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Generated {{ date.now | date.to_string `%Y-%m-%d %k:%M` }}
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Collections.Generic;
namespace Spectre.Console.Internal
{
internal static partial class ColorPalette
{
private static List<Color> GenerateLegacyPalette()
{
return new List<Color>
{
{{~ for number in 0..7 ~}}
Color.{{ colors[number].name }},
{{~ end ~}}
};
}
private static List<Color> GenerateStandardPalette(IReadOnlyList<Color> legacy)
{
return new List<Color>(legacy)
{
{{~ for number in 8..15 ~}}
Color.{{ colors[number].name }},
{{~ end ~}}
};
}
private static List<Color> GenerateEightBitPalette(IReadOnlyList<Color> standard)
{
return new List<Color>(standard)
{
{{~ for number in 16..255 ~}}
Color.{{ colors[number].name }},
{{~ end ~}}
};
}
}
}

View File

@ -0,0 +1,28 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Generated {{ date.now | date.to_string `%Y-%m-%d %k:%M` }}
//
// 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.Internal
{
internal static partial class ColorTable
{
private static Dictionary<string, int> GenerateTable()
{
return new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
{
{{~ for color in colors ~}}
{ "{{ string.downcase color.name }}", {{ color.number }} },
{{~ end ~}}
};
}
}
}