Skip to content

Commit

Permalink
Run command
Browse files Browse the repository at this point in the history
  • Loading branch information
CypherPotato committed Nov 17, 2023
1 parent 57749b2 commit 57a38f5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/bflat/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
internal class BuildCommand : CommandBase
{
private const string DefaultSystemModule = "System.Private.CoreLib";
private BuildCommand() { }
internal BuildCommand() { }

private static Option<bool> NoReflectionOption = new Option<bool>("--no-reflection", "Disable support for reflection");
private static Option<bool> NoStackTraceDataOption = new Option<bool>("--no-stacktrace-data", "Disable support for textual stack traces");
Expand Down Expand Up @@ -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<int>(), singleWarn: false, Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>());

BuildTargetType buildTargetType = result.GetValueForOption(CommonOptions.TargetOption);
Expand Down
1 change: 1 addition & 0 deletions src/bflat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
76 changes: 76 additions & 0 deletions src/bflat/RunCommand.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 57a38f5

Please sign in to comment.