Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hubert Rybak committed Apr 30, 2019
1 parent 42bcb24 commit af7e332
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 108 deletions.
38 changes: 38 additions & 0 deletions src/dotnet-warp/ActionsBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using DotnetWarp.CmdCommands;
using DotnetWarp.CmdCommands.Options;

namespace DotnetWarp
{
internal class ActionsBuilder
{
public IEnumerable<Expression<Func<Context, bool>>> GetActionsForContext(Context context)
{
var actions = new List<Expression<Func<Context, bool>>>();

var dotnetCli = new DotnetCli(context.ProjectFileOrFolder, context.IsVerbose, context.MsBuildProperties);
var warp = new WarpCli(context.CurrentPlatform, context.IsVerbose);

var wasLinkerPackageAdded = false;
if (context.ShouldAddLinkerPackage)
{
wasLinkerPackageAdded = true;
actions.Add(ctx => dotnetCli.AddLinkerPackage());
}

actions.Add(ctx =>
dotnetCli.Publish(ctx, new DotnetPublishOptions(context.Rid, context.ShouldNotRootApplicationAssemblies, context.IsNoCrossGen)));

actions.Add(ctx => warp.Pack(ctx, new WarpPackOptions(context.OutputPath)));

if (wasLinkerPackageAdded)
{
actions.Add(ctx => dotnetCli.RemoveLinkerPackage());
}

return actions;
}
}
}
7 changes: 5 additions & 2 deletions src/dotnet-warp/CmdCommands/CmdCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ protected CmdCommand(string command)
{
_commandWrapper = new CommandWrapper(command);
}

protected bool RunCommand(IEnumerable<string> arguments, bool isVerbose)
{
return _commandWrapper.Run(arguments, isVerbose);
var exitCode = _commandWrapper.Run(arguments, isVerbose);
var isSuccessful = exitCode == 0;

return isSuccessful;
}
}
}
26 changes: 16 additions & 10 deletions src/dotnet-warp/CmdCommands/CommandWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;

namespace DotnetWarp.CmdCommands
{
Expand All @@ -19,36 +20,41 @@ public CommandWrapper(string commandName)
};
}

public bool Run(IEnumerable<string> argumentList, bool isVerbose)
public int Run(IEnumerable<string> argumentList, bool isVerbose)
{
var arguments = string.Join(' ', argumentList);

_processStartInfo.Arguments = arguments;

var process = new Process
{
StartInfo = _processStartInfo
StartInfo = _processStartInfo,
EnableRaisingEvents = true
};

if (isVerbose)
{
process.OutputDataReceived += ProcessOnOutputDataReceived;
Console.WriteLine($"Running {process.StartInfo.FileName} {arguments}");
}

process.Start();

process.Start();
process.BeginOutputReadLine();

process.WaitForExit();
if (process.HasExited)
{
Task.FromResult(process.ExitCode);
}

if (isVerbose)
var tcs = new TaskCompletionSource<int>();
process.Exited += (sender, args) =>
{
process.OutputDataReceived -= ProcessOnOutputDataReceived;
}
tcs.SetResult(process.ExitCode);
};

return tcs.Task.Result;

return process.ExitCode == 0;

void ProcessOnOutputDataReceived(object sender, DataReceivedEventArgs args)
{
Console.WriteLine(args.Data);
Expand Down
26 changes: 8 additions & 18 deletions src/dotnet-warp/CmdCommands/DotnetCli.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DotnetWarp.CmdCommands.Options;
using DotnetWarp.Extensions;
Expand All @@ -10,7 +9,7 @@ internal class DotnetCli : CmdCommand
{
private readonly string _projectPath;
private readonly bool _isVerbose;
private readonly string[] _msbuildProperties;
private readonly IEnumerable<string> _msbuildProperties;

private static readonly Dictionary<Platform.Value, string> PlatformToRid = new Dictionary<Platform.Value, string>
{
Expand All @@ -19,7 +18,7 @@ internal class DotnetCli : CmdCommand
[Platform.Value.MacOs] = "osx-x64"
};

public DotnetCli(string projectPath, bool isVerbose, string[] msbuildProperties) : base("dotnet")
public DotnetCli(string projectPath, bool isVerbose, IEnumerable<string> msbuildProperties) : base(command: "dotnet")
{
_projectPath = projectPath;
_isVerbose = isVerbose;
Expand All @@ -33,8 +32,7 @@ public bool Publish(Context context, DotnetPublishOptions dotnetPublishOptions)
"publish",
"-c Release",
$"-r {dotnetPublishOptions.Rid ?? PlatformToRid[context.CurrentPlatform]}",
$"-o {context.TempPublishPath.WithQuotes()}",
"/p:ShowLinkerSizeComparison=true"
$"-o {context.TempPublishPath.WithQuotes()}"
};

if (dotnetPublishOptions.IsNoRootApplicationAssemblies)
Expand All @@ -47,16 +45,16 @@ public bool Publish(Context context, DotnetPublishOptions dotnetPublishOptions)
argumentList.Add("/p:CrossGenDuringPublish=false");
}

foreach (var prop in _msbuildProperties ?? new string[0])
{
argumentList.Add($"/p:{prop}");
}
argumentList.AddRange(_msbuildProperties.Select(prop => $"/p:{prop}"));

argumentList.Add($"{_projectPath.WithQuotes()}");

var isCommandSuccessful = RunCommand(argumentList, _isVerbose);

UpdateContext(context);
if (isCommandSuccessful)
{
context.FindAssemblyName();
}

return isCommandSuccessful;
}
Expand Down Expand Up @@ -84,13 +82,5 @@ public bool RemoveLinkerPackage()

return RunCommand(argumentList, _isVerbose);
}

private void UpdateContext(Context context)
{
const string depsJsonExtension = ".deps.json";
var depsJsonPath = Directory.EnumerateFiles(context.TempPublishPath, "*" + depsJsonExtension).Single();
var depsJsonFilename = Path.GetFileName(depsJsonPath);
context.AssemblyName = depsJsonFilename.Substring(0, depsJsonFilename.Length - depsJsonExtension.Length);
}
}
}
4 changes: 2 additions & 2 deletions src/dotnet-warp/CmdCommands/Options/WarpPackOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ namespace DotnetWarp.CmdCommands.Options
{
internal class WarpPackOptions
{
public string OutputExePath { get; }

public WarpPackOptions(string outputExePath)
{
OutputExePath = outputExePath;
}

public string OutputExePath { get; }
}
}
43 changes: 22 additions & 21 deletions src/dotnet-warp/CmdCommands/WarpCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ namespace DotnetWarp.CmdCommands
{
internal class WarpCli : CmdCommand
{
private static readonly Dictionary<Platform.Value, string> PlatformToWarpArch = new Dictionary<Platform.Value, string>
{
[Platform.Value.Windows] = "windows-x64",
[Platform.Value.Linux] = "linux-x64",
[Platform.Value.MacOs] = "macos-x64"
};

private static readonly Dictionary<Platform.Value, string> PlatformToWarpBinary = new Dictionary<Platform.Value, string>
{
[Platform.Value.Windows] = "windows-x64.warp-packer.exe",
[Platform.Value.Linux] = "linux-x64.warp-packer",
[Platform.Value.MacOs] = "macos-x64.warp-packer"
};

private static readonly Dictionary<Platform.Value, string> PlatformToWarpArch =
new Dictionary<Platform.Value, string>
{
[Platform.Value.Windows] = "windows-x64",
[Platform.Value.Linux] = "linux-x64",
[Platform.Value.MacOs] = "macos-x64"
};

private static readonly Dictionary<Platform.Value, string> PlatformToWarpBinary =
new Dictionary<Platform.Value, string>
{
[Platform.Value.Windows] = "windows-x64.warp-packer.exe",
[Platform.Value.Linux] = "linux-x64.warp-packer",
[Platform.Value.MacOs] = "macos-x64.warp-packer"
};

private readonly bool _isVerbose;

public WarpCli(Platform.Value platform, bool isVerbose) : base(GetWarpPath(platform))
Expand All @@ -32,17 +34,15 @@ public WarpCli(Platform.Value platform, bool isVerbose) : base(GetWarpPath(platf

public bool Pack(Context ctx, WarpPackOptions warpPackOptions)
{
var binFileName = ctx.CurrentPlatform == Platform.Value.Windows ? ctx.AssemblyName + ".exe" : ctx.AssemblyName;
File.Delete(ctx.OutputExeName);

File.Delete(binFileName);
var outputExePath = (warpPackOptions.OutputExePath ?? ctx.OutputExeName).WithQuotes();

var outputExePath = (warpPackOptions.OutputExePath ?? binFileName).WithQuotes();

var argumentList = new List<string>
{
$"--arch {PlatformToWarpArch[ctx.CurrentPlatform]}",
$"--input_dir {ctx.TempPublishPath.WithQuotes()}",
$"--exec {binFileName.WithQuotes()}",
$"--exec {ctx.OutputExeName.WithQuotes()}",
$"--output {outputExePath}"
};

Expand All @@ -55,10 +55,11 @@ public bool Pack(Context ctx, WarpPackOptions warpPackOptions)

return isCommandSuccessful;
}

private static string GetWarpPath(Platform.Value platform)
{
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "warp", PlatformToWarpBinary[platform]);
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly()
.Location), "warp", PlatformToWarpBinary[platform]);
}
}
}
53 changes: 50 additions & 3 deletions src/dotnet-warp/Context.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace DotnetWarp
{
internal class Context : IDisposable
{
public string AssemblyName { get; set; }
public string TempPublishPath { get; set; }
public Platform.Value CurrentPlatform { get; set; }
public Context(string rid,
string projectFileOrFolder,
bool isVerbose,
IEnumerable<string> msBuildProperties,
LinkLevel link,
bool isNoCrossGen,
string outputPath)
{
Rid = rid;
ProjectFileOrFolder = projectFileOrFolder;
IsVerbose = isVerbose;
MsBuildProperties = msBuildProperties;
Link = link;
IsNoCrossGen = isNoCrossGen;
OutputPath = outputPath;

CurrentPlatform = rid == null ? Platform.Current() :
rid.StartsWith("win") ? Platform.Value.Windows :
rid.StartsWith("osx") ? Platform.Value.MacOs : Platform.Value.Linux;

TempPublishPath = Path.Combine(projectFileOrFolder, "dotnetwarp_temp");
}

public string AssemblyName { get; private set; }
public string TempPublishPath { get; }
public Platform.Value CurrentPlatform { get; }
public string ProjectFileOrFolder { get; }
public bool IsVerbose { get; }
public IEnumerable<string> MsBuildProperties { get; }
private LinkLevel Link { get; }

public bool ShouldAddLinkerPackage => Link != LinkLevel.None;
public bool ShouldNotRootApplicationAssemblies => Link == LinkLevel.Aggressive;
public string Rid { get; }
public bool IsNoCrossGen { get; }
public string OutputPath { get; }

public string OutputExeName => CurrentPlatform == Platform.Value.Windows
? AssemblyName + ".exe"
: AssemblyName;

public void Dispose()
{
Expand All @@ -22,5 +60,14 @@ public void Dispose()
}
});
}

public void FindAssemblyName()
{
const string depsJsonExtension = ".deps.json";
var depsJsonPath = Directory.EnumerateFiles(TempPublishPath, "*" + depsJsonExtension)
.Single();
var depsJsonFilename = Path.GetFileName(depsJsonPath);
AssemblyName = depsJsonFilename.Substring(0, depsJsonFilename.Length - depsJsonExtension.Length);
}
}
}
4 changes: 2 additions & 2 deletions src/dotnet-warp/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static Value Current()
{
throw new DotnetWarpException("dotnet-warp only supports x64 architectures.");
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Value.Windows;
Expand All @@ -30,7 +30,7 @@ public static Value Current()

throw new Exception("Unknown platform.");
}

internal enum Value
{
Windows,
Expand Down
Loading

0 comments on commit af7e332

Please sign in to comment.