NuGet.targets 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 will used the registered sources under %APPDATA%\NuGet\NuGet.Config -->
  16. <PackageSource Include="http://apdnexus01.plano.intuit.com/nexus/service/local/nuget/dotnet-public/" />
  17. <PackageSource Include="https://nuget.org/api/v2/" />
  18. </ItemGroup>
  19. <PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
  20. <!-- Windows specific commands -->
  21. <NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
  22. <PackagesConfig>$([System.IO.Path]::Combine($(ProjectDir), "packages.config"))</PackagesConfig>
  23. <PackagesDir>$([System.IO.Path]::Combine($(SolutionDir), "packages"))</PackagesDir>
  24. </PropertyGroup>
  25. <PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
  26. <!-- We need to launch nuget.exe with the mono command if we're not on windows -->
  27. <NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
  28. <PackagesConfig>packages.config</PackagesConfig>
  29. <PackagesDir>$(SolutionDir)packages</PackagesDir>
  30. </PropertyGroup>
  31. <PropertyGroup>
  32. <!-- NuGet command -->
  33. <NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\nuget.exe</NuGetExePath>
  34. <PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>
  35. <NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
  36. <NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 $(NuGetExePath)</NuGetCommand>
  37. <PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>
  38. <RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
  39. <!-- Commands -->
  40. <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(RequireConsentSwitch) -o "$(PackagesDir)"</RestoreCommand>
  41. <BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols</BuildCommand>
  42. <!-- Make the build depend on restore packages -->
  43. <BuildDependsOn Condition="$(RestorePackages) == 'true'">
  44. RestorePackages;
  45. $(BuildDependsOn);
  46. </BuildDependsOn>
  47. <!-- Make the build depend on restore packages -->
  48. <BuildDependsOn Condition="$(BuildPackage) == 'true'">
  49. $(BuildDependsOn);
  50. BuildPackage;
  51. </BuildDependsOn>
  52. </PropertyGroup>
  53. <Target Name="CheckPrerequisites">
  54. <!-- Raise an error if we're unable to locate nuget.exe -->
  55. <Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
  56. <SetEnvironmentVariable EnvKey="VisualStudioVersion" EnvValue="$(VisualStudioVersion)" Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' " />
  57. <DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
  58. </Target>
  59. <Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
  60. <Exec Command="$(RestoreCommand)"
  61. Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />
  62. <Exec Command="$(RestoreCommand)"
  63. LogStandardErrorAsError="true"
  64. Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
  65. </Target>
  66. <Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
  67. <Exec Command="$(BuildCommand)"
  68. Condition=" '$(OS)' != 'Windows_NT' " />
  69. <Exec Command="$(BuildCommand)"
  70. LogStandardErrorAsError="true"
  71. Condition=" '$(OS)' == 'Windows_NT' " />
  72. </Target>
  73. <UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  74. <ParameterGroup>
  75. <OutputFilename ParameterType="System.String" Required="true" />
  76. </ParameterGroup>
  77. <Task>
  78. <Reference Include="System.Core" />
  79. <Using Namespace="System" />
  80. <Using Namespace="System.IO" />
  81. <Using Namespace="System.Net" />
  82. <Using Namespace="Microsoft.Build.Framework" />
  83. <Using Namespace="Microsoft.Build.Utilities" />
  84. <Code Type="Fragment" Language="cs">
  85. <![CDATA[
  86. try {
  87. OutputFilename = Path.GetFullPath(OutputFilename);
  88. Log.LogMessage("Downloading latest version of NuGet.exe...");
  89. WebClient webClient = new WebClient();
  90. webClient.DownloadFile("https://nuget.org/nuget.exe", OutputFilename);
  91. return true;
  92. }
  93. catch (Exception ex) {
  94. Log.LogErrorFromException(ex);
  95. return false;
  96. }
  97. ]]>
  98. </Code>
  99. </Task>
  100. </UsingTask>
  101. <UsingTask TaskName="SetEnvironmentVariable" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  102. <ParameterGroup>
  103. <EnvKey ParameterType="System.String" Required="true" />
  104. <EnvValue ParameterType="System.String" Required="true" />
  105. </ParameterGroup>
  106. <Task>
  107. <Using Namespace="System" />
  108. <Code Type="Fragment" Language="cs">
  109. <![CDATA[
  110. try {
  111. Environment.SetEnvironmentVariable(EnvKey, EnvValue, System.EnvironmentVariableTarget.Process);
  112. }
  113. catch {
  114. }
  115. ]]>
  116. </Code>
  117. </Task>
  118. </UsingTask>
  119. </Project>