Skip to content

Commit 75464cb

Browse files
committed
Add version management commands and update docs
- Consolidate UpdateVersion: patch increment (no args) or exact set (--VersionPrefix) - Add ShowVersion target to display current version - Rename to BuildTask.Targets.Version.cs - Remove unused Bump parameter - Update README, quick-start, and workflow guide with version commands Made-with: Cursor
1 parent ec1e5ac commit 75464cb

7 files changed

Lines changed: 102 additions & 55 deletions

.nuke/build.schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
"enum": [
2727
"Build",
2828
"Clean",
29+
"GenerateReleaseManifest",
2930
"Pack",
31+
"PackageApp",
3032
"Publish",
3133
"Restore",
34+
"ShowVersion",
3235
"Test",
3336
"UpdateVersion"
3437
]

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,19 @@ The pipeline runs automatically. When `build-and-test` completes, go to **Action
9696

9797
Versions follow a simple rule:
9898

99-
- **Edit `VersionPrefix`** in `Directory.Build.props` via PR (e.g., `0.2.0` -> `1.0.0`)
10099
- **CI appends the build number** on release: `0.2.0` becomes `0.2.0.42`
101100
- **Tags are created automatically**: `v0.2.0.42`
101+
- No manual tagging. No version input fields. The single source of truth is `VersionPrefix` in `Directory.Build.props`.
102102

103-
No manual tagging. No version input fields. Just change the number in one file.
103+
### Version commands
104+
105+
```bash
106+
./build.sh ShowVersion # show current version
107+
./build.sh UpdateVersion # patch increment: 0.2.0 -> 0.2.1
108+
./build.sh UpdateVersion --VersionPrefix 1.0.0 # set exact version
109+
```
110+
111+
Then commit and push -- CI takes care of the rest.
104112

105113
---
106114

build/BuildTask.Parameters.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33

44
partial class BuildTask
55
{
6-
// Stable external build interface (allowed CLI parameters):
7-
// - Configuration
8-
// - VersionPrefix (used only by UpdateVersion target)
9-
// - VersionSuffix (prerelease suffix, e.g., "ci.158")
10-
// - Runtime
11-
// - SelfContained
12-
// VersionPrefix is owned by Directory.Build.props (single source of truth).
13-
146
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
157
readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;
168

@@ -20,6 +12,7 @@ partial class BuildTask
2012
[Parameter("Version suffix for prerelease builds (e.g., ci.158)")]
2113
readonly string VersionSuffix = string.Empty;
2214

15+
2316
// Internal path conventions controlled by build tasks (not external parameters).
2417
readonly string BuildPath = "Dotnet.CI.Template.slnx";
2518
readonly string TestPath = "Dotnet.CI.Template.slnx";

build/BuildTask.Targets.UpdateVersion.cs

Lines changed: 0 additions & 41 deletions
This file was deleted.

build/BuildTask.Targets.Version.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.IO;
3+
using System.Text.RegularExpressions;
4+
using Nuke.Common;
5+
6+
partial class BuildTask
7+
{
8+
const string VersionPrefixPattern = @"<VersionPrefix>\s*([^<]+?)\s*</VersionPrefix>";
9+
10+
Target ShowVersion => _ => _
11+
.Executes(() =>
12+
{
13+
Console.WriteLine($"Current VersionPrefix: {ReadCurrentVersionPrefix()}");
14+
});
15+
16+
Target UpdateVersion => _ => _
17+
.Executes(() =>
18+
{
19+
var current = ReadCurrentVersionPrefix();
20+
21+
if (!string.IsNullOrWhiteSpace(VersionPrefix))
22+
{
23+
if (!Regex.IsMatch(VersionPrefix, @"^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$"))
24+
throw new ArgumentException($"Invalid VersionPrefix: '{VersionPrefix}'. Expected semver format like 1.2.3 or 1.2.3-rc.1.");
25+
WriteVersionPrefix(current, VersionPrefix);
26+
return;
27+
}
28+
29+
var next = PatchIncrement(current);
30+
WriteVersionPrefix(current, next);
31+
});
32+
33+
string ReadCurrentVersionPrefix()
34+
{
35+
var content = File.ReadAllText(DirectoryBuildPropsFile);
36+
var match = Regex.Match(content, VersionPrefixPattern, RegexOptions.Singleline);
37+
if (!match.Success)
38+
throw new InvalidOperationException($"<VersionPrefix> not found in {DirectoryBuildPropsFile}");
39+
return match.Groups[1].Value.Trim();
40+
}
41+
42+
static string PatchIncrement(string current)
43+
{
44+
var baseVersion = current.Split('-')[0];
45+
var parts = baseVersion.Split('.');
46+
if (parts.Length < 3 || !int.TryParse(parts[0], out var major) || !int.TryParse(parts[1], out var minor) || !int.TryParse(parts[2], out var patch))
47+
throw new InvalidOperationException($"Cannot parse current version '{current}' as X.Y.Z.");
48+
49+
return $"{major}.{minor}.{patch + 1}";
50+
}
51+
52+
void WriteVersionPrefix(string current, string next)
53+
{
54+
if (string.Equals(current, next, StringComparison.OrdinalIgnoreCase))
55+
{
56+
Console.WriteLine($"VersionPrefix is already {next}.");
57+
return;
58+
}
59+
60+
var content = File.ReadAllText(DirectoryBuildPropsFile);
61+
var updated = Regex.Replace(
62+
content,
63+
VersionPrefixPattern,
64+
$"<VersionPrefix>{next}</VersionPrefix>",
65+
RegexOptions.Singleline);
66+
67+
File.WriteAllText(DirectoryBuildPropsFile, updated);
68+
Console.WriteLine($"Updated VersionPrefix: {current} -> {next}");
69+
}
70+
}

docs/github-workflows-guide.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,14 @@ push / PR to main + weekly
102102
版本来自 `Directory.Build.props``VersionPrefix`。CI 不接受手动输入版本参数。
103103

104104
### Q2: 如何升级版本?
105-
通过 PR 修改 `VersionPrefix`(例如 `0.1.0 -> 0.2.0`),合并到 `main` 后 CI 自动基于新版本构建。
105+
106+
```bash
107+
./build.sh ShowVersion # 查看当前版本
108+
./build.sh UpdateVersion # patch 递增: 0.2.0 -> 0.2.1
109+
./build.sh UpdateVersion --VersionPrefix 1.0.0 # 精确设置
110+
```
111+
112+
修改后通过 PR 合并到 `main`,CI 自动基于新版本构建。
106113

107114
### Q3: 同一版本能否重新发布?
108115
每次 main push 都会生成唯一的四段式版本号(如 `0.2.0.42`),因此同一 push 不会冲突。如果需要发新版本(如 `0.3.0`),通过 PR 修改 `VersionPrefix`

docs/quick-start-release.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,17 @@ resolve-version → build-and-test (matrix) → release (需 approve) → deploy
4848

4949
---
5050

51-
## 4) 关于版本的两条规则(必读)
51+
## 4) 版本管理(必读)
5252

53-
- 版本来自 `Directory.Build.props``VersionPrefix`,不需要也不能手填。
54-
- 如果要升级版本(例如 `0.1.0 -> 1.0.0`),先通过 PR 修改 `VersionPrefix` 并合并到 `main`,CI 自动基于新版本构建和发布。
53+
版本来自 `Directory.Build.props``VersionPrefix`,CI 自动追加 build number(如 `0.2.0.42`)。
54+
55+
```bash
56+
./build.sh ShowVersion # 查看当前版本
57+
./build.sh UpdateVersion # patch 递增: 0.2.0 -> 0.2.1
58+
./build.sh UpdateVersion --VersionPrefix 1.0.0 # 精确设置
59+
```
60+
61+
修改后提交到 `main`,CI 自动基于新版本构建和发布。
5562

5663
---
5764

0 commit comments

Comments
 (0)