@@ -117,8 +117,19 @@ Task default -depends Build
117
117
Task Publish - depends Test, PrePublish, PublishImpl, PostPublish {
118
118
}
119
119
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
+ }
122
133
123
134
$publishParams = @ {
124
135
Path = $PublishDir
@@ -144,7 +155,7 @@ Task Test -depends Build {
144
155
}
145
156
146
157
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
148
159
149
160
# Get contents of the ReleaseNotes file and update the copied module manifest file
150
161
# with the release notes.
@@ -170,58 +181,109 @@ Task Init -requiredVariables PublishDir {
170
181
}
171
182
}
172
183
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
176
187
}
188
+ }
177
189
178
- $null = Get-NuGetApiKey $NuGetApiKey $EncryptedApiKeyPath
190
+ Task StoreKey - requiredVariables EncryptedApiKeyPath {
191
+ $nuGetApiKeyCred = PromptUserForNuGetApiKeyCredential - DestinationPath $EncryptedApiKeyPath
179
192
" The NuGetApiKey has been stored in $EncryptedApiKeyPath "
180
193
}
181
194
182
195
Task ShowKey - requiredVariables EncryptedApiKeyPath {
183
- $NuGetApiKey = Get-NuGetApiKey $NuGetApiKey $EncryptedApiKeyPath
184
- " The stored NuGetApiKey is: $NuGetApiKey "
196
+ $OFS = ' '
197
+
198
+ if ($NuGetApiKey ) {
199
+ " The embedded (partial) NuGetApiKey is: $ ( $NuGetApiKey [0 .. 7 ]) "
200
+ }
201
+ else {
202
+ $NuGetApiKey = LoadAndUnencryptNuGetApiKey - Path $EncryptedApiKeyPath
203
+ " The stored (partial) NuGetApiKey is: $ ( $NuGetApiKey [0 .. 7 ]) "
204
+ }
205
+
206
+ " To see the full key, use the task 'ShowFullKey'"
207
+ }
208
+
209
+ Task ShowFullKey - requiredVariables EncryptedApiKeyPath {
210
+ if ($NuGetApiKey ) {
211
+ " The embedded NuGetApiKey is: $NuGetApiKey "
212
+ }
213
+ else {
214
+ $NuGetApiKey = LoadAndUnencryptNuGetApiKey - Path $EncryptedApiKeyPath
215
+ " The stored NuGetApiKey is: $NuGetApiKey "
216
+ }
185
217
}
186
218
187
219
Task ? - description ' Lists the available tasks' {
188
220
" Available tasks:"
189
- $psake .context .Peek ().tasks .Keys | Sort
221
+ $PSake .Context .Peek ().Tasks .Keys | Sort-Object
190
222
}
191
223
192
224
# ##############################################################################
193
225
# Helper functions
194
226
# ##############################################################################
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
+ function PromptUserForNuGetApiKeyCredential {
228
+ param (
229
+ [Parameter ()]
230
+ [ValidateNotNullOrEmpty ()]
231
+ [string ]
232
+ $DestinationPath
233
+ )
234
+
235
+ $message = " Enter your NuGet API Key in the password field (or nothing, this isn't used yet in the preview)"
236
+ $nuGetApiKeyCred = Get-Credential - Message $message - UserName " ignored"
237
+
238
+ if ($DestinationPath ) {
239
+ EncryptAndSaveNuGetApiKey - NuGetApiKeySecureString $nuGetApiKeyCred.Password - Path $DestinationPath
240
+ }
241
+
242
+ $nuGetApiKeyCred
243
+ }
244
+
245
+ function EncryptAndSaveNuGetApiKey {
246
+ param (
247
+ [Parameter (Mandatory , ParameterSetName = ' SecureString' )]
248
+ [ValidateNotNull ()]
249
+ [SecureString ]
250
+ $NuGetApiKeySecureString ,
251
+
252
+ [Parameter (Mandatory , ParameterSetName = ' PlainText' )]
253
+ [ValidateNotNullOrEmpty ()]
254
+ [string ]
255
+ $NuGetApiKey ,
256
+
257
+ [Parameter (Mandatory )]
258
+ $Path
259
+ )
260
+
261
+ if ($PSCmdlet.ParameterSetName -eq ' PlainText' ) {
262
+ $NuGetApiKeySecureString = ConvertTo-SecureString - String $NuGetApiKey - AsPlainText - Force
263
+ }
264
+
265
+ $parentDir = Split-Path $Path - Parent
266
+ if (! (Test-Path - LiteralPath $parentDir )) {
267
+ $null = New-Item - Path $parentDir - ItemType Directory
268
+ }
269
+ elseif (Test-Path - LiteralPath $Path ) {
270
+ Remove-Item - LiteralPath $Path
271
+ }
272
+
273
+ $NuGetApiKeySecureString | ConvertFrom-SecureString | Export-Clixml $Path
274
+ Write-Verbose " The NuGetApiKey has been encrypted and saved to $Path "
275
+ }
276
+
277
+ function LoadAndUnencryptNuGetApiKey {
278
+ param (
279
+ [Parameter (Mandatory )]
280
+ [ValidateNotNullOrEmpty ()]
281
+ [string ]
282
+ $Path
283
+ )
284
+
285
+ $storedKey = Import-Clixml $Path | ConvertTo-SecureString
286
+ $cred = New-Object - TypeName PSCredential - ArgumentList ' jpgr' , $storedKey
287
+ $cred.GetNetworkCredential ().Password
288
+ Write-Verbose " The NuGetApiKey has been loaded and unencrypted from $Path "
227
289
}
0 commit comments