Skip to content

Instantly share code, notes, and snippets.

@ashumkin
Created July 29, 2016 07:54
Show Gist options
  • Save ashumkin/57b034e232be07452e9c4b865bc2896a to your computer and use it in GitHub Desktop.
Save ashumkin/57b034e232be07452e9c4b865bc2896a to your computer and use it in GitHub Desktop.
FileVersion info set via MSBuild target
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' ToolsVersion="12.0">
<PropertyGroup Condition="($(VersionCode)!='' or $(VersionName)!='' or $(FileVersion)!='')">
<_PreResourceBuildTargets>_SetFileVersion</_PreResourceBuildTargets>
<CoreBuildDependsOn>$(_PreResourceBuildTargets);$(CoreBuildDependsOn)</CoreBuildDependsOn>
</PropertyGroup>
<UsingTask TaskName="__SetFileVersion" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll">
<ParameterGroup>
<VerInfoKeys ParameterType="System.String" Required="true" />
<VersionCode ParameterType="System.String" Required="false" />
<VersionName ParameterType="System.String" Required="false" />
<FileVersion ParameterType="System.String" Required="false" />
<Out ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs"><![CDATA[
// split values as CSV (by ";")
String[] verInfoKeysList = VerInfoKeys.Split(';');
Dictionary<String, String> d = new Dictionary<String, String>();
foreach (String verInfoValue in verInfoKeysList) {
// split values as "key=value"
if (! String.IsNullOrEmpty(verInfoValue)) {
String[] kv = verInfoValue.Split('=');
d.Add(kv[0], kv[1]);
}
}
if (! String.IsNullOrEmpty(VersionCode)) {
d.Remove("versionCode");
d.Add("versionCode", VersionCode);
}
if (! String.IsNullOrEmpty(VersionName)) {
d.Remove("versionName");
d.Add("versionName", VersionName);
}
if (! String.IsNullOrEmpty(FileVersion)) {
d.Remove("FileVersion");
d.Add("FileVersion", FileVersion);
}
List<String> L = new List<String>();
foreach (KeyValuePair<String, String> kv in d) {
L.Add(kv.Key + "=" + kv.Value);
}
_Out = String.Join(";", L.ToArray());
]]></Code>
</Task>
</UsingTask>
<Target Name='_SetVersionCode_Name' >
<Message Text="$(VerInfo_Keys)" />
<__SetFileVersion VerInfoKeys="$(VerInfo_Keys)" VersionCode="$(VersionCode)" VersionName="$(VersionName)" FileVersion="$(FileVersion)">
<Output PropertyName="VerInfo_Keys" TaskParameter="Out" />
</__SetFileVersion>
<Message Text="$(VerInfo_Keys)" />
</Target>
<Target Name='_SetFileVersion'>
<CallTarget
Targets='_SetVersionCode_Name'
/>
</Target>
</Project>
@jchoover
Copy link

Just a suggestion, but for supporting differing versions of MSBuild

    <!-- Alow for multiple tool versions, as the CodeTaskFactory is in a version specific DLL -->
    <PropertyGroup>
        <TasksAssemblyName Condition="'$(MSBuildToolsVersion)'=='14.0'">Microsoft.Build.Tasks.Core</TasksAssemblyName>
        <TasksAssemblyName Condition="'$(TasksAssemblyName)'==''">Microsoft.Build.Tasks.v$(MSBuildToolsVersion)</TasksAssemblyName>
    </PropertyGroup>
    <!-- Inline task for replacing values in Delphi's evil VerInfo_Keys which is used but the BuildProjectResourceFile target -->
    <UsingTask TaskName="__SetFileVersion" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\$(TasksAssemblyName).dll">

@maiconcp
Copy link

Has a file for GroupProj?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment