Skip to content

Commit

Permalink
[build] Use GitInfo to Generate Version information. (#875)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dellis1972 authored Sep 8, 2021
1 parent f359e73 commit 3e6a623
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 32 deletions.
9 changes: 7 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<_OutputPath>$(MSBuildThisFileDirectory)bin\Build$(Configuration)\</_OutputPath>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
</PropertyGroup>

<PropertyGroup>
<XlfLanguages>cs;de;es;fr;it;ja;ko;pl;pt-BR;ru;tr;zh-Hans;zh-Hant</XlfLanguages>
<UpdateXlfOnBuild Condition="'$(RunningOnCI)' != 'true'">true</UpdateXlfOnBuild>
Expand All @@ -19,6 +20,10 @@
Project="$([System.IO.Path]::GetDirectoryName($(MSBuildThisFileDirectory))).override.props"
Condition=" Exists('$([System.IO.Path]::GetDirectoryName($(MSBuildThisFileDirectory))).override.props') "
/>
<Import
Project="$(MSBuildThisFileDirectory)bin\Build$(Configuration)\Version.props"
Condition=" Exists('$(MSBuildThisFileDirectory)bin\Build$(Configuration)\Version.props') "
/>
<Import
Project="$(_OutputPath)JdkInfo.props"
Condition="Exists('$(_OutputPath)JdkInfo.props')"
Expand Down Expand Up @@ -79,7 +84,7 @@
<_RunJNIEnvGen Condition=" '$(JIBuildingForNetCoreApp)' == 'True' ">$(DotnetToolPath) "$(_JNIEnvGenPath)"</_RunJNIEnvGen>
<_RunJNIEnvGen Condition=" '$(JIBuildingForNetCoreApp)' != 'True' ">$(Runtime) "$(_JNIEnvGenPath)"</_RunJNIEnvGen>
</PropertyGroup>

<!-- Add Roslyn analyzers NuGet to all projects -->
<ItemGroup Condition=" '$(DisableRoslynAnalyzers)' != 'True' ">
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
Expand Down
1 change: 1 addition & 0 deletions GitInfo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
</ProjectReference>
</ItemGroup>

<Import Project="$(MSBuildThisFileDirectory)..\scripts\VersionInfo.targets" />

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.IO;
using System.Collections.Generic;


namespace Java.Interop.BootstrapTasks
{
public class GenerateVersionFile : Task
{
public ITaskItem InputFile { get; set; }
public ITaskItem OutputFile { get; set; }

public ITaskItem [] Replacements { get; set; }
public override bool Execute ()
{
string text = File.ReadAllText (InputFile.ItemSpec);
foreach (var replacement in Replacements)
{
text = text.Replace (replacement.ItemSpec, replacement.GetMetadata ("Replacement"));
}
File.WriteAllText (OutputFile.ItemSpec, text);
return !Log.HasLoggedErrors;
}
}
}
33 changes: 24 additions & 9 deletions build-tools/automation/azure-pipelines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
solution: Java.Interop.sln
configuration: $(Build.Configuration)
msbuildArguments: /restore

- task: MSBuild@1
displayName: MSBuild RunNUnitTests.targets
inputs:
Expand Down Expand Up @@ -93,7 +93,22 @@ jobs:
runNativeDotnetTests: true

- template: templates\fail-on-issue.yaml


- task: ArchiveFiles@2
displayName: 'Archive build outputs'
inputs:
rootFolderOrFile: 'bin'
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/bin-dotnet.zip'
replaceExistingArchive: true
condition: succeededOrFailed()

- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact: debug'
inputs:
ArtifactName: debug
condition: succeededOrFailed()

- job: mac_build
displayName: Mac - Mono
pool:
Expand All @@ -114,10 +129,10 @@ jobs:
- script: make prepare CONFIGURATION=$(Build.Configuration) JI_MAX_JDK=$(MaxJdkVersion)
displayName: make prepare

- script: make all CONFIGURATION=$(Build.Configuration) JI_MAX_JDK=$(MaxJdkVersion)
displayName: make all

- script: |
r=0
make run-all-tests CONFIGURATION=$(Build.Configuration) JI_MAX_JDK=$(MaxJdkVersion) || r=$?
Expand All @@ -139,7 +154,7 @@ jobs:
SourceFolder: $(System.DefaultWorkingDirectory)
Contents: |
xatb.jar
bin.zip
bin.zip
TargetFolder: $(Build.ArtifactStagingDirectory)
condition: succeededOrFailed()

Expand All @@ -148,7 +163,7 @@ jobs:
inputs:
ArtifactName: debug
condition: succeededOrFailed()

- job: mac_dotnet_build
displayName: Mac - .NET Core
pool:
Expand All @@ -161,12 +176,12 @@ jobs:
submodules: recursive

- template: templates\install-dependencies.yaml

- script: make prepare-core CONFIGURATION=$(Build.Configuration) JI_MAX_JDK=$(MaxJdkVersion)
displayName: make prepare-core

- template: templates\core-build.yaml

- template: templates\core-tests.yaml
parameters:
runNativeTests: true
Expand Down
23 changes: 23 additions & 0 deletions build-tools/scripts/AssemblyInfo.g.cs.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Reflection;

[assembly: System.Reflection.AssemblyCompanyAttribute("Microsoft Corporation")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("@CONFIGURATION@")]
[assembly: System.Reflection.AssemblyCopyrightAttribute("Microsoft Corporation")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("@VERSION@.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("@INFORMATIONALVERSION@")]
[assembly: System.Reflection.AssemblyProductAttribute("@PRODUCT@")]
[assembly: System.Reflection.AssemblyTitleAttribute("@TITLE@")]
[assembly: System.Reflection.AssemblyVersionAttribute("@VERSION@.0")]

// Generated by the MSBuild WriteCodeFragment class.
5 changes: 4 additions & 1 deletion build-tools/scripts/Prepare.targets
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
<UsingTask AssemblyFile="$(_TopDir)\bin\Build$(Configuration)\Java.Interop.BootstrapTasks.dll" TaskName="Java.Interop.BootstrapTasks.JdkInfo" />
<Target Name="Prepare">
<Exec Command="git submodule update --init --recursive" WorkingDirectory="$(_TopDir)" />
<MSBuild Projects="$(MSBuildThisFileDirectory)..\..\build-tools\Java.Interop.BootstrapTasks\Java.Interop.BootstrapTasks.csproj" />
<MSBuild Projects="$(MSBuildThisFileDirectory)..\..\build-tools\Java.Interop.BootstrapTasks\Java.Interop.BootstrapTasks.csproj"
Targets="Build;GenerateVersionInfo"
RunEachTargetSeparately="true"
/>
<PropertyGroup>
<_MaxJdk>$(MaxJdkVersion)</_MaxJdk>
<_MaxJdk Condition=" '$(_MaxJdk)' == '' ">$(JI_MAX_JDK)</_MaxJdk>
Expand Down
8 changes: 8 additions & 0 deletions build-tools/scripts/Version.props.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>
<PropertyGroup>
<Version>@VERSION@</Version>
<InformationalVersion>@VERSION@ git-rev-head:@COMMIT@ git-branch:@BRANCH@</InformationalVersion>
<Company>Microsoft Corporation</Company>
<Copyright>Microsoft Corporation</Copyright>
</PropertyGroup>
</Project>
29 changes: 29 additions & 0 deletions build-tools/scripts/VersionInfo.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<UsingTask AssemblyFile="$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\Java.Interop.BootstrapTasks.dll"
TaskName="Java.Interop.BootstrapTasks.GenerateVersionFile" />
<PropertyGroup>
<GitDefaultBranch>main</GitDefaultBranch>
<GitThisAssembly>false</GitThisAssembly>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GitInfo" Version="2.1.2" PrivateAssets="all" />
</ItemGroup>

<Target Name="GenerateVersionInfo"
AfterTargets="Build"
DependsOnTargets="GitVersion"
Condition="!Exists ('$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\Version.props')">
<ItemGroup>
<Replacements Include="@VERSION@" Replacement="$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)"/>
<Replacements Include="@COMMIT@" Replacement="$(GitCommit)"/>
<Replacements Include="@BRANCH@" Replacement="$(GitBranch)"/>
</ItemGroup>
<GenerateVersionFile
InputFile="$(MSBuildThisFileDirectory)Version.props.in"
OutputFile="$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\Version.props"
Replacements="@(Replacements)"
/>
</Target>
</Project>
3 changes: 0 additions & 3 deletions src/Java.Interop.Dynamic/Java.Interop.Dynamic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
<AssemblyTitle>Java.Interop.Dynamic</AssemblyTitle>
<Company>Microsoft Corporation</Company>
<Copyright>Microsoft Corporation</Copyright>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
</PropertyGroup>
<PropertyGroup>
<OutputPath>$(ToolOutputFullPath)</OutputPath>
Expand Down
3 changes: 0 additions & 3 deletions src/Java.Interop.Export/Java.Interop.Export.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
<AssemblyTitle>Java.Interop.Export</AssemblyTitle>
<Company>Microsoft Corporation</Company>
<Copyright>Microsoft Corporation</Copyright>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
</PropertyGroup>
<PropertyGroup>
<OutputPath>$(ToolOutputFullPath)</OutputPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
<AssemblyTitle>Java.Interop.GenericMarshaler</AssemblyTitle>
<Company>Microsoft Corporation</Company>
<Copyright>Microsoft Corporation</Copyright>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
</PropertyGroup>
<PropertyGroup>
<OutputPath>$(ToolOutputFullPath)</OutputPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<DefineConstants>INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
<ProjectGuid>{5C0B3562-8DA0-4726-9762-75B9709ED6B7}</ProjectGuid>
<AssemblyTitle>Java.Interop.Tools.JavaSource</AssemblyTitle>
<Company>Microsoft Corporation</Company>
<Copyright>Microsoft Corporation</Copyright>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
</PropertyGroup>
<PropertyGroup>
<OutputPath>$(ToolOutputFullPath)</OutputPath>
Expand Down
19 changes: 19 additions & 0 deletions src/Java.Interop/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,23 @@
<Exec Command="&quot;$(JavaCPath)&quot; $(_JavacSourceOptions) -d &quot;$(IntermediateOutputPath)ji-classes&quot; @(CompileJavaInteropJar -&gt; '%(Identity)', ' ')" />
<Exec Command="&quot;$(JarPath)&quot; cf &quot;$(OutputPath)java-interop.jar&quot; -C &quot;$(IntermediateOutputPath)ji-classes&quot; ." />
</Target>
<UsingTask AssemblyFile="$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\Java.Interop.BootstrapTasks.dll" TaskName="Java.Interop.BootstrapTasks.GenerateVersionFile" />
<Target Name="BuildVersionInfo_g_cs"
Condition=" '$(GenerateAssemblyInfo)' == 'true' And '$(TargetFramework)' == 'monoandroid10' ">
<ItemGroup>
<Replacements Include="@VERSION@" Replacement="$(Version)"/>
<Replacements Include="@INFORMATIONALVERSION@" Replacement="$(InformationalVersion)"/>
<Replacements Include="@TITLE@" Replacement="$(AssemblyName)"/>
<Replacements Include="@PRODUCT@" Replacement="$(AssemblyName)"/>
<Replacements Include="@CONFIGURATION@" Replacement="$(Configuration)"/>
</ItemGroup>
<GenerateVersionFile
InputFile="$(MSBuildThisFileDirectory)..\..\build-tools\scripts\AssemblyInfo.g.cs.in"
OutputFile="$(IntermediateOutputPath)$(AssemblyName).AssemblyInfo.g.cs"
Replacements="@(Replacements)"
/>
<ItemGroup>
<Compile Include="$(IntermediateOutputPath)$(AssemblyName).AssemblyInfo.g.cs"/>
</ItemGroup>
</Target>
</Project>
2 changes: 2 additions & 0 deletions src/Java.Interop/Java.Interop-MonoAndroid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
<LangVersion>8.0</LangVersion>
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
</PropertyGroup>
<Import Project="..\..\Directory.Build.props" />
<PropertyGroup>
Expand Down Expand Up @@ -85,6 +86,7 @@
<Import Project="Directory.Build.targets" />
<PropertyGroup>
<BuildDependsOn>
BuildVersionInfo_g_cs;
BuildJniEnvironment_g_cs;
BuildInteropJar;
$(BuildDependsOn)
Expand Down
1 change: 0 additions & 1 deletion src/Java.Interop/Java.Interop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
<DefineConstants>INTEROP;FEATURE_JNIENVIRONMENT_JI_PINVOKES;FEATURE_JNIOBJECTREFERENCE_INTPTRS;INTERNAL_NULLABLE_ATTRIBUTES;$(JavaInteropDefineConstants)</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<IntermediateOutputPath>$(BaseIntermediateOutputPath)$(Configuration)\$(TargetFramework.ToLowerInvariant())\</IntermediateOutputPath>
<OutputPath>$(ToolOutputFullPath)</OutputPath>
<DocumentationFile>$(ToolOutputFullPath)Java.Interop.xml</DocumentationFile>
Expand Down
7 changes: 0 additions & 7 deletions src/Java.Interop/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
using System.Runtime.InteropServices;

[assembly: DefaultDllImportSearchPathsAttribute (DllImportSearchPath.SafeDirectories | DllImportSearchPath.AssemblyDirectory)]

[assembly: AssemblyTitle ("Java.Interop")]
[assembly: AssemblyDescription ("")]
[assembly: AssemblyCulture ("")]
[assembly: AssemblyConfiguration ("")]
[assembly: AssemblyCompany ("Microsoft Corporation")]
[assembly: AssemblyCopyright ("Microsoft Corporation")]
[assembly: AssemblyProduct ("")]
[assembly: AssemblyTrademark ("Microsoft Corporation")]
[assembly: AssemblyVersion ("0.1.0.0")]
[assembly: AssemblyMetadata ("IsTrimmable", "True")]

[assembly: InternalsVisibleTo (
Expand Down

0 comments on commit 3e6a623

Please sign in to comment.