From 57a38f576e4bdb87a22b86ad3f46737f0e966bac Mon Sep 17 00:00:00 2001 From: CypherPotato Date: Fri, 17 Nov 2023 19:47:40 -0300 Subject: [PATCH] Run command --- src/bflat/BuildCommand.cs | 6 ++-- src/bflat/Program.cs | 1 + src/bflat/RunCommand.cs | 76 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/bflat/RunCommand.cs diff --git a/src/bflat/BuildCommand.cs b/src/bflat/BuildCommand.cs index 271c9cb..6dbfa06 100644 --- a/src/bflat/BuildCommand.cs +++ b/src/bflat/BuildCommand.cs @@ -40,7 +40,7 @@ internal class BuildCommand : CommandBase { private const string DefaultSystemModule = "System.Private.CoreLib"; - private BuildCommand() { } + internal BuildCommand() { } private static Option NoReflectionOption = new Option("--no-reflection", "Disable support for reflection"); private static Option NoStackTraceDataOption = new Option("--no-stacktrace-data", "Disable support for textual stack traces"); @@ -205,13 +205,13 @@ public override int Handle(ParseResult result) OptimizationLevel optimizationLevel = nooptimize ? OptimizationLevel.Debug : OptimizationLevel.Release; - string userSpecificedOutputFileName = result.GetValueForOption(CommonOptions.OutputOption); + string userSpecificedOutputFileName = global::RunCommand.RunOutputFilename ?? result.GetValueForOption(CommonOptions.OutputOption); string outputNameWithoutSuffix = userSpecificedOutputFileName != null ? Path.GetFileNameWithoutExtension(userSpecificedOutputFileName) : CommonOptions.GetOutputFileNameWithoutSuffix(userSpecifiedInputFiles); ILProvider ilProvider = new NativeAotILProvider(); - bool verbose = result.GetValueForOption(CommonOptions.VerbosityOption); + bool verbose = global::RunCommand.RunVerboseOption ?? result.GetValueForOption(CommonOptions.VerbosityOption); var logger = new Logger(Console.Out, ilProvider, verbose, Array.Empty(), singleWarn: false, Array.Empty(), Array.Empty(), Array.Empty()); BuildTargetType buildTargetType = result.GetValueForOption(CommonOptions.TargetOption); diff --git a/src/bflat/Program.cs b/src/bflat/Program.cs index b6e6991..b54d82e 100644 --- a/src/bflat/Program.cs +++ b/src/bflat/Program.cs @@ -48,6 +48,7 @@ private static int Main(string[] args) "Copyright (c) 2021-2022 Michal Strehovsky\n" + "https://flattened.net\n") { + RunCommand.Create(), BuildCommand.Create(), ILBuildCommand.Create(), InfoOption, diff --git a/src/bflat/RunCommand.cs b/src/bflat/RunCommand.cs new file mode 100644 index 0000000..1c9f194 --- /dev/null +++ b/src/bflat/RunCommand.cs @@ -0,0 +1,76 @@ +using Internal.TypeSystem; +using Microsoft.CodeAnalysis; +using System; +using System.Collections.Generic; +using System.CommandLine; +using System.CommandLine.Parsing; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +#pragma warning disable 8509 +#nullable enable + +internal class RunCommand : CommandBase +{ + internal static string? RunOutputFilename = null; + internal static bool? RunVerboseOption = null; + + public static Command Create() + { + var command = new Command("run", "Compiles and runs the specified C# source files") + { + CommonOptions.InputFilesArgument, + CommonOptions.DefinedSymbolsOption, + CommonOptions.ReferencesOption + }; + command.Handler = new RunCommand(); + + return command; + } + + public override Int32 Handle(ParseResult result) + { + var c = new BuildCommand(); + string outputFile; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + outputFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".exe"); + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + outputFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + else + throw new NotImplementedException(); + + RunOutputFilename = outputFile; + RunVerboseOption = false; + + int n = c.Handle(result); + + ProcessStartInfo pstart = new ProcessStartInfo() + { + UseShellExecute = false, + FileName = outputFile + }; + + Process? outputProcess = Process.Start(pstart); + if (outputProcess == null) + return 51; + + outputProcess.WaitForExit(); + + string debugFile; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + debugFile = outputFile.Substring(0, outputFile.Length - 4) + ".pdb"; + else + debugFile = outputFile + ".pdb"; + + File.Delete(outputFile); + File.Delete(debugFile); + + return outputProcess.ExitCode; + } +}