GameBundle/GameBundle/Program.cs

87 lines
3.3 KiB
C#
Raw Normal View History

2020-04-09 18:06:01 +02:00
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using CommandLine;
namespace GameBundle {
2020-04-09 16:18:50 +02:00
internal static class Program {
2020-04-09 18:06:01 +02:00
private static int Main(string[] args) {
return Parser.Default.ParseArguments<Options>(args).MapResult(Run, _ => -1);
}
private static int Run(Options options) {
if (RunProcess(options, "dotnet", "tool install nulastudio.ncbeauty -g") < 0)
return -1;
2020-04-09 18:30:30 +02:00
var proj = GetProjectFile(options);
if (proj == null || !proj.Exists) {
Console.WriteLine("Project file not found");
2020-04-09 18:06:01 +02:00
return -1;
}
2020-04-09 18:30:30 +02:00
Console.WriteLine("Bundling project " + proj.FullName);
2020-04-09 18:06:01 +02:00
2020-04-09 18:30:30 +02:00
var bundleDir = new DirectoryInfo(options.OutputDirectory);
2020-04-09 18:06:01 +02:00
if (!bundleDir.Exists)
bundleDir.Create();
if (!options.NoWindows) {
2020-04-09 18:30:30 +02:00
Console.WriteLine("Bundling for windows");
2020-04-09 18:06:01 +02:00
Publish(options, proj, $"{bundleDir}/win", options.Publish32Bit ? "win-x86" : "win-x64");
2020-04-09 18:30:30 +02:00
}
if (!options.NoLinux) {
2020-04-09 18:30:30 +02:00
Console.WriteLine("Bundling for linux");
2020-04-09 18:06:01 +02:00
Publish(options, proj, $"{bundleDir}/linux", "linux-x64");
2020-04-09 18:30:30 +02:00
}
if (!options.NoMac) {
2020-04-09 18:30:30 +02:00
Console.WriteLine("Bundling for mac");
2020-04-09 18:06:01 +02:00
Publish(options, proj, $"{bundleDir}/mac", "osx-x64");
2020-04-09 18:30:30 +02:00
}
Console.WriteLine("Done");
2020-04-09 18:06:01 +02:00
return 0;
}
private static void Publish(Options options, FileInfo proj, string path, string rid) {
RunProcess(options, "dotnet", $"publish {proj.FullName} -o {path} -r {rid} -c {options.BuildConfig} /p:PublishTrimmed={!options.NoTrim}");
2020-04-09 18:06:01 +02:00
// Run beauty
var excludes = string.Empty;
if (options.ExcludedFiles.Length > 0)
excludes = "excludes=" + string.Join(";", options.ExcludedFiles);
var log = options.Verbose ? "Detail" : "Error";
RunProcess(options, "ncbeauty", $"--loglevel={log} --force=True {path} Lib {excludes}");
// Remove the beauty file since it's just a marker
var beautyFile = new FileInfo(Path.Combine(path, "NetCoreBeauty"));
if (beautyFile.Exists)
beautyFile.Delete();
}
private static int RunProcess(Options options, string program, string args) {
if (options.Verbose)
2020-04-09 18:30:30 +02:00
Console.WriteLine($"> {program} {args}");
var info = new ProcessStartInfo(program, args);
if (!options.Verbose)
info.CreateNoWindow = true;
var process = Process.Start(info);
2020-04-09 18:06:01 +02:00
process.WaitForExit();
2020-04-09 18:30:30 +02:00
if (options.Verbose)
Console.WriteLine($"{program} finished with exit code {process.ExitCode}");
2020-04-09 18:06:01 +02:00
return process.ExitCode;
2020-04-09 16:18:50 +02:00
}
2020-04-09 18:30:30 +02:00
private static FileInfo GetProjectFile(Options options) {
if (!string.IsNullOrEmpty(options.SourceFile))
return new FileInfo(options.SourceFile);
var dir = new DirectoryInfo(Environment.CurrentDirectory);
foreach (var file in dir.EnumerateFiles()) {
if (Path.GetExtension(file.FullName).Contains("proj"))
return file;
}
return null;
}
2020-04-09 16:18:50 +02:00
}
}