Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuget Powershell: how to add native dependencies? how to add files to a project inside a folder?

I'm creating a Nuget package that has native dependencies. I put them inside the package without problems by specifying additional file entries in the .nuspec file.

However, I also want to copy these to the output folder of the project that is going to use my package, so that the dependencies can be found at runtime.

My idea is to add the native dependencies to the project and set their BuildAction to CopyToOutputDirectory. This I have also managed with the PowerShell script below:

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

Function add_file($file)
{
    $do_add = 1
    foreach($item in $project.DTE.ActiveSolutionProjects[0].ProjectItems)
    {
       if ($item -eq $file)
       { $do_add = 0 }
    }
    if ($do_add -eq 1)
    {
        $added = $project.DTE.ItemOperations.AddExistingItem($file)
        $added.Properties.Item("CopyToOutputDirectory").Value = 2
       $added.Properties.Item("BuildAction").Value = 0        
    }
}
add_file(<dependency1>)
add_file(<dependency2>)
...
add_file(<dependencyN>)

So far so good.

But, now my project becomes completely polluted with these dependencies.

Is there a way to add files to a project using PowerShell and put them inside a folder?
Or is there another way to achieve what I want: adding native dependencies to a NuGet package and outputting them to the bin-folder of the project using my Nu-package?

like image 424
Menno Deij - van Rijswijk Avatar asked Mar 23 '12 16:03

Menno Deij - van Rijswijk


People also ask

How do I pack a NuGet package?

Run the pack commandSelect the AppLogger project in Solution Explorer, and then select Build > Pack. Visual Studio builds the project and creates the . nupkg file. If you don't see the Pack command on the menu, your project is probably not an SDK-style project, and you need to use the NuGet CLI.

What are Nuspec files?

A . nuspec file is an XML manifest that contains package metadata. This manifest is used both to build the package and to provide information to consumers. The manifest is always included in a package.


2 Answers

The SqlServerCompact package did something similar, copying the relevant dlls to the bin folder in the post build event. Here the relevant code:

File:install.ps1

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

. (Join-Path $toolsPath "GetSqlCEPostBuildCmd.ps1")

# Get the current Post Build Event cmd
$currentPostBuildCmd = $project.Properties.Item("PostBuildEvent").Value

# Append our post build command if it's not already there
if (!$currentPostBuildCmd.Contains($SqlCEPostBuildCmd)) {
    $project.Properties.Item("PostBuildEvent").Value += $SqlCEPostBuildCmd
}

File:GetSqlCEPostBuildCmd.ps1

$solutionDir = [System.IO.Path]::GetDirectoryName($dte.Solution.FullName) + "\"
$path = $installPath.Replace($solutionDir, "`$(SolutionDir)")

$NativeAssembliesDir = Join-Path $path "NativeBinaries"
$x86 = $(Join-Path $NativeAssembliesDir "x86\*.*")
$x64 = $(Join-Path $NativeAssembliesDir "amd64\*.*")

$SqlCEPostBuildCmd = "
if not exist `"`$(TargetDir)x86`" md `"`$(TargetDir)x86`"
xcopy /s /y `"$x86`" `"`$(TargetDir)x86`"
if not exist `"`$(TargetDir)amd64`" md `"`$(TargetDir)amd64`"
xcopy /s /y `"$x64`" `"`$(TargetDir)amd64`""
like image 123
Alexandre Dion Avatar answered Oct 22 '22 07:10

Alexandre Dion


I'd suggest you open the 4.0.8852.1 version of the SqlServerCompact Nuget package with NuGet Package Explorer (Microsoft Store, GitHub) and use it as a template. It worked for me.

like image 34
juanagui Avatar answered Oct 22 '22 07:10

juanagui