How can I reload a visual studio project thru a nuget powershell script

断了今生、忘了曾经 提交于 2019-12-07 22:50:32

问题


In Solution Explorer 'DependentUpon' project items are normally disabled as children of the other item (ex. web.config / web.Debug.config). The problem I have is when items are dynamically added via nuget/powershell at package install, Solution Explorer doesn't reload the project so the items don't show as dependent. Manually closing and reopening the solution or unload/reload the project fix the issue. I'd like to automate the Project Reload as part of the install.ps1 powershell script but when I do this I get 'Project Unloaded' errors and nuget rolls back the install. I think this is because the only way I know how to get the Reload context menu is to unload the project first.

I'm looking for the object that gets invoked behind this call. I think if I could execute directly, I wouldn't have to Unload the project first.

$dte.ExecuteCommand("Project.ReloadProject")

And here is the full code to unload/reload the project in Solution Explorer

# Reload a project thru dte/SolutionExplorer Window 
# using Unload and Reload Context Menus.

$project = Get-Project
$shortpath = $dte.Solution.Properties.Item("Name").Value + "\" + $project.Name

#following GUID = Constants.vsWindowKindSolutionExplorer
#magic 1 = vsUISelectionType.vsUISelectionTypeSelect
$dte.Windows.Item("{3AE79031-E1BC-11D0-8F78-00A0C9110057}").Activate()
$dte.ActiveWindow.Object.GetItem($shortpath).Select(1)
$dte.ExecuteCommand("Project.UnloadProject")
$dte.ExecuteCommand("Project.ReloadProject")

回答1:


I've got around this issue by 'touching' the project file and therefore triggering Visual Studio to prompt the user to reload the modified project after the package manager has closed. So my Install.ps1 file looks like this:

param($installPath, $toolsPath, $package, $project)

# if there isn't a project file, there is nothing to do
if (!$project) { return }

# Do some stuff here

$project.Save()

# Touch the file to force a project reload
$(get-item $project.FullName).lastwritetime=get-date



回答2:


Similar question answered here: Is there a setting in VS 2010 that will allow it to recover open files after a project file changes? However, it doesn't indicate that reload can be called without first calling unload and it doesn't provide a mechanism for calling directly w/o going thru dte.executecommand.



来源:https://stackoverflow.com/questions/12292577/how-can-i-reload-a-visual-studio-project-thru-a-nuget-powershell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!