用于安装 nuget 包的 PowerShell 脚本

发布于 2024-11-13 18:00:27 字数 462 浏览 1 评论 0原文

每当我启动一个项目时,我都想安装 NuGet 包 Elmah、Glimpse、MvcScaffolding、Squishit 等等 - 嗯,这些是我所知道的。也许我也应该安装其他的?

不管怎样,不必记住所有这些,最好将所有这些都放在一个脚本中,我所要做的就是运行它。然而,这是我第一次接触PowerShell,我不知道该怎么做。

我看过一个示例项目,脚本看起来很简单,但我很困惑为什么需要安装带有 .test 后缀的包?为什么还有 -Project 参数?

例如,

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

我通过谷歌搜索来了解更多信息,但我认为我找错了地方。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱你不解释 2024-11-20 18:00:27

正如 Fabrice 已经建议的那样,使用所需的 NuGet 包创建项目模板可能是我会采取的方法。要获取最新版本的 NuGet 包,您可以使用 NuGet Package Updater 包。这将绕过当前仅支持安装特定版本的 NuGet 包的项目模板。

或者,您可以将有用的 PowerShell 脚本存储在 NuGet 配置文件中。在 %UserProfile%\Documents\WindowsPowerShell 目录(例如 C:\Users[YourUserName]\Documents\WindowsPowerShell\NuGet_profile.ps1)中创建一个名为 NuGet_profile.ps1 的文件。

在 NuGet_profile.ps1 中,您可以添加可从 Visual Studio 内的包管理器控制台窗口调用的 PowerShell 函数。您需要重新启动 Visual Studio,然后才能调用函数。下面显示了 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
    }
}

现在定义了这些函数后,您可以在程序包管理器控制台中运行以下命令,将 elmah 和 SqlServerCompact 安装到名为 MyProject 的项目中。

Install-StandardPackages MyProject

或者,您可以通过在包管理器控制台中运行以下命令,将 elmah 和 SqlServerCompact 安装到当前打开的解决方案中的所有项目中。

Install-StandardPackagesForAllProjects

Install-Package 命令中的 -ProjectName 参数用于指定包将安装到的项目的名称。测试后缀看起来像是包含单元测试的项目名称的一部分。

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 (e.g. 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.

橙幽之幻 2024-11-20 18:00:27

您的解决方案可能是创建一个托管在本地存储库中的元 nuget 包。您创建一个 nuget 包,该包依赖于您想要为特定项目设置拥有的所有不同包。您所要做的就是安装元包,该包又将安装所有依赖项。

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.

朕就是辣么酷 2024-11-20 18:00:27

您可能想要创建自己的 mvc3 项目模板,在其中可以指定您首选的 Nuget 包。
为此,您应该查看 Phil Haack 博客文章

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文