Skip to content

Commit c3f064a

Browse files
committed
Merge pull request PowerShell#126 from rkeithhill/rkeithhill/fix-analyzer-warnings-buildfile
Tweaks to PSake build script
2 parents 7a0f7bd + 7579278 commit c3f064a

File tree

1 file changed

+106
-42
lines changed

1 file changed

+106
-42
lines changed

examples/Build.ps1

+106-42
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,19 @@ Task default -depends Build
117117
Task Publish -depends Test, PrePublish, PublishImpl, PostPublish {
118118
}
119119

120-
Task PublishImpl -depends Test -requiredVariables PublishDir, EncryptedApiKeyPath {
121-
$NuGetApiKey = Get-NuGetApiKey $NuGetApiKey $EncryptedApiKeyPath
120+
Task PublishImpl -depends Test -requiredVariables EncryptedApiKeyPath, PublishDir {
121+
if ($NuGetApiKey) {
122+
"Using script embedded NuGetApiKey"
123+
}
124+
elseif (Test-Path -LiteralPath $EncryptedApiKeyPath) {
125+
$NuGetApiKey = LoadAndUnencryptNuGetApiKey $EncryptedApiKeyPath
126+
"Using stored NuGetApiKey"
127+
}
128+
else {
129+
$cred = PromptUserForNuGetApiKeyCredential -DestinationPath $EncryptedApiKeyPath
130+
$NuGetApiKey = $cred.GetNetworkCredential().Password
131+
"The NuGetApiKey has been stored in $EncryptedApiKeyPath"
132+
}
122133

123134
$publishParams = @{
124135
Path = $PublishDir
@@ -144,7 +155,7 @@ Task Test -depends Build {
144155
}
145156

146157
Task Build -depends Clean -requiredVariables PublishDir, Exclude, ModuleName {
147-
Copy-Item $PSScriptRoot\* -Destination $PublishDir -Recurse -Exclude $Exclude
158+
Copy-Item -Path $PSScriptRoot\* -Destination $PublishDir -Recurse -Exclude $Exclude
148159

149160
# Get contents of the ReleaseNotes file and update the copied module manifest file
150161
# with the release notes.
@@ -170,58 +181,111 @@ Task Init -requiredVariables PublishDir {
170181
}
171182
}
172183

173-
Task StoreKey -requiredVariables EncryptedApiKeyPath {
174-
if (Test-Path $EncryptedApiKeyPath) {
175-
Remove-Item $EncryptedApiKeyPath
184+
Task RemoveKey -requiredVariables EncryptedApiKeyPath {
185+
if (Test-Path -LiteralPath $EncryptedApiKeyPath) {
186+
Remove-Item -LiteralPath $EncryptedApiKeyPath
176187
}
188+
}
177189

178-
$null = Get-NuGetApiKey $NuGetApiKey $EncryptedApiKeyPath
190+
Task StoreKey -requiredVariables EncryptedApiKeyPath {
191+
$nuGetApiKeyCred = PromptUserForNuGetApiKeyCredential -DestinationPath $EncryptedApiKeyPath
179192
"The NuGetApiKey has been stored in $EncryptedApiKeyPath"
180193
}
181194

182195
Task ShowKey -requiredVariables EncryptedApiKeyPath {
183-
$NuGetApiKey = Get-NuGetApiKey $NuGetApiKey $EncryptedApiKeyPath
184-
"The stored NuGetApiKey is: $NuGetApiKey"
196+
if ($NuGetApiKey) {
197+
"The embedded (partial) NuGetApiKey is: $($NuGetApiKey[0..7])"
198+
}
199+
else {
200+
$NuGetApiKey = LoadAndUnencryptNuGetApiKey -Path $EncryptedApiKeyPath
201+
"The stored (partial) NuGetApiKey is: $($NuGetApiKey[0..7])"
202+
}
203+
204+
"To see the full key, use the task 'ShowFullKey'"
205+
}
206+
207+
Task ShowFullKey -requiredVariables EncryptedApiKeyPath {
208+
if ($NuGetApiKey) {
209+
"The embedded NuGetApiKey is: $NuGetApiKey"
210+
}
211+
else {
212+
$NuGetApiKey = LoadAndUnencryptNuGetApiKey -Path $EncryptedApiKeyPath
213+
"The stored NuGetApiKey is: $NuGetApiKey"
214+
}
185215
}
186216

187217
Task ? -description 'Lists the available tasks' {
188218
"Available tasks:"
189-
$psake.context.Peek().tasks.Keys | Sort
219+
$PSake.Context.Peek().Tasks.Keys | Sort-Object
190220
}
191221

192222
###############################################################################
193223
# Helper functions
194224
###############################################################################
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
225+
function PromptUserForNuGetApiKeyCredential {
226+
[Diagnostics.CodeAnalysis.SuppressMessage("PSProvideDefaultParameterValue", '')]
227+
param(
228+
[Parameter()]
229+
[ValidateNotNullOrEmpty()]
230+
[string]
231+
$DestinationPath
232+
)
233+
234+
$message = "Enter your NuGet API Key in the password field (or nothing, this isn't used yet in the preview)"
235+
$nuGetApiKeyCred = Get-Credential -Message $message -UserName "ignored"
236+
237+
if ($DestinationPath) {
238+
EncryptAndSaveNuGetApiKey -NuGetApiKeySecureString $nuGetApiKeyCred.Password -Path $DestinationPath
239+
}
240+
241+
$nuGetApiKeyCred
242+
}
243+
244+
function EncryptAndSaveNuGetApiKey {
245+
[Diagnostics.CodeAnalysis.SuppressMessage("PSAvoidUsingConvertToSecureStringWithPlainText", '')]
246+
[Diagnostics.CodeAnalysis.SuppressMessage("PSProvideDefaultParameterValue", '')]
247+
param(
248+
[Parameter(Mandatory, ParameterSetName='SecureString')]
249+
[ValidateNotNull()]
250+
[SecureString]
251+
$NuGetApiKeySecureString,
252+
253+
[Parameter(Mandatory, ParameterSetName='PlainText')]
254+
[ValidateNotNullOrEmpty()]
255+
[string]
256+
$NuGetApiKey,
257+
258+
[Parameter(Mandatory)]
259+
$Path
260+
)
261+
262+
if ($PSCmdlet.ParameterSetName -eq 'PlainText') {
263+
$NuGetApiKeySecureString = ConvertTo-SecureString -String $NuGetApiKey -AsPlainText -Force
264+
}
265+
266+
$parentDir = Split-Path $Path -Parent
267+
if (!(Test-Path -LiteralPath $parentDir)) {
268+
$null = New-Item -Path $parentDir -ItemType Directory
269+
}
270+
elseif (Test-Path -LiteralPath $Path) {
271+
Remove-Item -LiteralPath $Path
272+
}
273+
274+
$NuGetApiKeySecureString | ConvertFrom-SecureString | Export-Clixml $Path
275+
Write-Verbose "The NuGetApiKey has been encrypted and saved to $Path"
276+
}
277+
278+
function LoadAndUnencryptNuGetApiKey {
279+
[Diagnostics.CodeAnalysis.SuppressMessage("PSProvideDefaultParameterValue", '')]
280+
param(
281+
[Parameter(Mandatory)]
282+
[ValidateNotNullOrEmpty()]
283+
[string]
284+
$Path
285+
)
286+
287+
$storedKey = Import-Clixml $Path | ConvertTo-SecureString
288+
$cred = New-Object -TypeName PSCredential -ArgumentList 'jpgr',$storedKey
289+
$cred.GetNetworkCredential().Password
290+
Write-Verbose "The NuGetApiKey has been loaded and unencrypted from $Path"
227291
}

0 commit comments

Comments
 (0)