使用 PowerShell 递归遍历 Visual Studio 解决方案 [英] Recursively traversing a Visual Studio Solution using PowerShell

查看:98
本文介绍了使用 PowerShell 递归遍历 Visual Studio 解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式从包含近 150 个项目的解决方案中提取信息.虽然解决方案文件不是平面的,所以一些项目被组织到文件夹中,文件夹层次结构可以更深.这适合递归解决方案:我可以编写一个函数,它枚举一个列表,如果元素是一个项目,它会检查它,如果它是一个文件夹,它会进入文件夹并递归调用自身以检查文件夹的内容.要点:

I need to programatically extract information from a solution which contains almost 150 projects in it. The solution file is not flat though, so some of the projects are organized into folders, the folder hierarchy can be more levels deep. This fits a recursive solution: I could write a function, which enumerates a list, and if the element is a project it would examine it, if it is a folder it would go into the folder and recursively call itself to examine the folder's content. The gist of it:

$dte = [System.Runtime.InteropServices.Marshal]::GetActiveObject("visualstudio.dte.11.0")

function traverseproject {
    param([object]$prjnode, [int]$level)
    if ($prjnode.Kind -eq "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}")
    {
        Write $prjnode.Name
        Write $level
    }
    if ($prjnode.Kind -eq "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}")
    {
        foreach ($prjsubnode in $prjnode)
        {
            traverseproject($prjsubnode, $level + 1)
        }
    }
}

foreach($prjn in $dte.solution.projects)
{
    traverseproject($prjn, 0)
}

问题是递归函数得到的 $prjnode 对象很奇怪.Write $prjnode.Name 不输出任何内容.可能出于同样的原因,我无法遍历文件夹对象的节点.现在在上面的代码中,它是 foreach ($prjsubnode in $prjnode),它不会默默地做任何事情.我试过 foreach ($prjnode.ProjectItems 中的 $prjsubnode),它给出了错误.我尝试了任何组合.

The problem is that the $prjnode object what the recursive function gets is weird. Write $prjnode.Name doesn't output anything. Probably for the same reason I cannot iterate through the nodes of the folder object. Right now in the code above it's foreach ($prjsubnode in $prjnode), that just doesn't do anything silently. I tried foreach ($prjsubnode in $prjnode.ProjectItems), that gives error. I tried any kind of combinations.

从错误消息看来 $prjnodeDTE ProjectItem link, 8E2F1269-185E-43C7-8899-950AD2769CCF.我可以打印出 Count 属性,它似乎有效,但我在界面上看不到任何可以获取包含元素的属性.所以也许这就是我无法迭代的原因?不可能?我在链接的 MSDN 页面底部看到了 Visual Basic 示例,但我需要一个有效的 PowerShell 解决方案.

From the error messages it seems that the $prjnode is type of a DTE ProjectItem link, 8E2F1269-185E-43C7-8899-950AD2769CCF. I can print out the Count property and it seems valid, but I don't see any property on the interface where I could get a hold of the contained elements. So maybe that's why I cannot iterate through? There's no way? I see the Visual Basic example at the bottom of the MSDN page I linked, but I need a working PowerShell solution.

该函数的第一次调用似乎工作正常,例如它看到了 $prjnode.Kind 属性,但是在第一次递归调用之后,事情就丢失了.

The first call of the function seems to work OK, for example it sees the $prjnode.Kind property, but after the first recursive call things are lost.

推荐答案

既然您已经加载了 dte,请查看 http://studioshell.codeplex.com/

Since you're already loading the dte, Check out http://studioshell.codeplex.com/

最能帮助您的功能:

  • 以一致且可发现的方式管理您的项目、引用、断点、堆栈帧局部变量、菜单、工具栏、Visual Studio 设置、IDE 窗口,甚至是来自 PowerShell 脚本的代码.

这篇关于使用 PowerShell 递归遍历 Visual Studio 解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆