在 nuget ps 中添加项目项后如何让 IDE 更新解决方案资源管理器窗口?

发布于 2024-12-09 01:10:21 字数 1173 浏览 0 评论 0原文

在 nuget 安装期间,我为用户提供了一个可以运行的命令。该命令基本上扫描一些文件并创建一些代码模板,然后将它们插入到当前项目中。这工作得很好 - 除了解决方案资源管理器不会使用新文件更新其树视图之外。我知道这是可行的,因为我可以卸载并重新加载项目文件,并且文件就在那里。

如果有帮助,这里是我用来将文件添加到项目的代码 - 第二个函数是用户实际调用的函数。

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 project" $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()
}

这段代码会弹出一个有趣的对话框 - “该项目已在磁盘上修改 - 请重新加载” - 希望用户能够重新加载,然后文件会正确显示......但最好避免这种情况,只是让脚本导致任何需要发生的事情。也许我必须弄清楚如何卸载和重新加载项目?

我需要做什么才能强制解决方案资源管理器更新?非常感谢!

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 project" $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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多彩岁月 2024-12-16 01:10:21

通过使用 MSBuild 项目,您将绕过 Visual Studio 并直接更新磁盘上的 MSBuild 项目文件。让 Visual Studio 更新解决方案资源管理器窗口的最简单方法是使用 Visual Studio 项目对象,而不是从 Get-Project 命令获取的项目对象。

下面是一个简单的示例,它将文件添加到解决方案并将其 ItemType 更改为 ROOTFileDataModel。该示例假设您的项目根目录中有一个 packages.config 文件,该文件当前尚未添加到项目中,因此它最初不会显示在解决方案资源管理器中。

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

这里使用的主要 Visual Studio 对象是 Project、< a href="http://msdn.microsoft.com/en-us/library/envdte.projectitem.aspx" rel="nofollow">ProjectItem 和 ProjectItems 对象。希望上面的代码可以适应您的具体要求。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文