diff --git a/build/Tasks/CreateBundleArchiveTask.cs b/build/Tasks/CreateBundleArchiveTask.cs index 54413b1..0abd876 100644 --- a/build/Tasks/CreateBundleArchiveTask.cs +++ b/build/Tasks/CreateBundleArchiveTask.cs @@ -1,5 +1,9 @@ -using System.IO; +using System.Diagnostics; +using System.IO; using System.IO.Compression; +using System.Text.RegularExpressions; +using Cake.Common.Build; +using Cake.Common.Tools.MSBuild; using Cake.Core.Diagnostics; using Cake.Frosting; @@ -75,7 +79,8 @@ public class CreateBundleArchiveTask : FrostingTask context.Log.Information($"Bundle files copied"); GenerateModuleImportScript(moduleFolderLocation, context); - CreateBundleZip(bundleRootLocation, moduleFolderLocation, context); + var moduleVersionForFilename = GetPowershellModuleVersion(powershelModuleOutputLocation); + CreateBundleZip(bundleRootLocation, moduleFolderLocation, moduleVersionForFilename, context); } private static void GenerateModuleImportScript(string bundleLocation, BuildContext context) @@ -120,11 +125,45 @@ public class CreateBundleArchiveTask : FrostingTask context.Log.Information($"Module import script created at '{scriptFileName}'"); } - private static void CreateBundleZip(string bundleLocation, string moduleFolderLocation, BuildContext context) + private static void CreateBundleZip(string bundleLocation, string moduleFolderLocation, string version, BuildContext context) { - var bundleZipFileLocaiton = Path.Combine(bundleLocation, "NulahModule.zip"); + var bundleZipFileLocaiton = Path.Combine(bundleLocation, $"NulahModule-{version}.zip"); context.Log.Information($"Bundling module into archive '{bundleZipFileLocaiton}'"); ZipFile.CreateFromDirectory(moduleFolderLocation, bundleZipFileLocaiton, CompressionLevel.Fastest, true); context.Log.Information($"Archive created at '{bundleZipFileLocaiton}'"); } + + private static string GetPowershellModuleVersion(string powershelModuleOutputLocation) + { + var moduleVersionInfo = FileVersionInfo.GetVersionInfo(Path.Combine(powershelModuleOutputLocation, "PowershellModule.dll")); + + // It's (almost) impossible to not have a product version tag here. There is a reason why the implementation + // returns a string? but I can't find it and I don't really care too much. If we have null return an empty string + // and append no version to the archive bundle. + if (moduleVersionInfo.ProductVersion == null) + { + return string.Empty; + } + + var versionRegex = new Regex(@"(\d+\.\d+\.\d+)(?:\-?([\w\-]+)\+?(\w+)?)?"); + var match = versionRegex.Match(moduleVersionInfo.ProductVersion); + if (match.Success) + { + // If we have 4 groups, we've got a version number, suffix, and commit hash, so we return the first 2 as is + // and the commit has capped to 8 characters + if (match.Groups.Count == 4) + { + return $"{match.Groups[1]}-{match.Groups[2]}+{match.Groups[3].Value.Substring(0, 8)}"; + } + + if (match.Groups.Count == 3) + { + return $"{match.Groups[1]}-{match.Groups[2]}"; + } + + return $"{match.Groups[1]}"; + } + + return string.Empty; + } } \ No newline at end of file