Net Standard Library - Add files to nuget content folder on 'dotnet pack' - nuget

Is there an automated way to include a config transform file in the nuget package that is created when using the dotnet pack command?

I was able to do this by adding the xdt transform files in the class library project and then editing the csproj file to have this ItemGroup.
<ItemGroup>
<Content Include="app.config.uninstall.xdt" />
<Content Include="app.config.install.xdt" />
</ItemGroup>
When doing a dotnet pack this automatically adds them to the content folder of the nuget package.

Related

Include my Nuget README in my Nuget Package

I maintain a few tiny Nuget Packages.
I have a Nuget README file in the git repository, and the .nupkg file is auto-built by VS, based on the "Package" config, stored in the .csproj file.
Whenever I need to release a new versio of the package, I upload the .nupkg file to nuget, in the web UI, and then it asks me for any documentation, at which point I have to manually upload the README file.
Is there any way to put that README file in the .nupkg so that I don't have to manually upload it every time?
NuGet have now added support to make this JustWork.
Announcement on the Issue thread: https://github.com/NuGet/Home/issues/6873#issuecomment-833829727
Announcement Blog post: https://devblogs.microsoft.com/nuget/add-a-readme-to-your-nuget-package/
All you need to do is reference the README.md file in the .csproj, using the PackageReadmeFile tag. If the README.md is in the git root (i.e. where GitHub auto-detects it) then it will look like this:
<PackageReadmeFile>README.md</PackageReadmeFile>
Currently there is not a way to do this, but it is WIP.
The new spec under review: https://github.com/NuGet/Home/wiki/Embedding-and-displaying-NuGet-READMEs
This issue for this feature: https://github.com/NuGet/Home/issues/6873
i do this via the csprj. file.
I created a .netstandard project and added a readme.txt.
add following to the x.csproj
<ItemGroup>
<None Include="readme.txt" pack="true" PackagePath="." />
</ItemGroup>
When installing the nuget the first time the file will be displayed in VS.
There is somewhere in the internet a artikel describing the solution, but i cannot find it now.
Here my complete csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net471;net35</TargetFrameworks>
.....
<FileVersion>1.0.1.11</FileVersion>
<AssemblyVersion>1.0.1.11</AssemblyVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="4.7.6" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="readme.txt" pack="true" PackagePath="." />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>

Add install.ps1 on Class Library (.NET Standard) Project

I have migrate my .NET Framework project to a .NET Standard project.
In the .NET Framework project i have a .nuspec file with additional file config and create the nuget package with "NuGet.exe pack"
<files>
<file src="Install.ps1" target="tools\Install.ps1" />
</files
In the .NET Standard project i have not longer a nuspec file and switch to "msbuild -t:Pack" to create the nuget package. I have try to set the install.ps1 to (BuildAction = Content) but then i see a warning in the log "Issue: PowerShell file out side tools folder." And in the nupkg file the directory is "content\tools\Install.ps1" i need "tools\Install.ps1".
To get file into a different path in the package you can use the <PackagePath> element in the <Content> element like this:
<ItemGroup>
<Content Include="install.ps1">
<PackagePath>tools\</PackagePath>
</Content>
</ItemGroup>
(providing the install.ps1 is in the root of your project, otherwise you'll have to adjust the Include attribute value)
For more information check out the docs about the pack MsBuild Target here:
https://github.com/NuGet/Home/wiki/Adding-nuget-pack-as-a-msbuild-target
I have try to set the install.ps1 to (BuildAction = Content) but then i see a warning in the log "Issue: PowerShell file out side tools folder." And in the nupkg file the directory is "content\tools\Install.ps1" i need "tools\Install.ps1"
When you use msbuild -t:Pack to create the nuget package, msbuild/VS expects the files to be in content folder. But if you still want to the install.ps1 file in the tools directory, you can still use the .nuspec file and nuget.exe to pack the package.
The detail steps to pack package:
Create the .nuspec as below settings:
<?xml version="1.0"?>
<package >
<metadata>
<id>TestInstall.ps1</id>
<version>1.0.0</version>
<authors>Test</authors>
<owners>Test</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package description</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
<files>
<file src="Install.ps1" target="tools" />
<file src="bin\Debug\netstandard1.4\TestInstall.ps1.dll" target="lib\netstandard1.4" />
</files>
</package>
Then use the command line: nuget.exe pack xxx.nuspec to pack the package, you will get the package with Install.ps1 in the tools directory:

Add custom DLL files in nuget package in new csproj

.nuspec file has section <files>, what are the alternatives for include localization resources for new csproj file? How to add custom DLL files?
The first part of the trick is to get your third party DLL added to the nupkg. This does it:
<ItemGroup>
<Reference Include="ThirdParty">
<HintPath>..\DLLs\ThirdParty.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="$(OutputPath)\ThirdParty.dll">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)</PackagePath>
</Content>
</ItemGroup>
If you install this package into an old-style csproj then a reference to ThirdParty.dll will be added.
However, if you install this package into a new-style csproj then a reference to ThirdParty.dll will not be added as a referece, irritatingly. Work in progress...

NuGet package adds incorrect hint path

During an automated build, my nuget package needs to be non framework dependent, however I keep finding that the nuget package getting added is incorrectly adding a HintPath.
Within my nuspec I've defined the files that are part of the package:
<files>
<file src="lib\xyz.dll" target="lib\xyz.dll" />
<file src="lib\xyz.xml" target="lib\xyz.xml" />
</files>
However whenever I add the package to my project/solution, it incorrectly adds a hint path specifying:
<Reference Include="xyz, Version=11.0.0.0, Culture=neutral, PublicKeyToken=4a3c0a4c668b48b4">
<HintPath>..\packages\xyz.11.0.0.0\xyz.dll</HintPath>
<Private>True</Private>
</Reference>
This is causing the automated build server to not find the assembly and fail to build. I can manually fix the hint path, but would rather not.
I took a look at this post (Failed to add NuGet package) but I don't find it relevant. This post (NuGet package install uses specific assembly version in csproj files) seemed to be referring to the same problem but with no answer. Anybody have any thoughts?
You can work around this by using a custom MSBuild task.
Instead of adding the assembly to the lib directory create an MSBuild .targets file named after the package id and put your xyz assembly next to it.
\build
\Net45
\MyPackage.targets
\xyz.dll
\xyz.xml
Then in the MSBuild .targets file add the reference exactly how you want it to be. Something like:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Reference Include="xyz">
<HintPath>$(MSBuildThisFileDirectory)\xyz.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
The above shows how to specify a hint path relative to the MSBuild .targets file. You said that you do want to use a hint path so you could remove that if xyz.dll can be resolved by MSBuild somehow, such as it being in the GAC.

NuGet Package Restore: Visual Studio Online & POSTSHARP

I installed POSTSHARP as a nuget package and I want Visual Studio Online to automatically restore it.
POSTSHARP must be restored before build though.
I am trying to follow this with no success: link
How can I run scripts / commands in Visual Studio Online BEFORE build?
There are instructions on nuget.org on how to set up a package restore with TFS, including Visual Studio Online: http://docs.nuget.org/docs/reference/package-restore-with-team-build
It mentions that the default Build Process Templates for VSO already implements NuGet Package Restore workflow. So, supposedly, you need to do additional setup only when you customize the templates.
The proposed approach is to create a simple MSBuild project file that will be used to build the solution. You can include all the required targets there (e.g. Build, Rebuild, Clean) that will just invoke MSBuild on your solution file with specifying the corresponding target.
Additionally create a target for package restore - it will invoke NuGet.exe restore MySolution.sln command. The common build targets will depend on this one.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0"
DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OutDir>$(MSBuildThisFileDirectory)bin</OutDir>
<Configuration>Release</Configuration>
<ProjectProperties>
OutDir=$(OutDir);
Configuration=$(Configuration);
</ProjectProperties>
</PropertyGroup>
<ItemGroup>
<Solution Include="$(MSBuildThisFileDirectory)src\*.sln" />
</ItemGroup>
<Target Name="RestorePackages">
<Exec Command=""$(MSBuildThisFileDirectory)tools\NuGet\NuGet.exe" restore "%(Solution.Identity)"" />
</Target>
<Target Name="Build" DependsOnTargets="RestorePackages">
<MSBuild Targets="Build"
Projects="#(Solution)"
Properties="$(ProjectProperties)" />
</Target>
<!-- other targets... -->
</Project>