How to install templates from nuget package? - nuget

I know I can install nuget packages from templates:
https://learn.microsoft.com/en-us/nuget/visual-studio-extensibility/visual-studio-templates
But I'd like to do the opposite, I'd like to install templates - provided by the nuget package - after the package was installed.
I read I could create a powershell script and run that after the package's been installed. Is that the only way? What are my options here?
Thanks.

For .NET Core it can be done using
dotnet new --install <id of your template package>
See docs for the CLI

I have the same problem, found that:
Dotnet 5.0 can't load templates from my nuget server by id, but dotnet 6.0 can, and puts .nupkg file to %USERPROFILE%.templateengine\packages
VS 2022 and new Rider view these templates.
Dotnet 5.0 can install template by .nupkg local path, and puts it to %USERPROFILE%.templateengine\dotnetcli\v5.0.404\packages
After step 3 you can use templates in Visual Studio 2019, but I also have ide.host.json in .template.config folder
https://github.com/sayedihashimi/template-sample#supporting-visual-studio-2019

Related

Nuget install failed: the package does not contain any assembly references or content files that are compatible with that framework

If I understand Scott Hanselman's post here correctly then I should be able to reference a .net standard library from a framework 4.7.2 solution.
I want to use Nuget to install such a reference.
I am using Azure DevOps Artifacts for the Nuget package. When I push my .net standard solution I can see that the package does create and that the Nuget install command is
Install-Package SBD.Standard -version 2.0.0-CI-etc
However when I try this at the PM console for my framework 4.7.2 project I get
Could not install package 'SBD.Standard 2.0.0-etc'. You are trying to install this package into a project that targets
'.NETFramework,Version=v4.7.2', but the package does not contain any assembly references or content files that are compatible with that
framework
moreover I do not see SBD.Standard as an avialable package in the relevant feed from within VS2019 -> Manage nuget packages for solution.
I think I need packages.config
I was able to create a new 4.7.2 project and add the nuget package that used .net standard 2.0 (but not 2.1)
When I tried to add the nuget package I got a dialog asking me to select the Nuget package manager format
so I chose Packages.config and that worked.
I am not sure I have it right. I solved my problem but am not sure whether this was it after all. SInce PackageReference is more recent it should work.
Some additional explanations for this issue.
There is an issue for NuGet restore when you using new SDK-based project type or PackageReference in the .net framework:
https://github.com/NuGet/Home/issues/5461
https://github.com/dotnet/sdk/issues/1755
When we use packages.config projects or non-SDK based project type, NuGet do not check compatibility during install/restore.
So, to resolve this issue, we need make sure the referenced project is an old style csproj or use packages.config.
That the reason why the packages.config works for you.
Hope this helps.

Adding references on Visual Studio Code from VSTS

I've got a private package stored on a feed in VSTS (Visual Studio Team Services). I want add it on a .NET Core project that I'm developing in Visual Studio Code.
Checking VSTS documentation, to add a package from private source it's necessary use the NuGet CLI, because .NET doesn't have support for private repositories (or at least I suppose it, because Microsoft recommend us to use NuGet CLI instead).
Using NuGet CLI, the command to add a package is NuGet install. But it downloads all packages, with .nupck, DLL files, a large, etc... (i.e., various XML files). And it doesn't add the necessary references to the project. Using .NET CLI to add the package, it adds the references in the .csproj file (project file), and it works as usual.
Is there a command that reproduces exactly the same behaviour like if we add package on Visual Studio, or Visual Code using .NET add package CLI?
If not, what I should do? Should I extract the DLL file and reference it manually in the .csproj file? If there's any update, how can I know it (in Visual Studio the NuGet UI shows me updates, but in Visual Studio Code, after adding a reference manually...)
There is no way to add a NuGet package into a project through Visual Studio Code nor through NuGet CLI.
And for the command nuget install (same function as nuget restore), it only downloads NuGet packages from the specified feed, but it does not make any changes for the project. You can also refer to the post How to install a NuGet package on the command line to a Visual Studio project.
The workarounds to add NuGet packages to project are as below:
Add packages in Visual Studio
You can open your project in Visual Studio, and then add NuGet packages through Visual Studio.
Manually change project file
If you have not Visual Studio installed. You can also manually change related files.
Such as add the private feed into nuget.config file, and then change .csproj file with the lines like:
<ItemGroup>
<PackageReference Include="xxx" Version="xxx" />
</ItemGroup>
Add packages by the dotnet add package command
You can also use dotnet add package command to add NuGet packages into your project:
dotnet add [<PROJECT>] package <PACKAGE_NAME> [-v|--version]

Why windows C# projects contain nuget executable in .nuget folder?

I don't understood the overall scheme on how NuGet is used on windows platform (because I never saw this, I work with Linux and monodevelop).
In many windows projects, there is a .nuget folder in repository, like
https://github.com/YAFNET/YAFNET/tree/master/yafsrc/.nuget
In this folder there is a nuget.exe executable.
Why it is necessary to add executables to source code control(SCC)?
How NuGet build tool differs from all other binary tools like compilers, which are not included into SCC?
Why other package managers like paludis are not added to SCC, but nuget does?
What are exactly the reasons to put NuGet to SCC?
It is not necessary to add NuGet.exe to source control.
The reason some projects have .nuget/NuGet.exe, with a set of other MSBuild files, is to use the MSBuild based package restore. The MSBuild based package restore will run NuGet.exe at build time to download any missing packages.
The MSBuild based package restore has been deprecated by the NuGet team.
Visual Studio with recent versions of the NuGet Package Manager will automatically restore NuGet packages before building the project. If you need to restore NuGet packages on a build server you can use NuGet.exe to restore the NuGet packages by running a command line similar to:
NuGet.exe restore YourSolution.sln

Using SlowCheetah without NuGet

I've just started using SlowCheetah. When I right click my config file and choose "Add Transform" it doesn't only add the extra config files for all my configuration, but it also install a NuGet package which in turn adds a bunch of files to my solution.
Is the NuGet package required? I'd really prefer NOT to use NuGet at all in this project.
If it is not required, is there an easy way to add the transforms without installing the NuGet package?
I will always build the solution in Visual Studio (not on a build server) and it's ok to require that SlowCheetah is installed on the computer that is used for the build.
I'm using Visual Studio 2010.
Edit:
I've tried just uninstalling the NuGet package, but then the transformation doesn't work... :(
The NuGet package is now required. You can edit the project/packages.config to get around it but that's not recommended.

Download Nuget Packages Without VS/NuGet Package Manager

How can I download NuGet Packages outside of visual studio? so it can be used to create offline packages.
How to download NuGet Package without Visual Studio or Nuget Package Manager:
Search your desired package at NuGet Official Site.
Copy the end of the URL of the package page.
For Example: http://nuget.org/packages/EntityFramework => Package Name is "EntityFramework"
Enter the URL: http://packages.nuget.org/api/v1/package/{Package Name}
For Example: http://packages.nuget.org/api/v1/package/EntityFramework
You can download NuGet packages outside of Visual Studio using:
NuGet Package Explorer
NuGet Package Explorer is a ClickOnce application which allows
creating and exploring NuGet packages easily. After installing it, you
can double click on a .nupkg file to view the package content. You can
also load packages directly from the official NuGet feed.
Open a package from online feed:
And export the package to the desired location:
Install the NuGet command line program:
The NuGet command line may be installed onto a machine in a few possible ways.
Direct download of the executable from https://dist.nuget.org/win-x86-commandline/latest/nuget.exe. The executable may be placed anywhere on the file system, and in most cases should be placed in a directory that is listed in the PATH environment variable.
Install the NuGet.CommandLine package from the NuGet Visual Studio client and either move nuget.exe to a common location or execute it in the context of your project.
Install the NuGet.CommandLine Chocolatey package using the Chocolatey client. More information on Chocolatey can be found at [http://chocolatey.org].
Then run nuget install package to download and install package in the current directory.
More about the NuGet command line program:
Command Line Reference
Chrome Plugin "NuTake" provides a direct download link.
Rename extension to .zip and extract
You can download nuget packages using - vnuget.org.
On this website you can also view content of nuget package - http://vnuget.org/packages/Microsoft.AspNet.Mvc/5.2.3.
Here are a few examples that can add to DeePak's answer:
This one downloads AutoMapper from NuGet.org
nuget.exe install AutoMapper -OutputDirectory c:\Temp\LotsOfPackages -Version 6.2.2
This one downloads MyCustomPackage from an internal TFS Nuget feed
nuget.exe install MyCustomPackage -OutputDirectory c:\Temp\LotsOfPackages -Source "http://tfs.myCompany.com:8080/tfs/TFSArea/_packaging/FeedName/nuget/v3/index.json" -Version 1.0.0.2
Notes
Keep in mind that the install command will get the package in question AND all its NuGet dependencies. So, be careful about just dumping this into the directory where you running. Thus, I added OutputDirectory to the command.
For internal Nuget packages/feeds, the Source URL is available via TFS. Go to your packages tab and find your specific feed URL. If it has any spaces that have been encoded with %20, you need to replace them with spaces.
CLI command reference
Copy packages from one NuGet feed to another