mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-11-04 18:40:50 +08:00 
			
		
		
		
	Autogenerate known colors and palettes
This will make it a bit easier to make changes
This commit is contained in:
		
				
					committed by
					
						
						Patrik Svensson
					
				
			
			
				
	
			
			
			
						parent
						
							5267ebda49
						
					
				
				
					commit
					e5bf2bd498
				
			
							
								
								
									
										59
									
								
								eng/Generator/Commands/ColorGeneratorCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								eng/Generator/Commands/ColorGeneratorCommand.cs
									
									
									
									
									
										Normal 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; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user