-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSModuleTest.psm1
More file actions
434 lines (375 loc) · 13.1 KB
/
PSModuleTest.psm1
File metadata and controls
434 lines (375 loc) · 13.1 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Contains long links.')]
[CmdletBinding()]
param()
$scriptName = $MyInvocation.MyCommand.Name
Write-Debug "[$scriptName] Importing module"
#region - Data import
Write-Debug "[$scriptName] - [data] - Processing folder"
$dataFolder = (Join-Path $PSScriptRoot 'data')
Write-Debug "[$scriptName] - [data] - [$dataFolder]"
Get-ChildItem -Path "$dataFolder" -Recurse -Force -Include '*.psd1' -ErrorAction SilentlyContinue | ForEach-Object {
Write-Debug "[$scriptName] - [data] - [$($_.Name)] - Importing"
New-Variable -Name $_.BaseName -Value (Import-PowerShellDataFile -Path $_.FullName) -Force
Write-Debug "[$scriptName] - [data] - [$($_.Name)] - Done"
}
Write-Debug "[$scriptName] - [data] - Done"
#endregion - Data import
#region - From /init
Write-Debug "[$scriptName] - [/init] - Processing folder"
#region - From /init/initializer.ps1
Write-Debug "[$scriptName] - [/init/initializer.ps1] - Importing"
Write-Verbose '-------------------------------'
Write-Verbose '--- THIS IS AN INITIALIZER ---'
Write-Verbose '-------------------------------'
Write-Debug "[$scriptName] - [/init/initializer.ps1] - Done"
#endregion - From /init/initializer.ps1
Write-Debug "[$scriptName] - [/init] - Done"
#endregion - From /init
#region - From /classes
Write-Debug "[$scriptName] - [/classes] - Processing folder"
#region - From /classes/Book.ps1
Write-Debug "[$scriptName] - [/classes/Book.ps1] - Importing"
class Book {
# Class properties
[string] $Title
[string] $Author
[string] $Synopsis
[string] $Publisher
[datetime] $PublishDate
[int] $PageCount
[string[]] $Tags
# Default constructor
Book() { $this.Init(@{}) }
# Convenience constructor from hashtable
Book([hashtable]$Properties) { $this.Init($Properties) }
# Common constructor for title and author
Book([string]$Title, [string]$Author) {
$this.Init(@{Title = $Title; Author = $Author })
}
# Shared initializer method
[void] Init([hashtable]$Properties) {
foreach ($Property in $Properties.Keys) {
$this.$Property = $Properties.$Property
}
}
# Method to calculate reading time as 2 minutes per page
[timespan] GetReadingTime() {
if ($this.PageCount -le 0) {
throw 'Unable to determine reading time from page count.'
}
$Minutes = $this.PageCount * 2
return [timespan]::new(0, $Minutes, 0)
}
# Method to calculate how long ago a book was published
[timespan] GetPublishedAge() {
if (
$null -eq $this.PublishDate -or
$this.PublishDate -eq [datetime]::MinValue
) { throw 'PublishDate not defined' }
return (Get-Date) - $this.PublishDate
}
# Method to return a string representation of the book
[string] ToString() {
return "$($this.Title) by $($this.Author) ($($this.PublishDate.Year))"
}
}
Write-Debug "[$scriptName] - [/classes/Book.ps1] - Done"
#endregion - From /classes/Book.ps1
#region - From /classes/BookList.ps1
Write-Debug "[$scriptName] - [/classes/BookList.ps1] - Importing"
class BookList {
# Static property to hold the list of books
static [System.Collections.Generic.List[Book]] $Books
# Static method to initialize the list of books. Called in the other
# static methods to avoid needing to explicit initialize the value.
static [void] Initialize() { [BookList]::Initialize($false) }
static [bool] Initialize([bool]$force) {
if ([BookList]::Books.Count -gt 0 -and -not $force) {
return $false
}
[BookList]::Books = [System.Collections.Generic.List[Book]]::new()
return $true
}
# Ensure a book is valid for the list.
static [void] Validate([book]$Book) {
$Prefix = @(
'Book validation failed: Book must be defined with the Title,'
'Author, and PublishDate properties, but'
) -join ' '
if ($null -eq $Book) { throw "$Prefix was null" }
if ([string]::IsNullOrEmpty($Book.Title)) {
throw "$Prefix Title wasn't defined"
}
if ([string]::IsNullOrEmpty($Book.Author)) {
throw "$Prefix Author wasn't defined"
}
if ([datetime]::MinValue -eq $Book.PublishDate) {
throw "$Prefix PublishDate wasn't defined"
}
}
# Static methods to manage the list of books.
# Add a book if it's not already in the list.
static [void] Add([Book]$Book) {
[BookList]::Initialize()
[BookList]::Validate($Book)
if ([BookList]::Books.Contains($Book)) {
throw "Book '$Book' already in list"
}
$FindPredicate = {
param([Book]$b)
$b.Title -eq $Book.Title -and
$b.Author -eq $Book.Author -and
$b.PublishDate -eq $Book.PublishDate
}.GetNewClosure()
if ([BookList]::Books.Find($FindPredicate)) {
throw "Book '$Book' already in list"
}
[BookList]::Books.Add($Book)
}
# Clear the list of books.
static [void] Clear() {
[BookList]::Initialize()
[BookList]::Books.Clear()
}
# Find a specific book using a filtering scriptblock.
static [Book] Find([scriptblock]$Predicate) {
[BookList]::Initialize()
return [BookList]::Books.Find($Predicate)
}
# Find every book matching the filtering scriptblock.
static [Book[]] FindAll([scriptblock]$Predicate) {
[BookList]::Initialize()
return [BookList]::Books.FindAll($Predicate)
}
# Remove a specific book.
static [void] Remove([Book]$Book) {
[BookList]::Initialize()
[BookList]::Books.Remove($Book)
}
# Remove a book by property value.
static [void] RemoveBy([string]$Property, [string]$Value) {
[BookList]::Initialize()
$Index = [BookList]::Books.FindIndex({
param($b)
$b.$Property -eq $Value
}.GetNewClosure())
if ($Index -ge 0) {
[BookList]::Books.RemoveAt($Index)
}
}
}
Write-Debug "[$scriptName] - [/classes/BookList.ps1] - Done"
#endregion - From /classes/BookList.ps1
Write-Debug "[$scriptName] - [/classes] - Done"
#endregion - From /classes
#region - From /private
Write-Debug "[$scriptName] - [/private] - Processing folder"
#region - From /private/Get-InternalPSModule.ps1
Write-Debug "[$scriptName] - [/private/Get-InternalPSModule.ps1] - Importing"
function Get-InternalPSModule {
<#
.SYNOPSIS
Performs tests on a module.
.EXAMPLE
Test-PSModule -Name 'World'
"Hello, World!"
#>
[CmdletBinding()]
param (
# Name of the person to greet.
[Parameter(Mandatory)]
[string] $Name
)
Write-Output "Hello, $Name!"
}
Write-Debug "[$scriptName] - [/private/Get-InternalPSModule.ps1] - Done"
#endregion - From /private/Get-InternalPSModule.ps1
#region - From /private/Set-InternalPSModule.ps1
Write-Debug "[$scriptName] - [/private/Set-InternalPSModule.ps1] - Importing"
function Set-InternalPSModule {
<#
.SYNOPSIS
Performs tests on a module.
.EXAMPLE
Test-PSModule -Name 'World'
"Hello, World!"
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function',
Justification = 'Reason for suppressing'
)]
[CmdletBinding()]
param (
# Name of the person to greet.
[Parameter(Mandatory)]
[string] $Name
)
Write-Output "Hello, $Name!"
}
Write-Debug "[$scriptName] - [/private/Set-InternalPSModule.ps1] - Done"
#endregion - From /private/Set-InternalPSModule.ps1
Write-Debug "[$scriptName] - [/private] - Done"
#endregion - From /private
#region - From /public
Write-Debug "[$scriptName] - [/public] - Processing folder"
#region - From /public/Get-PSModuleTest.ps1
Write-Debug "[$scriptName] - [/public/Get-PSModuleTest.ps1] - Importing"
#Requires -Modules Utilities
function Get-PSModuleTest {
<#
.SYNOPSIS
Performs tests on a module.
.EXAMPLE
Test-PSModule -Name 'World'
"Hello, World!"
#>
[CmdletBinding()]
param (
# Name of the person to greet.
[Parameter(Mandatory)]
[string] $Name
)
Write-Debug 'Debug message'
Write-Verbose 'Verbose message'
Write-Output "Hello, $Name!"
}
Write-Debug "[$scriptName] - [/public/Get-PSModuleTest.ps1] - Done"
#endregion - From /public/Get-PSModuleTest.ps1
#region - From /public/New-PSModuleTest.ps1
Write-Debug "[$scriptName] - [/public/New-PSModuleTest.ps1] - Importing"
#Requires -Modules @{ModuleName='PSSemVer'; ModuleVersion='1.0'}
function New-PSModuleTest {
<#
.SYNOPSIS
Performs tests on a module.
.EXAMPLE
Test-PSModule -Name 'World'
"Hello, World!"
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function',
Justification = 'Reason for suppressing'
)]
[CmdletBinding()]
param (
# Name of the person to greet.
[Parameter(Mandatory)]
[string] $Name
)
Write-Debug 'Debug message'
Write-Verbose 'Verbose message'
Write-Output "Hello, $Name!"
}
Write-Debug "[$scriptName] - [/public/New-PSModuleTest.ps1] - Done"
#endregion - From /public/New-PSModuleTest.ps1
#region - From /public/Set-PSModuleTest.ps1
Write-Debug "[$scriptName] - [/public/Set-PSModuleTest.ps1] - Importing"
function Set-PSModuleTest {
<#
.SYNOPSIS
Performs tests on a module.
.EXAMPLE
Test-PSModule -Name 'World'
"Hello, World!"
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function',
Justification = 'Reason for suppressing'
)]
[CmdletBinding(SupportsShouldProcess)]
param (
# Name of the person to greet.
[Parameter(Mandatory)]
[string] $Name
)
Write-Debug 'Debug message'
Write-Verbose 'Verbose message'
if ($PSCmdlet.ShouldProcess($Name, 'Set-PSModuleTest')) {
Write-Output "Hello, $Name!"
}
}
Write-Debug "[$scriptName] - [/public/Set-PSModuleTest.ps1] - Done"
#endregion - From /public/Set-PSModuleTest.ps1
#region - From /public/Test-PSModuleTest.ps1
Write-Debug "[$scriptName] - [/public/Test-PSModuleTest.ps1] - Importing"
function Test-PSModuleTest {
<#
.SYNOPSIS
Performs tests on a module.
.EXAMPLE
Test-PSModule -Name 'World'
"Hello, World!"
#>
[CmdletBinding()]
param (
# Name of the person to greet.
[Parameter(Mandatory)]
[string] $Name
)
Write-Debug 'Debug message'
Write-Verbose 'Verbose message'
Write-Output "Hello, $Name!"
}
Write-Debug "[$scriptName] - [/public/Test-PSModuleTest.ps1] - Done"
#endregion - From /public/Test-PSModuleTest.ps1
Write-Debug "[$scriptName] - [/public] - Done"
#endregion - From /public
#region - From /finally.ps1
Write-Debug "[$scriptName] - [/finally.ps1] - Importing"
Write-Verbose '------------------------------'
Write-Verbose '--- THIS IS A LAST LOADER ---'
Write-Verbose '------------------------------'
Write-Debug "[$scriptName] - [/finally.ps1] - Done"
#endregion - From /finally.ps1
#region Class exporter
# Get the internal TypeAccelerators class to use its static methods.
$TypeAcceleratorsClass = [psobject].Assembly.GetType(
'System.Management.Automation.TypeAccelerators'
)
# Ensure none of the types would clobber an existing type accelerator.
# If a type accelerator with the same name already exists, skip adding it.
$ExistingTypeAccelerators = $TypeAcceleratorsClass::Get
# Define the types to export with type accelerators.
$ExportableEnums = @(
)
$ExportableEnums | ForEach-Object { Write-Verbose "Exporting enum '$($_.FullName)'." }
foreach ($Type in $ExportableEnums) {
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
Write-Verbose "Enum already exists [$($Type.FullName)]. Skipping."
} else {
Write-Verbose "Importing enum '$Type'."
$TypeAcceleratorsClass::Add($Type.FullName, $Type)
}
}
$ExportableClasses = @(
[Book]
[BookList]
)
$ExportableClasses | ForEach-Object { Write-Verbose "Exporting class '$($_.FullName)'." }
foreach ($Type in $ExportableClasses) {
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
Write-Verbose "Class already exists [$($Type.FullName)]. Skipping."
} else {
Write-Verbose "Importing class '$Type'."
$TypeAcceleratorsClass::Add($Type.FullName, $Type)
}
}
# Remove type accelerators when the module is removed.
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
foreach ($Type in ($ExportableEnums + $ExportableClasses)) {
$null = $TypeAcceleratorsClass::Remove($Type.FullName)
}
}.GetNewClosure()
#endregion Class exporter
$exports = @{
Cmdlet = ''
Alias = '*'
Variable = ''
Function = @(
'Get-PSModuleTest'
'New-PSModuleTest'
'Set-PSModuleTest'
'Test-PSModuleTest'
)
}
Export-ModuleMember @exports