Skip to content

Instantly share code, notes, and snippets.

@clairernovotny
Created October 31, 2015 00:36
Show Gist options
  • Save clairernovotny/187d02f53e3371025438 to your computer and use it in GitHub Desktop.
Save clairernovotny/187d02f53e3371025438 to your computer and use it in GitHub Desktop.
VSO NuGet CI
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="UpdateVersion" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildProjectDirectory)\Build.tasks" />
<!-- Setup configuration variables -->
<PropertyGroup>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)..\</SolutionDir>
<Configuration Condition="'$(OS)' == 'Windows_NT' And '$(Configuration)' == ''">Debug</Configuration>
<ConfigFolderPath>$(Configuration)</ConfigFolderPath>
</PropertyGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<RootDir>$(MSBuildProjectDirectory)\..</RootDir>
</PropertyGroup>
<Target Name="RestorePackages">
<ItemGroup>
<NuGetSourcesList Include="$(NUGET_BUILD_FEEDS)" />
</ItemGroup>
<PropertyGroup>
<NuGetExeDir>$(RootDir)\.nuget</NuGetExeDir>
<NuGetExePath>$(NuGetExeDir)\nuget.exe</NuGetExePath>
<NuGetExeSources>@(NuGetSourcesList -> '-source %(Identity)', ' ')</NuGetExeSources>
</PropertyGroup>
<MakeDir Directories="$(NuGetExeDir)" Condition="!Exists('$(NuGetExeDir)')" />
<Message Text="Restoring packages ... " Importance="high" />
<DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" !Exists('$(NuGetExePath)')" />
</Target>
<Target Name="UpdateVersion" DependsOnTargets="RestorePackages">
<PropertyGroup>
<Version>$(GITVERSION_MAJOR).$(GITVERSION_MINOR).$(GITVERSION_COMMITSSINCELASTVERSION)</Version>
</PropertyGroup>
<ItemGroup>
<RegexTransform Include="$(RootDir)\MyApp\*.appxmanifest">
<Find><![CDATA[ Version="\d+\.\d+\.\d+\.\d+"]]></Find>
<ReplaceWith><![CDATA[ Version="$(Version).0"]]></ReplaceWith>
</RegexTransform>
<!--<RegexTransform Include="$(RootDir)\**\*.nuspec">
<Find><![CDATA[<version>\d+\.\d+\.\d+\.\d+</version>]]></Find>
<ReplaceWith><![CDATA[<version>$(PackageVersion)</version>]]></ReplaceWith>
</RegexTransform> -->
</ItemGroup>
<RegexTransform Items="@(RegexTransform)" />
<Message Text="Assm: Ver $(Version)" />
</Target>
</Project>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Go" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<RootDir>$(MSBuildStartupDirectory)</RootDir>
<CodeTaskFactoryDll Condition="'$(MSBuildToolsVersion)' != '14'">$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll</CodeTaskFactoryDll>
<CodeTaskFactoryDll Condition="'$(MSBuildToolsVersion)' == '14'">$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll</CodeTaskFactoryDll>
</PropertyGroup>
<UsingTask TaskName="RegexTransform" TaskFactory="CodeTaskFactory" AssemblyFile="$(CodeTaskFactoryDll)">
<ParameterGroup>
<Items ParameterType="Microsoft.Build.Framework.ITaskItem[]" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Using Namespace="Microsoft.Build.Framework" />
<Code Type="Fragment" Language="cs">
<![CDATA[
foreach(ITaskItem item in Items) {
string fileName = item.GetMetadata("FullPath");
string find = item.GetMetadata("Find");
string replaceWith = item.GetMetadata("ReplaceWith");
if(!File.Exists(fileName)) {
Log.LogError(null, null, null, null, 0, 0, 0, 0, String.Format("Could not find version file: {0}", fileName), new object[0]);
}
string content = File.ReadAllText(fileName);
File.WriteAllText(
fileName,
Regex.Replace(
content,
find,
replaceWith
)
);
}
]]>
</Code>
</Task>
</UsingTask>
<!-- This works for msbuild 12 -->
<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(CodeTaskFactoryDll)" >
<ParameterGroup>
<OutputFilename ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Net" />
<Using Namespace="Microsoft.Build.Framework" />
<Using Namespace="Microsoft.Build.Utilities" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
OutputFilename = Path.GetFullPath(OutputFilename);
Log.LogMessage("Downloading latest version of NuGet.exe...");
WebClient webClient = new WebClient();
webClient.DownloadFile("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", OutputFilename);
return true;
}
catch (Exception ex) {
Log.LogErrorFromException(ex);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
<Target Name="DownloadNuGetExe">
<PropertyGroup>
<NuGetExeDir>$(MsBuildThisFileDirectory)..\.nuget</NuGetExeDir>
<NuGetExePath>$(NuGetExeDir)\nuget.exe</NuGetExePath>
</PropertyGroup>
<MakeDir Directories="$(NuGetExeDir)" Condition="!Exists('$(NuGetExeDir)')" />
<Message Text="Restoring packages ... " Importance="high" />
<DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" !Exists('$(NuGetExePath)')" />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment