简体   繁体   English

在NuGet PowerShell安装中的Visual Studio项目中移动文件?

[英]Move file(s) in a Visual Studio project in NuGet PowerShell install?

I have a nuget powershell (install.ps1) where I organize the contents of my "Content" directory. 我有一个nuget powershell(install.ps1),在这里组织“目录”目录的内容。 I want to move the bootstrap files (bootstrap.css, bootstrap.min.css, etc) into a new folder called "lib" which is inside of the "Contents" folder. 我想将引导文件(bootstrap.css,bootstrap.min.css等)移动到名为“ lib”的新文件夹中,该文件夹位于“目录”文件夹中。

I tried using RoboCopy, which works, but it is done on the file system and not in the project 我尝试使用有效的RoboCopy,但它是在文件系统上完成的,而不是在项目中完成的

robocopy "C:\WebApplication1\Content" "C:\WebApplication1\Content\lib" bootstrap*.* /MOV

This code works in moving the files, but the solution then shows that the files are missing. 该代码可用于移动文件,但是该解决方案随后显示文件丢失。 How do I do this for the Visual Studio project? 如何为Visual Studio项目执行此操作?

You can't simply move files around and expect Visual Studio to magically know what you have done. 您不能简单地移动文件并期望Visual Studio神奇地知道您所做的事情。 Open a project file with Notepad and you'll see that all files in a project are explicitly referred to. 使用记事本打开项目文件,您将看到显式引用了项目中的所有文件。 In order to do what you want through PowerShell, you have basically two roads: fiddle with the XML project file using XML cmdlets OUTSIDE Visual Studio or -better- use the Visual Studio object model to move your files around. 为了通过PowerShell进行所需的工作,基本上有两条路:在XML Studio外部使用XML cmdlet来修饰XML项目文件,或者-更好地使用Visual Studio对象模型来移动文件。

To have an idea of how you do that, the easiest way is to take a Nuget package already doing similar things as an example. 要了解如何执行此操作,最简单的方法是以已经完成类似操作的Nuget包为例。 One good fit is the SQL Server Compact Nuget package . SQL Server Compact Nuget软件包是一个很好的选择。 Download it, rename it and give it a .zip extension, open it with your favourite zip manager and have a look at a PowerShell module called VS.psm1 available in the tools directory. 下载它,重命名并给它一个.zip扩展名,使用您最喜欢的zip管理器将其打开,然后在tools目录中查看一个名为VS.psm1的PowerShell模块。 That module allows you to add/remove files in your project. 该模块允许您在项目中添加/删除文件。

UPDATED ANSWER: So I guess I should have been a bit more specific in my question and stated that I was looking for help with the Visual Studio object model in my Nuget PowerShell install file (install.ps1). 更新后的答案:所以我想我应该在问题上说得更具体些,并说我正在Nuget PowerShell安装文件(install.ps1)中寻求有关Visual Studio对象模型的帮助。 The selected answer above definitely pointed me in the right direction and was very helpful. 上面选择的答案肯定为我指明了正确的方向,并且非常有帮助。

That answer also had the added benefit of including a backhanded, condescending comment about VS "magically" knowing what I'm trying to do. 该答案还具有一个额外的好处,包括“神奇地”知道我要做什么的关于VS的反手,屈尊的评论。 Next time I'll provide more detail regarding my understanding of the VS project file, and clarity that I was actually looking for info on how to move files within the VS object model to avoid the errors I was getting......but I digress. 下次,我将提供更多有关我对VS项目文件的理解的详细信息,并且清楚地表明我实际上是在寻找有关如何在VS对象模型中移动文件的信息,以避免出现错误……但是我离题了。 Below is a better explanation of what I was trying to do and the code I used to solve my issue. 以下是对我正在尝试做的事情以及用于解决问题的代码的更好解释。

In my Nuget PS script I was installing bootstrap, which places its CSS files in the Content folder of my project. 在我的Nuget PS脚本中,我正在安装引导程序,该程序将其CSS文件放置在项目的Content文件夹中。 I wanted to create a lib folder inside of that folder and then move those bootstrap files (copy and then delete the originals) to the new lib folder. 我想在该文件夹中创建一个lib文件夹,然后将那些引导文件(复制然后删除原始文件)移动到新的lib文件夹中。 Here is the code to do that: 这是执行此操作的代码:

install.ps1 source install.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()
}

There were a number of different articles I looked at in addition to the Nuget source code mentioned above ( SQL Server Compact Nuget package source ). 除了上面提到的Nuget源代码( SQL Server Compact Nuget包源代码 )之外,我还查看了许多其他文章。 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 . 但是一篇博客文章对帮助我理解VS对象模型以及我在install.ps1中正在做的其他事情特别有用,它是在NuGet软件包安装上设置DependentUpon文件属性

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

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