简体   繁体   中英

PowerShell scripts to install nuget packages

Whenever I start a project I want to install the NuGet packages Elmah, Glimpse, MvcScaffolding, Squishit and... - well those are the ones I am aware of. Maybe I should be installing others as well?

Anyway, instead of having to remember all these it would be good to put this all in a script and all I have to do is run that. However, this is the first time I have come into contact with PowerShell and I am not sure how to do this.

I have looked at an example project and the script looks simple enough, but I am confused as to why there is a need to install the package with a.test suffix? Why also the -Project parameter?

For example,

Install-Package SqlServerCompact -Project MileageStats.Data.SqlCe
Install-Package SqlServerCompact -Project MileageStats.Data.SqlCe.tests

I have done a Google search to find out more, but I think I am looking in the wrong places.

Creating a project template with the required NuGet packages, as Fabrice has already suggested, is probably the approach I would take. To get the latest versions of the NuGet packages you could use the NuGet Package Updater package. That would get around the project template currently only supporting installing a specific version of a NuGet package.

Alternatively you can store useful PowerShell scripts inside the NuGet profile . Create a file called NuGet_profile.ps1 in the %UserProfile%\Documents\WindowsPowerShell directory (eg C:\Users[YourUserName]\Documents\WindowsPowerShell\NuGet_profile.ps1).

Inside the NuGet_profile.ps1 you can add PowerShell functions that you can call from the Package Manager Console window inside Visual Studio. You will need to restart Visual Studio before the functions can be called. An example NuGet_profile.ps1 file is shown below.

function Install-StandardPackages {
    param(
        [Parameter(position=0, mandatory=$true)]
        [string]$projectName
    )

    Install-Package elmah -ProjectName $projectName
    Install-Package SqlServerCompact -ProjectName $projectName
}

function Install-StandardPackagesForAllProjects {
    foreach ($project in Get-Project -all) {
        Install-StandardPackages $project.Name
    }
}

Now with these functions defined you could run the following in the Package Manager Console to install elmah and SqlServerCompact into a project called MyProject.

Install-StandardPackages MyProject

Or you could install elmah and SqlServerCompact into all projects in the currently open solution by running the following in the Package Manager Console.

Install-StandardPackagesForAllProjects

The -ProjectName parameter in the Install-Package command is used to specify the name of the project that the package will be installed into. The tests suffix looks like it is part of the name of the project containing unit tests.

A solution for you might be to create a meta nuget package that you host in a local repository. You create a nuget package with dependencies on all the different packages that you want to have for your specific project setup. All you have to do is to install the meta package, which in it's turn will install all dependencies.

You might want to create your own mvc3 project template in which you can specify your preferred Nuget packages. To do so you should have a look at Phil Haack blog post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM