Skip to content

Instantly share code, notes, and snippets.

@adamzuckerman
Last active March 9, 2016 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamzuckerman/056e4ae3482b530493ec to your computer and use it in GitHub Desktop.
Save adamzuckerman/056e4ae3482b530493ec to your computer and use it in GitHub Desktop.
Get the NuGet CommandLine executable including path (version 2.8.6)
<UsingTask
TaskName="GetNuGetCommandLine"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<SolutionPath ParameterType="System.String" Required="true" />
<NuGetCommandLine ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq"/>
<Using Namespace="System.IO" />
<Using Namespace="System.Linq" />
<Using Namespace="System.Xml.Linq" />
<Code Type="Fragment" Language="cs">
<![CDATA[
Log.LogMessage("Getting the NuGet command line.", MessageImportance.High);
var solutionDirectory = this.SolutionPath;
var nugetDirectory = Path.Combine(solutionDirectory, ".nuget");
if (Directory.Exists(nugetDirectory))
{
var xml = XDocument.Load(Path.Combine(nugetDirectory, "packages.config"));
Log.LogMessage("XML: " + xml.Root.Elements("packages").ToString(), MessageImportance.High);
var nuget = (from package in xml.Root.Elements("package")
where package.Attribute("id").Value.Equals("NuGet.CommandLine", StringComparison.InvariantCultureIgnoreCase)
select new
{
Id = package.Attribute("id").Value,
Version = package.Attribute("version").Value
}).FirstOrDefault();
if (nuget != null)
{
this.NuGetCommandLine = Path.Combine(solutionDirectory,
string.Format("packages\\{0}.{1}\\tools\\NuGet.exe", nuget.Id, nuget.Version));
}
}
if (string.IsNullOrEmpty(this.NuGetCommandLine))
{
throw new Exception("NuGet.CommandLine package not installed.");
}
Log.LogMessage("Command Line: " + this.NuGetCommandLine, MessageImportance.High);
]]>
</Code>
</Task>
</UsingTask>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Build">
<GetNuGetCommandLine SolutionPath="$(SolutionDir)">
<Output TaskParameter="NuGetCommandLine" PropertyName="NuGetCommandLine" />
</GetNuGetCommandLine>
<!-- The version properties ($(Major), $(Minor), $(Build)) are built in a different target. -->
<!-- Package the project -->
<Exec Command="$(NuGetCommandLine) pack $(TargetName).nuspec -Properties &quot;Configuration=$(ConfigurationName);Version=$(Major).$(Minor).$(Build);ID=$(TargetName)&quot; -OutputDir &quot;$(OutputDirectory)&quot;" />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment