Skip to content

Commit 3e6a623

Browse files
authored
[build] Use GitInfo to Generate Version information. (#875)
Context: d16b1e5 Context: b7982e4 Our previous attempt to use the [`GitInfo`][0] [NuGet Package][1] to generate Version information in commit d16b1e5 did not go as planned. It turns out that old-style projects do NOT support the `$(GenerateAssemblyInfo)` MSBuild property. As such the version information is never generated. As a result when we built the Xamarin.Android version of `Java.Interop` it never includes version information, which results in errors such as: error CS1705: Assembly 'Xamarin.Forms.Platform.Android' with identity 'Xamarin.Forms.Platform.Android, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'Java.Interop, Version=0.1.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065' which has a higher version than referenced assembly 'Java.Interop' with identity 'Java.Interop, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065' Additionally, we were generating the version data on EVERY build. It could be optimized by only generating the required data on the first build, or in this case the `make prepare` step. Introduce a system where a `Version.props` file gets generated during the `make prepare` step and then that file is imported into all the sub-projects. We also have a system to make sure that for legacy projects the `$(GenerateAssemblyInfo)` property actually works. [0]: https://github.com/devlooped/GitInfo [1]: https://www.nuget.org/packages/GitInfo/2.1.2
1 parent f359e73 commit 3e6a623

File tree

17 files changed

+146
-32
lines changed

17 files changed

+146
-32
lines changed

Directory.Build.props

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
<PropertyGroup>
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
66
<_OutputPath>$(MSBuildThisFileDirectory)bin\Build$(Configuration)\</_OutputPath>
7+
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
78
</PropertyGroup>
8-
9+
910
<PropertyGroup>
1011
<XlfLanguages>cs;de;es;fr;it;ja;ko;pl;pt-BR;ru;tr;zh-Hans;zh-Hant</XlfLanguages>
1112
<UpdateXlfOnBuild Condition="'$(RunningOnCI)' != 'true'">true</UpdateXlfOnBuild>
@@ -19,6 +20,10 @@
1920
Project="$([System.IO.Path]::GetDirectoryName($(MSBuildThisFileDirectory))).override.props"
2021
Condition=" Exists('$([System.IO.Path]::GetDirectoryName($(MSBuildThisFileDirectory))).override.props') "
2122
/>
23+
<Import
24+
Project="$(MSBuildThisFileDirectory)bin\Build$(Configuration)\Version.props"
25+
Condition=" Exists('$(MSBuildThisFileDirectory)bin\Build$(Configuration)\Version.props') "
26+
/>
2227
<Import
2328
Project="$(_OutputPath)JdkInfo.props"
2429
Condition="Exists('$(_OutputPath)JdkInfo.props')"
@@ -79,7 +84,7 @@
7984
<_RunJNIEnvGen Condition=" '$(JIBuildingForNetCoreApp)' == 'True' ">$(DotnetToolPath) "$(_JNIEnvGenPath)"</_RunJNIEnvGen>
8085
<_RunJNIEnvGen Condition=" '$(JIBuildingForNetCoreApp)' != 'True' ">$(Runtime) "$(_JNIEnvGenPath)"</_RunJNIEnvGen>
8186
</PropertyGroup>
82-
87+
8388
<!-- Add Roslyn analyzers NuGet to all projects -->
8489
<ItemGroup Condition=" '$(DisableRoslynAnalyzers)' != 'True' ">
8590
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">

GitInfo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@
1818
</ProjectReference>
1919
</ItemGroup>
2020

21+
<Import Project="$(MSBuildThisFileDirectory)..\scripts\VersionInfo.targets" />
22+
2123
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.Build.Framework;
2+
using Microsoft.Build.Utilities;
3+
using System;
4+
using System.IO;
5+
using System.Collections.Generic;
6+
7+
8+
namespace Java.Interop.BootstrapTasks
9+
{
10+
public class GenerateVersionFile : Task
11+
{
12+
public ITaskItem InputFile { get; set; }
13+
public ITaskItem OutputFile { get; set; }
14+
15+
public ITaskItem [] Replacements { get; set; }
16+
public override bool Execute ()
17+
{
18+
string text = File.ReadAllText (InputFile.ItemSpec);
19+
foreach (var replacement in Replacements)
20+
{
21+
text = text.Replace (replacement.ItemSpec, replacement.GetMetadata ("Replacement"));
22+
}
23+
File.WriteAllText (OutputFile.ItemSpec, text);
24+
return !Log.HasLoggedErrors;
25+
}
26+
}
27+
}

build-tools/automation/azure-pipelines.yaml

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
solution: Java.Interop.sln
5959
configuration: $(Build.Configuration)
6060
msbuildArguments: /restore
61-
61+
6262
- task: MSBuild@1
6363
displayName: MSBuild RunNUnitTests.targets
6464
inputs:
@@ -93,7 +93,22 @@ jobs:
9393
runNativeDotnetTests: true
9494

9595
- template: templates\fail-on-issue.yaml
96-
96+
97+
- task: ArchiveFiles@2
98+
displayName: 'Archive build outputs'
99+
inputs:
100+
rootFolderOrFile: 'bin'
101+
archiveType: 'zip'
102+
archiveFile: '$(Build.ArtifactStagingDirectory)/bin-dotnet.zip'
103+
replaceExistingArchive: true
104+
condition: succeededOrFailed()
105+
106+
- task: PublishBuildArtifacts@1
107+
displayName: 'Publish Artifact: debug'
108+
inputs:
109+
ArtifactName: debug
110+
condition: succeededOrFailed()
111+
97112
- job: mac_build
98113
displayName: Mac - Mono
99114
pool:
@@ -114,10 +129,10 @@ jobs:
114129
115130
- script: make prepare CONFIGURATION=$(Build.Configuration) JI_MAX_JDK=$(MaxJdkVersion)
116131
displayName: make prepare
117-
132+
118133
- script: make all CONFIGURATION=$(Build.Configuration) JI_MAX_JDK=$(MaxJdkVersion)
119134
displayName: make all
120-
135+
121136
- script: |
122137
r=0
123138
make run-all-tests CONFIGURATION=$(Build.Configuration) JI_MAX_JDK=$(MaxJdkVersion) || r=$?
@@ -139,7 +154,7 @@ jobs:
139154
SourceFolder: $(System.DefaultWorkingDirectory)
140155
Contents: |
141156
xatb.jar
142-
bin.zip
157+
bin.zip
143158
TargetFolder: $(Build.ArtifactStagingDirectory)
144159
condition: succeededOrFailed()
145160

@@ -148,7 +163,7 @@ jobs:
148163
inputs:
149164
ArtifactName: debug
150165
condition: succeededOrFailed()
151-
166+
152167
- job: mac_dotnet_build
153168
displayName: Mac - .NET Core
154169
pool:
@@ -161,12 +176,12 @@ jobs:
161176
submodules: recursive
162177

163178
- template: templates\install-dependencies.yaml
164-
179+
165180
- script: make prepare-core CONFIGURATION=$(Build.Configuration) JI_MAX_JDK=$(MaxJdkVersion)
166181
displayName: make prepare-core
167-
182+
168183
- template: templates\core-build.yaml
169-
184+
170185
- template: templates\core-tests.yaml
171186
parameters:
172187
runNativeTests: true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
// Runtime Version:4.0.30319.42000
5+
//
6+
// Changes to this file may cause incorrect behavior and will be lost if
7+
// the code is regenerated.
8+
// </auto-generated>
9+
//------------------------------------------------------------------------------
10+
11+
using System;
12+
using System.Reflection;
13+
14+
[assembly: System.Reflection.AssemblyCompanyAttribute("Microsoft Corporation")]
15+
[assembly: System.Reflection.AssemblyConfigurationAttribute("@CONFIGURATION@")]
16+
[assembly: System.Reflection.AssemblyCopyrightAttribute("Microsoft Corporation")]
17+
[assembly: System.Reflection.AssemblyFileVersionAttribute("@[email protected]")]
18+
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("@INFORMATIONALVERSION@")]
19+
[assembly: System.Reflection.AssemblyProductAttribute("@PRODUCT@")]
20+
[assembly: System.Reflection.AssemblyTitleAttribute("@TITLE@")]
21+
[assembly: System.Reflection.AssemblyVersionAttribute("@[email protected]")]
22+
23+
// Generated by the MSBuild WriteCodeFragment class.

build-tools/scripts/Prepare.targets

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
<UsingTask AssemblyFile="$(_TopDir)\bin\Build$(Configuration)\Java.Interop.BootstrapTasks.dll" TaskName="Java.Interop.BootstrapTasks.JdkInfo" />
77
<Target Name="Prepare">
88
<Exec Command="git submodule update --init --recursive" WorkingDirectory="$(_TopDir)" />
9-
<MSBuild Projects="$(MSBuildThisFileDirectory)..\..\build-tools\Java.Interop.BootstrapTasks\Java.Interop.BootstrapTasks.csproj" />
9+
<MSBuild Projects="$(MSBuildThisFileDirectory)..\..\build-tools\Java.Interop.BootstrapTasks\Java.Interop.BootstrapTasks.csproj"
10+
Targets="Build;GenerateVersionInfo"
11+
RunEachTargetSeparately="true"
12+
/>
1013
<PropertyGroup>
1114
<_MaxJdk>$(MaxJdkVersion)</_MaxJdk>
1215
<_MaxJdk Condition=" '$(_MaxJdk)' == '' ">$(JI_MAX_JDK)</_MaxJdk>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project>
2+
<PropertyGroup>
3+
<Version>@VERSION@</Version>
4+
<InformationalVersion>@VERSION@ git-rev-head:@COMMIT@ git-branch:@BRANCH@</InformationalVersion>
5+
<Company>Microsoft Corporation</Company>
6+
<Copyright>Microsoft Corporation</Copyright>
7+
</PropertyGroup>
8+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
4+
<UsingTask AssemblyFile="$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\Java.Interop.BootstrapTasks.dll"
5+
TaskName="Java.Interop.BootstrapTasks.GenerateVersionFile" />
6+
<PropertyGroup>
7+
<GitDefaultBranch>main</GitDefaultBranch>
8+
<GitThisAssembly>false</GitThisAssembly>
9+
</PropertyGroup>
10+
<ItemGroup>
11+
<PackageReference Include="GitInfo" Version="2.1.2" 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)"/>
20+
<Replacements Include="@COMMIT@" Replacement="$(GitCommit)"/>
21+
<Replacements Include="@BRANCH@" Replacement="$(GitBranch)"/>
22+
</ItemGroup>
23+
<GenerateVersionFile
24+
InputFile="$(MSBuildThisFileDirectory)Version.props.in"
25+
OutputFile="$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\Version.props"
26+
Replacements="@(Replacements)"
27+
/>
28+
</Target>
29+
</Project>

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
<SignAssembly>true</SignAssembly>
88
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
99
<AssemblyTitle>Java.Interop.Dynamic</AssemblyTitle>
10-
<Company>Microsoft Corporation</Company>
11-
<Copyright>Microsoft Corporation</Copyright>
12-
<AssemblyVersion>0.1.0.0</AssemblyVersion>
1310
</PropertyGroup>
1411
<PropertyGroup>
1512
<OutputPath>$(ToolOutputFullPath)</OutputPath>

0 commit comments

Comments
 (0)