forked from KelvinTegelaar/CIPP-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfirm-FunctionRequirements.ps1
79 lines (70 loc) · 2.49 KB
/
Confirm-FunctionRequirements.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
# Set CippRoot directory and process requirements.psd1
$CippRoot = (Get-Item $PSScriptRoot).Parent.FullName
$Requirements = & { Get-Content $CippRoot\requirements.psd1 -Raw | Invoke-Expression }
$Modules = $Requirements.Keys
# Exclude top level Modules and Tools directories
$Exclude = @(
'Modules'
'Tools'
)
$Files = Get-ChildItem $CippRoot -Exclude $Exclude | Get-ChildItem -Recurse -Include @('*.ps1', '*.psm1')
# Process each module in requirements
$ModuleTests = $Modules | ForEach-Object -Parallel {
$ModuleRefs = 0
$Success = $true
$Module = $_
try {
# Update module from gallery
Save-Module -Name $Module -Path $using:CippRoot\Modules
$ModuleInfo = Get-Module $using:CippRoot\Modules\$Module -ListAvailable
# Remove old versions
while (($ModuleInfo | Measure-Object).Count -gt 1) {
Remove-Module $Module
$RemoveVersion = $ModuleInfo | Sort-Object -Property Version | Select-Object -First 1
Remove-Item -Path $RemoveVersion.ModuleBase -Recurse
}
# Check for module
if (-not ($ModuleInfo)) {
Import-Module $using:CippRoot\Modules\$Module -Force -ErrorAction Stop
}
# Get list of module commands
$Commands = (Get-Command -Module $Module -ErrorAction Stop).Name
# Review all powershell files and search for module commands
$Files = foreach ($File in $using:Files) {
$References = 0
$Content = Get-Content -Raw $File
$MatchedCommands = foreach ($Command in $Commands) {
if ($Content | Select-String -Pattern $Command) {
$References++
$ModuleRefs++
$Command
}
}
if ($References -gt 0) {
[pscustomobject]@{
File = $File | Resolve-Path -Relative
References = $References
Commands = $MatchedCommands
}
}
}
if ($ModuleRefs -eq 0) {
$ErrorMsg = 'No references found'
$Success = $false
}
}
catch {
$ErrorMsg = $_.Exception.Message
$Success = $false
}
# Return processed module object
[pscustomobject]@{
Module = $Module
Version = $ModuleInfo.Version
References = $ModuleRefs
Files = $Files
ErrorMsg = $ErrorMsg
Success = $Success
}
}
$ModuleTests