-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Initialize-Splatter.ps1
216 lines (198 loc) · 8.23 KB
/
Initialize-Splatter.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
function Initialize-Splatter
{
<#
.Synopsis
Initializes an embeddable version of Splatter
.Description
Initialize-Splatter enables you to embed Splatter into any module.
.Link
Get-Splat
.Link
Find-Splat
.Link
Use-Splat
.Link
Merge-Splat
.Example
Initialize-Splatter > '@.ps1' # Initialize Splatter
.Example
Initialize-Splatter -Compress > '@.ps1'
.Example
Initialize-Splatter -Verb Get > '@.ps1' # Initialize splatter
#>
param(
# The verbs to install.
[Parameter(ValueFromPipelineByPropertyName=$true)]
[ValidateSet('Get','Use','Find','Merge','Out')]
[string[]]
$Verb = @('Get','Find','Merge','Use'),
# If set, will not compress the definitions
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Alias('NoCompression')]
[switch]
$Compress,
# If set, will not minify the definitions
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Alias('NoMinification')]
[switch]
$Minify,
# If set, will not add a line of documentation linking to the module
[Parameter(ValueFromPipelineByPropertyName=$true)]
[switch]
$NoLogo,
# If set, will strip inline help from the commands.
[Parameter(ValueFromPipelineByPropertyName=$true)]
[switch]
$NoHelp,
# If set, will define the commands as functions and define aliases.
# If you use this, please use the manifest or Export-ModuleMember to hide Splatter's commands.
# If not set, Splatter will install as ScriptBlocks (these will not be exported from a module)
[switch]
$AsFunction,
# If set, splatter will be defined inline.
# This will not preface Splatter with a param() block and PSScriptAnalyzer suppression messages
[switch]
$Inline,
# The output path.
# If provided, will output to this file and return the file.
[string]
$OutputPath
)
begin {
$CompressScriptBlock = ([ScriptBlock]::Create((
[IO.StreamReader]::new(([IO.Compression.GZipStream]::new(
[IO.MemoryStream]::new([Convert]::FromBase64String(@'
H4sIAAAAAAAEAM1ZW08jNxT2T4mqaENUGLV97AiJZQvVSr2gsmofEKoyYRbSJpBmhnZpNv+93znH
Httje5IszKqKJmR8rj53m6WaqBWehTpQV+qieStVjWeF1R/xfq9u8F2rB6w8qWM1xO+VegTGWF2D
7lJN8T5TS6yfqjnwpupPQIYJyFgVoL3F+r1aq/fMt4SEqbqDxKH6XQ0YNlAjUNGvCUs+Ux/AiXAr
fGaguwfGIZ40JK5BGl9sUOI9jfMaq2SnLo0uYZ+iA/4Gvxf4iHVja2nat7DZJfYzYS8tWNtaw34A
3gPoUvDXmtst1gwkhXvBdithNfJB2ewsjv2OfZSC7rK3MaJhyLASXnoErUDe8Dfx/IDvDDJcOisx
g8aX6lf24AzfBTQvNX6tI+sQUfWFjuoJrGD1zGCbR47yBfMT7hksSri3gAl+Bj5rcNqwLWtwG6gj
wCr8vYIVnthm19jNRuXAlZ3b3UqE1cijNVa35R/tZs65do534tT2ipuPh4A+sGWM/sfqK87SIWjG
rAVlgGiyBs57tsuSrZSKWPLLSQuvjOBlEe9X6qN6BcuEdghxx7DjH8CacTTkeDa8zwrfrqZtC/j6
taHhyhnHxXYNQzmufh+T+qWq3hqxQhF+kLRIjE6ibLCF9wa8Y9oc4XeFX/vZrA8LrVnWhj+xvIhF
3ufNkatn1oXYDsR7lH9lJP/IA8ZXZeOrbT1v7GRuyZqUeKsb+hRN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F1WtwVL3WqTJ992+dLqd1kY7eL
1E7c2b5Wc2drw0WHQvc9CzX/rbUaxOeGnE/OPvWFd5rchUPZ4nCmb4S2UbpTs5Vu7OPfOJCeRUvP
EDNWYyfeHUjB3bfW/iy4D5taR7hSCVwvmZoQcl4GnMP7gq5byUnrdmabXoJl6lfYqcO7STtz2apS
NN3E/a++rXzFi9UHK3PZyGzfVVi5yx7ktrtYwRWK/ODqVHK8rjXtSUP5PG26ZZoKGjsBt230abds
cXimMTbqP/VR4etMIgAA
'@ )),
[IO.Compression.CompressionMode]'Decompress')),([Text.Encoding]::unicode))).ReadToEnd()
))
$myModule = $MyInvocation.MyCommand.ScriptBlock.Module
}
process {
$myParams = @{} + $PSBoundParameters
$c, $t, $id = 0, $Verb.Count, [Random]::new().Next()
$SplatterScript = @(
if (-not $NoLogo) {
$logo = @(
$myModule.Name
'['
$myModule.Version
']'
':'
$myModule.Description
"(Install-Module $($myModule.Name), then $($MyInvocation.MyCommand.Name)"
$(if ($myParams.Verb) {"-Verb $($verb -join ',')"})
@(foreach ($kv in $myParams.GetEnumerator()) {
if ($kv.Value -is [switch] -and $kv.Value) {
"-$($kv.Key)"
}
}) | Sort-Object
')'
) -join ' '
"#region $logo"
}
if ($verb -notcontains 'Get') {
$verb += 'Get'
}
$innerContent = foreach ($v in $Verb) {
$var = $ExecutionContext.SessionState.PSVariable.Get("${v}Splat")
if ($var.Value -isnot [ScriptBlock]) { continue }
$c++
$p = $c * 100 / $t
Write-Progress "Preparing" $v -PercentComplete $p -id $id
@(
$myModule.ExportedVariables.Values |
& { process {
if ($_.Value -eq $var.Value){ "`${$($_.Name)}"}
} }
$val = $var.Value
if ($AsFunction) {
"`${function:$v-Splat}"
}
if ($Minify) {
Write-Progress "Minifying" $v -PercentComplete $p -id $id
if ($val -isnot [ScriptBlock]) { $val = [ScriptBlock]::Create($val) }
"{$(& $CompressScriptBlock $val)}"
} elseif ($NoHelp) {
"{$($val -replace '\<\#(?<Block>(.|\s)+?(?=\#>))\#\>', '')}"
} else {
"{$val}"
}
) -join '='
if ($AsFunction) {
foreach ($a in $myModule.ExportedAliases.Values) {
if ($a.ResolvedCommand.Name -eq "$v-Splat") {
"Set-Alias '$a' '$v-Splat'"
}
}
}
}
if ($Compress) {
Write-Progress "Compressing" " " -PercentComplete 99 -id $id
$data = [Text.Encoding]::Unicode.GetBytes("$($innerContent -join [Environment]::NewLine)")
$ms = New-Object IO.MemoryStream
$cs = New-Object System.IO.Compression.GZipStream ($ms, [Io.Compression.CompressionMode]"Compress")
$cs.Write($Data, 0, $Data.Length)
$cs.Close()
$cs.Dispose()
".([ScriptBlock]::Create(([IO.StreamReader]::new((
[IO.Compression.GZipStream]::new([IO.MemoryStream]::new(
[Convert]::FromBase64String('
$([Convert]::ToBase64String($ms.ToArray(), 'InsertLineBreaks'))
')),
[IO.Compression.CompressionMode]'Decompress')),
[Text.Encoding]::unicode)).ReadToEnd()
))"
} else {
@(@(if (-not $inline) {'
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "", Justification="This Declares Variables for Other Scripts")]
param()'
}) + $innerContent) -join [Environment]::NewLine
}
if (-not $NoLogo) {
"#endregion $logo"
}) -join [Environment]::NewLine
if ($outputPath) {
if (-not (Test-Path $outputPath)) {
$null = New-Item -ItemType File -Path $outputPath -Force
}
"$SplatterScript" | Set-Content -Path $outputPath
Get-Item -Path $outputPath
} else {
try {
[ScriptBlock]::Create($SplatterScript)
} catch {
Write-Debug "$($_ | Out-String)"
$SplatterScript
}
}
Write-Progress "Initialized!" " " -Completed -id $id
}
}