你如何走解决方案层次结构?

时间:2014-05-28 12:45:26

标签: powershell envdte

我现在已经工作了大约14个小时了。这让我疯狂。

在没有引用任何自定义dll的情况下,我想为项目或项目项目$dte.Solution.Projects查看是否

$SourceControl = get-interface $dte.SourceControl ([EnvDTE.SourceControl])
$SourceControl.IsItemUnderScc()

解决方案文件夹之类的东西会受到影响。我已经在C#中做了很好的反复(和in F#),我似乎无法在powershell中做到这一点。我通过Dte.SolutionUIHierarchy

完成了两次(C#)

我的C#行走代码以T4 Nuget Package为例,但我所有的linqpad示例目前都使用UIHierarchy

以下是来自c#的示例,它似乎在powershell中不起作用:

C#Projects projects = dte.Solution.Projects;

Powershell $Projects= [EnvDTE.Projects]$dte.Solution.Projects

Cannot convert the "System.__ComObject" value of type "System.__ComObject#{e3ec0add-31b3-461f-8303-8a5e6931257a}" to type "EnvDTE.Projects".

失败

1 个答案:

答案 0 :(得分:5)

使用本答案后面提供的代码,仅为解决方案中的第一个项目调用它GetUnversionedFiles(GetProjectFiles((GetSolutionProjects).get_Item(1)))

运行此代码GetUnversionedFiles(GetProjectFiles((GetSolutionProjects).get_Item(0)))


获取解决方案中的项目

function GetSolutionProjects(){
    $projects = get-interface $dte.Solution.Projects ([EnvDTE.Projects])
    write-debug "projects=$projects"
    $result = new-object "System.Collections.Generic.List[EnvDTE.Project]"
    foreach($project in $projects.GetEnumerator()) {
            if($project -eq $null){
                continue;
            }

            write-debug "yay project or solution folder! $project $project.Kind"
            if($project.Kind -eq [EnvDTE80.ProjectKinds]::vsProjectKindSolutionFolder){
                write-debug ("Solution folder "+$project.Name)

                foreach($solutionFolderProject in RecurseSolutionFolderProjects($project)){
                    $result+=$solutionFolderProject
                }

            } else {
                write-debug ("else " +$project.Name +  " " + $project.Kind
                $result+=$project
            }
    }
    return $result
}

递归解决方案文件夹

function RecurseSolutionFolderProjects(){
    param($solutionFolder = $(throw "Please specify a solutionFolder"))
    $projectList = @()
    for($i = 1; $i -le $solutionFolder.ProjectItems.Count; $i++){
        $subProject = $solutionFolder.ProjectItems.Item($i).subProject
        if($subProject -eq $null){
            continue;
        }

        if($subProject.Kind -eq [EnvDTE80.ProjectKinds]::vsProjectKindSolutionFolder)
        {
            $projectList += RecurseSolutionFolderProjects($subProject)
        } else {
            $projectList += $subProject
        }
    }
    return $projectList
}

走下projectItems

function RecurseDescendants(){
    param($source  = $(throw "Please specify a source"))
    write-debug "starting RecurseDescendants"
    $result = new-object "System.Collections.Generic.List[EnvDTE.ProjectItem]"
    foreach($s in $source){
        #write-host "working on " $s.Kind $s.Name $s.FileNames(0)

        $pi = $s.Object -as [EnvDTE.ProjectItem]
        $result+=$s
        $children=$s.ProjectItems
        foreach($child in RecurseDescendants($children)){
            $result+=$child
        }
        write-debug "inner for each stopped"
    }
    write-debug "outer for each finished"
    return $result
}

中遍历文件的项目
function GetProjectFiles(){
    param($project = $(throw "Please specify a project"))

    write-debug ("getting project files for " + $project.Name + " "+ $project.ProjectName)

    $projectItems = RecurseDescendants($project.ProjectItems)
        return $projectItems | Where-Object {$_.Kind -ne [EnvDTE.Constants]::vsProjectItemKindPhysicalFolder}
    }

检查找到的文件是否在版本控制中

    function GetUnversionedFiles(){
        param($items = $(throw "Please specify items"))
        write-host "checking for unversioned files"

        $SourceControl = get-interface $dte.SourceControl ([EnvDTE.SourceControl])
        return $items | Where-Object {$SourceControl.IsItemUnderSCC($_.FileNames(0)) -eq $FALSE }

    }

中的项目的漫游项目
function GetProjectItems(){ 
    param($project = $(throw "Please specify a project"))
    if($project.ProjectItems.count -gt 0){
        write-debug "getting project items for '$project.Name' '$project.ProjectName'"
    }
    #example: GetProjectItems((GetSolutionProjects).get_Item(1))
    $result =RecurseDescendants($project.ProjectItems)
    return $result
}

最新版本位于https://github.com/ImaginaryDevelopment/config/blob/master/My%20Documents/WindowsPowerShell/NuGet_profile.ps1