Skip to content

Commit

Permalink
chore: add version-updater csx script
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezanet committed Sep 28, 2023
1 parent cca0a22 commit 0045306
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,4 @@ nupkg
.temp
nuget.config*
.vscode
!.husky/csx
115 changes: 115 additions & 0 deletions .husky/csx/version-updater.csx
Original file line number Diff line number Diff line change
@@ -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<IncrementMode>(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 = @"<Version>(\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
}
5 changes: 5 additions & 0 deletions .husky/task-runner.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 0045306

Please sign in to comment.