在 NuGet PowerShell 安装的 Visual Studio 项目中移动文件?
Move file(s) in a Visual Studio project in NuGet PowerShell install?
我有一个 nuget powershell (install.ps1),我在其中组织 "Content" 目录的内容。我想将 bootstrap 文件(bootstrap.css、bootstrap.min.css 等)移动到 "Contents" 文件夹内名为 "lib" 的新文件夹中。
我尝试使用 RoboCopy,它可以工作,但它是在文件系统上完成的,而不是在项目中完成的
robocopy "C:\WebApplication1\Content" "C:\WebApplication1\Content\lib" bootstrap*.* /MOV
此代码适用于移动文件,但解决方案随后显示文件丢失。如何为 Visual Studio 项目执行此操作?
您不能简单地四处移动文件并期望 Visual Studio 神奇地知道您做了什么。用记事本打开项目文件,您会看到项目中的所有文件都被明确引用。为了通过 PowerShell 做你想做的事,你基本上有两条路:fiddle 使用 XML 项目文件,使用 XML cmdlet 在 Visual Studio 之外或 - 更好 - 使用 Visual Studio 移动文件的对象模型。
要了解如何做到这一点,最简单的方法是以已经在执行类似操作的 Nuget 包为例。 SQL Server Compact Nuget package 是一个不错的选择。下载它,重命名并给它一个 .zip 扩展名,用你最喜欢的 zip 管理器打开它,然后在工具目录中查看一个名为 VS.psm1 的 PowerShell 模块。该模块允许您在项目中 add/remove 个文件。
更新的答案:所以我想我的问题应该更具体一些,并声明我正在寻求有关 Nuget PowerShell 安装文件中 Visual Studio 对象模型的帮助(install.ps1).上面选择的答案无疑为我指明了正确的方向并且非常有帮助。
这个答案还有一个额外的好处,那就是包含一个关于 VS "magically" 知道我正在尝试做什么的反手、居高临下的评论。下次我将提供更多关于我对 VS 项目文件的理解的细节,并清楚地表明我实际上是在寻找有关如何在 VS 对象模型中移动文件以避免我遇到的错误的信息......但是我离题了。下面是对我尝试做的事情以及我用来解决问题的代码的更好解释。
在我的 Nuget PS 脚本中,我正在安装 bootstrap,它将其 CSS 文件放置在我项目的 Content 文件夹中。我想在该文件夹中创建一个 lib 文件夹,然后将那些 bootstrap 文件(复制然后删除原件)移动到新的 lib 文件夹。这是执行此操作的代码:
安装。ps1来源
#Install Bootstrap
install-package bootstrap -source nuget.org
#Get the CONTENT folder object
contentFolder = $project.ProjectItems | Where-Object { $_.Properties.Item("Filename").Value -eq "Content" }
#Create the LIB folder in the CONTENT directory
$libFolder = (Get-Interface $contentFolder.ProjectItems "EnvDTE.ProjectItems").AddFolder("lib")
#Get the files to be moved
$filesToMove = $contentFolder.ProjectItems | Where-Object { $_.Properties.Item("Filename").Value -like "bootstrap*.*" }
#Copy each bootstrap item to the lib folder then delete the original
foreach($item in $filesToMove) {
Write-Host "Moving " $item.Properties.Item("FullPath").Value
(Get-Interface $libFolder.ProjectItems "EnvDTE.ProjectItems").AddFromFileCopy($item.Properties.Item("FullPath").Value)
(Get-Interface $item "EnvDTE.ProjectItem").Delete()
}
除了上面提到的 Nuget 源代码之外,我还看了很多不同的文章 (SQL Server Compact Nuget package source). But one blog post that was particularly helpful in both helping me understand the VS object model and a few other things I was doing in my install.ps1 was Setting DependentUpon File Properties on NuGet Package Install.
我有一个 nuget powershell (install.ps1),我在其中组织 "Content" 目录的内容。我想将 bootstrap 文件(bootstrap.css、bootstrap.min.css 等)移动到 "Contents" 文件夹内名为 "lib" 的新文件夹中。
我尝试使用 RoboCopy,它可以工作,但它是在文件系统上完成的,而不是在项目中完成的
robocopy "C:\WebApplication1\Content" "C:\WebApplication1\Content\lib" bootstrap*.* /MOV
此代码适用于移动文件,但解决方案随后显示文件丢失。如何为 Visual Studio 项目执行此操作?
您不能简单地四处移动文件并期望 Visual Studio 神奇地知道您做了什么。用记事本打开项目文件,您会看到项目中的所有文件都被明确引用。为了通过 PowerShell 做你想做的事,你基本上有两条路:fiddle 使用 XML 项目文件,使用 XML cmdlet 在 Visual Studio 之外或 - 更好 - 使用 Visual Studio 移动文件的对象模型。
要了解如何做到这一点,最简单的方法是以已经在执行类似操作的 Nuget 包为例。 SQL Server Compact Nuget package 是一个不错的选择。下载它,重命名并给它一个 .zip 扩展名,用你最喜欢的 zip 管理器打开它,然后在工具目录中查看一个名为 VS.psm1 的 PowerShell 模块。该模块允许您在项目中 add/remove 个文件。
更新的答案:所以我想我的问题应该更具体一些,并声明我正在寻求有关 Nuget PowerShell 安装文件中 Visual Studio 对象模型的帮助(install.ps1).上面选择的答案无疑为我指明了正确的方向并且非常有帮助。
这个答案还有一个额外的好处,那就是包含一个关于 VS "magically" 知道我正在尝试做什么的反手、居高临下的评论。下次我将提供更多关于我对 VS 项目文件的理解的细节,并清楚地表明我实际上是在寻找有关如何在 VS 对象模型中移动文件以避免我遇到的错误的信息......但是我离题了。下面是对我尝试做的事情以及我用来解决问题的代码的更好解释。
在我的 Nuget PS 脚本中,我正在安装 bootstrap,它将其 CSS 文件放置在我项目的 Content 文件夹中。我想在该文件夹中创建一个 lib 文件夹,然后将那些 bootstrap 文件(复制然后删除原件)移动到新的 lib 文件夹。这是执行此操作的代码:
安装。ps1来源
#Install Bootstrap
install-package bootstrap -source nuget.org
#Get the CONTENT folder object
contentFolder = $project.ProjectItems | Where-Object { $_.Properties.Item("Filename").Value -eq "Content" }
#Create the LIB folder in the CONTENT directory
$libFolder = (Get-Interface $contentFolder.ProjectItems "EnvDTE.ProjectItems").AddFolder("lib")
#Get the files to be moved
$filesToMove = $contentFolder.ProjectItems | Where-Object { $_.Properties.Item("Filename").Value -like "bootstrap*.*" }
#Copy each bootstrap item to the lib folder then delete the original
foreach($item in $filesToMove) {
Write-Host "Moving " $item.Properties.Item("FullPath").Value
(Get-Interface $libFolder.ProjectItems "EnvDTE.ProjectItems").AddFromFileCopy($item.Properties.Item("FullPath").Value)
(Get-Interface $item "EnvDTE.ProjectItem").Delete()
}
除了上面提到的 Nuget 源代码之外,我还看了很多不同的文章 (SQL Server Compact Nuget package source). But one blog post that was particularly helpful in both helping me understand the VS object model and a few other things I was doing in my install.ps1 was Setting DependentUpon File Properties on NuGet Package Install.