1717 </PropertyGroup >
1818
1919 <ItemGroup Condition =" '$(PackageSources)' == '' " >
20- <!-- Package sources used to restore packages. By default will used the registered sources under %APPDATA%\NuGet\NuGet.Config -->
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 -->
2122 <!--
22- <PackageSource Include="https://nuget.org/api/v2/" />
23+ <PackageSource Include="https://www. nuget.org/api/v2/" />
2324 <PackageSource Include="https://my-nuget-source/nuget/" />
2425 -->
2526 </ItemGroup >
2829 <!-- Windows specific commands -->
2930 <NuGetToolsPath >$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath >
3031 <PackagesConfig >$([System.IO.Path]::Combine($(ProjectDir), "packages.config"))</PackagesConfig >
31- <PackagesDir >$([System.IO.Path]::Combine($(SolutionDir), "packages"))</PackagesDir >
3232 </PropertyGroup >
3333
3434 <PropertyGroup Condition =" '$(OS)' != 'Windows_NT'" >
3535 <!-- We need to launch nuget.exe with the mono command if we're not on windows -->
3636 <NuGetToolsPath >$(SolutionDir).nuget</NuGetToolsPath >
3737 <PackagesConfig >packages.config</PackagesConfig >
38- <PackagesDir >$(SolutionDir)packages</PackagesDir >
3938 </PropertyGroup >
4039
4140 <PropertyGroup >
4241 <!-- NuGet command -->
43- <NuGetExePath Condition =" '$(NuGetExePath)' == '' " >$(NuGetToolsPath)\nuget .exe</NuGetExePath >
42+ <NuGetExePath Condition =" '$(NuGetExePath)' == '' " >$(NuGetToolsPath)\NuGet .exe</NuGetExePath >
4443 <PackageSources Condition =" $(PackageSources) == '' " >@(PackageSource)</PackageSources >
4544
4645 <NuGetCommand Condition =" '$(OS)' == 'Windows_NT'" >"$(NuGetExePath)"</NuGetCommand >
4948 <PackageOutputDir Condition =" $(PackageOutputDir) == ''" >$(TargetDir.Trim('\\'))</PackageOutputDir >
5049
5150 <RequireConsentSwitch Condition =" $(RequireRestoreConsent) == 'true' " >-RequireConsent</RequireConsentSwitch >
51+ <NonInteractiveSwitch Condition =" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' " >-NonInteractive</NonInteractiveSwitch >
52+
53+ <PaddedSolutionDir Condition =" '$(OS)' == 'Windows_NT'" >"$(SolutionDir) "</PaddedSolutionDir >
54+ <PaddedSolutionDir Condition =" '$(OS)' != 'Windows_NT' " >"$(SolutionDir)"</PaddedSolutionDir >
55+
5256 <!-- Commands -->
53- <RestoreCommand >$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(RequireConsentSwitch) -o "$(PackagesDir)" </RestoreCommand >
54- <BuildCommand >$(NuGetCommand) pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols</BuildCommand >
57+ <RestoreCommand >$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $( RequireConsentSwitch) -solutionDir $(PaddedSolutionDir) </RestoreCommand >
58+ <BuildCommand >$(NuGetCommand) pack "$(ProjectPath)" -Properties " Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand >
5559
56- <!-- Make the build depend on restore packages -->
60+ <!-- We need to ensure packages are restored prior to assembly resolve -->
5761 <BuildDependsOn Condition =" $(RestorePackages) == 'true'" >
5862 RestorePackages;
5963 $(BuildDependsOn);
6973 <Target Name =" CheckPrerequisites" >
7074 <!-- Raise an error if we're unable to locate nuget.exe -->
7175 <Error Condition =" '$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text =" Unable to locate '$(NuGetExePath)'" />
72- <SetEnvironmentVariable EnvKey =" VisualStudioVersion" EnvValue =" $(VisualStudioVersion)" Condition =" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' " />
73- <DownloadNuGet OutputFilename =" $(NuGetExePath)" Condition =" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
76+ <!--
77+ Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
78+ This effectively acts as a lock that makes sure that the download operation will only happen once and all
79+ parallel builds will have to wait for it to complete.
80+ -->
81+ <MsBuild Targets =" _DownloadNuGet" Projects =" $(MSBuildThisFileFullPath)" Properties =" Configuration=NOT_IMPORTANT;DownloadNuGetExe=$(DownloadNuGetExe)" />
82+ </Target >
83+
84+ <Target Name =" _DownloadNuGet" >
85+ <DownloadNuGet OutputFilename =" $(NuGetExePath)" Condition =" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
7486 </Target >
7587
7688 <Target Name =" RestorePackages" DependsOnTargets =" CheckPrerequisites" >
109121
110122 Log.LogMessage("Downloading latest version of NuGet.exe...");
111123 WebClient webClient = new WebClient();
112- webClient.DownloadFile("https://nuget.org/nuget.exe", OutputFilename);
124+ webClient.DownloadFile("https://www. nuget.org/nuget.exe", OutputFilename);
113125
114126 return true;
115127 }
121133 </Code >
122134 </Task >
123135 </UsingTask >
124-
125- <UsingTask TaskName =" SetEnvironmentVariable" TaskFactory =" CodeTaskFactory" AssemblyFile =" $(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
126- <ParameterGroup >
127- <EnvKey ParameterType =" System.String" Required =" true" />
128- <EnvValue ParameterType =" System.String" Required =" true" />
129- </ParameterGroup >
130- <Task >
131- <Using Namespace =" System" />
132- <Code Type =" Fragment" Language =" cs" >
133- <![CDATA[
134- try {
135- Environment.SetEnvironmentVariable(EnvKey, EnvValue, System.EnvironmentVariableTarget.Process);
136- }
137- catch {
138- }
139- ]]>
140- </Code >
141- </Task >
142- </UsingTask >
143136</Project >
0 commit comments