diff --git a/ExeToBat/App.config b/ExeToBat/App.config index 4bfa005..bcb2ae2 100644 --- a/ExeToBat/App.config +++ b/ExeToBat/App.config @@ -1,6 +1,14 @@ - + - + + + + + + + + + diff --git a/ExeToBat/Console.cs b/ExeToBat/Console.cs index fd1a5f0..159c47a 100644 --- a/ExeToBat/Console.cs +++ b/ExeToBat/Console.cs @@ -4,22 +4,70 @@ using System.Collections.Generic; using System.Linq; using static ExeToBat.Generator; using static System.ConsoleUtils; +using Mono.Options; +using System.Text.Json; namespace ExeToBat { class Console { - static void Main() => new Console().MainMenu(); + static void Main(string[] args) => new Console().Show(args); public Console() { } private readonly Generator generator = new Generator(); + public void Show(string[] args) + { + string config = null; + bool help = false; + + OptionSet options = new OptionSet() + { + { "c|config=", "the config file used for automatic generation", v => config = v }, + { "h|help", "show this message and exit", v => help = v != null }, + }; + + try + { + List extra = options.Parse(args); + if (help) + { + options.WriteOptionDescriptions(System.Console.Out); + } + else if (config != null) + { + try + { + generator.LoadConfig(GeneratorConfig.FromJson(File.ReadAllText(config))); + Generate(interactive: false); + } + catch (Exception e) when (e is IOException || e is JsonException) + { + System.Console.Write("Failed to load config {0}: {1}", config, e); + } + } + else + { + MainMenu(); + } + } + catch (OptionException e) + { + System.Console.Write("Invalid arguments: {0}", e); + options.WriteOptionDescriptions(System.Console.Out); + } + } + + public void Show() => MainMenu(); + private void MainMenu() { Dictionary options = new Dictionary { { "Files", ChooseSource }, + { "Save config", SaveConfig }, + { "Load config", LoadConfig }, { "Generate", Generate }, }; @@ -39,6 +87,64 @@ namespace ExeToBat }.Show(); } + private void SaveConfig() + { + bool IsInputValid = false; + while (!IsInputValid) + { + System.Console.Clear(); + System.Console.WriteLine("ExeToBat > Config > Save"); + System.Console.WriteLine(); + System.Console.Write("{0}> ", "Output File"); + string input = System.Console.ReadLine(); + + input.Trim(); + input = input.Replace("\"", ""); + if (!string.IsNullOrEmpty(input)) + { + try + { + File.WriteAllText(input, generator.SaveConfig().ToJson()); + IsInputValid = true; + } + catch (IOException e) + { + System.Console.Write("Failed to save config: {0}", e); + ResetInput(); + } + } + } + } + + private void LoadConfig() + { + bool IsInputValid = false; + while (!IsInputValid) + { + System.Console.Clear(); + System.Console.WriteLine("ExeToBat > Config > Load"); + System.Console.WriteLine(); + System.Console.Write("{0}> ", "Config File"); + string input = System.Console.ReadLine(); + + input.Trim(); + input = input.Replace("\"", ""); + if (!string.IsNullOrEmpty(input)) + { + try + { + generator.LoadConfig(GeneratorConfig.FromJson(File.ReadAllText(input))); + IsInputValid = true; + } + catch (Exception e) when (e is IOException || e is JsonException) + { + System.Console.Write("Failed to load config {0}: {1}", input, e); + ResetInput(); + } + } + } + } + private void ChooseSource() { new ListMenu(generator.Sources) @@ -79,35 +185,32 @@ namespace ExeToBat { System.Console.Clear(); System.Console.WriteLine("ExeToBat > Files > Add"); - System.Console.Write("\n"); + System.Console.WriteLine(); System.Console.Write("{0}> ", "File/Folder"); string input = System.Console.ReadLine(); input.Trim(); input = input.Replace("\"", ""); - if (!string.IsNullOrEmpty(input)) + switch (input) { - switch (input) - { - case var i when Directory.Exists(i): - IsInputValid = true; - foreach (string file in Directory.GetFiles(input)) - { - generator.Sources.Add(new SourceFile(file)); - } - break; - case var i when File.Exists(i): - IsInputValid = true; - generator.Sources.Add(new SourceFile(input)); - break; - default: - ResetInput(); - break; - } - } - else - { - IsInputValid = true; + case var i when string.IsNullOrEmpty(i): + IsInputValid = true; + break; + case var i when Directory.Exists(i): + IsInputValid = true; + foreach (string file in Directory.GetFiles(input)) + { + generator.Sources.Add(new SourceFile(file)); + } + break; + case var i when File.Exists(i): + IsInputValid = true; + generator.Sources.Add(new SourceFile(input)); + break; + + default: + ResetInput(); + break; } } } @@ -297,7 +400,9 @@ namespace ExeToBat } } - private void Generate() + private void Generate() => Generate(true); + + private void Generate(bool interactive) { System.Console.Clear(); System.Console.WriteLine("ExeToBat > Generate"); @@ -308,8 +413,11 @@ namespace ExeToBat generator.Generation -= OnGenerate; - System.Console.WriteLine("Press anything..."); - System.Console.ReadKey(); + if (interactive) + { + System.Console.WriteLine("Press anything..."); + System.Console.ReadKey(); + } } private void OnGenerate(object sender, GeneratorEvent e) @@ -318,6 +426,7 @@ namespace ExeToBat { case GenerationStartEvent s: System.Console.WriteLine("Starting generation..."); + System.Console.WriteLine("Config file: {0}", "no config file"); System.Console.WriteLine("{0} files scheduled", s.Files.Count); break; case ReadingFileEvent s: @@ -354,7 +463,7 @@ namespace ExeToBat System.Console.WriteLine("No files specified"); break; case GenerationFailedEvent s: - System.Console.Write("Generation failed with: {0}", s.Error.ToString()); + System.Console.Write("Generation failed with: {0}", s.Error); break; } } diff --git a/ExeToBat/ExeToBat.csproj b/ExeToBat/ExeToBat.csproj index aa9c9c8..983ab3c 100644 --- a/ExeToBat/ExeToBat.csproj +++ b/ExeToBat/ExeToBat.csproj @@ -13,6 +13,8 @@ true true + + AnyCPU @@ -34,8 +36,39 @@ 4 + + ..\packages\Microsoft.Bcl.AsyncInterfaces.6.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll + + + ..\packages\Mono.Options.6.12.0.148\lib\net40\Mono.Options.dll + + + ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + + + ..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll + + + + ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll + + + ..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Text.Encodings.Web.6.0.0\lib\net461\System.Text.Encodings.Web.dll + + + ..\packages\System.Text.Json.6.0.6\lib\net461\System.Text.Json.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll + + + ..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll + @@ -51,6 +84,14 @@ + + + + + Dieses Projekt verweist auf mindestens ein NuGet-Paket, das auf diesem Computer fehlt. Verwenden Sie die Wiederherstellung von NuGet-Paketen, um die fehlenden Dateien herunterzuladen. Weitere Informationen finden Sie unter "http://go.microsoft.com/fwlink/?LinkID=322105". Die fehlende Datei ist "{0}". + + + \ No newline at end of file diff --git a/ExeToBat/Generator.cs b/ExeToBat/Generator.cs index a225a3c..01b8740 100644 --- a/ExeToBat/Generator.cs +++ b/ExeToBat/Generator.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.Json; namespace ExeToBat { @@ -9,12 +10,21 @@ namespace ExeToBat { public Generator() { } + public Generator(GeneratorConfig config) + { + LoadConfig(config); + } + public const int ChunkSize = 8000; public List Sources = new List(); public class SourceFile { + /// + /// Represents a file that is later embedded in a bat. + /// + /// The path of the file. public SourceFile(string path) { Path = path; @@ -34,6 +44,48 @@ namespace ExeToBat } } + public class GeneratorConfig + { + /// + /// The configuration for a Generator. + /// + /// + public GeneratorConfig(List sources) + { + Sources = sources; + } + + public List Sources { get; private set; } + + public string ToJson() + { + return JsonSerializer.Serialize(this); + } + + public static GeneratorConfig FromJson(string raw) + { + return JsonSerializer.Deserialize(raw); + } + } + + /// + /// Exports the variables of this Generator as a configuration. + /// + /// + public GeneratorConfig SaveConfig() + { + return new GeneratorConfig(Sources); + } + + /// + /// Loads a configuration into this Generator. + /// + /// + public void LoadConfig(GeneratorConfig config) + { + Sources = config.Sources; + } + /// /// Sends progress updates about ongoing generation task. /// @@ -150,7 +202,7 @@ namespace ExeToBat public abstract class GeneratorEvent : EventArgs { } - public class GeneratorFileEvent : GeneratorEvent + public abstract class GeneratorFileEvent : GeneratorEvent { public SourceFile File { get; protected set; } } diff --git a/ExeToBat/packages.config b/ExeToBat/packages.config new file mode 100644 index 0000000..eae92fd --- /dev/null +++ b/ExeToBat/packages.config @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file