开发者

How to get the IDE to update solution explorer window after adding project items in nuget ps?

During nuget install I give the user a command they can run. This command basically scans some files and creates some code templates and then inserts them into the current project. This works just fine - except for the fact that Solution Explorer does not update its tree view with the new files. I know this works because I can unload and reload the project file and the files are there.

In case it helps, here is the code I use to add the files to the project - the second function is what the user actually calls.

function add-to-project ($itemType, $project)
{
  process
  {
    $bogus = $project.Xml.AddItem($itemType, $_)
  }
}

# Parse a file
function Write-TTree-MetaData ($Path = $(throw "-Path must be supplied"))
{
  $p = Get-Project
  Write-Host "Inserting the results of the parsing into projec开发者_JS百科t" $p.Name
  $ms = Get-MSBuildProject

  $destDir = ([System.IO.FileInfo] $p.FullName).Directory

  # Run the parse now

  CmdTFileParser -d $destDir.FullName $Path

  # Now, attempt to insert them all into the project

  $allFiles = Get-ChildItem -Path $destDir.FullName
  $allFiles | ? {$_.Extension -eq ".ntup"} | add-to-project "TTreeGroupSpec" $ms
  $allFiles | ? {$_.Extension -eq ".ntupom"} | add-to-project "ROOTFileDataModel" $ms

  # Make sure everything is saved!

  $ms.Save()
  $p.Save()
}

This code causes a funny dialog to pop up - "The project has been modified on disk - please reload" - and hopefully the user will reload, and then the files show up correctly... But it would be nice to avoid that and just have the script cause whatever is needed to happen. Perhaps I have to figure out how to unload and re-load the project?

What do I need to do to force solution explorer to update? Many thanks!


By using the MSBuild project you are bypassing Visual Studio and directly updating the MSBuild project file on disk. The easiest way to get Visual Studio to update the Solutions Explorer window is to use the Visual Studio project object instead which you get from the Get-Project command.

Below is a simple example which adds a file to the solution and changes its ItemType to be ROOTFileDataModel. The example assumes you have a packages.config file in your project's root directory which is not currently added to the project so it is not showing in Solution Explorer initially.

# Get project's root directory.
$p = Get-Project
$projectDir = [System.IO.Path]::GetDirectoryName($p.FileName)

# Add packages.config file to project. Should appear in Solution Explorer
$newFile = [System.IO.Path]::Combine($projectDir, "packages.config")
$projectItem = $p.ProjectItems.AddFromFile($newFile)

# Change file's ItemType to ROOTFileDataModel
$itemType = $projectItem.Properties.Item("ItemType")
$itemType.Value = "ROOTFileDataModel"

# Save the project.
$p.Save()

The main Visual Studio objects being used here are the Project, ProjectItem and the ProjectItems objects. Hopefully the above code can be adapted to your specific requirements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜