added config files

This commit is contained in:
dragon 2022-10-22 13:54:49 +02:00
commit 9507a43164
5 changed files with 254 additions and 31 deletions

View file

@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View file

@ -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<string> 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<string, Action> options = new Dictionary<string, Action>
{
{ "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<SourceFile>(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;
}
}

View file

@ -13,6 +13,8 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@ -34,8 +36,39 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.6.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="Mono.Options, Version=6.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Options.6.12.0.148\lib\net40\Mono.Options.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Text.Encodings.Web, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encodings.Web.6.0.0\lib\net461\System.Text.Encodings.Web.dll</HintPath>
</Reference>
<Reference Include="System.Text.Json, Version=6.0.0.6, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Json.6.0.6\lib\net461\System.Text.Json.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@ -51,6 +84,14 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\System.Text.Json.6.0.6\build\System.Text.Json.targets" Condition="Exists('..\packages\System.Text.Json.6.0.6\build\System.Text.Json.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}".</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\System.Text.Json.6.0.6\build\System.Text.Json.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.Text.Json.6.0.6\build\System.Text.Json.targets'))" />
</Target>
</Project>

View file

@ -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<SourceFile> Sources = new List<SourceFile>();
public class SourceFile
{
/// <summary>
/// Represents a file that is later embedded in a bat.
/// </summary>
/// <param name="path"></param> The path of the file.
public SourceFile(string path)
{
Path = path;
@ -34,6 +44,48 @@ namespace ExeToBat
}
}
public class GeneratorConfig
{
/// <summary>
/// The configuration for a Generator.
/// </summary>
/// <param name="sources"></param>
public GeneratorConfig(List<SourceFile> sources)
{
Sources = sources;
}
public List<SourceFile> Sources { get; private set; }
public string ToJson()
{
return JsonSerializer.Serialize(this);
}
public static GeneratorConfig FromJson(string raw)
{
return JsonSerializer.Deserialize<GeneratorConfig>(raw);
}
}
/// <summary>
/// Exports the variables of this Generator as a configuration.
/// </summary>
/// <returns></returns>
public GeneratorConfig SaveConfig()
{
return new GeneratorConfig(Sources);
}
/// <summary>
/// Loads a configuration into this Generator.
/// </summary>
/// <param name="config"></param>
public void LoadConfig(GeneratorConfig config)
{
Sources = config.Sources;
}
/// <summary>
/// Sends progress updates about ongoing generation task.
/// </summary>
@ -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; }
}

13
ExeToBat/packages.config Normal file
View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl.AsyncInterfaces" version="6.0.0" targetFramework="net48" />
<package id="Mono.Options" version="6.12.0.148" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.Memory" version="4.5.4" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
<package id="System.Text.Encodings.Web" version="6.0.0" targetFramework="net48" />
<package id="System.Text.Json" version="6.0.6" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
</packages>