-
Notifications
You must be signed in to change notification settings - Fork 7
/
Get-ProjectItems.ps1
51 lines (41 loc) · 2.03 KB
/
Get-ProjectItems.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<#
.SYNOPSIS
Gets VS project <Item> elements from a project
#>
function Get-ProjectItems {
[CmdletBinding()]
Param(
[string] $ProjFilePath,
[Parameter(Mandatory = $true)]
[ValidateSet('Compile', 'Content', 'None', 'Analyzer', 'Reference', 'EmbeddedResource')]
[string] $ItemType
)
if (-Not (Test-Path $ProjFilePath)) { throw "Project file does not exist at '$ProjFilePath'" }
Write-Verbose "Checking project file '$ProjFilePath' for '$ItemType'"
$projFileContent = [xml](Get-Content $ProjFilePath)
$projFolder = Split-Path -Path $ProjFilePath -Parent
$projItems = @()
foreach ($projItemGroup in $projFileContent.Project.ItemGroup) {
if ($projItemGroup.GetType() -eq [System.String]) {
Write-Verbose "Empty project item group"
continue
}
$items = $projItemGroup.GetElementsByTagName($ItemType)
foreach ($item in $items) {
if ($item | Get-Member -Name "Include") {
$absPath = Join-Path -Path $projFolder -ChildPath $item.Include
if (-not (Test-Path -Path $absPath)) {
Write-Warning "ProjectFile '$ProjFilePath' reports that file '$absPath' is part of the project but it cannot be found on disk "
continue
}
$isLinked = (-Not ([string]::IsNullOrWhiteSpace($item.Link)))
$subtype = if ($null -ne $item.SubType) { $item.SubType } else { "None" }
$copyToOutputDirectory = if ($null -ne $item.CopyToOutputDirectory) { $item.CopyToOutputDirectory } else { "Do not copy" }
$itemProps = @{'FullName' = $absPath; 'Path' = $item.Include; 'IsLinked' = $isLinked; 'Subtype' = $subtype; 'CopyToOutputDirectory' = $copyToOutputDirectory }
$projItems += (New-Object -TypeName PSObject -Property $itemProps)
}
}
}
Write-Verbose "Project has $($projItems.Count) items"
return $projItems
}