Skip to content

Commit

Permalink
Update tasks to reduce code duplication (issue gaelcolas#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju committed Apr 24, 2020
1 parent 081a551 commit 0918cce
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 159 deletions.
55 changes: 24 additions & 31 deletions .build/tasks/Build-Module.ModuleBuilder.build.ps1
@@ -1,39 +1,12 @@
Param (

param
(
[Parameter()]
[string]
$ProjectName = (property ProjectName $(
#Find the module manifest to deduce the Project Name
(Get-ChildItem $BuildRoot\*\*.psd1 -Exclude 'build.psd1', 'analyzersettings.psd1' | Where-Object {
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
$(try
{
Test-ModuleManifest $_.FullName -ErrorAction Stop
}
catch
{
Write-Warning $_
$false
}) }
).BaseName
)
),
$ProjectName = (property ProjectName ''),

[Parameter()]
[string]
$SourcePath = (property SourcePath ((Get-ChildItem $BuildRoot\*\*.psd1 -Exclude 'build.psd1', 'analyzersettings.psd1' | Where-Object {
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
$(try
{
Test-ModuleManifest $_.FullName -ErrorAction Stop
}
catch
{
Write-Warning $_
$false
}) }
).Directory.FullName)
),
$SourcePath = (property SourcePath ''),

[Parameter()]
[string]
Expand Down Expand Up @@ -74,6 +47,16 @@ Import-Module -Name "$PSScriptRoot/Common.Functions.psm1"

# Synopsis: Build the Module based on its Build.psd1 definition
Task Build_Module_ModuleBuilder {
if ([System.String]::IsNullOrEmpty($ProjectName))
{
$ProjectName = Get-ProjectName -BuildRoot $BuildRoot
}

if ([System.String]::IsNullOrEmpty($SourcePath))
{
$SourcePath = Get-SourcePath -BuildRoot $BuildRoot
}

$getModuleVersionParameters = @{
OutputDirectory = $OutputDirectory
ProjectName = $ProjectName
Expand Down Expand Up @@ -151,6 +134,16 @@ Task Build_Module_ModuleBuilder {
}

Task Build_NestedModules_ModuleBuilder {
if ([System.String]::IsNullOrEmpty($ProjectName))
{
$ProjectName = Get-ProjectName -BuildRoot $BuildRoot
}

if ([System.String]::IsNullOrEmpty($SourcePath))
{
$SourcePath = Get-SourcePath -BuildRoot $BuildRoot
}

" Project Name = $ProjectName"
" Source Path = $SourcePath"
" Output Directory = $OutputDirectory"
Expand Down
73 changes: 73 additions & 0 deletions .build/tasks/Common.Functions.psm1
Expand Up @@ -576,6 +576,76 @@ function Update-JaCoCoStatistics
return $Document
}

function Get-ProjectName
{
[CmdletBinding()]
[OutputType([System.String])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$BuildRoot
)

return (Get-ProjectModuleManifest -BuildRoot $BuildRoot).BaseName
}

function Get-SourcePath
{
[CmdletBinding()]
[OutputType([System.String])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$BuildRoot
)

return (Get-ProjectModuleManifest -BuildRoot $BuildRoot).Directory.FullName
}

function Get-ProjectModuleManifest
{
[CmdletBinding()]
[OutputType([System.IO.FileInfo])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$BuildRoot
)

$excludeFiles = @(
'build.psd1'
'analyzersettings.psd1'
)

$moduleManifestItem = @(
Get-ChildItem -Path "$BuildRoot\*\*.psd1" -Exclude $excludeFiles |
Where-Object -FilterScript {
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) `
-and $(
try
{
Test-ModuleManifest $_.FullName -ErrorAction Stop
}
catch
{
Write-Warning $_
$false
}
)
}
)

if ($moduleManifestItem.Count -gt 1)
{
throw ("Found more than one project folder containing a module manifest, please make sure there are only one; `n Manifest: {0}" -f ($moduleManifestItem.FullName -join "`n Manifest: "))
}

return $moduleManifestItem
}

Export-ModuleMember -Function @(
'Convert-HashtableToString'
'Get-CodeCoverageThreshold'
Expand All @@ -586,4 +656,7 @@ Export-ModuleMember -Function @(
'Get-CodeCoverageOutputFileEncoding'
'Merge-JaCoCoReports'
'Update-JaCoCoStatistics'
'Get-ProjectModuleManifest'
'Get-ProjectName'
'Get-SourcePath'
)
20 changes: 5 additions & 15 deletions .build/tasks/DeployAll.PSDeploy.build.ps1
Expand Up @@ -5,21 +5,7 @@ Param (

[Parameter()]
[string]
$ProjectName = (property ProjectName $(
(Get-ChildItem $BuildRoot\*\*.psd1 -Exclude 'build.psd1', 'analyzersettings.psd1' | Where-Object {
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
$(try
{
Test-ModuleManifest $_.FullName -ErrorAction Stop
}
catch
{
Write-Warning $_
$false
}) }
).BaseName
)
),
$ProjectName = (property ProjectName ''),

[Parameter()]
[string]
Expand Down Expand Up @@ -52,6 +38,10 @@ Param (

# Synopsis: Deploy everything configured in PSDeploy
task Deploy_with_PSDeploy {
if ([System.String]::IsNullOrEmpty($ProjectName))
{
$ProjectName = Get-ProjectName -BuildRoot $BuildRoot
}

if (![io.path]::IsPathRooted($BuildOutput))
{
Expand Down
21 changes: 6 additions & 15 deletions .build/tasks/DscResource.Test.build.ps1
Expand Up @@ -11,21 +11,7 @@ Param (

[Parameter()]
[string]
$ProjectName = (property ProjectName $(
(Get-ChildItem $BuildRoot\*\*.psd1 -Exclude 'build.psd1', 'analyzersettings.psd1' | Where-Object {
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
$(try
{
Test-ModuleManifest $_.FullName -ErrorAction Stop
}
catch
{
Write-Warning $_
$false
}) }
).BaseName
)
),
$ProjectName = (property ProjectName ''),

[Parameter()]
[string]
Expand Down Expand Up @@ -70,6 +56,11 @@ Import-Module -Name "$PSScriptRoot/Common.Functions.psm1"

# Synopsis: Making sure the Module meets some quality standard (help, tests)
task Invoke_DscResource_tests {
if ([System.String]::IsNullOrEmpty($ProjectName))
{
$ProjectName = Get-ProjectName -BuildRoot $BuildRoot
}

if (!(Split-Path -isAbsolute $OutputDirectory))
{
$OutputDirectory = Join-Path -Path $ProjectPath -ChildPath $OutputDirectory
Expand Down
34 changes: 19 additions & 15 deletions .build/tasks/Invoke-Pester.pester.build.ps1
Expand Up @@ -11,21 +11,7 @@ Param (

[Parameter()]
[string]
$ProjectName = (property ProjectName $(
(Get-ChildItem $BuildRoot\*\*.psd1 -Exclude 'build.psd1', 'analyzersettings.psd1' | Where-Object {
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
$(try
{
Test-ModuleManifest $_.FullName -ErrorAction Stop
}
catch
{
Write-Warning $_
$false
}) }
).BaseName
)
),
$ProjectName = (property ProjectName ''),

[Parameter()]
[string]
Expand Down Expand Up @@ -74,6 +60,11 @@ Import-Module -Name "$PSScriptRoot/Common.Functions.psm1"

# Synopsis: Making sure the Module meets some quality standard (help, tests).
task Invoke_pester_tests {
if ([System.String]::IsNullOrEmpty($ProjectName))
{
$ProjectName = Get-ProjectName -BuildRoot $BuildRoot
}

if (!(Split-Path -isAbsolute $OutputDirectory))
{
$OutputDirectory = Join-Path -Path $ProjectPath -ChildPath $OutputDirectory
Expand Down Expand Up @@ -307,6 +298,11 @@ task Fail_Build_if_Pester_Tests_failed {

"Asserting that no test failed"

if ([System.String]::IsNullOrEmpty($ProjectName))
{
$ProjectName = Get-ProjectName -BuildRoot $BuildRoot
}

if (!(Split-Path -isAbsolute $OutputDirectory))
{
$OutputDirectory = Join-Path -Path $ProjectPath -ChildPath $OutputDirectory
Expand Down Expand Up @@ -360,6 +356,10 @@ task Fail_Build_if_Pester_Tests_failed {

# Synopsis: Fails the build if the code coverage is under predefined threshold.
task Pester_if_Code_Coverage_Under_Threshold {
if ([System.String]::IsNullOrEmpty($ProjectName))
{
$ProjectName = Get-ProjectName -BuildRoot $BuildRoot
}

if (!$CodeCoverageThreshold)
{
Expand Down Expand Up @@ -438,6 +438,10 @@ task Pester_if_Code_Coverage_Under_Threshold {

# Synopsis: Uploading Unit Test results to AppVeyor.
task Upload_Test_Results_To_AppVeyor -If { (property BuildSystem 'unknown') -eq 'AppVeyor' } {
if ([System.String]::IsNullOrEmpty($ProjectName))
{
$ProjectName = Get-ProjectName -BuildRoot $BuildRoot
}

if (!(Split-Path -isAbsolute $OutputDirectory))
{
Expand Down
18 changes: 0 additions & 18 deletions .build/tasks/Merge-CodeCoverageFiles.pester.build.ps1
Expand Up @@ -9,24 +9,6 @@ Param (
[string]
$OutputDirectory = (property OutputDirectory (Join-Path $BuildRoot 'output')),

[Parameter()]
[string]
$ProjectName = (property ProjectName $(
(Get-ChildItem $BuildRoot\*\*.psd1 -Exclude 'build.psd1', 'analyzersettings.psd1' | Where-Object {
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
$(try
{
Test-ModuleManifest $_.FullName -ErrorAction Stop
}
catch
{
Write-Warning $_
$false
}) }
).BaseName
)
),

# Build Configuration object
[Parameter()]
$BuildInfo = (property BuildInfo @{ })
Expand Down
23 changes: 6 additions & 17 deletions .build/tasks/New-Release.GitHub.build.ps1
Expand Up @@ -13,22 +13,7 @@ param(

[Parameter()]
[string]
$ProjectName = (property ProjectName $(
#Find the module manifest to deduce the Project Name
(Get-ChildItem $BuildRoot\*\*.psd1 -Exclude 'build.psd1', 'analyzersettings.psd1' | Where-Object {
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
$(try
{
Test-ModuleManifest $_.FullName -ErrorAction Stop
}
catch
{
Write-Warning $_
$false
}) }
).BaseName
)
),
$ProjectName = (property ProjectName ''),

[Parameter()]
[string]
Expand Down Expand Up @@ -76,6 +61,11 @@ Import-Module -Name "$PSScriptRoot/Common.Functions.psm1"
. $PSScriptRoot/GitHubRelease.functions.ps1

task Publish_release_to_GitHub -if ($GitHubToken) {
if ([System.String]::IsNullOrEmpty($ProjectName))
{
$ProjectName = Get-ProjectName -BuildRoot $BuildRoot
}

if (!(Split-Path $OutputDirectory -IsAbsolute))
{
$OutputDirectory = Join-Path $BuildRoot $OutputDirectory
Expand Down Expand Up @@ -124,7 +114,6 @@ task Publish_release_to_GitHub -if ($GitHubToken) {
}
}


# if you want to create the tag on /release/v$ModuleVersion branch (default to master)
$ReleaseBranch = $ExecutionContext.InvokeCommand.ExpandString($ReleaseBranch)

Expand Down

0 comments on commit 0918cce

Please sign in to comment.