Skip to content

Commit

Permalink
src/loader: remove Executor package (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm authored Aug 1, 2024
1 parent 8d5ad94 commit 62beaa1
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 142 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
"features": {
"ghcr.io/devcontainers/features/dotnet:2": {
"version": "latest"
"version": "6.0"
},
"ghcr.io/devcontainers/features/github-cli:1": {
"installDirectlyFromGitHubRelease": true,
Expand Down
19 changes: 0 additions & 19 deletions Makefile

This file was deleted.

144 changes: 144 additions & 0 deletions src/Atlas.Provider.Loader/Exe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Text;

namespace Atlas.Provider.Loader;

// Copied from https://github.com/dotnet/efcore/blob/6df2608e585f85c7bce1b0a4832c4b519cf592ad/src/dotnet-ef/Exe.cs
internal static class Exe
{
public static int Run(
string executable,
IReadOnlyList<string> args,
string? workingDirectory = null,
Action<string?>? handleOutput = null,
Action<string?>? handleError = null,
Action<string>? processCommandLine = null)
{
var arguments = ToArguments(args);

processCommandLine?.Invoke(executable + " " + arguments);

var startInfo = new ProcessStartInfo
{
FileName = executable,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = handleOutput != null,
RedirectStandardError = handleError != null
};
if (workingDirectory != null)
{
startInfo.WorkingDirectory = workingDirectory;
}

var process = new Process
{
StartInfo = startInfo
};

if (handleOutput != null)
{
process.OutputDataReceived += (sender, args) => handleOutput(args.Data);
}

if (handleError != null)
{
process.ErrorDataReceived += (sender, args) => handleError(args.Data);
}

process.Start();

if (handleOutput != null)
{
process.BeginOutputReadLine();
}

if (handleError != null)
{
process.BeginErrorReadLine();
}

process.WaitForExit();

return process.ExitCode;
}

public static string ToArguments(IReadOnlyList<string> args)
{
var builder = new StringBuilder();
for (var i = 0; i < args.Count; i++)
{
if (i != 0)
{
builder.Append(' ');
}

if (args[i].Length == 0)
{
builder.Append("\"\"");

continue;
}

if (args[i].IndexOf(' ') == -1)
{
builder.Append(args[i]);

continue;
}

builder.Append('"');

var pendingBackslashes = 0;
for (var j = 0; j < args[i].Length; j++)
{
switch (args[i][j])
{
case '\"':
if (pendingBackslashes != 0)
{
builder.Append('\\', pendingBackslashes * 2);
pendingBackslashes = 0;
}

builder.Append("\\\"");
break;

case '\\':
pendingBackslashes++;
break;

default:
if (pendingBackslashes != 0)
{
if (pendingBackslashes == 1)
{
builder.Append('\\');
}
else
{
builder.Append('\\', pendingBackslashes * 2);
}

pendingBackslashes = 0;
}

builder.Append(args[i][j]);
break;
}
}

if (pendingBackslashes != 0)
{
builder.Append('\\', pendingBackslashes * 2);
}

builder.Append('"');
}

return builder.ToString();
}
}
119 changes: 0 additions & 119 deletions src/Atlas.Provider.Loader/Executor/Exe.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Atlas.Provider.Loader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static void Main(
arguments.Add("--nullable");
}
// dotnet exec [runtime-options] [path-to-application] [arguments]
Executor.Exe.Run("dotnet", [
Exe.Run("dotnet", [
"exec",
.. runtimeOpts,
Path.Combine(loaderDirPath!, AssemblyName + ".dll"),
Expand Down
5 changes: 3 additions & 2 deletions src/Atlas.Provider.Loader/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static Project FromFile(
{
propertyArg += ";RuntimeIdentifier=" + runtime;
}
var exitCode = Executor.Exe.Run("dotnet", [
var exitCode = Exe.Run("dotnet", [
"msbuild",
"/target:AtlasEFProjectMetadata",
propertyArg,
Expand Down Expand Up @@ -192,7 +192,8 @@ public void Build()
args.Add("/verbosity:quiet");
args.Add("/nologo");
args.Add("/p:PublishAot=false"); // Avoid NativeAOT warnings
var exitCode = Executor.Exe.Run("dotnet", args, interceptOutput: true);
// Discard the build output, but show the errors
var exitCode = Exe.Run("dotnet", args, handleOutput: delegate(string? line) {});
if (exitCode != 0)
{
throw new Exception("Build failed");
Expand Down

0 comments on commit 62beaa1

Please sign in to comment.