Extract CreateExecutable to own class

This commit is contained in:
2021-12-04 15:26:57 +01:00
parent 321e67e400
commit 6df7c2885b
3 changed files with 81 additions and 43 deletions

View File

@@ -0,0 +1,66 @@
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CSharp;
namespace ExeLauncher.GUI
{
public class ExeGenerator
{
static ExeGenerator()
{
var file = Assembly.GetExecutingAssembly().GetManifestResourceStream("ExeLauncher.Code.cs");
var reader = new StreamReader(file);
Code = reader.ReadToEnd();
reader.Close();
}
const string Paths = "%Paths%";
const string Arguments = "%Arguments%";
const string WorkingDirectories = "%WorkingDirectories%";
private static string Code { get; }
private string _code;
public ExeGenerator()
{
_code = Code;
}
public CompilerErrorCollection CreateExecutable(string location, string paths, string arguments, string workingDirectories, string icon)
{
_code = _code.Replace(Paths, paths);
_code = _code.Replace(Arguments, arguments);
_code = _code.Replace(WorkingDirectories, workingDirectories);
using (var codeProvider = new CSharpCodeProvider())
{
var compilerParameters = new CompilerParameters()
{
CompilerOptions = string.IsNullOrWhiteSpace(icon) ? "-target:winexe" : $" -target:winexe -win32icon:\"{icon}\"",
GenerateExecutable = true,
OutputAssembly = location
};
compilerParameters.ReferencedAssemblies.Add("System.dll");
var result = codeProvider.CompileAssemblyFromSource(compilerParameters, _code);
if (result.Errors.HasErrors)
{
return result.Errors;
}
}
return null;
}
}
}

View File

@@ -66,6 +66,7 @@
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Code.cs" />
<Compile Include="ExeGenerator.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

View File

@@ -1,4 +1,5 @@
using Microsoft.CSharp;
using ExeLauncher.GUI;
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Diagnostics;
@@ -10,22 +11,11 @@ namespace ExeLauncher
{
public class Program
{
const string Paths = "%Paths%";
const string Arguments = "%Arguments%";
const string WorkingDirectories = "%WorkingDirectories%";
static string code;
[STAThread]
static void Main(string[] args)
{
var file = Assembly.GetExecutingAssembly().GetManifestResourceStream("ExeLauncher.Code.cs");
var reader = new StreamReader(file);
code = reader.ReadToEnd();
reader.Close();
string path, arguments, workingDirectory, icon;
Console.Write("Path: ");
@@ -55,43 +45,24 @@ namespace ExeLauncher
Console.WriteLine();
Console.WriteLine("Generating file...");
CreateExecutable(location, path, arguments, workingDirectory, icon);
var errors = new ExeGenerator().CreateExecutable(location, path, arguments, workingDirectory, icon);
if (errors != null)
{
Console.WriteLine("An error occured while compiling the executable: ");
foreach (var compilerError in errors)
{
Console.WriteLine("\t{0}", compilerError.ToString());
Console.WriteLine();
}
}
Console.WriteLine();
Console.WriteLine("Done.");
Console.ReadKey(true);
}
public static void CreateExecutable(string location, string paths, string arguments, string workingDirectories, string icon)
{
code = code.Replace(Paths, paths);
code = code.Replace(Arguments, arguments);
code = code.Replace(WorkingDirectories, workingDirectories);
using (var codeProvider = new CSharpCodeProvider())
{
var compilerParameters = new CompilerParameters()
{
CompilerOptions = $"-target:winexe -win32icon:\"{icon}\"",
GenerateExecutable = true,
OutputAssembly = location
};
compilerParameters.ReferencedAssemblies.Add("System.dll");
var result = codeProvider.CompileAssemblyFromSource(compilerParameters, code);
if (result.Errors.HasErrors)
{
Console.WriteLine("An error occured while compiling the executable: ");
foreach (var compilerError in result.Errors)
{
Console.WriteLine("\t{0}", compilerError.ToString());
Console.WriteLine();
}
}
}
}
public static string GetIcon()
{
var dialog = new OpenFileDialog()