如何使用envdte

时间:2018-08-05 20:47:20

标签: powershell envdte

我试图通过Powershell(envdte)使用嵌套的解决方案文件夹创建一个Visual Studio解决方案。一切工作到1级深度,但是嵌套解决方案文件夹似乎不起作用(接口为null)。此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已引导为{66A26722-8FB5-11D2-AA7E-00C04F688DDE},它不是正确的对象。您必须在找到的项目中使用该对象。

1 个答案:

答案 0 :(得分:0)

因此,基本上,您正在寻找递归。您可以像这样递归。

对于解决方案文件夹

var counter = 0;

function dateMatch(data, value) {
        var d = new Date(data.properties.Date);
        var m = month[d.getMonth()];
        if (inputValue == m) {
            this.parentElement.appendChild(this);
            counter++;
            return "red";
        } else {
            return "#808080";
        };
    }

对于项目文件

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}
    }

请参考Solution Hierarchy答案,其中对上述功能进行了详细说明

您可以从此GitHub Link

获取最新版本

希望有帮助。