Nesting files in Nuget package without PowerShell - powershell

The title says it all. I have files that I want to nest during the installation of a NuGet package but can't use PowerShell scripts since they won't be run any longer (see here).
Are there any other ways to achieve this goal?
UPDATE: By nested I mean like *.resx and *.Designer.cs or *.xaml and code-behind files *.xaml.cs. I know I can achieve that by adding a <DependentUpon> element in the *.csproj file but I don't know how I can add that element without using PowerShell.
UPDATE2: init.ps1 runs the first time a package is installed in a solution. That won't cut it though. I would need the script to run when the package is installed into a project just like install.ps1 was run up to NuGet3.
UPDATE3: What I want to do is to add 3 files into the Properties folder of the target projects (Resources.resx, Resources.tt and Resources.Designer.cs). They are a replacement for the usual resources implementation. These files are installed by the nuget package when it is added to the project.
This is the part of the *.nuspec file that adds them to the Content folder of the package. As only one of them is actually content (the others being an Embedded Resource and Compile respectively) it would be nice to be able to set their build actions accordingly but one step at a time.
<files>
<file src="Properties\Resources.resx" target="content\Properties\Resources.resx" />
<file src="Properties\Resources.tt.pp" target="content\Properties\Resources.tt.pp" />
<file src="Properties\Resources.Designer.cs" target="content\Properties\Resources.Designer.cs" />
</files>
As these files are added to the projects I want the nesting inside the *.csproj file and not happen via a separate *.props file if that is somehow possible.

Packages can add MSBuild items like this to a project by using a .props file in the package. It would contain the same content that you would put into the .csproj file.
The down side of this is that the content cannot be modified by the user. If you need to modify the user's actual project file and copy content to the project folder you would have to include a .targets file in your package and set BeforeTargets="Build" on your target. This would give you a chance to run before build and make changes as needed.
The build folder works for both packages.config and PackageReference (NETCore SDK) projects. You can find more out about it here: https://learn.microsoft.com/en-us/nuget/create-packages/creating-a-package#including-msbuild-props-and-targets-in-a-package

Related

Insert Import into project file at position 0 and save without prompt using Nuget install.ps1

I am building a Nuget package which has a tools folder with install.ps1. This Nuget package is dependent on NugetPowerTools package which provides some APIs to work with the project and its properties. I have pasted the code used in my install.ps1 below.
param($installPath, $toolsPath, $package, $project)
$buildProject = Get-MSBuildProject
$import = $buildProject.Xml.AddImport("Custom.Targets")
$buildProject.Save()
I have 2 questions.
The AddImport appends the target at the end (i.e., after other Import lines). I need to have my Import as the first line (since I have some functionality in this targets file which needs to be executed before others). Is it possible to insert the Import at first (i.e., my import with Custom.Targets should be the first item of Import in the project file)? I can't find an Insert method.
Once the project file is modified by this install.ps1, there is a pop up /prompt in Visual Studio with 4 options namely, SaveAs, Discard, Overwrite and Ignore. I want to save & overwrite the project file without this prompt / pop up from my install.ps1. This is to avoid any accidental mistake by the user. To overcome, I found a link below, which has the following line of code, which helps me overcome this issue.
How can I reload a visual studio project thru a nuget powershell script
$(get-item $project.FullName).lastwritetime=get-date
But now, I get another pop up / prompt with options to Reload, Reload All, Ignore, Ignore All. Is there a way to Reload this project without this prompt?
Please help.
Note: I am OK not to use NugetPowerTools if someone can provide the correct syntax without NugetPowerTools also.
Is it possible to insert the Import at first (i.e., my import with
Custom.Targets should be the first item of Import in the project
file)? I can't find an Insert method.
Since you want to let Custom.Targets to be the first import project in the xxx.proj file, you can separate this from Powershell and then use Custom Build Targets or Props to realize it. Actually, l am not similar with powershell script, so l recommend this easy function which was released since NuGet 2.5.
Note that this function should use nuget.exe and nuspec file to pack your project.
Step 1)
Create a file in a new folder called build which exists in the root directory of the project.And then put <package_id>.props file into it.Besides, you would better add an appropriate sub folder which is related to the framework. Like this:
\build
\netstandard2.0
\<package_id>.props
Attention: see this from the document:
.props is added at the top of the project file; .targets is added at the bottom.
.targets and.props files are of the same nature, but under this nuget mechanism,.props files are placed at the top of the xxx.proj file. Besides, the file name should be the same as the package_id which is defined in the nuspec file.
So you should change your Custom.Targets to <package_id>.pros.
Step 2)
Then in the .nuspec file, be sure to refer to these files in the node:
<?xml version="1.0"?>
<package >
<metadata minClientVersion="2.5">
<!-- ... -->
</metadata>
<files>
<!-- Include everything in \build -->
<file src="build\**" target="build" />
<!-- Other files -->
<!-- ... -->
</files>
</package>
Step 3)
Finally, use the nuget.exe to pack your project.
In addition, please make sure that the properties, items or targets which you use are defined in your custom props in case when you use some common properties which are defined in the Microsoft.Common.props or Microsoft.CSharp.targets,they cannot be found and report errors because these files are imported after your custom props.
This is the result which l have tested:
But now, I get another pop up / prompt with options to Reload, Reload
All, Ignore, Ignore All. Is there a way to Reload this project without
this prompt?
I think this behavior is controlled by VS itself rather than powershell. VS will detect the status of these files and pop up a prompt. So this is controlled by the VS SDK. Or I guess you can refer to this document to override the behavior of VS via the VS SDK interface.
Hope it could help you.

How to execute custom script when installing nuget package?

I created this project https://github.com/RemiBou/RemiBou.CosmosDB.Migration, for working it needs the user to do 2 things : create the appropriate folders and edit the csproj so the file inside those folders are embedded.
Before we could do that automaticly when installing with install.ps1 but this feature has been deprecated. Do you know any way how I could do this ?
install.ps1 isn't exactly deprecated, but it's a feature unique to packages.config. PackageReference has no concept of install as anyone can simply edit the csproj and add a PackageReference. When you restore, NuGet has no way of knowing if this is the first time the package is restored for the project after the reference was added, or if it's just the first time the project was restored with a clean repo (after a "msbuild /t:clean" or "git clone", for example).
I don't know an alternative for creating the folders other than having documentation that says the convention is to use that folder name. But, an alternative to modifying the csproj is to take advantage of the fact that MSBuild is a generic build system and NuGet packages can include MSBuild props and targets file.
In your specific case, I would create a props file that defines a property something like <RemiBouCosmosDBMigrationPath>CosmosDB\Migrations\</RemiBouCosmosDBMigrationPath>, which allows your package users to change the property to a different path be overwriting the property value in their csproj, if they prefer.
Then create a targets file which contains a target something like
<Target name="RemiBouCosmosDBMigrationsEmbedMigrations" BeforeTargets="???">
<ItemGroup>
<EmbeddedResource Include="$(RemiBouCosmosDBMigrationPath)**\*.js" />
</ItemGroup>
</Target>
You'll need to figure out what the best target name to put in the BeforeTargets attribute, but I hope you understand the idea. A csproj file is nothing more than a MSBuild file with certain conventions. MSBuild files can import other MSBuild files, and MSBuild and NuGet work together to allow MSBuild to import MSBuild files that come from restored packages. Just compose the MSBuild properties and items in a different way, and the end result can still be the same.

Automated injection of props/targets files not working for native C++ project

I am using automatic package restore in NuGet v2.8.50506.491 with Visual Studio 2013.
I have added a build folder to my package with a {package ID}.props file. However, the file is apparently not being injected into the vcxproj at restore time. The package and all its content are being restored correctly but none of the definitions are visible in vcxproj properties. This may be expected if property injection occurs in memory, but the build fails due to paths that are clearly defined in the props not having been inherited.
If I add an explicit reference to the props file in my local packages repository, the project builds successfully, therefore there is no issue with the paths in props file.
I have also tried adding the props within a "native" subfolder under build, also to no avail.
An extract from the nuspec:
<file src="build\MyPackage.targets" target="build\MyPackage.targets" />
I have also tried a targets file instead of/as well as a props file, but this does not work either.
I should add that I have defined Nuget.config in the sln folder, with an absolute path to my packages repository:
<config>
<add key="repositoryPath" value="C:\Packages" />
</config>
I was having this problem today, and eventually I realized that the names of my .nuspec and .targets files were different than the id of my package, which is apparently a problem. Renaming the .nuspec and .targets files to match the package id made NuGet start injecting into the vcxproj correctly. I'm not sure which of the two files was the problem, or if it was both, but it's working correctly now that all three names match.
Injection of .targets and .props file references happens only at the time when you install the NuGet package. This is the same as with .NET projects where assembly references are created only at package install time.
Later when you build the project the package restore mechanism merely downloads and extracts the NuGet package so that the previously "dangling" .target / .props / assembly references become valid references.

Build a NuGet package from project but without any references

I have created a T4 template that I want to reuse in several projects using NuGet. This template is based on a custom class so I need to distribute a DLL with the template. The DLL is used by the template during generation, but is not used by the project itself so I do NOT want it added to the target project as a reference.
My .nuspec file includes my DLL and places it in the root of the package which will prevent it from creating a reference and everything works fine if I build my package by hand. Unfortunately, I want to use the "nuget pack" command to automatically pick up the version number. When I run this command and reference the project file, it includes my DLL twice, once in the root (as I specified in my nuspec file) and an additional time in the lib folder (along with everything in the bin folder).
How can I automatically build my NuGet package without including any references. It seems there are several solutions, but I can't figure any of them out:
Let nuget include my DLL in the lib folder, but prevent it from creating the reference. I know I can use the <references> section to reference some DLLs and not others, but in this case I don't want it to reference any. If I leave the <references> section blank it is either ignored or I get an error depending on what level I leave blank.
Prevent nuget from including any DLLs automatically and only include the files in the <files> section of the nuspec file. Unfortunately, I can't figure out how to build the package from the project file without it including my DLL automatically.
Any thoughts?
If you right-click the DLL, you'll probably see that its "build action" is set to "content". Try setting it to "none".

Create nuget package with multiple DLLs

Let's say I have a project with this structure:
MyLibrary\
MyLibrary.sln
MyLibrary.Core\
MyLibrary.Core.csproj
MyLibrary.Extensions\
MyLibrary.Extensions.csproj
MyLibrary.Tests\
MyLibrary.Tests.csproj
I want to create a single NuGet package which packages MyLibrary.Core.dll and MyLibrary.Extensions.dll. I can't seem to figure out how to get NuGet to do this. I've tried building a spec file manually and I've tried building one using "nuget spec MyLibrary.Core.csproj". I've tried adding all of the DLLs to a lib/ folder which I understand to be the convention-based mechanism for adding DLLs to the package. In every case I can get the MyLibary.Core.dll to get into the package but the MyLibrary.Extensions.dll does not end up packaged along with it.
TLDR: What is the best practice for creating a NuGet package with multiple projects / assemblies? Is there a tutorial out there that focuses on this? The tutorials I've found all focus on simple single-project demos.
You'll run NuGet on a single project (or nuspec file), but it supports pointers to other projects via the file element. This element uses the names of your project's References, so you avoid having to a) find the location of other project files, and b) copy files to a particular place as a post-build step.
Supposing you have a nuspec file for MyLibrary.Core.csproj, and it references MyLibrary.Extensions and MyLibrary.Tests such that they end up in the bin directory after a build:
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
...
</metadata>
<files>
<file src="bin\Release\MyLibrary.Extensions.dll" target="lib\net40" />
<file src="bin\Release\MyLibrary.Tests.dll" target="lib\net40" />
</files>
</package>
With this setup, all of your references should end up in the appropriate place in the NuGet package. You still have the hard-coded 'Release' in there, but I'd wager most probably don't distribute NuGet packages of their debug builds anyway.
Did you generate a blank nuspec file with:
nuget spec
If you use that file and then put your dlls in a folder under it named lib, it will package them up.
I had a little trouble with trying to generate a nuspec file from a project or dll. Also, if you manually reference any files in the nuspec file, the conventions are not used. This is probably the problem with nuspecs generated from dlls or projects.
Also, if you are trying to run this from a build script that executes in a different folder, you can tell nuget the location of your .\lib folder via the -BasePath command line:
build\nuget.exe pack nuget\Company.Project.nuspec -BasePath nuget\
Have you tried NuGet Package Explorer? Might be the easiest way:
http://nuget.codeplex.com/releases/view/59864
It seems your problem is the same as this question: Why doesn't nuget include the referenced project when packing?. If so, you can use the -includereferencedprojects option (See http://docs.nuget.org/docs/reference/command-line-reference#Pack_Command).
I recently published a solution for this...
My solution enables automatic creation of NuGet packages when you build the solution where each package can contain multiple assemblies, references to both external NuGets and NuGets created during the same build and even include the source code for debugging.
In your case, all you will need to do is add a new class library project to your solution, reference the projects you want to package, then add a post build event.
You can find an article with a walk-through guide here
and the source code here.
i have some tutorial how i did it with windows and visual studio:
create local folder and call it packages like: c:/packages//lib - important to create another folder in folder call it lib and past there dll.
open nuget package explorer - https://npe.codeplex.com/downloads/get/clickOnce/NuGetPackageExplorer.application
the ui very intuitive just add dll and export it to /lib (for tutorial https://blog.zwezdin.com/2014/building-nuget-packages-with-gui-tool/ it in russian but see on pictures the flow it's about 3 clicks)
it will create nuspec file
open GIT BASH - https://git-for-windows.github.io/ and navigate to: cd c: => cd packages (the path of )
*maybe on windows you will need provide developer options for windows's linux stuff (https://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/)
in GIT BASH enter command: nuget add -source [options]
where: : the full name of nuspec (include .nuspec)
and: the path of folder lib in folder (c:/packages//lib)
after the action ended successfully
in GIT BASH enter another command:
nuget pack .nuspec
not in this folder you have .nupkg file.
How to install a Nuget Package .nupkg file locally? - tutorial how to add it to visual studio.
I had the same problem and I decided to create Nuget which will allow to create other nugets from chosen project.
Package is deployed on the Nuget.org site. After referencing it in the project You need to add nuspeck file to the projects which should generate the projects.
Project with the required nuspeck file
Last thing which should be done by you is invoke command Create-Nuspec in Package Manager. Than the powershell module will take all libraries which are result of the build it will add also the required dependencies and create the nuget in the output directory.
Description about this package is placed here.
I had an issue when adding extra dlls references to a Nuget packages, and testing the package on a sample project, the extra dlls was not being added apparently, no matter the way that I create the Nuget Package.
Then I released that when you uninstall and install again a local Nuget Package with the same version number the changes no take effect, the extra dlls are not added.
So each time you uninstall the package, close visual studio and clear the Nuget Cache,
How to clear NuGet package cache using command line?
Then open again Visual Studio and reinstall the local package to make the change takes effect.
Or make the package version to increase each time to be for Visual Studio to recognize your changes.
For example:
Package-1.0.0
Package-1.0.1
Package-1.0.2
.....
To create a Nuget package from a sample project there are some ways, for example:
Right click to your Visual Studio project and choose the "Pack" option
Then install the nuget package explorer:
https://www.microsoft.com/es-ec/p/nuget-package-explorer/9wzdncrdmdm3?activetab=pivot:overviewtab
And add manually your extra references (dlls files) to your specific targets.
Create a nuget package using a .nuspec file how specified in the documentation:
https://learn.microsoft.com/en-us/nuget/guides/create-packages-for-xamarin
<files>
<file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.dll" target="lib\netstandard1.4\Plugin.LoggingLibrary.dll" />
<file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.xml" target="lib\netstandard1.4\Plugin.LoggingLibrary.xml" />
<file src="Plugin.LoggingLibrary.iOS\bin\Release\iOsDependence.dll" target="lib\Xamarin.iOS10\iOsDependence.dll" />
<file src="Plugin.LoggingLibrary.Android\bin\Release\AndroidDependence.dll" target="lib\MonoAndroid10\AndroidDependence.dll" />
</files>
In your files part add your .dll files.
And dont forget, each time you uninstall and install again the nuget package from your local source.
Or you increase the version of the package each time:
Or close Visual Studio, clean the nuget cache and Rebuild your project.
In order to take effect the changes.