|
| 1 | +############################################################################## |
| 2 | +# PREVIEW VERSION OF PSAKE SCRIPT FOR MODULE BUILD & PUBLISH TO THE PSGALLERY |
| 3 | +############################################################################## |
| 4 | +# |
| 5 | +# We are hoping to add support for publishing modules to the PowerShell gallery |
| 6 | +# and private repositories in a future release of this extension. This is an |
| 7 | +# early look at the approach we are considering which is to supply a |
| 8 | +# PSake-based script that will: |
| 9 | +# |
| 10 | +# 1. Create a directory from which to publish your module. |
| 11 | +# 2. Copy the appropriate module files to that directory excluding items like |
| 12 | +# the .vscode directory, Pester tests, etc. These are configurable in Build.ps1. |
| 13 | +# 3. Verify all existing Pester tests pass. |
| 14 | +# 4. Publish the module to the desired repository (defaulting to the PSGallery). |
| 15 | +# |
| 16 | +# Requirements: PSake. If you don't have this module installed use the following |
| 17 | +# command to install it: |
| 18 | +# |
| 19 | +# PS C:\> Install-Module PSake -Scope CurrentUser |
| 20 | +# |
| 21 | +############################################################################## |
| 22 | +# This is a PSake script that supports the following tasks: |
| 23 | +# clean, build, test and publish. The default task is build. |
| 24 | +# |
| 25 | +# The publish task uses the Publish-Module command to publish |
| 26 | +# to either the PowerShell Gallery (the default) or you can change |
| 27 | +# the $Repository property to the name of an alternate repository. |
| 28 | +# |
| 29 | +# The test task invokes Pester to run any Pester tests in your |
| 30 | +# workspace folder. Name your test scripts <TestName>.Tests.ps1 |
| 31 | +# and Pester will find and run the tests contained in the files. |
| 32 | +# |
| 33 | +# You can run this build script directly using the invoke-psake |
| 34 | +# command which will execute the build task. This task "builds" |
| 35 | +# a temporary folder from which the module can be published. |
| 36 | +# |
| 37 | +# PS C:\> invoke-psake build.ps1 |
| 38 | +# |
| 39 | +# You can run your Pester tests (if any) by running the following command. |
| 40 | +# |
| 41 | +# PS C:\> invoke-psake build.ps1 -taskList test |
| 42 | +# |
| 43 | +# You can execute the publish task with the following command. Note that |
| 44 | +# the publish task will run the test task first. The Pester tests must pass |
| 45 | +# before the publish task will run. The first time you run the publish |
| 46 | +# command, you will be prompted to enter your PowerShell Gallery NuGetApiKey. |
| 47 | +# After entering the key, it is encrypted and stored so you will not have to |
| 48 | +# enter it again. |
| 49 | +# |
| 50 | +# PS C:\> invoke-psake build.ps1 -taskList publish |
| 51 | +# |
| 52 | +# You can verify the stored and encrypted NuGetApiKey by running the following |
| 53 | +# command. This will display your NuGetApiKey in plain text! |
| 54 | +# |
| 55 | +# PS C:\> invoke-psake build.ps1 -taskList showKey |
| 56 | +# |
| 57 | +# You can store a new NuGetApiKey with this command. You can leave off |
| 58 | +# the -properties parameter and you'll be prompted for the key. |
| 59 | +# |
| 60 | +# PS C:\> invoke-psake build.ps1 -taskList storeKey -properties @{NuGetApiKey='test123'} |
| 61 | +# |
| 62 | + |
| 63 | +############################################################################### |
| 64 | +# Customize these properties for your module. |
| 65 | +############################################################################### |
| 66 | +Properties { |
| 67 | + # The name of your module should match the basename of the PSD1 file. |
| 68 | + $ModuleName = (Get-Item $PSScriptRoot\*.psd1)[0].BaseName |
| 69 | + |
| 70 | + # Path to the release notes file. Set to $null if the release notes reside in the manifest file. |
| 71 | + $ReleaseNotesPath = "$PSScriptRoot\ReleaseNotes.md" |
| 72 | + |
| 73 | + # The directory used to publish the module from. If you are using Git, the |
| 74 | + # $PublishDir should be ignored if it is under the workspace directory. |
| 75 | + $PublishDir = "$PSScriptRoot\Release\$ModuleName" |
| 76 | + |
| 77 | + # The following items will not be copied to the $PublishDir. |
| 78 | + # Add items that should not be published with the module. |
| 79 | + $Exclude = @( |
| 80 | + 'Release', |
| 81 | + 'Tests', |
| 82 | + '.git*', |
| 83 | + '.vscode', |
| 84 | + # The next three files are unique to this examples dir. |
| 85 | + 'DebugTest.ps1', |
| 86 | + 'Stop*.ps1', |
| 87 | + 'Readme.md', |
| 88 | + (Split-Path $PSCommandPath -Leaf) |
| 89 | + ) |
| 90 | + |
| 91 | + # Name of the repository you wish to publish to. Default repo is the PSGallery. |
| 92 | + $PublishRepository = $null |
| 93 | + |
| 94 | + # Your NuGet API key for the PSGallery. Leave it as $null and the first time |
| 95 | + # you publish you will be prompted to enter your API key. The build will |
| 96 | + # store the key encrypted in a file, so that on subsequent publishes you |
| 97 | + # will no longer be prompted for the API key. |
| 98 | + $NuGetApiKey = $null |
| 99 | + $EncryptedApiKeyPath = "$env:LOCALAPPDATA\vscode-powershell\NuGetApiKey.clixml" |
| 100 | +} |
| 101 | + |
| 102 | +############################################################################### |
| 103 | +# Customize these tasks for performing operations before and/or after publish. |
| 104 | +############################################################################### |
| 105 | +Task PrePublish { |
| 106 | +} |
| 107 | + |
| 108 | +Task PostPublish { |
| 109 | +} |
| 110 | + |
| 111 | +############################################################################### |
| 112 | +# Core task implementations - this possibly "could" ship as part of the |
| 113 | +# vscode-powershell extension and then get dot sourced into this file. |
| 114 | +############################################################################### |
| 115 | +Task default -depends Build |
| 116 | + |
| 117 | +Task Publish -depends Test, PrePublish, PublishImpl, PostPublish { |
| 118 | +} |
| 119 | + |
| 120 | +Task PublishImpl -depends Test -requiredVariables PublishDir, EncryptedApiKeyPath { |
| 121 | + $NuGetApiKey = Get-NuGetApiKey $NuGetApiKey $EncryptedApiKeyPath |
| 122 | + |
| 123 | + $publishParams = @{ |
| 124 | + Path = $PublishDir |
| 125 | + NuGetApiKey = $NuGetApiKey |
| 126 | + } |
| 127 | + |
| 128 | + if ($PublishRepository) { |
| 129 | + $publishParams['Repository'] = $PublishRepository |
| 130 | + } |
| 131 | + |
| 132 | + # Consider not using -ReleaseNotes parameter when Update-ModuleManifest has been fixed. |
| 133 | + if ($ReleaseNotesPath) { |
| 134 | + $publishParams['ReleaseNotes'] = @(Get-Content $ReleaseNotesPath) |
| 135 | + } |
| 136 | + |
| 137 | + "Calling Publish-Module..." |
| 138 | + Publish-Module @publishParams -WhatIf |
| 139 | +} |
| 140 | + |
| 141 | +Task Test -depends Build { |
| 142 | + Import-Module Pester |
| 143 | + Invoke-Pester $PSScriptRoot |
| 144 | +} |
| 145 | + |
| 146 | +Task Build -depends Clean -requiredVariables PublishDir, Exclude, ModuleName { |
| 147 | + Copy-Item $PSScriptRoot\* -Destination $PublishDir -Recurse -Exclude $Exclude |
| 148 | + |
| 149 | + # Get contents of the ReleaseNotes file and update the copied module manifest file |
| 150 | + # with the release notes. |
| 151 | + # DO NOT USE UNTIL UPDATE-MODULEMANIFEST IS FIXED - HORRIBLY BROKEN RIGHT NOW. |
| 152 | + # if ($ReleaseNotesPath) { |
| 153 | + # $releaseNotes = @(Get-Content $ReleaseNotesPath) |
| 154 | + # Update-ModuleManifest -Path $PublishDir\${ModuleName}.psd1 -ReleaseNotes $releaseNotes |
| 155 | + # } |
| 156 | +} |
| 157 | + |
| 158 | +Task Clean -depends Init -requiredVariables PublishDir { |
| 159 | + # Sanity check the dir we are about to "clean". If $PublishDir were to |
| 160 | + # inadvertently get set to $null, the Remove-Item commmand removes the |
| 161 | + # contents of \*. That's a bad day. Ask me how I know? :-( |
| 162 | + if ($PublishDir.Contains($PSScriptRoot)) { |
| 163 | + Remove-Item $PublishDir\* -Recurse -Force |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +Task Init -requiredVariables PublishDir { |
| 168 | + if (!(Test-Path $PublishDir)) { |
| 169 | + $null = New-Item $PublishDir -ItemType Directory |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +Task StoreKey -requiredVariables EncryptedApiKeyPath { |
| 174 | + if (Test-Path $EncryptedApiKeyPath) { |
| 175 | + Remove-Item $EncryptedApiKeyPath |
| 176 | + } |
| 177 | + |
| 178 | + $null = Get-NuGetApiKey $NuGetApiKey $EncryptedApiKeyPath |
| 179 | + "The NuGetApiKey has been stored in $EncryptedApiKeyPath" |
| 180 | +} |
| 181 | + |
| 182 | +Task ShowKey -requiredVariables EncryptedApiKeyPath { |
| 183 | + $NuGetApiKey = Get-NuGetApiKey $NuGetApiKey $EncryptedApiKeyPath |
| 184 | + "The stored NuGetApiKey is: $NuGetApiKey" |
| 185 | +} |
| 186 | + |
| 187 | +Task ? -description 'Lists the available tasks' { |
| 188 | + "Available tasks:" |
| 189 | + $psake.context.Peek().tasks.Keys | Sort |
| 190 | +} |
| 191 | + |
| 192 | +############################################################################### |
| 193 | +# Helper functions |
| 194 | +############################################################################### |
| 195 | +function Get-NuGetApiKey($NuGetApiKey, $EncryptedApiKeyPath) { |
| 196 | + $storedKey = $null |
| 197 | + if (!$NuGetApiKey) { |
| 198 | + if (Test-Path $EncryptedApiKeyPath) { |
| 199 | + $storedKey = Import-Clixml $EncryptedApiKeyPath | ConvertTo-SecureString |
| 200 | + $cred = New-Object -TypeName PSCredential -ArgumentList 'kh',$storedKey |
| 201 | + $NuGetApiKey = $cred.GetNetworkCredential().Password |
| 202 | + Write-Verbose "Retrieved encrypted NuGetApiKey from $EncryptedApiKeyPath" |
| 203 | + } |
| 204 | + else { |
| 205 | + $cred = Get-Credential -Message "Enter your NuGet API Key in the password field (or nothing, this isn't used yet in the preview)" -UserName "user" |
| 206 | + $apiKeySS = $cred.Password |
| 207 | + $NuGetApiKey = $cred.GetNetworkCredential().Password |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + if (!$storedKey) { |
| 212 | + # Store encrypted NuGet API key to use for future invocations |
| 213 | + if (!$apiKeySS) { |
| 214 | + $apiKeySS = ConvertTo-SecureString -String $NuGetApiKey -AsPlainText -Force |
| 215 | + } |
| 216 | + |
| 217 | + $parentDir = Split-Path $EncryptedApiKeyPath -Parent |
| 218 | + if (!(Test-Path -Path $parentDir)) { |
| 219 | + $null = New-Item -Path $parentDir -ItemType Directory |
| 220 | + } |
| 221 | + |
| 222 | + $apiKeySS | ConvertFrom-SecureString | Export-Clixml $EncryptedApiKeyPath |
| 223 | + Write-Verbose "Stored encrypted NuGetApiKey to $EncryptedApiKeyPath" |
| 224 | + } |
| 225 | + |
| 226 | + $NuGetApiKey |
| 227 | +} |
0 commit comments