NuGet.targets 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  3. <PropertyGroup>
  4. <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>
  5. <!-- Enable the restore command to run before builds -->
  6. <RestorePackages Condition=" '$(RestorePackages)' == '' ">false</RestorePackages>
  7. <!-- Property that enables building a package from a project -->
  8. <BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>
  9. <!-- Determines if package restore consent is required to restore packages -->
  10. <RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>
  11. <!-- Download NuGet.exe if it does not already exist -->
  12. <DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
  13. </PropertyGroup>
  14. <ItemGroup Condition=" '$(PackageSources)' == '' ">
  15. <!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
  16. <!-- 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 -->
  17. <!--
  18. <PackageSource Include="https://www.nuget.org/api/v2/" />
  19. <PackageSource Include="https://my-nuget-source/nuget/" />
  20. -->
  21. </ItemGroup>
  22. <PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
  23. <!-- Windows specific commands -->
  24. <NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
  25. </PropertyGroup>
  26. <PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
  27. <!-- We need to launch nuget.exe with the mono command if we're not on windows -->
  28. <NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
  29. </PropertyGroup>
  30. <PropertyGroup>
  31. <PackagesProjectConfig Condition=" '$(OS)' == 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config</PackagesProjectConfig>
  32. <PackagesProjectConfig Condition=" '$(OS)' != 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config</PackagesProjectConfig>
  33. </PropertyGroup>
  34. <PropertyGroup>
  35. <PackagesConfig Condition="Exists('$(MSBuildProjectDirectory)\packages.config')">$(MSBuildProjectDirectory)\packages.config</PackagesConfig>
  36. <PackagesConfig Condition="Exists('$(PackagesProjectConfig)')">$(PackagesProjectConfig)</PackagesConfig>
  37. </PropertyGroup>
  38. <PropertyGroup>
  39. <!-- NuGet command -->
  40. <NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\NuGet.exe</NuGetExePath>
  41. <PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>
  42. <NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
  43. <NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 "$(NuGetExePath)"</NuGetCommand>
  44. <PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>
  45. <RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
  46. <NonInteractiveSwitch Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' ">-NonInteractive</NonInteractiveSwitch>
  47. <PaddedSolutionDir Condition=" '$(OS)' == 'Windows_NT'">"$(SolutionDir) "</PaddedSolutionDir>
  48. <PaddedSolutionDir Condition=" '$(OS)' != 'Windows_NT' ">"$(SolutionDir)"</PaddedSolutionDir>
  49. <!-- Commands -->
  50. <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>
  51. <BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand>
  52. <!-- We need to ensure packages are restored prior to assembly resolve -->
  53. <BuildDependsOn Condition="$(RestorePackages) == 'true'">
  54. RestorePackages;
  55. $(BuildDependsOn);
  56. </BuildDependsOn>
  57. <!-- Make the build depend on restore packages -->
  58. <BuildDependsOn Condition="$(BuildPackage) == 'true'">
  59. $(BuildDependsOn);
  60. BuildPackage;
  61. </BuildDependsOn>
  62. </PropertyGroup>
  63. <Target Name="CheckPrerequisites">
  64. <!-- Raise an error if we're unable to locate nuget.exe -->
  65. <Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
  66. <!--
  67. Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
  68. This effectively acts as a lock that makes sure that the download operation will only happen once and all
  69. parallel builds will have to wait for it to complete.
  70. -->
  71. <MsBuild Targets="_DownloadNuGet" Projects="$(MSBuildThisFileFullPath)" Properties="Configuration=NOT_IMPORTANT;DownloadNuGetExe=$(DownloadNuGetExe)" />
  72. </Target>
  73. <Target Name="_DownloadNuGet">
  74. <DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
  75. </Target>
  76. <Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
  77. <Exec Command="$(RestoreCommand)"
  78. Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />
  79. <Exec Command="$(RestoreCommand)"
  80. LogStandardErrorAsError="true"
  81. Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
  82. </Target>
  83. <Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
  84. <Exec Command="$(BuildCommand)"
  85. Condition=" '$(OS)' != 'Windows_NT' " />
  86. <Exec Command="$(BuildCommand)"
  87. LogStandardErrorAsError="true"
  88. Condition=" '$(OS)' == 'Windows_NT' " />
  89. </Target>
  90. <UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  91. <ParameterGroup>
  92. <OutputFilename ParameterType="System.String" Required="true" />
  93. </ParameterGroup>
  94. <Task>
  95. <Reference Include="System.Core" />
  96. <Using Namespace="System" />
  97. <Using Namespace="System.IO" />
  98. <Using Namespace="System.Net" />
  99. <Using Namespace="Microsoft.Build.Framework" />
  100. <Using Namespace="Microsoft.Build.Utilities" />
  101. <Code Type="Fragment" Language="cs">
  102. <![CDATA[
  103. try {
  104. OutputFilename = Path.GetFullPath(OutputFilename);
  105. Log.LogMessage("Downloading latest version of NuGet.exe...");
  106. WebClient webClient = new WebClient();
  107. webClient.DownloadFile("https://www.nuget.org/nuget.exe", OutputFilename);
  108. return true;
  109. }
  110. catch (Exception ex) {
  111. Log.LogErrorFromException(ex);
  112. return false;
  113. }
  114. ]]>
  115. </Code>
  116. </Task>
  117. </UsingTask>
  118. </Project>