Skip to content

Commit

Permalink
Assume --platform argument for TGS
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit committed Oct 20, 2023
1 parent e2aa920 commit b877680
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 45 deletions.
44 changes: 7 additions & 37 deletions OpenDreamPackageTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ public class Options {
}

public class ServerOptions : Options {
public PlatformReg? Platform;
public string? Platform;
public bool HybridAcz;
public bool InPlatformSubDir = true;
public bool TgsEngineBuild;
}

public class ClientOptions : Options {
public PlatformReg? Platform;

}

public class TgsOptions : Options {
public PlatformReg? Platform;
// Avoid adding arguments for TGS, to give us more flexibility while keeping compatibility
}

public static readonly string[] SharedIgnoredResources = {
Expand Down Expand Up @@ -97,21 +97,13 @@ private static bool TryParseArgs(string[] args, [NotNullWhen(true)] out Options?
return false;
}

var platformRId = args[++i];

serverOptions.Platform =
ServerPackaging.Platforms.First(p => p.RId == platformRId);
if (serverOptions.Platform == null) {
Console.Error.WriteLine($"Invalid platform '{platformRId}'");
return false;
}

serverOptions.Platform = args[++i];
break;
case "--hybrid-acz":
serverOptions.HybridAcz = true;
break;
default:
Console.Error.WriteLine($"Unknown argument '{arg}'");
Console.Error.WriteLine($"Invalid argument '{arg}'");
return false;
}
}
Expand All @@ -135,7 +127,7 @@ private static bool TryParseArgs(string[] args, [NotNullWhen(true)] out Options?
clientOptions.SkipBuild = true;
break;
default:
Console.Error.WriteLine($"Unknown argument '{arg}'");
Console.Error.WriteLine($"Invalid argument '{arg}'");
return false;
}
}
Expand All @@ -157,35 +149,13 @@ private static bool TryParseArgs(string[] args, [NotNullWhen(true)] out Options?
break;
case "--skip-build":
tgsOptions.SkipBuild = true;
break;
case "--platform":
case "-p":
if (i + 1 >= args.Length) {
Console.Error.WriteLine("No platform given");
return false;
}

var platformRId = args[++i];

tgsOptions.Platform =
ServerPackaging.Platforms.First(p => p.RId == platformRId);
if (tgsOptions.Platform == null) {
Console.Error.WriteLine($"Invalid platform '{platformRId}'");
return false;
}

break;
default:
Console.Error.WriteLine($"Unknown argument '{arg}'");
Console.Error.WriteLine($"Invalid argument '{arg}'");
return false;
}
}

if (tgsOptions.Platform == null) {
Console.Error.WriteLine("A '--platform' argument is required for a tgs package");
return false;
}

return true;
}

Expand Down
21 changes: 15 additions & 6 deletions OpenDreamPackageTool/ServerPackaging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace OpenDreamPackageTool;

public static class ServerPackaging {
public static readonly PlatformReg[] Platforms = {
private static readonly PlatformReg[] Platforms = {
new("win-x64", "Windows", true),
new("linux-x64", "Linux", true),
new("linux-arm64", "Linux", true),
Expand Down Expand Up @@ -64,16 +64,17 @@ public static class ServerPackaging {
"zh-Hant"
};

public static IEnumerable<string> PlatformRIds => Platforms.Select(platform => platform.RId);

public static IEnumerable<PlatformReg> PlatformsDefault => Platforms.Where(platform => platform.BuildByDefault);
private static IEnumerable<PlatformReg> PlatformsDefault => Platforms.Where(platform => platform.BuildByDefault);

public static void Package(Program.ServerOptions options) {
IEnumerable<PlatformReg> platforms = (options.Platform != null) ? new[] {options.Platform} : PlatformsDefault;
IEnumerable<PlatformReg> platforms = PlatformsDefault;
if (options.Platform != null) {
platforms = new[] { GetPlatform(options.Platform) };
}

if (!options.InPlatformSubDir && options.Platform == null) {
Console.Error.WriteLine(
"Packaging the server without a platform subdirectory requires selecting a single platform");
"Packaging the server without a platform subdirectory requires a '--platform' argument");
}

if (Directory.Exists(options.OutputDir)) {
Expand All @@ -99,6 +100,14 @@ public static void Package(Program.ServerOptions options) {
}
}

public static PlatformReg GetPlatform(string rId) {
var platform = Platforms.FirstOrDefault(p => p.RId == rId);
if (platform == null)
throw new NotSupportedException($"Platform \"{rId}\" is not supported");

return platform;
}

private static void BuildPlatform(PlatformReg platform, Program.ServerOptions options) {
Console.WriteLine($"Building project for {platform.RId}");

Expand Down
34 changes: 32 additions & 2 deletions OpenDreamPackageTool/TgsPackaging.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using Robust.Packaging.Utility;

namespace OpenDreamPackageTool;
Expand All @@ -10,17 +11,18 @@ public static void Package(Program.TgsOptions options) {
Directory.Delete(options.OutputDir, true);
}

var platform = DeterminePlatform();

// Package the server to <output dir>/bin/server
ServerPackaging.Package(new Program.ServerOptions {
OutputDir = Path.Combine(options.OutputDir, "bin", "server"),
Platform = options.Platform,
Platform = platform.RId,
HybridAcz = true, // Force Hybrid ACZ with TGS
SkipBuild = options.SkipBuild,
InPlatformSubDir = false,
TgsEngineBuild = true
});

var platform = options.Platform!;
if (!options.SkipBuild) {
ProcessHelpers.RunCheck(new ProcessStartInfo {
FileName = "dotnet",
Expand Down Expand Up @@ -57,4 +59,32 @@ private static void PublishCompiler(string platformRId, string targetOs) {
}
}).Wait();
}

/// <summary>
/// Determine what platform to package for, based on what OS we're currently running on
/// </summary>
/// <returns>The platform</returns>
private static PlatformReg DeterminePlatform() {
string rId;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
rId = "win";
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
rId = "linux";
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
rId = "osx";
} else {
throw new NotSupportedException("Your OS is not supported");
}

rId += RuntimeInformation.OSArchitecture switch {
Architecture.X64 => "-x64",
Architecture.X86 => "-x86",
Architecture.Arm64 => "-arm64",
Architecture.Arm => "-arm",
_ => throw new NotSupportedException(
$"Your architecture ({RuntimeInformation.OSArchitecture}) is not supported")
};

return ServerPackaging.GetPlatform(rId);
}
}

0 comments on commit b877680

Please sign in to comment.