Skip to content

Commit cbd9666

Browse files
authored
[Java.Interop] $(Version) depends on TargetFramework (#952)
Context: 2d5431f Context: 88d6093 Context: #936 Context: https://github.com/jonpryor/java.interop/commits/jonp-registration-scope Versioning is hard. Way back in 3e6a623 we tried to use the `GitInfo` NuGet package so that *all* assemblies would have a version number which contained the number of commits since `GitInfo.txt` changed. This turned out to have unanticipated breakage, and was largely reverted in 2d5431f for "core" libs like `Java.Interop.dll`, but retained for "utility" apps and libs like `generator.exe`. This still presents a problem, though: the *point* to assembly versioning is to prevent accidental breaking of referencing assemblies. If we add a new member to `Java.Interop.dll` but *fail to update the version*, then that *permits* a scenario in which an app/lib depends on the new member, but is built against a version missing that member. This results in runtime exceptions. The whole reason this hasn't been a problem so far is because `Java.Interop.dll` has barely changed in *years*. (Plus, most usage is hidden behind other layers and libs…) However, *I want this to change*: #936 and jonpryor/java.interop/jonp-registration-scope both *add* new public API to `Java.Interop.dll`, and there are other features and optimizations we're considering that would also require API changes. A policy of "no changes" isn't tenable. Thus: allow `Java.Interop.dll` built for .NET 6 to have a different `$(Version)` than the one built for .NET Standard 2.0. Fixing this was unexpectedly problematic, as commit 88d6093: > Update[d] `Java.Interop.BootstrapTasks.csproj` to *no longer* > `<Import/>` `VersionInfo.targets`, as this causes a circular > dependency (`VersionInfo.targets` uses tasks from > `Java.Interop.BootstrapTasks.dll`). This is fine, as > `Java.Interop.BootstrapTasks.dll` doesn't need to be versioned. `Java.Interop.BootstrapTasks.dll` doesn't need to be versioned, but *other assemblies **do***, and this change change meant that `VersionInfo.targets` was *never* used, which in turn meant that e.g. `bin/BuildDebug/Version.props` was never created. This change *also* implicitly reverted all remaining 3e6a623 behavior; in commit 13def0e (one prior to 88d6093), `generator.exe` *had* a version of 0.1.45.0. Starting with commit 88d6093, `generator.exe` had version 1.0.0.0, which re-introduces the "baseline" scenario which first necessitated 3e6a623! Re-think this process, while avoiding the circular dependency: 1. `Java.Interop.BootstrapTasks.targets` now *generates* a `Version.props` file, re-introducing its existence. `Version.props` now contains *four* version values: * `$(JINetToolVersion)`: Version to use for "utility" assemblies which target .NET. * `$(JINetCoreLibVersion)`: Version to use for "core library" assemblies which target .NET. * `$(JIOldToolVersion)`: Version to use for "utility" assemblies which target .NET Standard/Framework/etc. * `$(JIOldCoreLibVersion)`: Version to use for "core library" assemblies which target .NET Standard/Framework/etc. The `$(JINet*)` values derive their major, minor, and patch values from `GitInfo`, while the `$(JIOld*)` values are backward compatible (-ish). 2. Update/replace `VersionInfo.targets` with a `_SetInformationalVersion` target, which sets the `$(InformationalVersion)` MSBuild property based on the value of `$(Version)` and various other properties. 3. `Directory.Build.props` is updated to provide new `$(JIUtilityVersion)` and `$(JICoreLibVersion)` properties, which are set based on the `$(TargetFramework)` value. 4. Set the default `$(Version)` value to `$(JIUtilityVersion)`. "Core library" assemblies must explicitly set `$(Version)` in their `.csproj` to instead be `$(JICoreLibVersion)`. The result is that for "core libraries" such as `Java.Interop.dll`, when building for .NET Standard or MonoAndroid they will continue to use version 0.1.0.0 (see also 2d5431f), while these assemblies will use version 6.0.0.0 when built for .NET Core/6. "Utility" assemblies such as `generator.exe` will contain a version number which changes based on when `GitInfo.txt` was last changed. When building for .NET Standard, they will use version `0.2.$(GitBaseVersionPatch).$(GitCommits)`, which differs from the pre-88d6093c pattern of `0.1.$(GitSemVerPatch).0`, but as my current system Xamarin.Android v12.1.99.117 install shows that `generator.exe` has version 0.1.31.0, "rebasing" on 0.2.x feels easiest. When utility assemblies are built for .NET, they will instead use the values from `GitInfo.txt`, e.g. `6.0.0.$(GitCommits)`. Finally, *always* set `$(FileVersion)` to: $(GitBaseVersionMajor).$(GitBaseVersionMinor).$(GitBaseVersionPatch).$(GitCommits) [`$(FileVersion)`][0] is used to set [`$(AssemblyFileVersionAttribute)`][1], which is *not* used for assembly binding, but *is* shown in the Windows File Properties dialog, and can be used to (indirectly) determine which commit an assembly was built from. [0]: https://docs.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props [1]: https://docs.microsoft.com/en-us/dotnet/api/system.reflection.assemblyfileversionattribute?view=net-6.0
1 parent e587cf6 commit cbd9666

File tree

13 files changed

+63
-37
lines changed

13 files changed

+63
-37
lines changed

Directory.Build.props

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,17 @@
6262
<UtilityOutputFullPath Condition=" '$(UtilityOutputFullPathCoreApps)' != '' ">$(UtilityOutputFullPathCoreApps)</UtilityOutputFullPath>
6363
<UtilityOutputFullPath Condition=" '$(UtilityOutputFullPathCoreApps)' == '' ">$(ToolOutputFullPath)</UtilityOutputFullPath>
6464
<RollForward>Major</RollForward>
65+
<JIUtilityVersion>$(JINetToolVersion)</JIUtilityVersion>
66+
<JICoreLibVersion>$(JINetCoreLibVersion)</JICoreLibVersion>
6567
</PropertyGroup>
6668
<PropertyGroup Condition=" '$(JIBuildingForNetCoreApp)' != 'True' ">
6769
<IntermediateOutputPath>$(BaseIntermediateOutputPath)\$(Configuration)</IntermediateOutputPath>
6870
<BuildToolOutputFullPath>$(MSBuildThisFileDirectory)bin\Build$(Configuration)\</BuildToolOutputFullPath>
6971
<ToolOutputFullPath>$(MSBuildThisFileDirectory)bin\$(Configuration)\</ToolOutputFullPath>
7072
<TestOutputFullPath>$(MSBuildThisFileDirectory)bin\Test$(Configuration)\</TestOutputFullPath>
7173
<UtilityOutputFullPath Condition=" '$(UtilityOutputFullPath)' == '' ">$(ToolOutputFullPath)</UtilityOutputFullPath>
74+
<JIUtilityVersion>$(JIOldToolVersion)</JIUtilityVersion>
75+
<JICoreLibVersion>$(JIOldCoreLibVersion)</JICoreLibVersion>
7276
</PropertyGroup>
7377
<PropertyGroup>
7478
<XamarinAndroidToolsDirectory Condition=" '$(XamarinAndroidToolsDirectory)' == '' ">$(MSBuildThisFileDirectory)external\xamarin-android-tools</XamarinAndroidToolsDirectory>
@@ -119,4 +123,8 @@
119123
<NoWarn>$(NoWarn);CA1307;CA1309;CA1310</NoWarn>
120124
</PropertyGroup>
121125

126+
<PropertyGroup>
127+
<Version>$(JIUtilityVersion)</Version>
128+
</PropertyGroup>
129+
122130
</Project>

Directory.Build.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@
3434
<PackageReference Update="XliffTasks" Version="1.0.0-beta.20420.1" />
3535
</ItemGroup>
3636

37+
<Import Project="build-tools\scripts\VersionInfo.targets" />
38+
3739
</Project>

GitInfo.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1
1+
6.0

build-tools/Java.Interop.BootstrapTasks/Java.Interop.BootstrapTasks.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
<OutputPath>$(BuildToolOutputFullPath)</OutputPath>
77
</PropertyGroup>
88

9+
<PropertyGroup>
10+
<GitDefaultBranch>main</GitDefaultBranch>
11+
<GitThisAssembly>false</GitThisAssembly>
12+
</PropertyGroup>
13+
914
<ItemGroup>
15+
<PackageReference Include="GitInfo" />
1016
<PackageReference Include="Microsoft.Build.Framework" />
1117
<PackageReference Include="Microsoft.Build.Utilities.Core" />
1218
<PackageReference Include="NUnit.ConsoleRunner" GeneratePathProperty="True" />

build-tools/Java.Interop.BootstrapTasks/Java.Interop.BootstrapTasks.targets

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,37 @@
105105
<Touch Files="$(_OutputPath)PackagePaths.props" />
106106
</Target>
107107

108+
<Target Name="_CreateVersionProps"
109+
AfterTargets="Build"
110+
DependsOnTargets="GitVersion"
111+
Inputs="$(MSBuildThisFileFullPath);$(MSBuildThisFileDirectory)Java.Interop.BootstrapTasks.csproj"
112+
Outputs="$(_OutputPath)Versions.props">
113+
<PropertyGroup>
114+
<_NetToolVersion>$(GitBaseVersionMajor).$(GitBaseVersionMinor).$(GitBaseVersionPatch).$(GitCommits)</_NetToolVersion>
115+
<_OldToolVersion>0.2.$(GitBaseVersionPatch).$(GitCommits)</_OldToolVersion>
116+
<_NetCoreLibVersion>$(GitBaseVersionMajor).$(GitBaseVersionMinor).0.0</_NetCoreLibVersion>
117+
<_OldCoreLibVersion>0.1.0.0</_OldCoreLibVersion>
118+
<_FileVersion>$(GitBaseVersionMajor).$(GitBaseVersionMinor).$(GitBaseVersionPatch).$(GitCommits)</_FileVersion>
119+
</PropertyGroup>
120+
<ItemGroup>
121+
<_VersionsLine Include="&lt;Project&gt;" />
122+
<_VersionsLine Include=" &lt;PropertyGroup&gt;" />
123+
<_VersionsLine Include=" &lt;FileVersion&gt;$(_FileVersion)&lt;/FileVersion&gt;" />
124+
<_VersionsLine Include=" &lt;JINetToolVersion&gt;$(_NetToolVersion)&lt;/JINetToolVersion&gt;" />
125+
<_VersionsLine Include=" &lt;JIOldToolVersion&gt;$(_OldToolVersion)&lt;/JIOldToolVersion&gt;" />
126+
<_VersionsLine Include=" &lt;JINetCoreLibVersion&gt;$(_NetCoreLibVersion)&lt;/JINetCoreLibVersion&gt;" />
127+
<_VersionsLine Include=" &lt;JIOldCoreLibVersion&gt;$(_OldCoreLibVersion)&lt;/JIOldCoreLibVersion&gt;" />
128+
<_VersionsLine Include=" &lt;JIBuildBranch&gt;$(GitBranch)&lt;/JIBuildBranch&gt;" />
129+
<_VersionsLine Include=" &lt;JIBuildCommit&gt;$(GitCommit)&lt;/JIBuildCommit&gt;" />
130+
<_VersionsLine Include=" &lt;/PropertyGroup&gt;" />
131+
<_VersionsLine Include="&lt;/Project&gt;" />
132+
</ItemGroup>
133+
<WriteLinesToFile
134+
File="$(_OutputPath)Version.props"
135+
Lines="@(_VersionsLine)"
136+
Overwrite="True"
137+
/>
138+
<Touch Files="$(_OutputPath)Version.props" />
139+
</Target>
140+
108141
</Project>

build-tools/scripts/Version.props.in

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Project>
33

4-
<UsingTask AssemblyFile="$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\Java.Interop.BootstrapTasks.dll"
5-
TaskName="Java.Interop.BootstrapTasks.ReplaceFileContents" />
6-
<PropertyGroup>
7-
<GitDefaultBranch>main</GitDefaultBranch>
8-
<GitThisAssembly>false</GitThisAssembly>
9-
</PropertyGroup>
10-
<ItemGroup>
11-
<PackageReference Include="GitInfo" PrivateAssets="all" />
12-
</ItemGroup>
13-
14-
<Target Name="GenerateVersionInfo"
15-
AfterTargets="Build"
16-
DependsOnTargets="GitVersion"
17-
Condition="!Exists ('$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\Version.props')">
18-
<ItemGroup>
19-
<Replacements Include="@VERSION@" Replacement="$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch).0"/>
20-
<Replacements Include="@COMMIT@" Replacement="$(GitCommit)"/>
21-
<Replacements Include="@BRANCH@" Replacement="$(GitBranch)"/>
22-
</ItemGroup>
23-
<ReplaceFileContents
24-
TemplateFile="$(MSBuildThisFileDirectory)Version.props.in"
25-
OutputFile="$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\Version.props"
26-
Replacements="@(Replacements)"
27-
/>
4+
<Target Name="_SetInformationalVersion"
5+
BeforeTargets="GetAssemblyVersion;GetPackageVersion">
6+
<PropertyGroup>
7+
<InformationalVersion>$(Version) git-rev-head:$(JIBuildCommit) git-branch:$(JIBuildBranch)</InformationalVersion>
8+
</PropertyGroup>
289
</Target>
10+
2911
</Project>

src/Java.Base/Java.Base.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
<Nullable>enable</Nullable>
77
<NoWarn>$(NoWarn);8764</NoWarn>
8+
<Version>$(JICoreLibVersion)</Version>
89
</PropertyGroup>
910

1011
<ItemGroup>

src/Java.Interop.Dynamic/Java.Interop.Dynamic.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<SignAssembly>true</SignAssembly>
88
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
99
<AssemblyTitle>Java.Interop.Dynamic</AssemblyTitle>
10-
<Version>0.1.0.0</Version>
10+
<Version>$(JICoreLibVersion)</Version>
1111
</PropertyGroup>
1212
<PropertyGroup>
1313
<OutputPath>$(ToolOutputFullPath)</OutputPath>

src/Java.Interop.Export/Java.Interop.Export.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<SignAssembly>true</SignAssembly>
88
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
99
<AssemblyTitle>Java.Interop.Export</AssemblyTitle>
10-
<Version>0.1.0.0</Version>
10+
<Version>$(JICoreLibVersion)</Version>
1111
</PropertyGroup>
1212
<PropertyGroup>
1313
<OutputPath>$(ToolOutputFullPath)</OutputPath>

0 commit comments

Comments
 (0)