powershell - 如何使用 envdte 创建嵌套的解决方案文件夹

标签 powershell envdte

我试图通过 Powershell (envdte) 创建一个带有嵌套解决方案文件夹的 Visual Studio 解决方案。一切都工作到 1 级深,但嵌套解决方案文件夹似乎不起作用(接口(interface)为空)。这个问题在这个 SO 问题中描述:

Creating a tree of Solution Folders using DTE and Package Manager Console

不幸的是,这个问题还没有得到回答。我已经联系了那个问题的发帖人,但他在他的解决方案中采取了另一条路线,所以这个问题仍然悬而未决。

我的代码的摘录。 Find-Solution 方法在给定的上下文(= startfolder)中找到我正在寻找的解决方案文件夹。找到后,它会返回此项目:

function Find-SolutionFolder {
    Param($SearchedFolderName, $StartFolder)

    Write-Output "number " $StartFolder.ProjectItems.Count
    For ($i = 1; $i -le $StartFolder.ProjectItems.Count; $i++) {
        $Item = $StartFolder.ProjectItems.Item($i)

        if ($Null -Eq $Item.Object) {
            continue;
        }

        if ($Item.Object.Kind -eq [EnvDTE80.ProjectKinds]::vsProjectKindSolutionFolder) {
            if ($Item.Name -Eq $SearchedFolderName) {
                return $Item
            }
            Find-SolutionFolder $SearchedFolderName $Item
        }
    }
}

Add-Projects 方法负责将结构保存到解决方案中。结构是:
  • 解决方案
  • ModuleTypeFolder(即基础)
  • ModuleGroupFolder(可选)
  • 项目文件夹
  • 项目文件

  • 这是一个嵌套结构。在没有 ModuleGroupFolder 的情况下一切正常,但是当结构具有 ModuleGroupFolder 时,由于 Get-Interface 的结果为空,它会导致错误。我已经确认找到了正确的解决方案文件夹。只是变量 $moduleGroupNameFolderInterface 为空。
    参数modulePath是磁盘上的路径
    function Add-Projects {
        Param(
            [Parameter(Position = 0, Mandatory = $True)]
            [string]$ModulePath
        )
    
        Write-Output "Adding project(s)..."
    
        # For the sake of the example always use a folder named 'Foundation'
        $moduleTypeFolder = Get-FoundationSolutionFolder
    
        # When the literal 'Foundation' solution folder does not exist in the solution it will be created.
        if (-Not $moduleTypeFolder) {
            $dte.Solution.AddSolutionFolder($config.ModuleType)
            $moduleTypeFolder = Get-ModuleTypeSolutionFolder
        }
        $moduleTypeFolderInterface = Get-Interface $moduleTypeFolder.Object ([EnvDTE80.SolutionFolder])
    
        # Add ModuleGroup Folder if needed
        if (-Not [string]::IsNullOrWhiteSpace($config.ModuleGroupName)) {
            $moduleGroupNameFolder = Find-SolutionFolder $config.ModuleGroupName $moduleTypeFolder
            if (-Not $moduleGroupNameFolder) {
                $moduleTypeFolderInterface.AddSolutionFolder($config.ModuleGroupName)
                $moduleGroupNameFolder = Find-SolutionFolder $config.ModuleGroupName $moduleTypeFolder
            }
            $moduleGroupNameFolderInterface = Get-Interface $moduleGroupNameFolder.Object ([EnvDTE80.SolutionFolder])
            if ($Null -eq $moduleGroupNameFolderInterface) {
                Write-Output "moduleGroupNameFolderInterface is null; this is wrong"
            } else {
                $moduleNameFolder = $moduleGroupNameFolderInterface.AddSolutionFolder($config.ModuleName)
                $moduleNameFolderInterface = Get-Interface $moduleNameFolder.SubProject ([EnvDTE80.SolutionFolder])
                # Search in the new module folder for csproj files and add those to the solution.
                Get-ChildItem -File -Path $ModulePath -Filter "*$csprojExtension" -Recurse | ForEach-Object { $moduleNameFolderInterface.AddFromFile("$($_.FullName)")}
            }
        } else {
            $moduleNameFolder = $moduleTypeFolderInterface.AddSolutionFolder($config.ModuleName)
            $moduleNameFolderInterface = Get-Interface $moduleNameFolder.Object ([EnvDTE80.SolutionFolder])
            # Search in the new module folder for csproj files and add those to the solution.
            Get-ChildItem -File -Path $ModulePath -Filter "*$csprojExtension" -Recurse | ForEach-Object { $moduleNameFolderInterface.AddFromFile("$($_.FullName)")}
        }
    
        Write-Output "Saving solution..."
        $dte.Solution.SaveAs($dte.Solution.FullName)
    }
    

    笔记。该示例未优化(即重复代码)

    任何人都可以帮我解决这个问题。

    更新 - 对问题的回答
    我终于弄明白了。显然,当找到属性 Kind 具有 guid {66A26722-8FB5-11D2-AA7E-00C04F688DDE} 的嵌套解决方案文件夹时,它还不是正确的对象。您必须使用找到的项目中的对象。

    最佳答案

    所以基本上你正在寻找递归。你可以像这样递归。

    对于解决方案文件夹 :

    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
    }
    

    对于项目文件 :
    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 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
    }
    

    引用 Solution Hierarchy回答整齐地解释了上述功能

    您可以从此 GitHub Link 获取最新版本

    希望能帮助到你。

    关于powershell - 如何使用 envdte 创建嵌套的解决方案文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51698149/

    相关文章:

    c# - 如何将 Windows Phone 10 应用程序部署到设备?

    regex - 如何使用 powershell 将包含 2 个数字的字符串转换为货币?

    powershell - 使用powershell在文本文件中找到特定行后如何停止Get-Content -wait?按需退出管道

    c# - 如何使用 DTE 从 VS 2015/2017 的错误列表中获取错误代码?或者其他方式可以获得错误代码?

    visual-studio - 以编程方式导入/导出 VS 设置?

    git - 推送到github时,为什么总是要我的ssh密码?

    visual-studio - 使用宏在 Visual Studio 中删除键盘快捷键绑定(bind)

    .net - 不编码插件时获取当前的 EnvDTE 或 IServiceProvider

    build - 如何从上次构建中获取输出目录?

    powershell - 'Scheduled Job"和 "Scheduled Task"在 Powershell 上下文中是否相同?