8
8
9
9
<!-- Property that enables building a package from a project -->
10
10
<BuildPackage Condition =" '$(BuildPackage)' == '' " >false</BuildPackage >
11
+
12
+ <!-- Determines if package restore consent is required to restore packages -->
13
+ <RequireRestoreConsent Condition =" '$(RequireRestoreConsent)' != 'false' " >true</RequireRestoreConsent >
11
14
12
15
<!-- Download NuGet.exe if it does not already exist -->
13
16
<DownloadNuGetExe Condition =" '$(DownloadNuGetExe)' == '' " >false</DownloadNuGetExe >
14
17
</PropertyGroup >
15
18
16
19
<ItemGroup Condition =" '$(PackageSources)' == '' " >
17
- <!-- 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://nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
18
22
<!--
19
23
<PackageSource Include="https://nuget.org/api/v2/" />
20
24
<PackageSource Include="https://my-nuget-source/nuget/" />
25
29
<!-- Windows specific commands -->
26
30
<NuGetToolsPath >$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath >
27
31
<PackagesConfig >$([System.IO.Path]::Combine($(ProjectDir), "packages.config"))</PackagesConfig >
28
- <PackagesDir >$([System.IO.Path]::Combine($(SolutionDir), "packages"))</PackagesDir >
29
32
</PropertyGroup >
30
33
31
34
<PropertyGroup Condition =" '$(OS)' != 'Windows_NT'" >
32
35
<!-- We need to launch nuget.exe with the mono command if we're not on windows -->
33
36
<NuGetToolsPath >$(SolutionDir).nuget</NuGetToolsPath >
34
37
<PackagesConfig >packages.config</PackagesConfig >
35
- <PackagesDir >$(SolutionDir)packages</PackagesDir >
36
38
</PropertyGroup >
37
39
38
40
<PropertyGroup >
39
41
<!-- NuGet command -->
40
- <NuGetExePath Condition =" '$(NuGetExePath)' == '' " >$(NuGetToolsPath)\nuget .exe</NuGetExePath >
42
+ <NuGetExePath Condition =" '$(NuGetExePath)' == '' " >$(NuGetToolsPath)\NuGet .exe</NuGetExePath >
41
43
<PackageSources Condition =" $(PackageSources) == '' " >@(PackageSource)</PackageSources >
42
44
43
45
<NuGetCommand Condition =" '$(OS)' == 'Windows_NT'" >"$(NuGetExePath)"</NuGetCommand >
44
46
<NuGetCommand Condition =" '$(OS)' != 'Windows_NT' " >mono --runtime=v4.0.30319 $(NuGetExePath)</NuGetCommand >
45
47
46
48
<PackageOutputDir Condition =" $(PackageOutputDir) == ''" >$(TargetDir.Trim('\\'))</PackageOutputDir >
47
-
49
+
50
+ <RequireConsentSwitch Condition =" $(RequireRestoreConsent) == 'true' " >-RequireConsent</RequireConsentSwitch >
51
+ <NonInteractiveSwitch Condition =" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' " >-NonInteractive</NonInteractiveSwitch >
52
+
48
53
<!-- Commands -->
49
- <RestoreCommand >$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" -o "$(PackagesDir)" </RestoreCommand >
50
- <BuildCommand >$(NuGetCommand) pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols</BuildCommand >
54
+ <RestoreCommand >$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir "$(SolutionDir) " </RestoreCommand >
55
+ <BuildCommand >$(NuGetCommand) pack "$(ProjectPath)" -Properties Configuration=$(Configuration) $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand >
51
56
52
- <!-- Make the build depend on restore packages -->
57
+ <!-- We need to ensure packages are restored prior to assembly resolve -->
53
58
<BuildDependsOn Condition =" $(RestorePackages) == 'true'" >
54
59
RestorePackages;
55
60
$(BuildDependsOn);
65
70
<Target Name =" CheckPrerequisites" >
66
71
<!-- Raise an error if we're unable to locate nuget.exe -->
67
72
<Error Condition =" '$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text =" Unable to locate '$(NuGetExePath)'" />
68
- <SetEnvironmentVariable EnvKey =" VisualStudioVersion" EnvValue =" $(VisualStudioVersion)" Condition =" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' " />
69
- <DownloadNuGet OutputFilename =" $(NuGetExePath)" Condition =" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
73
+ <!--
74
+ Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
75
+ This effectively acts as a lock that makes sure that the download operation will only happen once and all
76
+ parallel builds will have to wait for it to complete.
77
+ -->
78
+ <MsBuild Targets =" _DownloadNuGet" Projects =" $(MSBuildThisFileFullPath)" Properties =" Configuration=NOT_IMPORTANT;DownloadNuGetExe=$(DownloadNuGetExe)" />
79
+ </Target >
80
+
81
+ <Target Name =" _DownloadNuGet" >
82
+ <DownloadNuGet OutputFilename =" $(NuGetExePath)" Condition =" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
70
83
</Target >
71
84
72
85
<Target Name =" RestorePackages" DependsOnTargets =" CheckPrerequisites" >
117
130
</Code >
118
131
</Task >
119
132
</UsingTask >
120
-
121
- <UsingTask TaskName =" SetEnvironmentVariable" TaskFactory =" CodeTaskFactory" AssemblyFile =" $(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
122
- <ParameterGroup >
123
- <EnvKey ParameterType =" System.String" Required =" true" />
124
- <EnvValue ParameterType =" System.String" Required =" true" />
125
- </ParameterGroup >
126
- <Task >
127
- <Using Namespace =" System" />
128
- <Code Type =" Fragment" Language =" cs" >
129
- <![CDATA[
130
- try {
131
- Environment.SetEnvironmentVariable(EnvKey, EnvValue, System.EnvironmentVariableTarget.Process);
132
- }
133
- catch {
134
- }
135
- ]]>
136
- </Code >
137
- </Task >
138
- </UsingTask >
139
133
</Project >
0 commit comments