forked from couleur-tweak-tips/TweakList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildManifests.ps1
154 lines (119 loc) · 5.01 KB
/
buildManifests.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<#
.DESCRIPTION
Splits string-based description from hashtable-based key;values
.EXAMPLE
$desc, $value = Get-Metadata @"
Hey this is a description
key1: value1
key2: value2
"@
#>
function Get-Metadata ($string) {
$desc = $string -split "`n" | Where-Object { $_ -NotLike "*: *" -and $_.trim() } | Where-Object { $_ -notlike "//*" }
$values = $string -split "`n" | Where-Object { $_ -Like "*: *" } | Where-Object { $_ -notlike "//*" }
$value_table = [PSCustomObject]@{}
if ($values) {
foreach ($line in $values) {
[string]$key, [string]$value = ($line -split ':', 2).trim()
if (!$key -or !$value) {
Write-Error "Failed getting metadata from`n$($string | ConvertTo-Json -Depth 3)"
}
$value_table | Add-Member -MemberType NoteProperty -Name $key -Value $value
}
}
else {
$values = ""
}
if (!$desc) { $desc = "" }
return @($desc, $value_table)
}
function buildManifests2 {
param(
$Path = (Get-ChildItem ./modules/ -File -Recurse -Include *.ps1)
)
$ManifestList = [PSCustomObject]::new()
foreach($i in $Path){
$filepath = Get-Item $i -ErrorAction Stop
$funcName = $filepath.BaseName
. $filepath.FullName -ErrorAction Stop
$HelpInfo = Get-Help $funcName -ErrorAction Stop
$Manifest = [PSCustomObject]::new()
$relativePath = $filepath.FullName.Replace($PSScriptRoot,'') -replace '\\', '/'
$Manifest | Add-Member -MemberType NoteProperty -Name path -Value $relativePath
if ($desc = $HelpInfo.description.Text) {
# .DESCRIPTION
$Manifest | Add-Member -MemberType NoteProperty -Name description -Value $desc
}
else {
# function is not documented, skip
continue
}
if ($null, $values = Get-Metadata $HelpInfo.details.description.Text) {
foreach ($object in $values.psobject.properties) {
$key, $value = $object.name, $object.value
$Manifest | Add-Member -MemberType NoteProperty -Name $key -Value $value
}
}
if (!$Manifest.display) {
$Manifest | Add-Member -MemberType NoteProperty -Name display -Value ($FuncName -replace '-', ' ')
}
$Manifest | Add-Member -MemberType NoteProperty -Name parameters -Value ([PSCustomObject]::new())
foreach ($parameter in $HelpInfo.parameters.parameter) {
[PSCustomObject]$param = [PSCustomObject]::new()
# if ($parameter.name -in @('misctweaks')){wait-debugger}
if ($desc, $values = Get-Metadata $parameter.description.Text) {
if ($desc) {
$param | Add-Member -MemberType NoteProperty -Name description -Value $desc
}
if ($values.PSObject.Properties.Count) {
if ($values -is [String]){
$values = @($values)
}
$param | Add-Member -MemberType NoteProperty -Name values -Value $values
}
}
if (!$param.values) {
if ($ValidateSets = (Get-Command $funcName).Parameters.$($Parameter.Name).Attributes.ValidValues) {
if ($ValidateSets -is [String]){
$ValidateSets = @($ValidateSets)
}
$param | Add-Member -MemberType NoteProperty -Name values -Value $ValidateSets
}
}
if ($parameter.defaultValue -and $parameter.type.name -ne "Array") {
$param | Add-Member -MemberType NoteProperty -Name default -Value $parameter.defaultValue
}
$param | Add-Member -MemberType NoteProperty -Name type -Value $(switch ($parameter.type.name) {
Array {
if ($param.values) {
"enum[]"
}
else {
Wait-Debugger
}
}
String {
if ($param.values) {
"enum"
}
else {
"string"
}
}
SwitchParameter {
"boolean"
}
default {
Write-Warning "Unknown type $()"
}
})
if ($parameter.required -eq "true") {
$param | Add-Member -MemberType NoteProperty -Name required -Value $true
}
$Manifest.parameters | Add-Member -MemberType NoteProperty -Name $parameter.name -Value $param
} # foreach param
$ManifestList | Add-Member -MemberType NoteProperty -Name $funcName -Value $Manifest
} # foreach file
return $ManifestList
}
buildManifests2