diff --git a/GameBundle/Options.cs b/GameBundle/Options.cs index 9dcd2a0..55a10d7 100644 --- a/GameBundle/Options.cs +++ b/GameBundle/Options.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; using CommandLine; -namespace GameBundle; +namespace GameBundle; public class Options { @@ -65,5 +65,7 @@ public class Options { public bool NameBuilds { get; set; } [Option('N', "name-addition", HelpText = "An additional string of text that should be included in the names of the output directories")] public string NameAddition { get; set; } + [Option('V', "include-version", HelpText = "Include the project's version in the names of the output directories")] + public bool IncludeVersion { get; set; } } \ No newline at end of file diff --git a/GameBundle/Program.cs b/GameBundle/Program.cs index c251eb8..774318d 100644 --- a/GameBundle/Program.cs +++ b/GameBundle/Program.cs @@ -7,7 +7,7 @@ using System.Linq; using System.Text.RegularExpressions; using CommandLine; -namespace GameBundle; +namespace GameBundle; internal static class Program { @@ -74,19 +74,26 @@ internal static class Program { return beautyResult; } + // Add version if named builds are enabled + if (options.IncludeVersion) { + var version = Program.GetBuildVersion(buildDir); + if (version == null) { + Console.WriteLine("Couldn't determine build version, aborting"); + return -1; + } + var dest = Path.Combine(buildDir.Parent.FullName, $"{version}-{buildDir.Name}"); + Program.MoveDirectory(options, buildDir, dest); + } + // Rename build folder if named builds are enabled if (options.NameBuilds) { - var name = Program.GetBuildName(options, buildDir); + var name = Program.GetBuildName(buildDir); if (name == null) { Console.WriteLine("Couldn't determine build name, aborting"); return -1; } var dest = Path.Combine(buildDir.Parent.FullName, $"{name}-{buildDir.Name}"); - if (Directory.Exists(dest)) - Directory.Delete(dest, true); - buildDir.MoveTo(dest); - if (options.Verbose) - Console.WriteLine($"Moved build directory to {buildDir.FullName}"); + Program.MoveDirectory(options, buildDir, dest); } // Run any additional actions like creating the mac bundle @@ -134,7 +141,7 @@ internal static class Program { } private static int CreateMacBundle(Options options, DirectoryInfo buildDir) { - var buildName = Program.GetBuildName(options, buildDir); + var buildName = Program.GetBuildName(buildDir); var app = buildDir.CreateSubdirectory($"{buildName}.app"); var contents = app.CreateSubdirectory("Contents"); var resources = contents.CreateSubdirectory("Resources"); @@ -183,19 +190,38 @@ internal static class Program { return new DirectoryInfo(Path.Combine(Path.GetFullPath(options.OutputDirectory), name)); } - private static string GetBuildName(Options options, DirectoryInfo buildDir) { - // determine build name based on the names of the exe or binary that have a matching dll file + private static string GetBuildName(DirectoryInfo buildDir) { + return Path.GetFileNameWithoutExtension(Program.GetAssemblyFile(buildDir)?.Name); + } + + private static string GetBuildVersion(DirectoryInfo buildDir) { + var assemblyFile = Program.GetAssemblyFile(buildDir); + if (assemblyFile == null) + return null; + var version = FileVersionInfo.GetVersionInfo(assemblyFile.FullName); + return version.ProductVersion; + } + + private static FileInfo GetAssemblyFile(DirectoryInfo buildDir) { var files = buildDir.GetFiles(); foreach (var file in files) { - if (file.Extension != ".exe" && file.Extension != string.Empty) + if (file.Extension != ".dll") continue; - var name = Path.GetFileNameWithoutExtension(file.Name); - if (files.Any(f => f.Extension == ".dll" && Path.GetFileNameWithoutExtension(f.Name) == name)) - return name; + // the assembly is (most likely) the dll file that has a matching binary or exe file in the same location + if (files.Any(f => f.Extension is ".exe" or "" && Path.GetFileNameWithoutExtension(f.Name) == Path.GetFileNameWithoutExtension(file.Name))) + return file; } return null; } + private static void MoveDirectory(Options options, DirectoryInfo dir, string dest) { + if (Directory.Exists(dest)) + Directory.Delete(dest, true); + dir.MoveTo(dest!); + if (options.Verbose) + Console.WriteLine($"Moved build directory to {dir.FullName}"); + } + private readonly struct BuildConfig { public readonly string DisplayName; diff --git a/Test/Bundle.bat b/Test/Bundle.bat index e8a6459..4b1a8d7 100644 --- a/Test/Bundle.bat +++ b/Test/Bundle.bat @@ -1 +1,2 @@ -"../GameBundle/bin/Debug/net6.0/GameBundle.exe" -wlmWL -bzn -s Test.csproj -o bin/Bundled -v --mac-bundle-ignore macmain.txt -N 1.0.0 \ No newline at end of file +rmdir /S /Q "bin/Bundled" +"../GameBundle/bin/Debug/net6.0/GameBundle.exe" -wlmWL -bznV -s Test.csproj -o bin/Bundled -v --mac-bundle-ignore macmain.txt -N beta \ No newline at end of file diff --git a/Test/Test.csproj b/Test/Test.csproj index 05870fb..ed0cf4d 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -6,6 +6,7 @@ false false Test Project + 1.2.3