Set custom ID for a NuGet package without using .nuspec - visual-studio

I need my Visual Studio C# library published to NuGet with a custom name from command line.
As far as I understood there is only one way to do it - using .nuspec file with the ID specified. This is what I wouldn't like to do because I'd have to manually fix the file on every project change. I also was not able to find a tool who generates a .nuspec (with dependencies) from a .cproj file.
I've been looking and the nuget pack options, but only found -Properties who seems not to do the job.

I also was not able to find a tool who generates a .nuspec (with
dependencies) from a .cproj file.
As far as I know, there's no easy tool(nuget spec command does't support doing that) to generate the .nuspec with package dependencies from a xx.csproj. And since your actual question is Set custom ID for a NuGet package **without** using .nuspec, I won't talk about how to generate and modify it here.
I've been looking and the nuget pack options, but only found
-Properties who seems not to do the job.
I assume you're using a .net framework project that uses PackageReference format. Since you're not willing to use .nuspec file, you have to use nuget pack xx.csproj or nuget pack(run this command in where the csproj exists) command.
But as I know there's also a known issue that nuget pack won't show dependencies when using this command with projects that use PackageReference format. And yes, nuget pack -Properties won't meet your needs for your current project type. So it's not a good choice.
Set custom ID for a NuGet package without using .nuspec
Suggests for your original question:
Since you don't want to use .nuspec file, nuget pack is not an option for your situation. To create a nuget package, nuget.exe is not your only choice. You can also consider dotnet CLI and MSBuild.exe.
1.You can choose to change your project from non-SDK format to new SDK format, then you can specify the ID in project file(.csproj), and you don't need a .nuspec any more. Also, after the migration, it will support specifying the ID in command-line.
So you can use command like dotnet pack -p:PackageID=TestName to specify the ID(or other package metadata) in command-line easily. (For sdk format, you can also use msbuild.exe)
2.If you don't want to change your current project format to SDK format, you can follow this topic: If you are using MSBuild with a non-SDK-style project and PackageReference, add the NuGet.Build.Tasks.Pack package to your project.
Consume that package and then use command msbuild -t:restore to restore nuget packages in command-line. Then use command like msbuild -t:pack -p:PackageID=xxx to specify ID in command-line.(Or set that ID in xx.csproj)

Related

Why is TinyIoC not picked up in my Xamarin.Android project when added using nuget?

So I installed TinyIoC v1.3.0 using nuget in my Xamarin.Android project, its in the references, but I cannot type using TinyIoC; without a compile error. I would like to understand why this is?
PS. I know I can use the TinyIoC.cs file directly, but I thought it would be more convenient to update if I added using nuget.
I get this error:
I think the issue is related to the nuget package TinyIoC 1.3.0 itself.
The nuget package does not contain the lib folder with the related dll so that you can not use the format by using namespace.
Note: Important
The nuget package contains a folder called Content. This folder will copy its content into the main project with packages.config when you install the nuget pacakge. See this similar issue on so.
And it will make TinyloC.cs file directly in your main project and you can just modify it there.
I have created a net framework project with packages.config format.
However, since your project is xamarin andorid app, it uses PackageReference nuget manage format, and content folder does not work for it. Instead, you should use ContentFiles folder, but this nuget package does not contain it.
To make this issue more clearly, you should contact the author of the nuget package to report this problem.

How to modify .nuspec file before NuGet pack in Visual Studio

Visual Studio makes creating NuGet packages easier these days by automating the process of creating the .nuspec file. It means that version dependencies etc. are automatically created and you can simply set up the textual values here:
So, most of the work is already done by the time the NuGet package is built. However, there are external assemblies that I need to add to the .nuspec file before it is built. I know I can add external files in the files collection of the outputted .nuspec file (in obj\Release):
My question is, what build event or otherwise should I use to edit the .nuspec after it is created, but before the pack command is called by msbuild or Visual Studio? Or, should I just run a separate build process afterwards?
Note: a bonus would be to find a tool which I can call that will add the files collection. Without that, I'm guessing I will need to write powershell code to add the files...
You could use Pack element in csproj file: https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#including-content-in-a-package
Add something like this to your project:
<ItemGroup>
<Content Pack="True"
PackagePath="lib\netcoreapp2.2"
Include="C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All\2.1.2\Microsoft.AI.DependencyCollector.dll" />
</ItemGroup>
This will add a Microsoft.AI.DependencyCollector.dll library to your package:
This way you can add whatever you like to your library.

Visual Studio dotnet core copy and paste references

I've known about copy/paste references in Visual Studio since 2010/2012. Has this been updated to work with Core 2?
Here's the SO Question asking about the old style references (before Core, and before the reboot of csproj format): Is it possible to copy / paste References from one project to another in Visual Studio?
Maybe now that dotnet add package is available, we don't need copy/paste references in VS?
"Add Google social login" walkthrough for ASP.NET Core 2.0 suggests using the dotnet CLI to add a package reference:
To install with .NET Core CLI, execute the following in your project
directory:
dotnet add package Microsoft.AspNetCore.Authentication.Google
source: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins?tabs=aspnetcore2x
Writing this answer to my own question so the "just use the CLI" folks have something to upvote.
If you are talking about NuGet package references, don't try to copy the resolved references to dll files manually (the are generated from the obj\project.assets.json file during the design-time build after loading the project).
In the new SDK-based project model and the PackageReference way of referencing NuGet packages (also available for "classic" projects), NuGet references automatically flow transitively across package references. So when your app references a library that uses a NuGet package, you no longer need to install the NuGet package in both the library and the app (and potentially test projects).
Only for "classic" references, this issue remains. However, if you need to import them into all projects (say you got a few .dll files from a 3rd party), you can create a Directory.Build.targets in the solution folder to add them to all projects in your solution (technically, this file is automatically imported into all projects in the directory hierarchy):
<Project>
<ItemGroup>
<Reference Include="AssemblyName">
<HintPath>shared-libs\AssemblyName.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
(you may want to change the reference if you want the "specific version" feature or strong-name references as described in https://stackoverflow.com/a/16580870/784387)

Nuget: Add file to consuming project?

I would like to add an XSD file to a Nuget package. When a project installs the Nuget package, this file should be included in the project, in order to allow Intellisense in the project's app.config file.
I can add the XSD to the Nuget package just fine, but I don't know how to make it show up in the project that uses the dependency.
Can this be done?
FWIW There are some extra sections injected in the project's app.config file (via Nuget's "transform" capability). The XSD offers Intellisense for those extra sections, and Visual Studio automatically picks up XSD schemas included in the project (or even solution) that match the declared namespace of the XML elements.
Nuget has the option to run custom install Powershell scripts that get a reference to the project that is installing the package.
Details:
http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package#Automatically_Running_PowerShell_Scripts_During_Package_Installation_and_Removal
Here are some usage examples:
http://www.codeproject.com/Articles/209522/PowerShell-Script-Examples-for-NuGet-Packages

Merge C# assemblies and create a nuget package

I have a solution that generates DLLs. There are good reasons to have multiple DLLs and I cannot just combine all of the dll projects into one, so I am using ilmerge to generate a single DLL as my final product. The ilmerge is done in the post-build.
The problem that I have is that I would like to create a nuget package using this generated DLL.
Normally, you could use this in the post build:
c:\NuGet.exe pack "$(ProjectPath)" -Symbols -OutputDir "\\Some Path\NuGet Packages"
This uses your solution's target to create the nuget package. The problem, of course, is that the target that the solution has is not the target that I want to use.
I could use a nuspec file. The problem with that is that I want to use the version number of the DLL as the version number of the nuget package.
So, my issue could be solved in a couple of different ways:
Create some sort of project that has MyPackage.dll as a target but that doesn't otherwise do anything.
Somehow create a nuspec file that uses the version number of the DLL file as the nuget version.
Any ideas?
Somehow create a nuspec file that uses the version number of the DLL file as the nuget version.
$version$ replacement token can be user more details here http://docs.nuget.org/docs/reference/nuspec-reference

Resources