NuGet.targets 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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' ">false</RequireRestoreConsent>
  11. <!-- Download NuGet.exe if it does not already exist -->
  12. <DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">true</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://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://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), "..", "lib", ".nuget"))</NuGetToolsPath>
  25. <PackagesConfig>$([System.IO.Path]::Combine($(ProjectDir), "..", "lib", "packages.config"))</PackagesConfig>
  26. <RepositoryPath>$([System.IO.Path]::Combine($(SolutionDir), "..", "lib"))</RepositoryPath>
  27. </PropertyGroup>
  28. <PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
  29. <!-- We need to launch nuget.exe with the mono command if we're not on windows -->
  30. <NuGetToolsPath>$(SolutionDir)..\lib\.nuget</NuGetToolsPath>
  31. <PackagesConfig>$(SolutionDir)..\lib\packages.config</PackagesConfig>
  32. <RepositoryPath>$(SolutionDir)..\lib</RepositoryPath>
  33. </PropertyGroup>
  34. <PropertyGroup>
  35. <!-- NuGet command -->
  36. <NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\NuGet.exe</NuGetExePath>
  37. <PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>
  38. <NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
  39. <NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono $(NuGetExePath)</NuGetCommand>
  40. <PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>
  41. <RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
  42. <NonInteractiveSwitch Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' ">-NonInteractive</NonInteractiveSwitch>
  43. <!-- Commands -->
  44. <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -OutputDirectory "$(RepositoryPath)"</RestoreCommand>
  45. <BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -Properties Configuration=$(Configuration) $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand>
  46. <!-- We need to ensure packages are restored prior to assembly resolve -->
  47. <BuildDependsOn Condition="$(RestorePackages) == 'true'">
  48. RestorePackages;
  49. $(BuildDependsOn);
  50. </BuildDependsOn>
  51. <!-- Make the build depend on restore packages -->
  52. <BuildDependsOn Condition="$(BuildPackage) == 'true'">
  53. $(BuildDependsOn);
  54. BuildPackage;
  55. </BuildDependsOn>
  56. </PropertyGroup>
  57. <Target Name="CheckPrerequisites">
  58. <!-- Raise an error if we're unable to locate nuget.exe -->
  59. <Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
  60. <!--
  61. Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
  62. This effectively acts as a lock that makes sure that the download operation will only happen once and all
  63. parallel builds will have to wait for it to complete.
  64. -->
  65. <MsBuild Targets="_DownloadNuGet" Projects="$(MSBuildThisFileFullPath)" Properties="Configuration=NOT_IMPORTANT;DownloadNuGetExe=$(DownloadNuGetExe)" />
  66. </Target>
  67. <Target Name="_DownloadNuGet">
  68. <DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
  69. </Target>
  70. <Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
  71. <Exec Command="$(RestoreCommand)"
  72. Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />
  73. <Exec Command="$(RestoreCommand)"
  74. LogStandardErrorAsError="true"
  75. Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
  76. </Target>
  77. <Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
  78. <Exec Command="$(BuildCommand)"
  79. Condition=" '$(OS)' != 'Windows_NT' " />
  80. <Exec Command="$(BuildCommand)"
  81. LogStandardErrorAsError="true"
  82. Condition=" '$(OS)' == 'Windows_NT' " />
  83. </Target>
  84. <UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  85. <ParameterGroup>
  86. <OutputFilename ParameterType="System.String" Required="true" />
  87. </ParameterGroup>
  88. <Task>
  89. <Reference Include="System.Core" />
  90. <Using Namespace="System" />
  91. <Using Namespace="System.IO" />
  92. <Using Namespace="System.Net" />
  93. <Using Namespace="Microsoft.Build.Framework" />
  94. <Using Namespace="Microsoft.Build.Utilities" />
  95. <Code Type="Fragment" Language="cs">
  96. <![CDATA[
  97. try {
  98. OutputFilename = Path.GetFullPath(OutputFilename);
  99. Log.LogMessage("Downloading latest version of NuGet.exe...");
  100. WebClient webClient = new WebClient();
  101. webClient.DownloadFile("https://nuget.org/nuget.exe", OutputFilename);
  102. return true;
  103. }
  104. catch (Exception ex) {
  105. Log.LogErrorFromException(ex);
  106. return false;
  107. }
  108. ]]>
  109. </Code>
  110. </Task>
  111. </UsingTask>
  112. </Project>