From 0918cce9a1e625c95586aea6271f1457e6a8be7c Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Fri, 24 Apr 2020 13:03:08 +0200 Subject: [PATCH] Update tasks to reduce code duplication (issue #125) --- .../Build-Module.ModuleBuilder.build.ps1 | 55 ++++++-------- .build/tasks/Common.Functions.psm1 | 73 +++++++++++++++++++ .build/tasks/DeployAll.PSDeploy.build.ps1 | 20 ++--- .build/tasks/DscResource.Test.build.ps1 | 21 ++---- .build/tasks/Invoke-Pester.pester.build.ps1 | 34 +++++---- .../Merge-CodeCoverageFiles.pester.build.ps1 | 18 ----- .build/tasks/New-Release.GitHub.build.ps1 | 23 ++---- .build/tasks/generateHelp.PlatyPS.build.ps1 | 52 ++++++------- .build/tasks/release.module.build.ps1 | 36 +++++---- CHANGELOG.md | 15 +++- 10 files changed, 188 insertions(+), 159 deletions(-) diff --git a/.build/tasks/Build-Module.ModuleBuilder.build.ps1 b/.build/tasks/Build-Module.ModuleBuilder.build.ps1 index 35386172..2aa37a95 100644 --- a/.build/tasks/Build-Module.ModuleBuilder.build.ps1 +++ b/.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] @@ -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 @@ -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" diff --git a/.build/tasks/Common.Functions.psm1 b/.build/tasks/Common.Functions.psm1 index 24f5ee32..20a21b73 100644 --- a/.build/tasks/Common.Functions.psm1 +++ b/.build/tasks/Common.Functions.psm1 @@ -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' @@ -586,4 +656,7 @@ Export-ModuleMember -Function @( 'Get-CodeCoverageOutputFileEncoding' 'Merge-JaCoCoReports' 'Update-JaCoCoStatistics' + 'Get-ProjectModuleManifest' + 'Get-ProjectName' + 'Get-SourcePath' ) diff --git a/.build/tasks/DeployAll.PSDeploy.build.ps1 b/.build/tasks/DeployAll.PSDeploy.build.ps1 index e3c778d9..91f7c45c 100644 --- a/.build/tasks/DeployAll.PSDeploy.build.ps1 +++ b/.build/tasks/DeployAll.PSDeploy.build.ps1 @@ -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] @@ -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)) { diff --git a/.build/tasks/DscResource.Test.build.ps1 b/.build/tasks/DscResource.Test.build.ps1 index 7dde9b40..060dddce 100644 --- a/.build/tasks/DscResource.Test.build.ps1 +++ b/.build/tasks/DscResource.Test.build.ps1 @@ -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] @@ -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 diff --git a/.build/tasks/Invoke-Pester.pester.build.ps1 b/.build/tasks/Invoke-Pester.pester.build.ps1 index bb6ad583..6d80ded3 100644 --- a/.build/tasks/Invoke-Pester.pester.build.ps1 +++ b/.build/tasks/Invoke-Pester.pester.build.ps1 @@ -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] @@ -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 @@ -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 @@ -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) { @@ -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)) { diff --git a/.build/tasks/Merge-CodeCoverageFiles.pester.build.ps1 b/.build/tasks/Merge-CodeCoverageFiles.pester.build.ps1 index 8cac25f0..ef103b13 100644 --- a/.build/tasks/Merge-CodeCoverageFiles.pester.build.ps1 +++ b/.build/tasks/Merge-CodeCoverageFiles.pester.build.ps1 @@ -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 @{ }) diff --git a/.build/tasks/New-Release.GitHub.build.ps1 b/.build/tasks/New-Release.GitHub.build.ps1 index 009fb73e..c8b5d351 100644 --- a/.build/tasks/New-Release.GitHub.build.ps1 +++ b/.build/tasks/New-Release.GitHub.build.ps1 @@ -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] @@ -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 @@ -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) diff --git a/.build/tasks/generateHelp.PlatyPS.build.ps1 b/.build/tasks/generateHelp.PlatyPS.build.ps1 index e2b0fd91..1558f84b 100644 --- a/.build/tasks/generateHelp.PlatyPS.build.ps1 +++ b/.build/tasks/generateHelp.PlatyPS.build.ps1 @@ -5,39 +5,11 @@ 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] - $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] @@ -58,6 +30,16 @@ Param ( ) Task UpdateHelp { + if ([System.String]::IsNullOrEmpty($ProjectName)) + { + $ProjectName = Get-ProjectName -BuildRoot $BuildRoot + } + + if ([System.String]::IsNullOrEmpty($SourcePath)) + { + $SourcePath = Get-SourcePath -BuildRoot $BuildRoot + } + $LineSeparation "`t`t`t UPDATE HELP MARKDOWN FILE" $LineSeparation @@ -79,6 +61,16 @@ Task UpdateHelp { Task GenerateMamlFromMd { + if ([System.String]::IsNullOrEmpty($ProjectName)) + { + $ProjectName = Get-ProjectName -BuildRoot $BuildRoot + } + + if ([System.String]::IsNullOrEmpty($SourcePath)) + { + $SourcePath = Get-SourcePath -BuildRoot $BuildRoot + } + $LineSeparation "`t`t`t GENERATE MAML IN BUILD OUTPUT" $LineSeparation diff --git a/.build/tasks/release.module.build.ps1 b/.build/tasks/release.module.build.ps1 index 8fc98189..fed1d285 100644 --- a/.build/tasks/release.module.build.ps1 +++ b/.build/tasks/release.module.build.ps1 @@ -12,22 +12,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] @@ -67,6 +52,11 @@ Import-Module -Name "$PSScriptRoot/Common.Functions.psm1" # Synopsis: Create ReleaseNotes from changelog and update the Changelog for release task Create_changelog_release_output { + if ([System.String]::IsNullOrEmpty($ProjectName)) + { + $ProjectName = Get-ProjectName -BuildRoot $BuildRoot + } + " OutputDirectory = $OutputDirectory" " ReleaseNotesPath = $ReleaseNotesPath" @@ -186,6 +176,11 @@ task Create_changelog_release_output { } task publish_nupkg_to_gallery -if ((Get-Command nuget -ErrorAction SilentlyContinue) -and $GalleryApiToken) { + if ([System.String]::IsNullOrEmpty($ProjectName)) + { + $ProjectName = Get-ProjectName -BuildRoot $BuildRoot + } + $getModuleVersionParameters = @{ OutputDirectory = $OutputDirectory ProjectName = $ProjectName @@ -208,6 +203,10 @@ task publish_nupkg_to_gallery -if ((Get-Command nuget -ErrorAction SilentlyConti # Synopsis: Packaging the module by Publishing to output folder (incl dependencies) task package_module_nupkg { + if ([System.String]::IsNullOrEmpty($ProjectName)) + { + $ProjectName = Get-ProjectName -BuildRoot $BuildRoot + } # Force registering the output repository mapping to the Project's output path $null = Unregister-PSRepository -Name output -ErrorAction SilentlyContinue @@ -279,6 +278,11 @@ task package_module_nupkg { } task publish_module_to_gallery -if ((!(Get-Command nuget -ErrorAction SilentlyContinue)) -and $GalleryApiToken) { + if ([System.String]::IsNullOrEmpty($ProjectName)) + { + $ProjectName = Get-ProjectName -BuildRoot $BuildRoot + } + if (!(Split-Path $OutputDirectory -IsAbsolute)) { $OutputDirectory = Join-Path $BuildRoot $OutputDirectory diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fa2be0c..28c453c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- When there are multiple module manifest found in the repository the build + now fails with a a better error error message ([issue #155](https://github.com/gaelcolas/Sampler/issues/155)). + +### Changed + +- Update tasks to reduce code duplication ([issue #125](https://github.com/gaelcolas/Sampler/issues/125)). +- Task 'Merge_CodeCoverage_Files' did not use the parameter `ProjectName` + to that parameter was removed. + ## [0.105.0] - 2020-04-21 ### Added @@ -21,8 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Now the prerelease is cleaned so that it does not contain any dashes by - removing any suffix after a dash, for example `pr054-0012' will be changed - to just `pr054' as the prerelease string. This is due to a bug in the + removing any suffix after a dash, for example `pr054-0012` will be changed + to just `pr054` as the prerelease string. This is due to a bug in the cmdlet `Publish-Module` together with a newer version of the module `ModuleBuilder`.