Skip to content

Commit 1dfe7d6

Browse files
committed
🐛 ShouldProcess fails if Debug is specified in NonInteractive Mode (#78)
Fixes #73
1 parent dfa20b9 commit 1dfe7d6

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

ModuleFast.build.ps1

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,6 @@ Task AddNugetVersioningAssemblyRequired {
6262
(Get-Content -Raw -Path $ModuleOutFolderPath\ModuleFast.psd1) -replace [Regex]::Escape('# RequiredAssemblies = @()'), 'RequiredAssemblies = @(".\lib\netstandard2.0\NuGet.Versioning.dll")' | Set-Content -Path $ModuleOutFolderPath\ModuleFast.psd1
6363
}
6464

65-
Task Test {
66-
#Run this in a separate job so as not to lock any NuGet DLL packages for future runs. Runspace would lock the package to this process still.
67-
Start-Job {
68-
Invoke-Pester
69-
} | Receive-Job -Wait -AutoRemoveJob
70-
}
71-
72-
Task Build @(
73-
'Clean'
74-
'CopyFiles'
75-
'GetNugetVersioningAssembly'
76-
'AddNugetVersioningAssemblyRequired'
77-
)
78-
7965
Task Package.Nuget {
8066
[string]$repoName = 'ModuleFastBuild-' + (New-Guid)
8167
Get-ChildItem $ModuleOutFolderPath -Recurse -Include '*.nupkg' | Remove-Item @c -Force

ModuleFast.psm1

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ using namespace System.Threading.Tasks
2323
#Probably need to take into account inconsistent state, such as if a dependent module fails then the depending modules should be removed.
2424
$ErrorActionPreference = 'Stop'
2525

26+
if ($ENV:CI) {
27+
Write-Verbose 'CI Environment Variable is set, this indicates a Continuous Integration System is being used. ModuleFast will suppress prompts by setting ConfirmPreference to None and forcing confirmations to false. This is to ensure that ModuleFast can be used in CI/CD systems without user interaction.'
28+
#Module Scope which should carry to other called commands
29+
$SCRIPT:ConfirmPreference = 'None'
30+
$PSDefaultParameterValues['Install-ModuleFast:Confirm'] = $false
31+
}
32+
2633
#Default Source is PWSH Gallery
2734
$SCRIPT:DefaultSource = 'https://pwsh.gallery/index.json'
2835

@@ -32,6 +39,8 @@ enum InstallScope {
3239
CurrentUser
3340
}
3441

42+
43+
3544
function Install-ModuleFast {
3645
<#
3746
.SYNOPSIS
@@ -270,7 +279,7 @@ function Install-ModuleFast {
270279

271280
# Autocreate the default as a convenience, otherwise require the path to be present to avoid mistakes
272281
if ($Destination -eq $defaultRepoPath -and -not (Test-Path $Destination)) {
273-
if ($PSCmdlet.ShouldProcess('Create Destination Folder', $Destination)) {
282+
if (Approve-Action 'Create Destination Folder' $Destination) {
274283
New-Item -ItemType Directory -Path $Destination -Force | Out-Null
275284
}
276285
}
@@ -386,7 +395,7 @@ function Install-ModuleFast {
386395

387396
#Unless Plan was specified, run the process (WhatIf will also short circuit).
388397
#Plan is specified first so that WhatIf message will only show if Plan is not specified due to -or short circuit logic.
389-
if ($Plan -or -not $PSCmdlet.ShouldProcess($Destination, "Install $($installPlan.Count) Modules")) {
398+
if ($Plan -or -not (Approve-Action $Destination "Install $($installPlan.Count) Modules")) {
390399
if ($Plan) {
391400
Write-Verbose "📑 -Plan was specified. Returning a plan including $($installPlan.Count) Module Specifications"
392401
}
@@ -1697,7 +1706,7 @@ function Add-DestinationToPSModulePath {
16971706
$myProfile = $profile.CurrentUserAllHosts
16981707

16991708
if (-not (Test-Path $myProfile)) {
1700-
if (-not $PSCmdlet.ShouldProcess($myProfile, "Allow ModuleFast to work by creating a profile at $myProfile.")) { return }
1709+
if (-not (Approve-Action $myProfile "Allow ModuleFast to work by creating a profile at $myProfile.")) { return }
17011710
Write-Verbose 'User All Hosts profile not found, creating one.'
17021711
New-Item -ItemType File -Path $myProfile -Force | Out-Null
17031712
}
@@ -1722,7 +1731,7 @@ function Add-DestinationToPSModulePath {
17221731
$profileLine = $profileLine -replace '##DESTINATION##', $Destination
17231732

17241733
if ((Get-Content -Raw $myProfile) -notmatch [Regex]::Escape($ProfileLine)) {
1725-
if (-not $PSCmdlet.ShouldProcess($myProfile, "Allow ModuleFast to work by adding $Destination to your PSModulePath on startup by appending to your CurrentUserAllHosts profile. If you do not want this, add -NoProfileUpdate to Install-ModuleFast or add the specified destination to your powershell.config.json or to your PSModulePath another way.")) { return }
1734+
if (-not (Approve-Action $myProfile "Allow ModuleFast to work by adding $Destination to your PSModulePath on startup by appending to your CurrentUserAllHosts profile. If you do not want this, add -NoProfileUpdate to Install-ModuleFast or add the specified destination to your powershell.config.json or to your PSModulePath another way.")) { return }
17261735
Write-Verbose "Adding $Destination to profile $myProfile"
17271736
Add-Content -Path $myProfile -Value "`n`n"
17281737
Add-Content -Path $myProfile -Value $ProfileLine
@@ -2125,6 +2134,26 @@ filter ConvertFrom-ModuleManifest {
21252134
return $moduleFastInfo
21262135
}
21272136

2137+
#Fixes an issue where ShouldProcess will not respect ConfirmPreference if -Debug is specified
2138+
function Approve-Action {
2139+
param(
2140+
[ValidateNotNullOrEmpty()][string]$Target,
2141+
[ValidateNotNullOrEmpty()][string]$Action,
2142+
$ThisCmdlet = $PSCmdlet
2143+
)
2144+
$ShouldProcessMessage = 'Performing the operation "{0}" on target "{1}"' -f $Action, $Target
2145+
if ($ENV:CI -or $CI) {
2146+
Write-Verbose "$ShouldProcessMessage (Auto-Confirmed because `$ENV:CI is specified)"
2147+
return $true
2148+
}
2149+
if ($ConfirmPreference -eq 'None') {
2150+
Write-Verbose "$ShouldProcessMessage (Auto-Confirmed because `$ConfirmPreference is set to 'None')"
2151+
return $true
2152+
}
2153+
2154+
return $ThisCmdlet.ShouldProcess($Target, $Action)
2155+
}
2156+
21282157
#endregion Helpers
21292158

21302159
### ISSUES

0 commit comments

Comments
 (0)