using System.Diagnostics; using System.IO; using System.Text.RegularExpressions; namespace Build; public class Helpers { /// /// Returns a formatted version of the assembly that contains PowerShell cmdlets /// /// /// public static string GetPowershellModuleVersion(string powershelModuleOutputLocation) { // When the project name changes, this dll will also need to be updated 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; } }