diff --git a/ExeLauncher/ExeGenerator.cs b/ExeLauncher/ExeGenerator.cs new file mode 100644 index 0000000..cdd1964 --- /dev/null +++ b/ExeLauncher/ExeGenerator.cs @@ -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; + } + + } +} diff --git a/ExeLauncher/ExeLauncher.csproj b/ExeLauncher/ExeLauncher.csproj index eb8ffdd..3501cdb 100644 --- a/ExeLauncher/ExeLauncher.csproj +++ b/ExeLauncher/ExeLauncher.csproj @@ -66,6 +66,7 @@ + diff --git a/ExeLauncher/Program.cs b/ExeLauncher/Program.cs index 3ece5b9..2c4d5eb 100644 --- a/ExeLauncher/Program.cs +++ b/ExeLauncher/Program.cs @@ -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()