-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpsakefile.ps1
178 lines (159 loc) · 6.34 KB
/
psakefile.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
Properties {
if (-not $Stage) {
$Stage = 'Debug'
}
if ($DryRun -eq $null) {
$DryRun = $true
}
$ModuleName = Get-ChildItem ./src/*/*.psd1 | Select-Object -ExpandProperty BaseName
$ModuleVersion = (Resolve-Path "./src/${ModuleName}/*.fsproj" | Select-Xml '//Version/text()').Node.Value
$ModuleSrcPath = Resolve-Path "./src/${ModuleName}/"
$ModulePublishPath = Resolve-Path "./publish/${ModuleName}/"
"Module: ${ModuleName} ver${ModuleVersion} root=${ModuleSrcPath} publish=${ModulePublishPath}"
}
Task default -Depends TestAll
# NOTE: I don't know why, but if I add Lint before Test, the module import will be doubled.
Task TestAll -Depends Init, Build, UnitTest, Test, Lint
Task Init {
'Init is running!'
dotnet tool restore
}
Task Clean {
'Clean is running!'
Get-Module pocof -All | Remove-Module -Force -ErrorAction SilentlyContinue
@(
"./src/*/*/${Stage}"
'./release'
"${ModulePublishPath}/*"
) | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue -Exclude .gitkeep
}
function Get-ValidMarkdownCommentHelp {
if (Get-Command Measure-PlatyPSMarkdown -ErrorAction SilentlyContinue) {
$help = Measure-PlatyPSMarkdown .\docs\$ModuleName\*.md | Where-Object Filetype -Match CommandHelp
$validations = $help.FilePath | Test-MarkdownCommandHelp -DetailView
if (-not $validations.IsValid) {
$validations.Messages | Where-Object { $_ -notlike 'PASS:*' } | Write-Error
throw 'Invalid markdown help files.'
}
$help
}
else {
Write-Warning 'PlatyPS is not installed.'
}
}
Task Lint {
# TODO: not work with .NET 9 because of the error: "Lint failed to parse files. Failed with: Aborted type check.'
# dotnet fsharplint lint "${ModuleName}.sln"
# if (-not $?) {
# throw 'dotnet fsharplint failed.'
# }
dotnet fantomas ./src --check
if (-not $?) {
throw 'dotnet fantomas failed.'
}
$warn = Invoke-ScriptAnalyzer -Path .\psakefile.ps1 -Settings .\PSScriptAnalyzerSettings.psd1
if ($warn) {
throw 'Invoke-ScriptAnalyzer for psakefile.ps1 failed.'
}
$warn = Invoke-ScriptAnalyzer -Path .\tests\pocof.Tests.ps1 -Settings .\PSScriptAnalyzerSettings.psd1
if ($warn) {
throw 'Invoke-ScriptAnalyzer for pocof.Tests.ps1 failed.'
}
Get-ValidMarkdownCommentHelp | Out-Null
}
Task Build -Depends Clean {
'Build command let!'
Import-LocalizedData -BindingVariable module -BaseDirectory $ModuleSrcPath -FileName "${ModuleName}.psd1"
if ($module.ModuleVersion -ne (Resolve-Path "./src/*/${ModuleName}.fsproj" | Select-Xml '//Version/text()').Node.Value) {
throw 'Module manifest (.psd1) version does not match project (.fsproj) version.'
}
dotnet publish -c $Stage
"Completed to build $ModuleName ver$ModuleVersion"
}
Task UnitTest {
Remove-Item ./src/pocof.Test/TestResults/* -Recurse -ErrorAction SilentlyContinue
dotnet test --collect:"XPlat Code Coverage" --nologo --logger:"console;verbosity=detailed" --blame-hang-timeout 5s --blame-hang-dump-type full
if (-not $?) {
throw 'dotnet test failed.'
}
Move-Item ./src/pocof.Test/TestResults/*/coverage.cobertura.xml ./src/pocof.Test/TestResults/coverage.cobertura.xml -Force
}
Task Coverage -Depends UnitTest {
Remove-Item ./coverage/*
reportgenerator -reports:'./src/pocof.Test/TestResults/coverage.cobertura.xml' -targetdir:'coverage' -reporttypes:Html
}
Task WorkflowTest {
if (-not (Get-Command act -ErrorAction SilentlyContinue)) {
throw 'act is not installed. Read https://github.com/nektos/act and install it.'
}
act pull_request --verbose --platform ubuntu-latest=catthehacker/ubuntu:pwsh-latest
}
Task Benchmark {
dotnet run --project ./src/pocof.Benchmark -c Release --filter $Filter
}
Task MemoryLayout {
# NOTE: ex) Invoke-psake -taskList MemoryLayout -parameters @{'Group'='Pocof';}
dotnet run --project ./src/pocof.Inspector $Group
}
Task UbuntuPwsh {
if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
throw 'docker is not installed. Read https://docs.docker.com/engine/install/ and install it.'
}
docker build -t ubuntu-dotnet-pwsh .
if (-not $?) {
throw 'docker build failed.'
}
docker run --rm -it ubuntu-dotnet-pwsh
if (-not $?) {
throw 'docker run failed.'
}
}
Task Import -Depends Build {
"Import $ModuleName ver$ModuleVersion"
if ( -not ($ModuleName -and $ModuleVersion)) {
throw "ModuleName or ModuleVersion not defined. $ModuleName, $ModuleVersion"
}
switch ($Stage) {
'Debug' {
Import-Module (Resolve-Path "${ModuleSrcPath}/bin/Debug/*/publish/*.psd1") -Global
}
'Release' {
$installPath = Join-Path ($env:PSModulePath -split ';' -like '*Users*') $ModuleName -AdditionalChildPath $ModuleVersion
New-Item -Path $installPath -ItemType Directory -ErrorAction SilentlyContinue
$sourcePath = Resolve-Path "${ModuleSrcPath}/bin/Release/*/publish/*"
Copy-Item $sourcePath $installPath -Verbose -Force
Copy-Item $sourcePath $ModulePublishPath -Verbose -Force
Import-Module $ModuleName -Global
}
}
}
Task Test -Depends Import {
$result = Invoke-Pester -PassThru
if ($result.Failed) {
throw 'Invoke-Pester failed.'
}
}
Task ExternalHelp -Depends Import {
$help = Get-ValidMarkdownCommentHelp
$help.FilePath | Update-MarkdownCommandHelp -NoBackup
$help.FilePath | Import-MarkdownCommandHelp | Export-MamlCommandHelp -OutputFolder ./src/ -Force | Out-Null
}
Task Release -PreCondition { $Stage -eq 'Release' } -Depends TestAll, ExternalHelp {
"Release $($ModuleName)! version=$ModuleVersion dryrun=$DryRun"
$m = Get-Module $ModuleName
if ($m.Version -ne $ModuleVersion) {
throw "Version inconsistency between project and module. $($m.Version), $ModuleVersion"
}
$p = Get-ChildItem "${ModulePublishPath}/*.psd1"
if (-not $p) {
throw "Module manifest not found. $($m.ModuleBase)/*.psd1"
}
$Params = @{
Path = $p.FullName
Repository = 'PSGallery'
ApiKey = (Get-Credential API-key -Message 'Enter your API key as the password').GetNetworkCredential().Password
WhatIf = $DryRun
Verbose = $true
}
Publish-PSResource @Params
}