1 <?xml version="1.0" encoding="utf-8"?>
2 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
4 <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>
6 <!-- Enable the restore command to run before builds -->
7 <RestorePackages Condition=" '$(RestorePackages)' == '' ">false</RestorePackages>
9 <!-- Property that enables building a package from a project -->
10 <BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>
12 <!-- Determines if package restore consent is required to restore packages -->
13 <RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>
15 <!-- Download NuGet.exe if it does not already exist -->
16 <DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
19 <ItemGroup Condition=" '$(PackageSources)' == '' ">
20 <!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
21 <!-- The official NuGet package source (https://www.nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
23 <PackageSource Include="https://www.nuget.org/api/v2/" />
24 <PackageSource Include="https://my-nuget-source/nuget/" />
28 <PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
29 <!-- Windows specific commands -->
30 <NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
33 <PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
34 <!-- We need to launch nuget.exe with the mono command if we're not on windows -->
35 <NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
39 <PackagesProjectConfig Condition=" '$(OS)' == 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config</PackagesProjectConfig>
40 <PackagesProjectConfig Condition=" '$(OS)' != 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config</PackagesProjectConfig>
44 <PackagesConfig Condition="Exists('$(MSBuildProjectDirectory)\packages.config')">$(MSBuildProjectDirectory)\packages.config</PackagesConfig>
45 <PackagesConfig Condition="Exists('$(PackagesProjectConfig)')">$(PackagesProjectConfig)</PackagesConfig>
49 <!-- NuGet command -->
50 <NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\NuGet.exe</NuGetExePath>
51 <PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>
53 <NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
54 <NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 "$(NuGetExePath)"</NuGetCommand>
56 <PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>
58 <RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
59 <NonInteractiveSwitch Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' ">-NonInteractive</NonInteractiveSwitch>
61 <PaddedSolutionDir Condition=" '$(OS)' == 'Windows_NT'">"$(SolutionDir) "</PaddedSolutionDir>
62 <PaddedSolutionDir Condition=" '$(OS)' != 'Windows_NT' ">"$(SolutionDir)"</PaddedSolutionDir>
65 <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>
66 <BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand>
68 <!-- We need to ensure packages are restored prior to assembly resolve -->
69 <BuildDependsOn Condition="$(RestorePackages) == 'true'">
74 <!-- Make the build depend on restore packages -->
75 <BuildDependsOn Condition="$(BuildPackage) == 'true'">
81 <Target Name="CheckPrerequisites">
82 <!-- Raise an error if we're unable to locate nuget.exe -->
83 <Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
85 Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
86 This effectively acts as a lock that makes sure that the download operation will only happen once and all
87 parallel builds will have to wait for it to complete.
89 <MsBuild Targets="_DownloadNuGet" Projects="$(MSBuildThisFileFullPath)" Properties="Configuration=NOT_IMPORTANT;DownloadNuGetExe=$(DownloadNuGetExe)" />
92 <Target Name="_DownloadNuGet">
93 <DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
96 <Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
97 <Exec Command="$(RestoreCommand)"
98 Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />
100 <Exec Command="$(RestoreCommand)"
101 LogStandardErrorAsError="true"
102 Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
105 <Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
106 <Exec Command="$(BuildCommand)"
107 Condition=" '$(OS)' != 'Windows_NT' " />
109 <Exec Command="$(BuildCommand)"
110 LogStandardErrorAsError="true"
111 Condition=" '$(OS)' == 'Windows_NT' " />
114 <UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
116 <OutputFilename ParameterType="System.String" Required="true" />
119 <Reference Include="System.Core" />
120 <Using Namespace="System" />
121 <Using Namespace="System.IO" />
122 <Using Namespace="System.Net" />
123 <Using Namespace="Microsoft.Build.Framework" />
124 <Using Namespace="Microsoft.Build.Utilities" />
125 <Code Type="Fragment" Language="cs">
128 OutputFilename = Path.GetFullPath(OutputFilename);
130 Log.LogMessage("Downloading latest version of NuGet.exe...");
131 WebClient webClient = new WebClient();
132 webClient.DownloadFile("https://www.nuget.org/nuget.exe", OutputFilename);
136 catch (Exception ex) {
137 Log.LogErrorFromException(ex);