Skip to content

Commit ca66be5

Browse files
committed
Set Version and PackageVersion to a default value
It's not entirely intuitive that you install GitInfo and by default "nothing happens", as in, you get new code from ThisAssembly, but otherwise, no versioning changes happen. This might be counterintuitive to newcomers, who might just care to get something (a sensible default) going, before investing more in customizing GitInfo. This is how MinVer does it (provided there's a version tag in the repository) and it's quite intuitive. The way we do it here should be mostly backwards compat because: 1. Users leveraging assembly-version attributes will already have disabled .NET SDK assembly attribute generation 2. Users leveraging MSBuild targets to do this would be following the recommended guidance of creating a target that runs before specific targets, not as InitialTargets We make our version-setting target run as an inital target, which may have a minor impact on initial build perf, but but shouldn't be a problem since the same targets are run for DTB anyway (if you make them run before GetAssemblyVersion, which also runs before BeforeCompile and CoreCompile in the SDK). By being an initial target, we allow for existing customization in pretty much any target, prevail our own one, which becomes just a default. Even so, we check for GitVersion=false as an escape hatch for users who may encounter unforseen broken scenarios.
1 parent d485b19 commit ca66be5

File tree

2 files changed

+68
-57
lines changed

2 files changed

+68
-57
lines changed

readme.md

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,34 @@ After installing via [NuGet](https://www.nuget.org/packages/GitInfo):
1717

1818
PM> Install-Package GitInfo
1919

20-
By default, if the containing project is a C#, F# or VB project, a compile-time generated source file will contain
21-
all the git information and can be accessed from anywhere within the assembly, as constants in a
22-
`ThisAssembly` (partial) class and its nested `Git` static class:
20+
By default, if the containing project is a C#, F# or VB project, a compile-time generated
21+
source file will contain all the git information and can be accessed from anywhere within
22+
the assembly, as constants in a `ThisAssembly` (partial) class and its nested `Git` static class:
2323

2424
Console.WriteLine(ThisAssembly.Git.Commit);
2525

26-
All generated constants also have a Summary documentation tag that shows the current
27-
value in the intellisense tooltip, making it easier to see what the different values contain:
28-
29-
![](https://raw.githubusercontent.com/devlooped/GitInfo/main/assets/images/tooltip.png)
30-
3126
> NOTE: you may need to close and reopen the solution in order
3227
> for Visual Studio to refresh intellisense and show the
3328
> ThisAssembly type the first time after installing the package.
3429
35-
With this information at your fingertips, you can build any versioning attributes you want,
30+
By default, GitInfo will also set `$(Version)` and `$(PackageVersion)` which the .NET
31+
SDK uses for deriving the AssemblyInfo, FileVersion and InformationalVersion values,
32+
as well as for packing. This default version is formatted from the following populated
33+
MSBuild properties: `$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit)`.
34+
35+
So, straight after install and build/pack, you will get some versioning in place :).
36+
37+
Alternatively, you can opt-out of this default versioning by setting `GitVersion=false`
38+
in your project file, if you want to just leverage the Git information and/or version
39+
properties/constants yourself:
40+
41+
```xml
42+
<PropertyGroup>
43+
<GitVersion>false</GitVersion>
44+
</PropertyGroup>
45+
```
46+
47+
This allows you to use the provided constants to build any versioning attributes you want,
3648
with whatever information you want, without resorting to settings, format strings or anything,
3749
just plain code:
3850

@@ -82,23 +94,42 @@ VB:
8294
ThisAssembly.Git.Commit)>
8395
```
8496

97+
> NOTE: when generating your own assembly version attributes, you will need to turn off
98+
> the corresponding assembly version attribute generation from the .NET SDK, by setting
99+
> the relevant properties to false: `GenerateAssemblyVersionAttribute`,
100+
> `GenerateAssemblyFileVersionAttribute` and `GenerateAssemblyInformationalVersionAttribute`.
101+
102+
85103
MSBuild:
86104

87105
```
88106
<!-- Just edit .csproj file -->
107+
<PropertyGroup>
108+
<!-- We'll do our own versioning -->
109+
<GitVersion>false</GitVersion>
110+
</PropertyGroup>
89111
<ItemGroup>
90112
<PackageReference Include="GitInfo" PrivateAssets="all" />
91113
</ItemGroup>
92114
93-
<Target Name="PopulateInfo" DependsOnTargets="GitInfo" BeforeTargets="PrepareForBuild">
115+
<Target Name="PopulateInfo" DependsOnTargets="GitVersion" BeforeTargets="GetAssemblyVersion;GenerateNuspec;GetPackageContents">
94116
<PropertyGroup>
117+
<Version>$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit)</Version>
118+
<PackageVersion>$(Version)</PackageVersion>
119+
95120
<RepositoryBranch>$(GitBranch)</RepositoryBranch>
96121
<RepositoryCommit>$(GitCommit)</RepositoryCommit>
97122
<SourceRevisionId>$(GitBranch) $(GitCommit)</SourceRevisionId>
98123
</PropertyGroup>
99124
</Target>
100125
```
101126

127+
> NOTE: because the provided properties are populated via targets that need to run
128+
> before they are available, you cannot use the GitInfo-provided properties in a
129+
> PropertyGroup at the project level. You can only use them from within a target that
130+
> in turn depends on the relevant target from GitInfo (typically, `GitVersion` as
131+
> shown above, if you consume the SemVer properties).
132+
102133
Because this information is readily available whenever you build the project, you
103134
never depend on CI build scripts that generate versions for you, and you can
104135
always compile locally exactly the same version of an assembly that was built by
@@ -107,22 +138,6 @@ a CI server.
107138
You can read more about this project at the
108139
[GitInfo announcement blog post](http://www.cazzulino.com/git-info-from-msbuild-and-code.html).
109140

110-
### MSBuild
111-
112-
If you want to set other properties in your project, such as `$(Version)` or `$(PackageVersion)`
113-
based on the populated values from GitInfo, you must do so from a target, such as:
114-
115-
```xml
116-
<Target Name="_SetVersion" BeforeTargets="GetAssemblyVersion;GenerateNuspec;GetPackageContents">
117-
<PropertyGroup>
118-
<Version>$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit)</Version>
119-
<PackageVersion>$(Version)</PackageVersion>
120-
</PropertyGroup>
121-
</Target>
122-
```
123-
124-
In this case, we're setting the version and package version.
125-
126141
## Details
127142

128143
Exposes the following information for use directly from any MSBuild
@@ -148,31 +163,8 @@ target that depends on the GitInfo target:
148163
$(GitIsDirty)
149164
```
150165

151-
From C#, F# and VB, by default code is generated too so that the same
152-
information can be accessed from code, to construct your own
153-
assembly/file version attributes with whatever format you want:
154-
155-
```csharp
156-
[assembly: AssemblyVersion (ThisAssembly.Git.SemVer.Major + "." + ThisAssembly.Git.SemVer.Minor + "." + ThisAssembly.Git.SemVer.Patch)]
157-
[assembly: AssemblyInformationalVersion (
158-
ThisAssembly.Git.SemVer.Major + "." +
159-
ThisAssembly.Git.SemVer.Minor + "." +
160-
ThisAssembly.Git.SemVer.Patch + "-" +
161-
ThisAssembly.Git.Branch + "+" +
162-
ThisAssembly.Git.Commit)]
163-
// i..e ^: 1.0.2-main+c218617
164-
```
165-
166-
> NOTE: you may need to close and reopen the solution in order
167-
> for Visual Studio to refresh intellisense and show the
168-
> ThisAssembly type right after package installation for
169-
> the first time.
170-
171-
All generated constants also have a Summary documentation tag
172-
that shows the current value in the intellisense tooltip, making
173-
it very easy to see what the different values contain.
174-
175-
The available constants from code are:
166+
For C#, F# and VB, constants are generated too so that the same information can be
167+
accessed from code:
176168

177169
```
178170
ThisAssembly.Git.RepositoryUrl
@@ -193,15 +185,21 @@ The available constants from code are:
193185
ThisAssembly.Git.IsDirty
194186
```
195187

196-
Available [MSBuild properties](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-properties):
188+
Available [MSBuild properties](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-properties)
189+
to customize the behavior:
197190

198191
```
192+
$(GitVersion): set to 'false' to prevent setting Version
193+
and PackageVersion.
194+
199195
$(GitThisAssembly): set to 'false' to prevent assembly
200196
metadata and constants generation.
201197
202198
$(GitThisAssemblyMetadata): set to 'false' to prevent assembly
203199
metadata generation only. Defaults
204-
to 'false'.
200+
to 'false'. If 'true', it will also
201+
provide assembly metadata attributes
202+
for each of the populated values.
205203
206204
$(ThisAssemblyNamespace): allows overriding the namespace
207205
for the ThisAssembly class.

src/GitInfo/build/GitInfo.targets

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" InitialTargets="SetGitExe" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Project ToolsVersion="4.0" InitialTargets="SetGitExe;GitSetVersion" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<!--
44
==============================================================
55
Retrieves and exposes Git information
@@ -12,12 +12,18 @@
1212
1313
Customization:
1414
15+
$(GitVersion): set to 'false' to avoid setting Version and PackageVersion
16+
to a default version with format:
17+
$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit)
18+
1519
$(GitThisAssembly): set to 'false' to prevent assembly
1620
metadata and constants generation.
1721
18-
$(GitThisAssemblyMetadata): set to 'false' to prevent assembly
19-
metadata generation only. Defaults
20-
to 'false'.
22+
$(GitThisAssemblyMetadata): set to 'false' to prevent assembly
23+
metadata generation only. Defaults
24+
to 'false'. If 'true', it will also
25+
provide assembly metadata attributes
26+
for each of the populated values.
2127
2228
$(ThisAssemblyNamespace): allows overriding the namespace
2329
for the ThisAssembly class.
@@ -563,6 +569,13 @@
563569

564570
<Target Name="GitVersion" DependsOnTargets="$(GitVersionDependsOn)" Returns="@(GitInfo)" />
565571

572+
<Target Name="GitSetVersion" DependsOnTargets="GitVersion" Condition="'$(GitVersion)' != 'false'">
573+
<PropertyGroup>
574+
<Version>$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit)</Version>
575+
<PackageVersion>$(Version)</PackageVersion>
576+
</PropertyGroup>
577+
</Target>
578+
566579
<Target Name="_GitBaseVersionFile" Returns="$(GitBaseVersion)"
567580
Inputs="@(_GitInput)"
568581
Outputs="$(_GitInfoFile)"

0 commit comments

Comments
 (0)