How to add commands to VisualStudio console? - visual-studio

Entity Framework and Nuget both do this. They add powershell commandlets that can be launched from the visual studio package manager console.
It would be really great if I could write some project-centric utilities that could be committed to my source control and be available for all developers from that console.
How do I do this?
Note that I'm not looking for a 3rd party solution (eg StudioShell), and that I'm aware that I can just write normal powershell scripts to do many things. I'm interested specifically in how to write functions that are first-class citizens in the Visual Studio package manager console like Get-Package and Update-Database.

EntityFramework's integration is done via NuGet, so you can't really have it in a project in your solution easily. You'd have to go through a NuGet package. Although, you could probably make it work using a local folder for the package. Essentially, EF includes a normal powershell module in its package's tools folder along with an init.ps1 that loads the module. Its contents:
param($installPath, $toolsPath, $package, $project)
if (Get-Module | ?{ $_.Name -eq 'EntityFramework' })
{
Remove-Module EntityFramework
}
Import-Module (Join-Path $toolsPath EntityFramework.psd1)
init.ps1 is run by NuGet/VS when opening the solution file. From the NuGet docs:
Init.ps1 runs the first time a package is installed in a solution.
* If the same package is installed into additional projects in the solution, the script is not run during those installations.
* The script also runs every time the solution is opened (Package Manager Console window has to be open at the same for the script to run). For example, if you install a package, close Visual Studio, and then start Visual Studio and open the solution with Package Manager Console window, the Init.ps1 script runs again.

Related

Need PowerShell Script in NuGet to install selected DLLs from Package into a VS Project

Can anyone please explain me the detailed steps to include in a PowerShell script for installing selected DLLs from a package into a VS project, based on the References in a project (say from the .csproj file)?
Can anyone please explain me the detailed steps to include in a PowerShell script for installing selected DLLs from a package into a VS project, based on the References in a project (say from the .csproj file)?
As we know, there is a PowerShell script install.ps1 that can be included in the package, which is by convention named and located in tools folder.
Download a NuGet package, for example, Newtonsoft.Json.10.0.3. Open the install.ps1 file in the package with notepad, the scripts should begin with the following line:
param($installPath, $toolsPath, $package, $project)
$installPath path to where the project is installed
$toolsPath path to the extracted tools directory
$package information about the currently installing package
$project reference to the EnvDTE project the package is being installed into
See Running PowerShell scripts during NuGet package installation and removal for more detail.
Then after above scripts, you can find the below scripts, which used to install the dll from a package into a VS project:
$newRef = $project.Object.References.Add("PathToMyDLL")
Note: Install.ps will only be invoked if there is something in the \lib or \content folder, not for a "tools only" package.

Automate nuget update-package operation on many VS solutions

I've got 10+ Visual Studio solutions that are referencing a nuget package.
When I decide to update that package in every solution it's a very long work to open each solution manually, update its package and check-in all changes to TFS. I'd like to "automate" this process with a batch operation that does it on behalf of me.
I've made some experiments with nuget.exe by command line but it doesn't care about TFS read-only files: it overwrites the content of my files without checking-out them so this way seems not the best way to do it.
Any other suggestion to achieve the aim?
You can use nuget update command to do this. VS/TFS can detect the changes even thought you are running the command outside from VS. And usually, only "packages.config" file will be updated during the nuget package update.
Update:
Following is a simple code to find the solution files under the given path and update the nuget packages for the solution and then check in the pending changes via TFS PowerShell CommandLets (You need to install "TFS Power Tool" to use this commandlet). You can update according to your scenario.
param([String]$dirpath)
add-pssnapin Microsoft.TeamFoundation.PowerShell;
Get-ChildItem -Path $dirpath -Filter *.sln -Recurse | ForEach-Object { & nuget update $_.FullName 2>1; cd $_.Directory; get-tfspendingchange; new-tfschangeset}
Save the code as a powershell script and run it from Windows PowerShell with argument "-dirpath yoursolutionpath". It will run as following:
Download Nuget Comanline
nuget.exe restore YourSolution.sln
nuget.exe update YourSolution.sln
Nuget Docs

Is there a nuget.exe command-line equivalent of Uninstall-Package?

I'm working on a NuGet package that installs a bunch of content - views, scripts, CSS files - into a web application, and trying to improve the change-compile-test cycle. I have two projects - the framework itself ("Package") and the demo web app that consumes it ("Website")
What I need to do as part of the Visual Studio build process is:
(as part of Package post-build) Nuget pack Package.nuspec -OutputDirectory ..\pkg\
(as part of Website pre-build) Nuget uninstall Package
(as part of Website pre-build) Nuget install package -source ..\pkg\
The problem is - there doesn't seem to be any command-line equivalent of doing Uninstall-Package from the NuGet Package Manager console. Am I missing something?
No there isn't currently.
Also, nuget.exe install doesn't really install anything. What nuget.exe install really does is nuget.exe restore: it restores the extracted package in the output directory. It doesn't run the PowerShell hooks (e.g. install.ps1) and it doesn't modify any target project (as there's none being targeted).
There is a way but using neither Visual Studio nor NuGet.exe. Using a custom build of SharpDevelop you can install and uninstall NuGet packages from the command line and have their PowerShell scripts run.
This custom build of SharpDevelop and its NuGet addin allows you to run the commands, such as Install-Package and Uninstall-Package, from PowerShell but outside of Visual Studio.
The limitations are that it needs SharpDevelop to be available and it also does not support any PowerShell scripts that are Visual Studio specific.

Debugging init.ps1 and install.ps1 during package installation within Visual Studio (using PowerShell VSX) or Powershell scripting IDE

My goal is to debug within Visual Studio 2012 Nuget powershell scripts and modules that are installed using Nuget powershell console. Ideally I would like to intercept running these .ps1 scripts under folder:
\packages\MvcMembership
\tools
init.ps1 << Run only once on Solution level
\net40
install.ps1 << Run for each Project during installation
uninstall.ps1
More info:
http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package
When installing a Nuget package: Install-Package MvcMembership for example I believe the first thing is to download a .zip Nuget package (e.x. MvcMembership.nupkg) and extract it into \packages\ folder and then execute init.ps1 conditionally (if MvcMembership is already installed it is not run) and then run install.ps1 for a currently selected project.
So how to intercept running init.ps1 and install.ps1, load it into PowerGui Visual Studio editor and break on first line (or Powershell scripting IDE)?
Would I need to add something to these .ps1 files like: Set-PSBreakpoint(which would require me to extract .nupkg edit .ps1 script files and re-zip them again
Thanks
Rad

'nuget' is not recognized but other nuget commands working

I am trying to create a nuget package using http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package#From_a_convention_based_working_directory
as a reference.
My Package Manger Console in Visual Studio is not allowing me to use the 'nuget' command.
I am able to 'Get-help nuguet' and it displays:
The following NuGet cmdlets are included.
Cmdlet Description
------------------ ----------------------------------------------
Get-Package Gets the set of packages available from the package source.
Install-Package Installs a package and its dependencies into the project.
Uninstall-Package Uninstalls a package. If other packages depend on this package,
the command will fail unless the –Force option is specified.
Update-Package Updates a package and its dependencies to a newer version.
Add-BindingRedirect Examines all assemblies within the output path for a project
and adds binding redirects to the application (or web)
configuration file where necessary.
Get-Project Returns a reference to the DTE (Development Tools Environment)
for the specified project. If none is specifed, returns the
default project selected in the Package Manager Console.
Open-PackagePage Open the browser pointing to ProjectUrl, LicenseUrl or
ReportAbuseUrl of the specified package.
Register-TabExpansion Registers a tab expansion for the parameters of a command.
However, whenever I start off commands with nuget is gives :
The term 'nuget' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try ag
ain.
At line:1 char:6
+ nuget <<<<
+ CategoryInfo : ObjectNotFound: (nuget:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I have tried the following solutions:
1>closing down all items and restarting
2> uninstalling and reinstalling
3>creating a powershell profile file (this didn't exist before and this actually broke everything)
The problem started to appear after I upgraded my Visual Studio 2012 Ultimate trial version to registered. I had originally had the VS 12 Pro installed. I don't know if that really has anything to do with it but I noticed that others that had similar problems have vs 10 and 12 installed.
My question is "does anyone know what else to try?" My theory is that the path to the nuget command is missing but I cannot find how to configure the paths the package manager console uses and I am not sure where the cmdlet nuget is actually stored.
Updated---tried downloading the command line tool as suggested below. This lead to nothing working again. I tried to uninstall and now I have a item in vs2010 extentions for nuget that doesn't have a install or unistall button enabled. This leads me to believe that it has to do with extensions installed via 2010 and 2012 that has a part in my little drama. If anyone also knows how to nuke an uninstallable extension, please advise also but I will try another question for that.
Nuget.exe is placed at .nuget folder of your project. It can't be executed directly in Package Manager Console, but is executed by Powershell commands because these commands build custom path for themselves.
My steps to solve are:
Download NuGet.exe from https://github.com/NuGet/NuGet.Client/releases (give preference for the latest release);
Place NuGet.exe in C:\Program Files\NuGet\Visual Studio 2012 (or your VS version);
Add C:\Program Files\NuGet\Visual Studio 2012 (or your VS version) in PATH environment variable (see http://www.itechtalk.com/thread3595.html as a How-to) (instructions here).
Close and open Visual Studio.
Update
NuGet can be easily installed in your project using the following command:
Install-Package NuGet.CommandLine
In [Package Manager Console] try the below
Install-Package NuGet.CommandLine
There are much nicer ways to do it.
Install Nuget.Build package in you project that you want to pack. May need to close and re-open solution after install.
Install nuget via chocolatey - much nicer. Install chocolatey: https://chocolatey.org/, then run
cinst Nuget.CommandLine
in your command prompt. This will install nuget and setup environment paths, so nuget is always available.
You can also try setting the system variable path to the location of your nuget exe and restarting VS.
Open your system PATH variable and add the location of your nuget.exe (for me this is: C:\Program Files (x86)\NuGet\Visual Studio 2013)
Restart Visual Studio
I would have posted this as a comment to your answer #done_merson but I didn't have the required reputation to do that.
In Visual Studio:
Tools -> Nuget Package Manager -> Package Manager Console.
In PM:
Install-Package NuGet.CommandLine
Close Visual Studio and open it again.
The nuget commandline tool does not come with the vsix file, it's a separate download
https://github.com/nuget/home
Right-click on your project in solution explorer.
Select Manage NuGet Packages for Solution.
Search NuGet.CommandLine by Microsoft and Install it.
On complete installation, you will find a folder named packages in
your project. Go to solution explorer and look for it.
Inside packages look for a folder named NuGet.CommandLine.3.5.0, here 3.5.0 is just version name your folder name will change accordingly.
Inside NuGet.CommandLine.3.5.0 look for a folder named tools.
Inside tools you will get your nuget.exe
Retrieve nuget.exe from https://www.nuget.org/downloads. Copy it to a local folder and add that folder to the PATH environment variable.
This is will make nuget available globally, from any project.
I got around this by finding the nuget.exe and moving to an easy to type path (c:\nuget\nuget) and then calling the nuget with this path. This seems to solve the problem.
c:\nuget\nuget at the package manager console works as expected.
I tried to find the path that the console was using and changing the environment path but was never able to get it to work in that way.
Follow these steps.
In visual studio go to Tools-> NuGet Package Manager->Package Manager Console
Run below command
Install-Package NuGet.CommandLine
Close visual studio and reOpen again
repeat step 1
run your nuget command
eg. nuget push C:\Users\syaads\Debug\Library.1.0.32.nupkg -Source Artifactory
You can find the nuget.exe in your profile folder:
C:\Users\YourProfileName\.nuget\packages\nuget.commandline\6.0.0\tools
If you want to use it gloablly, please register above path in PATH environment variable.
For detailed guide how to do it, please see Add to the PATH in Windows 10
Download nuget.exe from https://www.nuget.org/downloads.
create a new folder in root of C drive e.g c:\nuget, copy the nuget.ext to nuget folder in c drive and paste.
Go to environmental settings.
Go to System Variable Section => select the variable name as Path and double click on path variable => and click on new button in the last add c:\nuget => then apply => save => save.
Download the nuget.exe from the https://www.nuget.org/downloads.
Copy and paste the downloaded file to the relevant folder where your .nupkg is created.
Try to execute the command.

Resources