|
| 1 | +param( |
| 2 | + # Base directory of all output (default to 'output') |
| 3 | + [Parameter()] |
| 4 | + [string] |
| 5 | + $OutputDirectory = (property OutputDirectory (Join-Path $BuildRoot 'output')), |
| 6 | + |
| 7 | + [Parameter()] |
| 8 | + [string] |
| 9 | + $ChangelogPath = (property ChangelogPath 'CHANGELOG.md'), |
| 10 | + |
| 11 | + [Parameter()] |
| 12 | + [string] |
| 13 | + $ReleaseNotesPath = (property ReleaseNotesPath (Join-Path $OutputDirectory 'ReleaseNotes.md')), |
| 14 | + |
| 15 | + [Parameter()] |
| 16 | + [string] |
| 17 | + $ProjectName = (property ProjectName $( |
| 18 | + #Find the module manifest to deduce the Project Name |
| 19 | + ( |
| 20 | + Get-ChildItem $BuildRoot\*\*.psd1 -Exclude 'build.psd1', 'analyzersettings.psd1' | |
| 21 | + Where-Object { |
| 22 | + ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and |
| 23 | + $( |
| 24 | + try |
| 25 | + { |
| 26 | + Test-ModuleManifest $_.FullName -ErrorAction Stop |
| 27 | + } |
| 28 | + catch |
| 29 | + { |
| 30 | + Write-Warning $_ |
| 31 | + $false |
| 32 | + } |
| 33 | + ) |
| 34 | + } |
| 35 | + ).BaseName |
| 36 | + ) |
| 37 | + ), |
| 38 | + |
| 39 | + [Parameter()] |
| 40 | + [string] |
| 41 | + $ModuleVersion = ( |
| 42 | + property ModuleVersion $( |
| 43 | + try |
| 44 | + { |
| 45 | + (gitversion | ConvertFrom-Json -ErrorAction Stop).NuGetVersionV2 |
| 46 | + } |
| 47 | + catch |
| 48 | + { |
| 49 | + Write-Verbose "Error attempting to use GitVersion $($_)" |
| 50 | + '' |
| 51 | + } |
| 52 | + ) |
| 53 | + ), |
| 54 | + |
| 55 | + [Parameter()] |
| 56 | + [string] |
| 57 | + # retrieves from Environment variable |
| 58 | + $GitHubToken = (property GitHubToken ''), |
| 59 | + |
| 60 | + [Parameter()] |
| 61 | + [string] |
| 62 | + $GalleryApiToken = (property GalleryApiToken ''), |
| 63 | + |
| 64 | + [Parameter()] |
| 65 | + [string] |
| 66 | + $NuGetPublishSource = (property NuGetPublishSource 'https://www.powershellgallery.com/'), |
| 67 | + |
| 68 | + [Parameter()] |
| 69 | + $PSModuleFeed = (property PSModuleFeed 'PSGallery'), |
| 70 | + |
| 71 | + [Parameter()] |
| 72 | + $SkipPublish = (property SkipPublish '') |
| 73 | +) |
| 74 | + |
| 75 | +Import-Module -Name "$PSScriptRoot/Custom.Functions.psm1" |
| 76 | + |
| 77 | +# Synopsis: Packaging the module by Publishing to output folder (incl dependencies) |
| 78 | +task package_module_nupkg { |
| 79 | + |
| 80 | + # Force registering the output repository mapping to the Project's output path |
| 81 | + $null = Unregister-PSRepository -Name output -ErrorAction SilentlyContinue |
| 82 | + $repositoryParams = @{ |
| 83 | + Name = 'output' |
| 84 | + SourceLocation = $OutputDirectory |
| 85 | + PublishLocation = $OutputDirectory |
| 86 | + ErrorAction = 'Stop' |
| 87 | + } |
| 88 | + |
| 89 | + $null = Register-PSRepository @repositoryParams |
| 90 | + |
| 91 | + # Cleaning up existing packaged module |
| 92 | + $moduleToRemove = Get-ChildItem -Path (Join-Path -Path $OutputDirectory -ChildPath "$ProjectName.*.nupkg") |
| 93 | + if ($null -ne $moduleToRemove) |
| 94 | + { |
| 95 | + Write-Build DarkGray " Remove existing $ProjectName package" |
| 96 | + Remove-Item -Path $ModuleToRemove -Force -ErrorAction Stop |
| 97 | + } |
| 98 | + |
| 99 | + # find Module manifest |
| 100 | + $builtModuleManifest = (Get-ChildItem -Path (Join-Path -Path $OutputDirectory -ChildPath $ProjectName) -Depth 2 -Filter "$ProjectName.psd1").FullName | |
| 101 | + Where-Object { |
| 102 | + try |
| 103 | + { |
| 104 | + Test-ModuleManifest -Path $_ -ErrorAction Stop |
| 105 | + } |
| 106 | + catch |
| 107 | + { |
| 108 | + $false |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if (-not $builtModuleManifest) |
| 113 | + { |
| 114 | + throw "No valid manifest found for project $ProjectName." |
| 115 | + } |
| 116 | + |
| 117 | + Write-Build -Color DarkGray -Text " Built module's Manifest found at $builtModuleManifest" |
| 118 | + |
| 119 | + # load module manifest |
| 120 | + $moduleInfo = Import-PowerShellDataFile -Path $builtModuleManifest |
| 121 | + |
| 122 | + # Publish dependencies (from environment) so we can publish the built module |
| 123 | + foreach ($module in $moduleInfo.RequiredModules) |
| 124 | + { |
| 125 | + if ( |
| 126 | + -not ([Microsoft.PowerShell.Commands.ModuleSpecification]$module | |
| 127 | + Find-Module -Repository output -ErrorAction SilentlyContinue) |
| 128 | + ) |
| 129 | + { |
| 130 | + # Replace the module by first (path & version) resolved in PSModulePath |
| 131 | + $module = Get-Module -ListAvailable -FullyQualifiedName $module | Select-Object -First 1 |
| 132 | + if ($null -ne $module.PrivateData.PSData.Prerelease) |
| 133 | + { |
| 134 | + $Prerelease = "-" + $module.PrivateData.PSData.Prerelease |
| 135 | + } |
| 136 | + |
| 137 | + $writeBuildText = " Packaging Required Module {0} v{1}{2}" -f $Module.Name, $Module.Version.ToString(), $Prerelease |
| 138 | + Write-Build -Color Yellow -Text $writeBuildText |
| 139 | + Publish-Module -Repository output -Path $module.ModuleBase -ErrorAction SilentlyContinue |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + Write-Build -Color DarkGray -Text " Creating nuspec file" |
| 144 | + $projectPath = Join-Path -Path $OutputDirectory -ChildPath $ProjectName |
| 145 | + $manifestFileName = '{0}.psd1' -f $ProjectName |
| 146 | + $moduleManifestPath = Get-ChildItem -Path $projectPath -Filter $manifestFileName -Recurse |
| 147 | + $newNuspecFileParams = @{ |
| 148 | + ModuleManifestPath = $moduleManifestPath.FullName |
| 149 | + DestinationPath = $OutputDirectory |
| 150 | + } |
| 151 | + $projectNuspecFile = New-NuspecFile @newNuspecFileParams |
| 152 | + $nugetResults = Get-Command -Name nuget.exe | Select-Object -First 1 |
| 153 | + $nugetFilePath = $nugetResults.Source |
| 154 | + Write-Build -Color DarkGray -Text " nuget Path: $($nugetFilePath)" |
| 155 | + if ((Test-Path -Path $nugetFilePath) -eq $false) |
| 156 | + { |
| 157 | + throw "nuget.exe not found, aborting task package_module_nupkg" |
| 158 | + } |
| 159 | + else |
| 160 | + { |
| 161 | + $startProcessNugetParams = @{ |
| 162 | + FilePath = $nugetFilePath |
| 163 | + Wait = $true |
| 164 | + ArgumentList = @( |
| 165 | + 'Pack', $projectNuspecFile |
| 166 | + '-OutputDirectory', $OutputDirectory |
| 167 | + ) |
| 168 | + } |
| 169 | + |
| 170 | + Start-Process @startProcessNugetParams |
| 171 | + Write-Build -Color Green -Text " Packaged $ProjectName NuGet package" |
| 172 | + } |
| 173 | + |
| 174 | + Write-Build -Color DarkGray -Text " Cleaning up" |
| 175 | + $null = Unregister-PSRepository -Name output -ErrorAction SilentlyContinue |
| 176 | +} |
0 commit comments