Powershell 您如何处理解决方案层次结构?
我已经做了14个小时了。这简直快把我逼疯了 在不引用任何自定义DLL的情况下,我希望遍历项目或项目项的Powershell 您如何处理解决方案层次结构?,powershell,envdte,Powershell,Envdte,我已经做了14个小时了。这简直快把我逼疯了 在不引用任何自定义DLL的情况下,我希望遍历项目或项目项的$dte.Solution.Projects,以检查 $SourceControl = get-interface $dte.SourceControl ([EnvDTE.SourceControl]) $SourceControl.IsItemUnderScc() 像解决方案文件夹这样的东西会妨碍您。我在C#中反复(而且)做得很好,但在powershell中似乎做不到。我通过Dte.Solu
$dte.Solution.Projects
,以检查
$SourceControl = get-interface $dte.SourceControl ([EnvDTE.SourceControl])
$SourceControl.IsItemUnderScc()
像解决方案文件夹这样的东西会妨碍您。我在C#中反复(而且)做得很好,但在powershell中似乎做不到。我通过Dte.Solution
和
我的C#walking代码以a为例,但我的所有linqpad示例当前都使用UIHierarchy
下面是一个在powershell中似乎不起作用的c#示例:
C#Projects=dte.Solution.Projects代码>
Powershell$Projects=[EnvDTE.Projects]$dte.Solution.Projects
无法将“System.\u ComObject”类型的值“System.\u ComObject”{e3ec0add-31b3-461f-8303-8a5e6931257a}转换为“EnvDTE.Projects”。
使用此答案后面提供的代码,仅为解决方案中的第一个项目调用它将是GetUnversionedFiles(GetProjectFiles((GetSolutionProjects)。获取项目(1)))
要运行此代码,请执行GetUnversionedFile(GetProjectFiles((GetSolutionProjects.get_项(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
}
走下项目
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 }
}
内项目的walk项目
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
}
最新的版本是在使用本答案后面提供的代码,仅为解决方案中的第一个项目调用它将是
GetUnversionedFile(GetProjectFiles((GetSolutionProjects)。get_项(1))
要运行此代码,请执行GetUnversionedFile(GetProjectFiles((GetSolutionProjects.get_项(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
}
走下项目
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 }
}
内项目的walk项目
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
}
最新版本是在为什么要强制转换它?$Projects=$dte.Solution.Projects有效。计划是将它们放在
System.Collections.Generic.List[EnvDTE.Project]中
但显然,如果我在该对象上使用+=
而不是调用。Add
我不必执行任何强制转换。在这种情况下,+=
是否添加属性或添加到列表中
?为什么强制转换?$Projects=$dte.Solution.Projects有效。计划是将它们粘贴到System.Collections.genr中ic.List[EnvDTE.Project]
但显然,如果我在该对象上使用+=
而不是调用。Add
我不必进行任何强制转换。在这种情况下,+=
是否添加属性或添加到列表中
中?在哪里定义了get接口命令?在我的系统上“get command-Name get Int*”不返回任何命令。get接口命令定义在哪里?在我的系统上,“get命令-名称get Int*”不返回任何命令。