简体   繁体   English

PowerShell 脚本安装 nuget 包

[英]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.每当我开始一个项目时,我都想安装 NuGet 包 Elmah、Glimpse、MvcScaffolding、Squishit 和... - 这些是我所知道的。 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.然而,这是我第一次接触到 PowerShell,我不知道该怎么做。

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?我看过一个示例项目,脚本看起来很简单,但我很困惑为什么需要安装带有 .test 后缀的 package? Why also the -Project parameter?为什么还有 -Project 参数?

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.正如 Fabrice 已经建议的那样,使用所需的 NuGet 包创建项目模板可能是我会采用的方法。 To get the latest versions of the NuGet packages you could use the NuGet Package Updater package.要获取最新版本的 NuGet 软件包,您可以使用NuGet Package 更新程序ZEFE90A8E604A3A7C840E8ZF0。 That would get around the project template currently only supporting installing a specific version of a NuGet package.这将绕过目前仅支持安装特定版本的 NuGet package 的项目模板。

Alternatively you can store useful PowerShell scripts inside the NuGet profile .或者,您可以在NuGet 配置文件中存储有用的 PowerShell 脚本。 Create a file called NuGet_profile.ps1 in the %UserProfile%\Documents\WindowsPowerShell directory (eg C:\Users[YourUserName]\Documents\WindowsPowerShell\NuGet_profile.ps1).%UserProfile%\Documents\WindowsPowerShell目录中创建一个名为NuGet_profile.ps1的文件(例如 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.在 NuGet_profile.ps1 中,您可以添加 PowerShell 函数,您可以从 Visual Studio 中的 Package 管理器控制台 window 调用这些函数。 You will need to restart Visual Studio before the functions can be called.在调用这些函数之前,您需要重新启动 Visual Studio。 An example NuGet_profile.ps1 file is shown below.下面显示了一个示例 NuGet_profile.ps1 文件。

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.现在定义了这些函数,您可以在 Package 管理器控制台中运行以下命令,将 elmah 和 SqlServerCompact 安装到名为 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.或者,您可以通过在 Package 管理器控制台中运行以下命令,将 elmah 和 SqlServerCompact 安装到当前打开的解决方案中的所有项目中。

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. Install-Package 命令中的 -ProjectName 参数用于指定 package 将安装到的项目的名称。 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.您的解决方案可能是创建您在本地存储库中托管的元 nuget package。 You create a nuget package with dependencies on all the different packages that you want to have for your specific project setup.您创建一个 nuget package ,其中依赖于您希望为特定项目设置拥有的所有不同包。 All you have to do is to install the meta package, which in it's turn will install all dependencies.您所要做的就是安装 meta package,然后它会安装所有依赖项。

You might want to create your own mvc3 project template in which you can specify your preferred Nuget packages.您可能想要创建自己的 mvc3 项目模板,您可以在其中指定首选的 Nuget 包。 To do so you should have a look at Phil Haack blog post为此,您应该查看 Phil Haack 博客文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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