Skip to content

Instantly share code, notes, and snippets.

@pke
Created June 18, 2014 22:20
Show Gist options
  • Save pke/e1d2c5061d535be48d75 to your computer and use it in GitHub Desktop.
Save pke/e1d2c5061d535be48d75 to your computer and use it in GitHub Desktop.
MSBuild targets for git and other build versioning infomations
define(function() {
return {
gitCommitHash: "${gitCommitHash}",
gitBranchDirty: ${gitPendingChanges},
gitBranch: "${gitBranch}",
buildEnv: "${buildEnv}",
buildTime: "${buildTime}",
buildConfiguration: "${buildConfiguration}",
buildPlatform: "${buildPlatform}"
};
});
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildCommunityTasksPath>$(SolutionDir)\.build</MSBuildCommunityTasksPath>
</PropertyGroup>
<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets" />
<Target Name="CreateBuildVersion">
<PropertyGroup>
<BuildTime>$([System.DateTime]::UtcNow.ToString("yyyy-MM-dd HH:mm:ss"))</BuildTime>
</PropertyGroup>
<GitVersion LocalPath="$(SolutionDir)">
<Output TaskParameter="CommitHash" PropertyName="GitCommitHash" />
</GitVersion>
<GitBranch LocalPath="$(SolutionDir)">
<Output TaskParameter="Branch" PropertyName="GitBranch" />
</GitBranch>
<GitPendingChanges>
<Output TaskParameter="HasPendingChanges" PropertyName="GitPendingChanges"/>
</GitPendingChanges>
<Computer>
<Output TaskParameter="Name" PropertyName="BuildMachineName" />
<Output TaskParameter="OSPlatform" PropertyName="BuildMachineOSPlatform" />
<Output TaskParameter="OSVersion" PropertyName="BuildMachineOSVersion" />
</Computer>
<ItemGroup>
<TemplateContext Include="gitCommitHash">
<ReplacementValue>$(GitCommitHash)</ReplacementValue>
</TemplateContext>
<TemplateContext Include="gitPendingChanges">
<!-- The GitPendingChanges is True/False, and we want to see a '+' if its true and nothing if its false
So we chain replace 'True' with '+' and if it was 'False' with an empty string.
Maybe there is a better way to do that?
-->
<ReplacementValue>$(GitPendingChanges.Replace('True', 'true').Replace('False','false'))</ReplacementValue>
</TemplateContext>
<TemplateContext Include="gitBranch">
<ReplacementValue>$(GitBranch)</ReplacementValue>
</TemplateContext>
<TemplateContext Include="buildEnv">
<ReplacementValue>$(BuildMachineName) $(BuildMachineOSPlatform) $(BuildMachineOSVersion)</ReplacementValue>
</TemplateContext>
<TemplateContext Include="buildTime">
<ReplacementValue>$(BuildTime)</ReplacementValue>
</TemplateContext>
<TemplateContext Include="buildConfiguration">
<ReplacementValue>$(Configuration)</ReplacementValue>
</TemplateContext>
<TemplateContext Include="buildPlatform">
<ReplacementValue>$(Platform)</ReplacementValue>
</TemplateContext>
</ItemGroup>
<TemplateFile Template="$(MSBuildThisFileDirectory)buildVersion.js" OutputFilename="$(ProjectDir)js\buildVersion.js" Tokens="@(TemplateContext)"/>
</Target>
</Project>
define(function() {
return {
gitCommitHash: "75f0708",
gitBranchDirty: true,
gitBranch: "making_it_work_again",
buildEnv: "somedevmachine Win32NT 6.3",
buildTime: "2014-06-18 22:12:08",
buildConfiguration: "Debug",
buildPlatform: "x86"
};
});
define(["buildVersion"], function(buildVersion) {
if (buildVersion.buildConfiguration === "Debug") {
// Do something only in debug version
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment