简体   繁体   中英

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. 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.

I tried using RoboCopy, which works, but it is done on the file system and not in the project

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?

You can't simply move files around and expect Visual Studio to magically know what you have done. 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.

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. One good fit is the SQL Server Compact Nuget package . 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. 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). 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. 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. 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. 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. Here is the code to do that:

install.ps1 source

#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 ). 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 .

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