Nuget package init.ps1 script and PackageReference modification - powershell

I want to have referenced dll(s) in my package in "copy local = false" mode. In other words, I don't want that the dll(s) my package ships is/are copied into a output directory after the build.
The way this can be done in "packages.config" scenario is by having a following script in the tools/install.ps1 file.
param($installPath, $toolsPath, $package, $project)
$asms = $package.AssemblyReferences | %{$_.Name}
foreach ($reference in $project.Object.References)
{
if ($asms -contains $reference.Name + ".dll")
{
$reference.CopyLocal = $false;
}
}
I know that the newest Nuget version with PackageReference functionality uses only init.ps1 script. But I don't have any clue what so ever, what shall I write into that script.
I also know that the package user can add...
<ExcludeAssets>Runtime</ExcludeAssets>
to make this happen. But as a package author, I really would like to enforce that behaviour. So would it be possible to add that in the script?

Related

Prevent embedding interop types in nuspec package file

I've created a nuget package for an interop library. When using this package, Nuget automatically sets "Embed Interop Types" to "True".
However, in my project this should be "False".
Is there a way in Nuget to specify that interop types should not be embedded?
I know it's quite an old question but if someone faces this then follow the below steps :
While creating a NuGet package, add a tools folder and add a file named: install.ps1
Write below PowerShell commands which define the project requirement, In my case, I had to add EmbedInteropTypes: false for SapROTWr library.
param($installPath, $toolsPath, $package, $project)
$project.Object.References | Where-Object { $_.Name -eq "Interop.SapROTWr" } | ForEach-Object { $_.EmbedInteropTypes = $false }
Now add that package to your project and checkout the properties.

How to delete files a dependency installed after your package is installed

I'm working on a nuget package that depends on the Unity and Unity.Mvc4 packages. My nuspec file has them listed as dependencies. Everything works, but when my package is installed the dependencies are installed first.
The Unity dependencies deploy files to the root of the project that my package has moved to a different location and customized, as such I don't want those files to exist in the root after my package is installed.
Is there any way to override dependency files in nuspec, or run a powershell script after install to clean them up?
You might add a Powershell script that move those files created by Unity to your actual project root.
For information about executing Powershell scripts during Nuget package installation, see the Nuget documentation. Note that these scripts must be placed within the tools folder of your Nuget package to be automatically executed.
Here's the code in my install.ps1 that did the trick if anyone wants it for reference.
# Runs every time a package is installed in a project
param($installPath, $toolsPath, $package, $project)
# $installPath is the path to the folder where the package is installed.
# $toolsPath is the path to the tools directory in the folder where the package is installed.
# $package is a reference to the package object.
# $project is a reference to the project the package was installed to.
#EnvDTE Project Reference
#https://msdn.microsoft.com/en-us/library/envdte.project.projectitems.aspx
function deleteProjectItem($fileName) {
$file = $null
foreach ($item in $project.ProjectItems) {
if ($item.Name.Equals($fileName, "InvariantCultureIgnoreCase")) {
$file = $item
break
}
}
if ($file -ne $null) {
Write-Host "Deleting: $($item.Name) ..." -NoNewline
$item.Delete()
Write-Host "Deleted!"
}
}
deleteProjectItem "BootStrapper.cs"
deleteProjectItem "job_scheduling_data_2_0.xsd"
deleteProjectItem "Unity.Mvc4.README.txt"

NuGet init.ps1 executing twice

I am following the solution found at "Create a NuGet package that shows update notifications" to get a notification of a nuget package update.
However the init.ps1 script is executing twice.
I stripped all the code out so that only the following is in init.ps1.
param($installPath, $toolsPath, $package, $project)
if ($project -eq $null) {
$project = Get-Project
}
Write-Host "Hello, I'm running inside of init.ps1"
When I close the solution and reopen it, in the output window the text is there twice.
I am using VS 2012, NuGet 2.2.31210
I checked the packages.config file and there is only one entry for my package.
Why is it running twice and is there any way to get it to only run once?
Thanks,
Joe
In init.ps1 script, Get-Project will return a random project in the solution but not the particular project the package has been installed.
Use $projects = Get-Project -All to get the list of projects in solution, loop through the projects and search the packages.config to check if the current project has the package installed.

how to output debugging messages from install.ps1 in NuGet

I am developing a NuGet package, including an install.ps1 script which runs during the package installation. I would like to be able to output messages from my script and also output the results of running .bat files from within my sript.
Here is my install.ps1:
param($installPath, $toolsPath, $package, $project)
Write-Output "Running install.ps1 for MyPkg"
Set-Location $toolsPath
.\helper.bat | Write-Output
When I install my package in Visual Studio, then I look in the Package Manager option in the Output page, I see:
Executing script file 'C:\Test\packages\MyPkg.1\tools\install.ps1'.
and it seems the script is working (I can tell in other ways that helper.bat ran), but I don't see any of the output. How can I get the output working?
I could not get the output when installing from the NuGet Package Manager Dialog, I'll dig a bit later to see where it's going.
But you should be able to see it when installing from the Nuget console (Tools->Library Package Manager->Package Manager Console). The output went directly in the console. Example :
PM> uninstall-package samplepackage
hello from unninstal.ps1
Successfully removed 'samplepackage 1.0.0' from WebApplication24.
unninstal.ps1 :
param($installPath, $toolsPath, $package, $project)
Write-Host "hello from unninstal.ps1"

NuGet: How can I change property of files with Install.ps1 file?

I am creating NuGet package and for that I have created Nuspec manifest file. In content folder I have two files, test.exe and test.config. Now I would like to change property "Copy To Output Directory" of these the files to "Copy Always" in project, when any user installs this package.
I found related question NuGet how to apply properties to files, that shows can do this using PowerShell install.ps1 script, but I have no idea how to create that file.
Your install.ps1 file should look something like this.
param($installPath, $toolsPath, $package, $project)
$file1 = $project.ProjectItems.Item("test.exe")
$file2 = $project.ProjectItems.Item("test.config")
# 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
Here is a bit more detail on how to solve this problem end to end:
You need to do two things to ensure the status are set correctly on install...
Write the install.ps1 script to mark the status of the files.
Ensure the install.ps1 script is in the Tools directory in the nuget package.
Install.ps1 Script
The following example script will recursively mark every file in the "Content" and "View" directory as "Copy to newer". Note this example script is written to make it clear to read and understand. It will mark every file in the Content and Views folders in the root directory of the Visual Studios project.
param($installPath, $toolsPath, $package, $project)
function MarkDirectoryAsCopyToOutputRecursive($item)
{
$item.ProjectItems | ForEach-Object { MarkFileASCopyToOutputDirectory($_) }
}
function MarkFileASCopyToOutputDirectory($item)
{
Try
{
Write-Host Try set $item.Name
$item.Properties.Item("CopyToOutputDirectory").Value = 2
}
Catch
{
Write-Host RecurseOn $item.Name
MarkDirectoryAsCopyToOutputRecursive($item)
}
}
#Now mark everything in the a directory as "Copy to newer"
MarkDirectoryAsCopyToOutputRecursive($project.ProjectItems.Item("Content"))
MarkDirectoryAsCopyToOutputRecursive($project.ProjectItems.Item("Views"))
Copy To Tools
You must copy the install.ps1 file to the Tools directory for script to be executed by nuget. You can add the following to the nuspec template to do this.
<files>
<file src="install.ps1" target="Tools"/>
</files>
Note in this case I have the install.ps1 file in the root of my Visual Studios project and marked as "Copy if newer".