diff --git a/.gitignore b/.gitignore index 3010e54..bb0958d 100644 --- a/.gitignore +++ b/.gitignore @@ -405,3 +405,4 @@ nupkg .temp nuget.config* .vscode +!.husky/csx diff --git a/.husky/csx/version-updater.csx b/.husky/csx/version-updater.csx new file mode 100644 index 0000000..4742d77 --- /dev/null +++ b/.husky/csx/version-updater.csx @@ -0,0 +1,115 @@ +#r "System.IO" +using System; +using System.IO; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +var paths = new[] { + @"src/Husky/Husky.csproj", + @"docs\.vuepress\configs\version.ts" +}; + +private string customVersion = null; +private IncrementMode incrementMode = IncrementMode.Build; + +var arg = Args[0]; + +if (Enum.TryParse(arg, true, out var mode)) + incrementMode = mode; +else if (IsValidVersion(arg)) + customVersion = arg; +else +{ + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Invalid argument: " + arg); + Console.WriteLine("Valid arguments: Major, Minor, Build, MajorPreview, MinorPreview, BuildPreview or a Custom version"); + Console.ResetColor(); + return 1; +} + +foreach(var path in paths) +{ + var fullPath = Path.GetFullPath(path); + + var content = await File.ReadAllTextAsync(fullPath); + const string pattern = @"(\d+(\.\d+)*(-\w+)?)<\/Version>"; + var regex = new Regex(pattern, RegexOptions.Compiled); + Match match = regex.Match(content); + + if (!match.Success) + { + // Checking TypeScript files in the docs + regex = new Regex(@"export\s+const\s+version:\s+string\s*=\s*'([^']+)'"); + match = regex.Match(content); + + if (!match.Success) + throw new Exception("Version not found!"); + } + + var currentVersion = match.Groups[1].Value; + var newVersion = !string.IsNullOrEmpty(customVersion) ? customVersion : IncrementVersion(currentVersion, incrementMode); + + var newContent = ReplaceVersion(regex, content, newVersion); + await File.WriteAllTextAsync(fullPath, newContent); + + Console.WriteLine($"{path} updated to version {newVersion}"); +} + +// ------------------ Methods --------------------------- + +string ReplaceVersion(Regex regex, string content, string newVersion) +{ + Match match = regex.Match(content); + + if (match.Success) + { + string originalValue = match.Value; + string newValue = match.Value.Replace(match.Groups[1].Value, newVersion); + + return content.Substring(0, match.Index) + newValue + content.Substring(match.Index + match.Length); + } + + return content; +} + +string IncrementVersion(string currentVersion, IncrementMode incrementMode) +{ + const string previewPattern = @"-preview(\d+)$"; + Match previewMatch = Regex.Match(currentVersion, previewPattern); + var previewNumber = 0; + if (previewMatch.Success) + { + previewNumber = int.Parse(previewMatch.Groups[1].Value); + currentVersion = Regex.Replace(currentVersion, previewPattern, ""); + } + var version = new Version(currentVersion); + return incrementMode switch + { + IncrementMode.Major => new Version(version.Major + 1, 0, 0).ToString(), + IncrementMode.Minor => new Version(version.Major, version.Minor + 1, 0).ToString(), + IncrementMode.Build => new Version(version.Major, version.Minor, version.Build + 1).ToString(), + IncrementMode.MajorPreview when !previewMatch.Success => new Version(version.Major + 1, 0, 0).ToString() + "-preview" + ++previewNumber, + IncrementMode.MinorPreview when !previewMatch.Success => version.Major.ToString() + "." + (version.Minor + 1).ToString() + ".0-preview" + ++previewNumber, + IncrementMode.BuildPreview when !previewMatch.Success => version.Major.ToString() + "." + version.Minor.ToString() + "." + (version.Build + 1).ToString() + "-preview" + ++previewNumber, + IncrementMode.MajorPreview or IncrementMode.MinorPreview or IncrementMode.BuildPreview when previewMatch.Success + => currentVersion + "-preview" + ++previewNumber, + _ => throw new ArgumentOutOfRangeException(nameof(incrementMode), incrementMode, null) + }; +} + +bool IsValidVersion(string customVersion) +{ + string versionPattern = @"^\d+\.\d+\.\d+(-\w+)?$"; + return Regex.IsMatch(customVersion, versionPattern); +} + + +public enum IncrementMode +{ + Major, + Minor, + Build, + MajorPreview, + MinorPreview, + BuildPreview +} diff --git a/.husky/task-runner.json b/.husky/task-runner.json index 52765a2..2321858 100644 --- a/.husky/task-runner.json +++ b/.husky/task-runner.json @@ -19,6 +19,11 @@ "args": ["dotnet-format", "--include" , "${staged}"], "include": ["**/*.cs"] }, + { + "name": "update-version", + "command": "dotnet", + "args" :["husky", "exec", ".husky/csx/version-updater.csx", "--args", "${args}"] + }, { "name": "echo staged files", "pathMode": "absolute",