Skip to content

Commit 4cc535e

Browse files
authored
Added support for CSemVer v1.0.0-rc.1 pre-release formatting (#26)
1 parent 6f6a779 commit 4cc535e

File tree

8 files changed

+325
-62
lines changed

8 files changed

+325
-62
lines changed

Show-Docs.ps1

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@ using module "PSModules/RepoBuild/RepoBuild.psd1"
55
.SYNOPSIS
66
Shows docs using the docfx built-in server
77
8-
.PARAMETER DocsPathToHost
9-
Path of docs to host. Default is the local build but any legit path is accepted.
10-
This is useful when downloading docs build artifacts to ensure they are built correctly.
11-
128
.PARAMETER buildInfo
139
BuildInfo to use for current repo build. The hashtable provided for this must include the
1410
`DocsOutputPath` value for the path to use. This is normally set by a call to `Initialize-Environment`
11+
12+
.PARAMETER DocsPathToHost
13+
Path of docs to host. Default is the local build but any legit path is accepted.
14+
This is useful when downloading docs build artifacts to ensure they are built correctly.
1515
#>
1616
Param(
17-
[Parameter(Mandatory, ParameterSetName = 'UsePath', Position = 0)]
18-
[string]$DocsPathToHost,
19-
20-
[Parameter(ParameterSetName = 'UseRepo', Position = 0)]
21-
[hashtable]$buildInfo
17+
[hashtable]$buildInfo,
18+
[string]$DocsPathToHost
2219
)
2320

2421
$docFXToolVersion = '2.78.3'
@@ -30,7 +27,7 @@ Push-Location $PSScriptRoot
3027
$oldPath = $env:Path
3128
try
3229
{
33-
if ($PSCmdlet.ParameterSetName -eq 'UseRepo')
30+
if (!$DocsPathToHost)
3431
{
3532
if (!$buildInfo)
3633
{

docfx/build-tasks/index.md

Lines changed: 86 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ using a Constrained Semantic Version ([CSemVer](https://csemver.org/)).
1717
> who are aware of the change and those who use this library to set an explicit build meta data
1818
> string. (Principle of least surprise for what this library can control).
1919
>
20-
> The default OPT-OUT behavior is to use the Repository ID (usually a GIT commit hash). This
21-
> is appended with a leading `+` if one isn't already in the `InformationalVersion` property. If
22-
> build metadata is already included (Like from use of this task) the id is appended using a `.`
23-
> instead. Unless the project has opted out of this behavior by setting the property as
24-
> previously described.
20+
> The default .NET build behavior is to use the Repository ID (usually a GIT commit hash).
21+
> This is appended with a leading `+` if one isn't already in the `InformationalVersion`
22+
> property. If build metadata is already included (Like from use of this task) the id is
23+
> appended using a `.` instead. So it is ALWAYS appended unless the project has opted out of
24+
> this behavior by setting the property as previously described.
2525
>
26-
>Thus, it is ***strongly recommended*** that projects using this package OPT-OUT
26+
> Thus, it is ***strongly recommended*** that projects using this package OPT-OUT
2727
> of the new behavior.
2828
2929

@@ -50,13 +50,13 @@ The following is a list of the version formats in descending order of precedence
5050

5151
| Build Type | Format |
5252
|------------|--------|
53-
| Local build | `{BuildMajor}.{BuildMinor}.{BuildPatch}--ci-{UTCTIME of build }-ZZZ` |
54-
| Pull Request | `{BuildMajor}.{BuildMinor}.{BuildPatch}--ci-{UTCTIME of PR Commit}-PRQ+{COMMIT ID}` |
55-
| Official CI builds | `{BuildMajor}.{BuildMinor}.{BuildPatch}--ci-{UTCTIME of HEAD Commit}-BLD+{COMMIT ID}` |
56-
| Official PreRelease | `{BuildMajor}.{BuildMinor}.{BuildPatch}-{PreReleaseName}[.PreReleaseNumber][.PreReleaseFix]+{COMMIT ID}` |
57-
| Official Release | `{BuildMajor}.{BuildMinor}.{BuildPatch}+{COMMIT ID}` |
53+
| Local build | `{BuildMajor}.{BuildMinor}.{BuildPatch}--ci-{CiBuildIndex}-ZZZ+{BuildMeta}` |
54+
| Pull Request | `{BuildMajor}.{BuildMinor}.{BuildPatch}--ci-{CiBuildIndex}-PRQ+{BuildMeta}` |
55+
| Official CI builds | `{BuildMajor}.{BuildMinor}.{BuildPatch}--ci-{CiBuildIndex}-BLD+{BuildMeta}` |
56+
| Official PreRelease | `{BuildMajor}.{BuildMinor}.{BuildPatch}-{PreReleaseName}[.PreReleaseNumber][.PreReleaseFix]+{BuildMeta}` |
57+
| Official Release | `{BuildMajor}.{BuildMinor}.{BuildPatch}+{BuildMeta}` |
5858

59-
This package provides a single package to automate the generation of these versions in an easy
59+
This project provides an MSBUILD task to automate the generation of these versions in an easy
6060
to use NuGet Package.
6161

6262
The package creates File and Assembly Versions and defines the appropriate MsBuild properties
@@ -92,6 +92,9 @@ CSemVer.Build uses MSBuild properties to determine the final version number.
9292
| BuildMeta | `<undefined>` | Build meta for the version
9393
| CiBuildIndex | ISO 8601 formated UTC time-stamp for the build or the commit ID for automated builds | Provides a unique build to build value guaranteed to increase with each build
9494
| CiBuildName | `<see notes>` | CSemVer CI name
95+
| PackageVersionUsesShortForm | `<Undefined>` => false | Determines if the PackageVersion is set to the short form for legacy NuGet clients |
96+
| GeneratedVersionInfoHeader | `<undefined>` | Full path of the header file to generate [No header generated if not set] [Only applies to a VCXPROJ file ] |
97+
| GenerateAssemblyInfo | `<undefined>` | If set, creates `$(IntermediateOutputPath)AssemblyVersionInfo.g.cs` and includes it for compilation. Used for legacy CSPROJ files as new SDK projects handle this automatically now |
9598

9699
### CiBuildName
97100
Unless explicitly provided, the CiBuildName is determined by a set of properties that indicate
@@ -124,6 +127,11 @@ example for setting them based on an AppVeyor build in the `Directory.Build.prop
124127
</PropertyGroup>
125128
```
126129

130+
### PackageVersionUsesShortForm
131+
The MSBuild Property `PackageVersionUsesShortForm` is used by the task to determine the format to use for the
132+
`PackageVersion` property. The default is to use the long form supported by modern NuGet clients, however if your are
133+
in need of the legacy format support a project may set this property to `true` to observe the legacy behavior.
134+
127135
## BuildVersion.xml
128136
If the MSBuild property `BuildMajor` is not set, then the base build version is read from the
129137
repository file specified in the BuildVersion.xml, typically this is located at the root of a
@@ -132,13 +140,71 @@ the file is specified by an MSBuild property `BuildVersionXml`. The contents of
132140
fairly simple and only requires a single `BuildVersionData` element with a set of attributes.
133141
The available attributes are:
134142

135-
|Name |Description|
136-
|-------------------|-----------|
137-
| BuildMajor | Major portion of the build number |
138-
| BuildMinor | Minor portion of the build number |
139-
| BuildPatch | Patch portion of the build number |
140-
| PreReleaseName | PreRelease Name of the CSemVer |
141-
| PreReleaseNumber | PreRelease Number of the CSemVer |
142-
| PreReleaseFix | PreRelease Fix of the CSemVer |
143+
|Name |Description|
144+
|------------------|-----------|
145+
| BuildMajor | Major portion of the build number |
146+
| BuildMinor | Minor portion of the build number |
147+
| BuildPatch | Patch portion of the build number |
148+
| PreReleaseName | PreRelease Name of the CSemVer |
149+
| PreReleaseNumber | PreRelease Number of the CSemVer |
150+
| PreReleaseFix | PreRelease Fix of the CSemVer |
143151

144152
Only the Major, minor and Patch numbers are required.
153+
Example:
154+
```xml
155+
<BuildVersionData
156+
BuildMajor = "5"
157+
BuildMinor = "0"
158+
BuildPatch = "0"
159+
PreReleaseName = "alpha"
160+
/>
161+
```
162+
163+
## Generated Properties
164+
|Name |Description|
165+
|--------------------|-----------|
166+
| BuildTime | Set to the current time (UTC ISO-8601 format) if not already set by build tooling) |
167+
| IsAutomatedBuild | Automated build system value to indicate this is an automated build |
168+
| IsPullRequestBuild | Automated build system value to indicate this is a build for an untrusted PR |
169+
| IsReleaseBuild | Automated build system value to indicate this is an official release build (No CI information) |
170+
| CiBuildName | If not set externally, this is set based on the kind of build |
171+
| CiBuildIndex | If not set externally, this is set based on the $(BuildTime) property by parsing the ISO-8601 string and computing an index from that |
172+
| BuildMajor | Major portion of the build; If not set externally, this is set based on the information in the $(BuildVersionXml) file |
173+
| BuildMinor | Minor portion of the build; If not set externally, this is set based on the information in the $(BuildVersionXml) file |
174+
| BuildPatch | Patch portion of the build; If not set externally, this is set based on the information in the $(BuildVersionXml) file |
175+
| PreReleaseName | PreRelease Name for the build [Optional]; If not set externally, this is set based on the information in the $(BuildVersionXml) file, which may not include a value for this |
176+
| PreReleaseNumber | PreRelease Number for the build [Optional]; If not set externally, this is set based on the information in the $(BuildVersionXml) file, which may not include a value for this |
177+
| PreReleaseFix | PreRelease Fix for the build [Optional]; If not set externally, this is set based on the information in the $(BuildVersionXml) file, which may not include a value for this |
178+
| FullBuildNumber | String form of the full CSemVer value for a build |
179+
| ShortBuildNumber | Short form of the CSemVer for use with legacy NuGet clients (Modern clients support the full name) |
180+
| FileVersionMajor | Major portion of the FileVersion number (Used for vcxproj files to generate the version header) |
181+
| FileVersionMinor | Minor portion of the FileVersion number (Used for vcxproj files to generate the version header) |
182+
| FileVersionBuild | Build portion of the FileVersion number (Used for vcxproj files to generate the version header) |
183+
| FileVersionRevision | Revision portion of the FileVersion number (Used for vcxproj files to generate the version header) |
184+
185+
### BuildTime
186+
Ordinarily this is set for an entire solution by build scripting to ensure that all components using this
187+
build task report the same version number. If it is not set the current time at the moment of property evaluation
188+
for a project is used. This will result in a distinct CI version for each project in a solution. Whether, that
189+
is desired or not is left for the consumer. If it is not desired, then a centralized setting as a build property
190+
or environment variable is warranted.
191+
192+
## Automated build flags
193+
`IsAutomatedBuild`, `IsPullRequestBuild`, and `IsReleaseBuild` are normally set by an automated build script/action
194+
based on the build environment used and aid in determining the CI build name as previously described in
195+
[CiBuildName](#cibuildname).
196+
197+
## CiBuildName
198+
If not explicitly set this is determined by the automated build flags as described in the [CiBuildName](#cibuildname)
199+
section of this document.
200+
201+
## Detected Error Conditions
202+
|Code |Description|
203+
|---------|-----------|
204+
| CSM001 | BuildMajor is a required property, either set it as a global or in the build version XML |
205+
| CSM002 | BuildMinor is a required property, either set it as a global or in the build version XML |
206+
| CSM003 | BuildPatch is a required property, either set it as a global or in the build version XML |
207+
| CSM004 | FileVersion property not provided AND FileVersionMajor property not found to create it from |
208+
| CSM005 | FileVersion property not provided AND FileVersionMinor property not found to create it from |
209+
| CSM006 | FileVersion property not provided AND FileVersionBuild property not found to create it from |
210+
| CSM007 | FileVersion property not provided AND FileVersionRevision property not found to create it from |

src/Ubiquity.NET.Versioning.Build.Tasks/CreateVersionInfo.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public override bool Execute( )
9292
CSemVer = CreateSemVerString( preRelIndex );
9393
Log.LogMessage(MessageImportance.Low, "CSemVer={0}", CSemVer ?? string.Empty);
9494

95-
ShortCSemVer = CreateSemVerString( preRelIndex, useShortForm: true );
95+
ShortCSemVer = CreateSemVerString( preRelIndex, useShortForm: true, includeMetadata: false );
9696
Log.LogMessage(MessageImportance.Low, "ShortCSemVer={0}", ShortCSemVer ?? string.Empty);
9797

9898
SetFileVersion( preRelIndex );
@@ -109,9 +109,21 @@ public override bool Execute( )
109109
}
110110
}
111111

112-
private string CreateSemVerString( int preRelIndex, bool useShortForm = false, bool includeMetadata = true )
112+
/// <summary>Creates a formatted CSemver from the properties of this instance</summary>
113+
/// <param name="preRelIndex">Numeric form of the build index [(-1)-7] where -1 indicates not a pre-release</param>
114+
/// <param name="useShortForm">Flag to indicate if the short form is desired</param>
115+
/// <param name="includeMetadata">Flag to indicate if the metadata is included</param>
116+
/// <param name="alwaysIncludeZero">Flag to indicate if a 0 pre-release is ALWAYs included [see remarks]</param>
117+
/// <returns>Formatted CSemVer string</returns>
118+
/// <remarks>
119+
/// <para>The <paramref name="alwaysIncludeZero"/> is for legacy behavior and should generally be left at the default. In
120+
/// prior releases the pre-release number and fix were always included for pre-release builds in the short form. In
121+
/// the current version based on CSemVer v1.0.0-rc.1 the behavior is the same as for a full version. (The Number is omitted
122+
/// unless it is > 0 OR Fix >0).</para>
123+
/// <para>Additionally, the short form uses a two digit leading 0 conversion.</para>
124+
/// </remarks>
125+
private string CreateSemVerString( int preRelIndex, bool useShortForm = false, bool includeMetadata = true, bool alwaysIncludeZero = false)
113126
{
114-
bool alwaysIncludeZero = useShortForm;
115127
var bldr = new StringBuilder()
116128
.AppendFormat(CultureInfo.InvariantCulture, "{0}.{1}.{2}", BuildMajor, BuildMinor, BuildPatch);
117129

@@ -123,10 +135,10 @@ private string CreateSemVerString( int preRelIndex, bool useShortForm = false, b
123135

124136
if(PreReleaseNumber > 0 || PreReleaseFix > 0 || alwaysIncludeZero)
125137
{
126-
bldr.AppendFormat( CultureInfo.InvariantCulture, ".{0}", PreReleaseNumber );
138+
bldr.AppendFormat( CultureInfo.InvariantCulture, useShortForm ? "{0:D02}" : ".{0}", PreReleaseNumber );
127139
if(PreReleaseFix > 0 || alwaysIncludeZero)
128140
{
129-
bldr.AppendFormat( CultureInfo.InvariantCulture, ".{0}", PreReleaseFix );
141+
bldr.AppendFormat( CultureInfo.InvariantCulture, useShortForm ? "-{0:D02}" : ".{0}", PreReleaseFix );
130142
}
131143
}
132144
}
@@ -251,6 +263,15 @@ private bool ValidateInput( )
251263
return !hasInputError;
252264
}
253265

266+
//private void LogError(
267+
// string code,
268+
// /*[StringSyntax(StringSyntaxAttribute.CompositeFormat)]*/ string message,
269+
// params object[] messageArgs
270+
// )
271+
//{
272+
// Log.LogError("Task", code, null, null, 0, 0, 0, 0, message, messageArgs);
273+
//}
274+
254275
private static int ComputePreReleaseIndex( string preRelName )
255276
{
256277
int index = Find( PreReleaseNames, preRelName ).Index;

src/Ubiquity.NET.Versioning.Build.Tasks/build/Ubiquity.NET.Versioning.Build.Tasks.targets

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@
5454
BuildMeta = "$(BuildMeta)"
5555
>
5656
<Output TaskParameter="CSemVer" PropertyName="FullBuildNumber" />
57+
<Output TaskParameter="ShortCSemVer" PropertyName="ShortBuildNumber"/>
5758
<Output TaskParameter="FileVersionMajor" PropertyName="FileVersionMajor" />
5859
<Output TaskParameter="FileVersionMinor" PropertyName="FileVersionMinor" />
5960
<Output TaskParameter="FileVersionBuild" PropertyName="FileVersionBuild" />
6061
<Output TaskParameter="FileVersionRevision" PropertyName="FileVersionRevision" />
61-
<Output TaskParameter="ShortCSemVer" PropertyName="PackageVersion"/>
6262
</CreateVersionInfo>
6363
</Target>
6464

@@ -68,6 +68,12 @@
6868
<FileVersion Condition="'$(FileVersion)'==''">$(__FileVersion)</FileVersion>
6969
<AssemblyVersion Condition="'$(AssemblyVersion)'==''">$(__FileVersion)</AssemblyVersion>
7070
<InformationalVersion Condition="$(InformationalVersion)==''">$(FullBuildNumber)</InformationalVersion>
71+
<!--
72+
NuGet clients prior to 4.3.0 don't support the full name, and instead should use the short name.
73+
Default is to use the newer format, but allow specification of the legacy format if needed.
74+
-->
75+
<PackageVersion Condition="$(FullBuildNumber)!='' AND '$(PackageVersionUsesShortForm)'!='true'">$(FullBuildNumber)</PackageVersion>
76+
<PackageVersion Condition="$(ShortBuildNumber)!='' AND '$(PackageVersionUsesShortForm)'=='true'">$(ShortBuildNumber)</PackageVersion>
7177
</PropertyGroup>
7278
</Target>
7379

0 commit comments

Comments
 (0)