-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckMate.psm1
More file actions
107 lines (80 loc) · 3.1 KB
/
CheckMate.psm1
File metadata and controls
107 lines (80 loc) · 3.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
function Invoke-CheckMate {
<#
.SYNOPSIS
This is CheckMate. He will run everything inside a checks folder.
.DESCRIPTION
CheckMate is intended to run automated checks for a software projects repository. Its also meant to be easily extended by just adding PowerShell scripts to its checks/-folder.
.PARAMETER RepoRoot
The root folder to run the checks from.
.PARAMETER ReportPath
(optional) Specifies the output path for the report - will be generated inside the repoPath otherwise.
.PARAMETER ChecksBasePath
(optional) Specifies the output path for the report - will be generated inside the repoPath otherwise.
.EXAMPLE
Run only the checks from the `checks/Sanity` directory on the `./MySolution/MyProject` path and generate a `ChecksResult.md` file with the results:
Invoke-CheckMate -RepoRoot "./MySolution/ProjectDirectory" -ReportPath "CheckResults.md" -ChecksBasePath "checks/Sanity"
#>
param(
# Root directory to run the checks for
[string]$RepoRoot = ".",
# Base directory for the checks to run (relative to this script)
[string]$ChecksBasePath = "checks",
# name of the report file to be created
[string]$ReportPath = "$(get-date -f yyyy-MM-dd-HH-mm-ss)_autoreview-report.md"
)
$workingDirectory = Resolve-RepoRoot -RepoRoot $RepoRoot
if ($null -eq $workingDirectory) {
throw "RepoRoot is not valid or does not exist: $RepoRoot"
}
$checkFolder = Join-Path $PSScriptRoot $ChecksBasePath
$results = @{}
# Alle Checks rekursiv einsammeln
Get-ChildItem -Path $checkFolder -Filter *.ps1 -Recurse | ForEach-Object {
$scriptPath = $_.FullName
try
{
$simplifiedCheckPath = [System.IO.Path]::GetRelativePath($checkFolder, $scriptPath)
$result = & $scriptPath -RepoRoot $workingDirectory
$checkSuccess = $LASTEXITCODE
$results[$simplifiedCheckPath] = @($checkSuccess, $result)
}
catch
{
Write-Error "Exception while running `n$_"
$results[$scriptPath] = @(1, "Threw Exception: $_")
}
}
Import-Module "$PSScriptRoot/common/MarkdownReport.psd1"
$markdownReport = New-MarkdownReport -results $results
$reportFile = if ([System.IO.Path]::IsPathRooted($ReportPath)) {
$ReportPath
} else {
Join-Path $workingDirectory $ReportPath
}
Set-Content -Value $markdownReport -Encoding UTF8 $reportFile
return Get-OverallResult $results
}
function Resolve-RepoRoot {
param(
[string] $repoRoot
)
$resolvedPath = Resolve-Path $repoRoot -ErrorAction SilentlyContinue
if (
(-not $resolvedPath) -or
(-not $(Test-Path -Path $resolvedPath -PathType Container)))
{
return $null
}
return $resolvedPath.Path
}
function Get-OverallResult {
param (
[Hashtable] $results
)
$allSuccess = $results.Values | ForEach-Object { $_[0] } | Where-Object { $_ -ne 0 }
if ($allSuccess.Count -eq 0) {
return 0
} else {
return 1
}
}