|
| 1 | +#requires -Modules InvokeBuild, PSDeploy, BuildHelpers, PSScriptAnalyzer, PlatyPS, Pester |
| 2 | +$script:ModuleName = 'MSSQL-CICD-Helper' |
| 3 | + |
| 4 | +$script:Source = Join-Path $BuildRoot $ModuleName |
| 5 | +$script:Output = Join-Path $BuildRoot output |
| 6 | +$script:Destination = Join-Path $Output $ModuleName |
| 7 | +$script:ModulePath = "$Destination\$ModuleName.psm1" |
| 8 | +$script:ManifestPath = "$Destination\$ModuleName.psd1" |
| 9 | +$script:Imports = ( 'Private', 'Public' ) |
| 10 | +$script:TestFile = "$PSScriptRoot\output\TestResults_PS$PSVersion`_$TimeStamp.xml" |
| 11 | +$script:HelpRoot = Join-Path $Output 'help' |
| 12 | + |
| 13 | +function TaskX($Name, $Parameters) {task $Name @Parameters -Source $MyInvocation} |
| 14 | + |
| 15 | +Task Default Clean, Build, Pester, UpdateSource |
| 16 | +Task Build CopyToOutput, BuildPSM1, BuildPSD1 |
| 17 | +Task Pester Build, ImportModule, UnitTests, FullTests |
| 18 | +Task Local Build, Pester, UpdateSource |
| 19 | + |
| 20 | +Task Clean { |
| 21 | + |
| 22 | + If (Test-Path $Output) |
| 23 | + { |
| 24 | + $null = Remove-Item $Output -Recurse -ErrorAction Ignore |
| 25 | + } |
| 26 | + $null = New-Item -Type Directory -Path $Destination -ErrorAction Ignore |
| 27 | +} |
| 28 | + |
| 29 | +Task UnitTests { |
| 30 | + $TestResults = Invoke-Pester -Path Tests\*unit* -PassThru -Tag Build -ExcludeTag Slow |
| 31 | + if ($TestResults.FailedCount -gt 0) |
| 32 | + { |
| 33 | + Write-Error "Failed [$($TestResults.FailedCount)] Pester tests" |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +Task FullTests { |
| 38 | + $TestResults = Invoke-Pester -Path Tests -PassThru -OutputFormat NUnitXml -OutputFile $testFile -Tag Build |
| 39 | + if ($TestResults.FailedCount -gt 0) |
| 40 | + { |
| 41 | + Write-Error "Failed [$($TestResults.FailedCount)] Pester tests" |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +Task CopyToOutput { |
| 46 | + |
| 47 | + " Create Directory [$Destination]" |
| 48 | + $null = New-Item -Type Directory -Path $Destination -ErrorAction Ignore |
| 49 | + |
| 50 | + Get-ChildItem $source -File | |
| 51 | + where name -NotMatch "$ModuleName\.ps[dm]1" | |
| 52 | + Copy-Item -Destination $Destination -Force -PassThru | |
| 53 | + ForEach-Object { " Create [.{0}]" -f $_.fullname.replace($PSScriptRoot, '')} |
| 54 | + |
| 55 | + Get-ChildItem $source -Directory | |
| 56 | + where name -NotIn $imports | |
| 57 | + Copy-Item -Destination $Destination -Recurse -Force -PassThru | |
| 58 | + ForEach-Object { " Create [.{0}]" -f $_.fullname.replace($PSScriptRoot, '')} |
| 59 | +} |
| 60 | + |
| 61 | +TaskX BuildPSM1 @{ |
| 62 | + Inputs = (Get-Item "$source\*\*.ps1") |
| 63 | + Outputs = $ModulePath |
| 64 | + Jobs = { |
| 65 | + [System.Text.StringBuilder]$stringbuilder = [System.Text.StringBuilder]::new() |
| 66 | + foreach ($folder in $imports ) |
| 67 | + { |
| 68 | + [void]$stringbuilder.AppendLine( "Write-Verbose 'Importing from [$Source\$folder]'" ) |
| 69 | + if (Test-Path "$source\$folder") |
| 70 | + { |
| 71 | + $fileList = Get-ChildItem $source\$folder\ -Recurse -include *.ps1 | Where Name -NotLike '*.Tests.ps1' |
| 72 | + foreach ($file in $fileList) |
| 73 | + { |
| 74 | + $shortName = $file.fullname.replace($PSScriptRoot, '') |
| 75 | + " Importing [.$shortName]" |
| 76 | + [void]$stringbuilder.AppendLine( "# .$shortName" ) |
| 77 | + [void]$stringbuilder.AppendLine( [System.IO.File]::ReadAllText($file.fullname) ) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + " Creating module [$ModulePath]" |
| 83 | + Set-Content -Path $ModulePath -Value $stringbuilder.ToString() |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +TaskX BuildPSD1 @{ |
| 88 | + Inputs = (Get-ChildItem $Source -Recurse -File) |
| 89 | + Outputs = $ManifestPath |
| 90 | + Jobs = { |
| 91 | + |
| 92 | + Write-Output " Update [$ManifestPath]" |
| 93 | + Copy-Item "$source\$ModuleName.psd1" -Destination $ManifestPath |
| 94 | + |
| 95 | + $functions = Get-ChildItem $ModuleName\Public -Recurse -include *.ps1 | Where-Object { $_.name -notmatch 'Tests'} | Select-Object -ExpandProperty basename |
| 96 | + Set-ModuleFunctions -Name $ManifestPath -FunctionsToExport $functions |
| 97 | + |
| 98 | + Write-Output " Detecting semantic versioning" |
| 99 | + |
| 100 | + Import-Module ".\$ModuleName" |
| 101 | + $commandList = Get-Command -Module $ModuleName |
| 102 | + Remove-Module $ModuleName |
| 103 | + |
| 104 | + Write-Output " Calculating fingerprint" |
| 105 | + $fingerprint = foreach ($command in $commandList ) |
| 106 | + { |
| 107 | + foreach ($parameter in $command.parameters.keys) |
| 108 | + { |
| 109 | + '{0}:{1}' -f $command.name, $command.parameters[$parameter].Name |
| 110 | + $command.parameters[$parameter].aliases | Foreach-Object { '{0}:{1}' -f $command.name, $_} |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + $fingerprint = $fingerprint | Sort-Object |
| 115 | + |
| 116 | + if (Test-Path .\fingerprint) |
| 117 | + { |
| 118 | + $oldFingerprint = Get-Content .\fingerprint |
| 119 | + } |
| 120 | + |
| 121 | + $bumpVersionType = 'Patch' |
| 122 | + ' Detecting new features' |
| 123 | + $fingerprint | Where {$_ -notin $oldFingerprint } | % {$bumpVersionType = 'Minor'; " $_"} |
| 124 | + ' Detecting breaking changes' |
| 125 | + $oldFingerprint | Where {$_ -notin $fingerprint } | % {$bumpVersionType = 'Major'; " $_"} |
| 126 | + |
| 127 | + Set-Content -Path .\fingerprint -Value $fingerprint |
| 128 | + |
| 129 | + # Bump the module version |
| 130 | + $version = [version] (Get-Metadata -Path $manifestPath -PropertyName 'ModuleVersion') |
| 131 | + |
| 132 | + if ( $version -lt ([version]'1.0.0') ) |
| 133 | + { |
| 134 | + # Still in beta, don't bump major version |
| 135 | + if ( $bumpVersionType -eq 'Major' ) |
| 136 | + { |
| 137 | + $bumpVersionType = 'Minor' |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + $bumpVersionType = 'Patch' |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if(-not(Test-Path "$output\version.xml")){ |
| 146 | + " PSXML not found. Determining version with Get-NextPSGalleryVersion and creating PSXML" |
| 147 | + $galleryVersion = Get-NextPSGalleryVersion -Name $ModuleName |
| 148 | + $galleryVersion | Export-Clixml -Path "$output\version.xml" |
| 149 | + }else{ |
| 150 | + " PSXML found. Grabbing value from previous build." |
| 151 | + $galleryVersion = Import-Clixml -Path "$output\version.xml" |
| 152 | + } |
| 153 | + |
| 154 | + if ( $version -lt $galleryVersion ) |
| 155 | + { |
| 156 | + $version = $galleryVersion |
| 157 | + } |
| 158 | + |
| 159 | + Write-Output " Stepping [$bumpVersionType] version [$version]" |
| 160 | + $version = [version] (Step-Version $version -Type $bumpVersionType) |
| 161 | + Write-Output " Using version: $version" |
| 162 | + |
| 163 | + Update-Metadata -Path $ManifestPath -PropertyName ModuleVersion -Value $version |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +Task UpdateSource { |
| 168 | + Copy-Item $ManifestPath -Destination "$source\$ModuleName.psd1" |
| 169 | +} |
| 170 | + |
| 171 | +Task ImportModule { |
| 172 | + if ( -Not ( Test-Path $ManifestPath ) ) |
| 173 | + { |
| 174 | + " Modue [$ModuleName] is not built, cannot find [$ManifestPath]" |
| 175 | + Write-Error "Could not find module manifest [$ManifestPath]. You may need to build the module first" |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + if (Get-Module $ModuleName) |
| 180 | + { |
| 181 | + " Unloading Module [$ModuleName] from previous import" |
| 182 | + Remove-Module $ModuleName |
| 183 | + } |
| 184 | + " Importing Module [$ModuleName] from [$ManifestPath]" |
| 185 | + Import-Module $ManifestPath -Force |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +TaskX CreateHelp @{ |
| 190 | + Partial = $true |
| 191 | + Inputs = {Get-ChildItem "$ModuleName\Public\*.ps1"} |
| 192 | + Outputs = { |
| 193 | + process |
| 194 | + { |
| 195 | + Get-ChildItem $_ | % {'{0}\{1}.md' -f $HelpRoot, $_.basename} |
| 196 | + } |
| 197 | + } |
| 198 | + Jobs = 'ImportModule', { |
| 199 | + process |
| 200 | + { |
| 201 | + $null = New-Item -Path $HelpRoot -ItemType Directory -ErrorAction SilentlyContinue |
| 202 | + $mdHelp = @{ |
| 203 | + #Module = $script:ModuleName |
| 204 | + OutputFolder = $HelpRoot |
| 205 | + AlphabeticParamsOrder = $true |
| 206 | + Verbose = $true |
| 207 | + Force = $true |
| 208 | + Command = Get-Item $_ | % basename |
| 209 | + } |
| 210 | + New-MarkdownHelp @mdHelp | % fullname |
| 211 | + } |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +TaskX PackageHelp @{ |
| 216 | + Inputs = {Get-ChildItem $HelpRoot -Recurse -File} |
| 217 | + Outputs = "$Destination\en-us\$ModuleName-help.xml" |
| 218 | + Jobs = 'CreateHelp', { |
| 219 | + New-ExternalHelp -Path $HelpRoot -OutputPath "$Destination\en-us" -force | % fullname |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +task Install Uninstall, { |
| 224 | + $version = [version] (Get-Metadata -Path $manifestPath -PropertyName 'ModuleVersion') |
| 225 | + |
| 226 | + $path = $env:PSModulePath.Split(';').Where( { |
| 227 | + $_ -like 'C:\Users\*' |
| 228 | + }, 'First', 1) |
| 229 | + |
| 230 | + if ($path -and (Test-Path -Path $path)) |
| 231 | + { |
| 232 | + "Using [$path] as base path..." |
| 233 | + $path = Join-Path -Path $path -ChildPath $ModuleName |
| 234 | + $path = Join-Path -Path $path -ChildPath $version |
| 235 | + |
| 236 | + "Creating directory at [$path]..." |
| 237 | + New-Item -Path $path -ItemType 'Directory' -Force -ErrorAction 'Ignore' |
| 238 | + |
| 239 | + "Copying items from [$Destination] to [$path]..." |
| 240 | + Copy-Item -Path "$Destination\*" -Destination $path -Recurse -Force |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +task Uninstall { |
| 245 | + 'Unloading Modules...' |
| 246 | + Get-Module -Name $ModuleName -ErrorAction 'Ignore' | Remove-Module |
| 247 | + |
| 248 | + 'Uninstalling Module packages...' |
| 249 | + $modules = Get-Module $ModuleName -ErrorAction 'Ignore' -ListAvailable |
| 250 | + foreach ($module in $modules) |
| 251 | + { |
| 252 | + Uninstall-Module -Name $module.Name -RequiredVersion $module.Version -ErrorAction 'Ignore' |
| 253 | + } |
| 254 | + |
| 255 | + 'Cleaning up manually installed Modules...' |
| 256 | + $path = $env:PSModulePath.Split(';').Where( { |
| 257 | + $_ -like 'C:\Users\*' |
| 258 | + }, 'First', 1) |
| 259 | + |
| 260 | + $path = Join-Path -Path $path -ChildPath $ModuleName |
| 261 | + if ($path -and (Test-Path -Path $path)) |
| 262 | + { |
| 263 | + 'Removing files... (This may fail if any DLLs are in use.)' |
| 264 | + Get-ChildItem -Path $path -File -Recurse | |
| 265 | + Remove-Item -Force | ForEach-Object 'FullName' |
| 266 | + |
| 267 | + 'Removing folders... (This may fail if any DLLs are in use.)' |
| 268 | + Remove-Item $path -Recurse -Force |
| 269 | + } |
| 270 | +} |
0 commit comments