Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get location of nuget package directory

Tags:

c#

nuget

msbuild

I am creating a nuget package, and I want to make sure that it will work in all scenarios.

In this case, I am adding build targets with the "Build" convention folder. The target that I am adding needs to know the location of the nuget packages folder.

A lot of the examples I have found assume that they can just use "..\Packages", but Nuget allows you to relocate the packages folder via the nuget.config file.

Is there a way to get the correct location of the nuget packages folder from within a build.targets file?

(background information)

I'm writing a package that needs to copy native dlls into the bin directory. I could use a post build event, but that seems a little hacky. I would rather include a target that invokes the msbuild <copy> task with SkipUnchangedFiles=true, however for that to work, I need to know the location of the packages folder.

like image 994
JMarsch Avatar asked Jan 17 '14 17:01

JMarsch


People also ask

Where are NuGet packages installed in project?

go to the Project or Solution in question. right click, Manage NuGet Packages... on the left, you will see 'Installed Packages' click on this and you will see the list.

Where is the NuGet package in Visual Studio?

From Visual Studio, select Tools > NuGet Package Manager > Package Manager Console.

Where are .NET packages stored?

By default, the output of the command is a package store under the . dotnet/store subdirectory of the user's profile. You can specify a different location using the --output <OUTPUT_DIRECTORY> option.


2 Answers

From within the .targets file, you can access the current file's location using the $(MSBuildThisFileDirectory) property.

Assuming the .targets file is in the nuget build folder, you could get to its own package folder using relative paths like this:

<PropertyGroup>
    <ThisPackageDirectory>$(MSBuildThisFileDirectory)..\</ThisPackageDirectory>
    ...
like image 71
Joseph Gabriel Avatar answered Oct 03 '22 18:10

Joseph Gabriel


To solve the problem described in your background information section you have 2 steps.

A. Ensure the native dlls are copied to the project - edit a section of the nuspec to copy your files into the project root when the package is being built:

<?xml version="1.0"?>
<package>
  <metadata>
    ...
  </metadata>
  <files>
    <file src="install.ps1" target="Tools"/>
    <file src="nativedll1.dll" target="content" />
    <file src="nativedll2.dll" target="content" />
  </files>
</package>

B. Create an install.ps1 file (note that I included it in the above sample) Your install.ps1 file should look something like this.

param($installPath, $toolsPath, $package, $project)

$file1 = $project.ProjectItems.Item("nativedll1.dll")
$file2 = $project.ProjectItems.Item("nativedll2.dll")

# set 'Copy To Output Directory' to 'Copy if newer'
$copyToOutput1 = $file1.Properties.Item("CopyToOutputDirectory")
$copyToOutput1.Value = 2

$copyToOutput2 = $file2.Properties.Item("CopyToOutputDirectory")
$copyToOutput2.Value = 2
like image 38
CarllDev Avatar answered Oct 03 '22 18:10

CarllDev