Skip to content

Commit d2ff81e

Browse files
adding build system
1 parent 96fa56e commit d2ff81e

23 files changed

+731
-50
lines changed

PSModuleDevelopment/PSModuleDevelopment.psd1

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# Modules that must be imported into the global environment prior to importing
2828
# this module
2929
RequiredModules = @(
30-
@{ ModuleName = 'PSFramework'; ModuleVersion = '1.4.149' }
30+
@{ ModuleName = 'PSFramework'; ModuleVersion = '1.6.198' }
3131
@{ ModuleName = 'string'; ModuleVersion = '0.6.1' }
3232
)
3333

@@ -50,45 +50,57 @@
5050

5151
# Functions to export from this module
5252
FunctionsToExport = @(
53-
'Convert-PSMDMessage',
54-
'Expand-PSMDTypeName',
55-
'Export-PSMDString',
56-
'Find-PSMDFileContent',
57-
'Find-PSMDType',
58-
'Format-PSMDParameter',
59-
'Get-PSMDArgumentCompleter',
60-
'Get-PSMDAssembly',
61-
'Get-PSMDConstructor',
62-
'Get-PSMDFileCommand',
63-
'Get-PSMDHelp',
64-
'Get-PSMDMember',
65-
'Get-PSMDModuleDebug',
66-
'Get-PSMDTemplate',
67-
'Import-PSMDModuleDebug',
68-
'Invoke-PSMDTemplate',
69-
'Measure-PSMDCommand',
70-
'Measure-PSMDLinesOfCode',
71-
'New-PSMDDotNetProject',
72-
'New-PSMDHeader',
73-
'New-PSMDFormatTableDefinition',
74-
'New-PSMDModuleNugetPackage',
75-
'New-PSMDTemplate',
76-
'New-PssModuleProject',
77-
'Publish-PSMDScriptFile',
78-
'Publish-PSMDStagedModule',
79-
'Read-PSMDScript',
80-
'Remove-PSMDModuleDebug',
81-
'Remove-PSMDTemplate',
82-
'Rename-PSMDParameter',
83-
'Restart-PSMDShell',
84-
'Search-PSMDPropertyValue',
85-
'Set-PSMDEncoding',
86-
'Set-PSMDModuleDebug',
87-
'Set-PSMDCmdletBinding',
88-
'Set-PSMDModulePath',
89-
'Set-PSMDParameterHelp',
90-
'Set-PSMDStagingRepository',
91-
'Show-PSMDSyntax',
53+
'Convert-PSMDMessage'
54+
'Expand-PSMDTypeName'
55+
'Export-PSMDString'
56+
'Find-PSMDFileContent'
57+
'Find-PSMDType'
58+
'Format-PSMDParameter'
59+
'Get-PSMDArgumentCompleter'
60+
'Get-PSMDAssembly'
61+
'Get-PSMDBuildAction'
62+
'Get-PSMDBuildArtifact'
63+
'Get-PSMDBuildProject'
64+
'Get-PSMDBuildStep'
65+
'Get-PSMDConstructor'
66+
'Get-PSMDFileCommand'
67+
'Get-PSMDHelp'
68+
'Get-PSMDMember'
69+
'Get-PSMDModuleDebug'
70+
'Get-PSMDTemplate'
71+
'Import-PSMDModuleDebug'
72+
'Invoke-PSMDBuildProject'
73+
'Invoke-PSMDTemplate'
74+
'Measure-PSMDCommand'
75+
'Measure-PSMDLinesOfCode'
76+
'New-PSMDBuildProject'
77+
'New-PSMDDotNetProject'
78+
'New-PSMDFormatTableDefinition'
79+
'New-PSMDHeader'
80+
'New-PSMDModuleNugetPackage'
81+
'New-PSMDTemplate'
82+
'New-PssModuleProject'
83+
'Publish-PSMDBuildArtifact'
84+
'Publish-PSMDScriptFile'
85+
'Publish-PSMDStagedModule'
86+
'Read-PSMDScript'
87+
'Register-PSMDBuildAction'
88+
'Remove-PSMDBuildArtifact'
89+
'Remove-PSMDBuildProject'
90+
'Remove-PSMDModuleDebug'
91+
'Remove-PSMDTemplate'
92+
'Rename-PSMDParameter'
93+
'Restart-PSMDShell'
94+
'Search-PSMDPropertyValue'
95+
'Select-PSMDBuildProject'
96+
'Set-PSMDBuildStep'
97+
'Set-PSMDCmdletBinding'
98+
'Set-PSMDEncoding'
99+
'Set-PSMDModuleDebug'
100+
'Set-PSMDModulePath'
101+
'Set-PSMDParameterHelp'
102+
'Set-PSMDStagingRepository'
103+
'Show-PSMDSyntax'
92104
'Split-PSMDScriptFile'
93105
)
94106

@@ -99,15 +111,16 @@
99111
# VariablesToExport = ''
100112

101113
# Aliases to export from this module
102-
AliasesToExport = @(
103-
'dotnetnew',
104-
'find',
105-
'hex',
106-
'imt',
107-
'ipmod',
108-
'parse',
109-
'Restart-Shell',
110-
'rss',
114+
AliasesToExport = @(
115+
'build'
116+
'dotnetnew'
117+
'find'
118+
'hex'
119+
'imt'
120+
'ipmod'
121+
'parse'
122+
'Restart-Shell'
123+
'rss'
111124
'smd'
112125
)
113126

PSModuleDevelopment/en-us/strings.psd1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
'Get-PSMDFileCommand.SyntaxError' = 'Syntax error in file: {0}' # $pathItem
66

7+
'Invoke-PSMDBuildProject.Step.Executing' = '[{0}] Executing step {1} ({2})' # $count, $step.Name, $step.Action
8+
79
'Invoke-PSMDTemplate.Template.NotFound' = 'Unable to find template data for "{0}"' # $TemplateName
810
'Invoke-PSMDTemplate.Invoking' = 'Creating file/project from template {0}' # $item
911

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function Get-PSMDBuildAction {
2+
[CmdletBinding()]
3+
param (
4+
[PsfArgumentCompleter('PSModuleDevelopment.Build.Action')]
5+
[string]
6+
$Name = '*'
7+
)
8+
9+
process {
10+
$script:buildActions.Values | Where-Object Name -Like $Name
11+
}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function Get-PSMDBuildArtifact {
2+
[CmdletBinding()]
3+
param (
4+
[string]
5+
$Name = '*',
6+
7+
[string[]]
8+
$Tag
9+
)
10+
11+
process {
12+
$script:buildArtifacts.Values | Where-Object Name -Like $Name | Where-Object {
13+
if (-not $Tag) { return $true }
14+
foreach ($tagName in $Tag) {
15+
if ($_.Tags -contains $Tag) { return $true }
16+
}
17+
return $false
18+
}
19+
}
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function Get-PSMDBuildProject {
2+
[CmdletBinding(DefaultParameterSetName = 'Path')]
3+
param (
4+
[Parameter(Mandatory = $true, ParameterSetName = 'Path')]
5+
[string]
6+
$Path,
7+
8+
[Parameter(ParameterSetName = 'Path')]
9+
[string]
10+
$Name,
11+
12+
[Parameter(Mandatory = $true, ParameterSetName = 'Selected')]
13+
[switch]
14+
$Selected
15+
)
16+
17+
process {
18+
#region By Path
19+
if ($Path) {
20+
$importPath = $Path
21+
if ($Name) { $importPath = Join-Path -Path $Path -ChildPath "$Name.build.json" }
22+
23+
Get-Content -Path $importPath -Encoding UTF8 | ConvertFrom-Json
24+
}
25+
#endregion By Path
26+
27+
#region Selected
28+
else {
29+
Get-Content -Path (Get-PSFConfigValue -FullName 'PSModuleDevelopment.Build.Project.Selected') -Encoding UTF8 | ConvertFrom-Json
30+
}
31+
#endregion Selected
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function Get-PSMDBuildStep {
2+
[CmdletBinding()]
3+
param (
4+
[string]
5+
$Name = '*',
6+
7+
[string]
8+
$BuildProject
9+
)
10+
11+
begin {
12+
$projectPath = $BuildProject
13+
if (-not $projectPath) { $projectPath = Get-PSFConfigValue -FullName 'PSModuleDevelopment.Build.Project.Selected' }
14+
if (-not $projectPath) { throw "No Project path specified and none selected!" }
15+
if (-not (Test-Path -Path $projectPath)) {
16+
throw "Project file not found: $projectPath"
17+
}
18+
}
19+
process {
20+
$projectObject = Get-PSMDBuildProject -Path $projectPath
21+
$projectObject.Steps | Where-Object Name -Like $Name
22+
}
23+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
function Invoke-PSMDBuildProject {
2+
[Alias('build')]
3+
[CmdletBinding()]
4+
param (
5+
[string]
6+
$Path,
7+
8+
[switch]
9+
$RetainArtifacts
10+
)
11+
12+
begin {
13+
$script:buildArtifacts = @{ }
14+
$buildStatus = @{ }
15+
16+
$projectPath = $Path
17+
if (-not $projectPath) { $projectPath = Get-PSFConfigValue -FullName 'PSModuleDevelopment.Build.Project.Selected' }
18+
if (-not $projectPath) { throw "No Project path specified and none selected!" }
19+
if (-not (Test-Path -Path $projectPath)) {
20+
throw "Project file not found: $projectPath"
21+
}
22+
23+
function Write-StepResult {
24+
[CmdletBinding()]
25+
param (
26+
[int]
27+
$Count,
28+
29+
[ValidateSet('Success', 'Failed', 'ConditionNotMet', 'DependencyNotMet', 'BadAction')]
30+
[string]
31+
$Status,
32+
33+
$StepObject,
34+
35+
$Data,
36+
37+
[hashtable]
38+
$BuildStatus,
39+
40+
[string]
41+
$ContinueLabel
42+
)
43+
44+
$BuildStatus[$StepObject.Name] = $Status -eq 'Success'
45+
46+
$paramWritePSFMessage = @{
47+
Level = 'Warning'
48+
String = "Invoke-PSMDBuildProject.Step.$Status"
49+
}
50+
51+
switch ($Status) {
52+
Failed { Write-PSFMessage @paramWritePSFMessage -StringValues $StepObject.Name, $StepObject.Action -ErrorRecord $Data }
53+
ConditionNotMet { Write-PSFMessage @paramWritePSFMessage -StringValues $StepObject.Name, $StepObject.Action, $StepObject.Condition }
54+
DependencyNotMet { Write-PSFMessage @paramWritePSFMessage -StringValues $StepObject.Name, $StepObject.Action, $Data }
55+
BadAction { Write-PSFMessage @paramWritePSFMessage -StringValues $StepObject.Name, $StepObject.Action }
56+
}
57+
58+
[PSCustomObject]@{
59+
PSTypeName = 'PSModuleDevelopment.Build.StepResult'
60+
Count = $Count
61+
Action = $StepObject.Action
62+
Status = $Status
63+
Step = $StepObject.Name
64+
Data = $Data
65+
}
66+
67+
if ($ContinueLabel) {
68+
continue $ContinueLabel
69+
}
70+
}
71+
}
72+
process {
73+
$projectObject = Get-PSMDBuildProject -Path $projectPath
74+
$steps = $projectObject.Steps | Sort-Object Weight
75+
76+
$count = 0
77+
$stepResults = :main foreach ($step in $steps) {
78+
$count++
79+
$resultDef = @{
80+
Count = $count
81+
StepObject = $step
82+
BuildStatus = $buildStatus
83+
}
84+
85+
Write-PSFMessage -Level Host -String 'Invoke-PSMDBuildProject.Step.Executing' -StringValues $count, $step.Name, $step.Action
86+
87+
#region Validation
88+
$actionObject = $script:buildActions[$step.Action]
89+
if (-not $actionObject) {
90+
Write-StepResult @resultDef -Status BadAction -ContinueLabel main
91+
}
92+
93+
foreach ($dependency in $step.Dependency) {
94+
if (-not $buildStatus[$dependency]) {
95+
Write-StepResult @resultDef -Status DependencyNotMet -Data $dependency -ContinueLabel main
96+
}
97+
}
98+
99+
if ($step.Condition -and $step.ConditionSet) {
100+
$cModule, $cSetName = $step.ConditionSet -split " ", 2
101+
$conditionSet = Get-PSFFilterConditionSet -Module $cModule -Name $cSetName
102+
if (-not $conditionSet) {
103+
Write-StepResult @resultDef -Status ConditionNotMet -ContinueLabel main
104+
}
105+
106+
$filter = New-PSFFilter -Expression $step.Condition -ConditionSet $conditionSet
107+
if (-not $filter.Evaluate()) {
108+
Write-StepResult @resultDef -Status ConditionNotMet -ContinueLabel main
109+
}
110+
}
111+
#endregion Validation
112+
113+
#region Execution
114+
$parameters = @{
115+
RootPath = Split-Path -Path $projectPath
116+
Parameters = $step.Parameters
117+
}
118+
try { $null = & $actionObject.Action $parameters }
119+
catch {
120+
Write-StepResult @resultDef -Status Failed -Data $_ -ContinueLabel main
121+
}
122+
Write-StepResult @resultDef -Status Success
123+
#endregion Execution
124+
}
125+
$stepResults
126+
}
127+
end {
128+
if (-not $RetainArtifacts) {
129+
$script:buildArtifacts = @{ }
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)