Skip to content

Commit

Permalink
🐛 ShouldProcess fails if Debug is specified in NonInteractive Mode (#78)
Browse files Browse the repository at this point in the history
Fixes #73
  • Loading branch information
JustinGrote committed Feb 3, 2024
1 parent dfa20b9 commit 1dfe7d6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
14 changes: 0 additions & 14 deletions ModuleFast.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,6 @@ Task AddNugetVersioningAssemblyRequired {
(Get-Content -Raw -Path $ModuleOutFolderPath\ModuleFast.psd1) -replace [Regex]::Escape('# RequiredAssemblies = @()'), 'RequiredAssemblies = @(".\lib\netstandard2.0\NuGet.Versioning.dll")' | Set-Content -Path $ModuleOutFolderPath\ModuleFast.psd1
}

Task Test {
#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.
Start-Job {
Invoke-Pester
} | Receive-Job -Wait -AutoRemoveJob
}

Task Build @(
'Clean'
'CopyFiles'
'GetNugetVersioningAssembly'
'AddNugetVersioningAssemblyRequired'
)

Task Package.Nuget {
[string]$repoName = 'ModuleFastBuild-' + (New-Guid)
Get-ChildItem $ModuleOutFolderPath -Recurse -Include '*.nupkg' | Remove-Item @c -Force
Expand Down
37 changes: 33 additions & 4 deletions ModuleFast.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ using namespace System.Threading.Tasks
#Probably need to take into account inconsistent state, such as if a dependent module fails then the depending modules should be removed.
$ErrorActionPreference = 'Stop'

if ($ENV:CI) {
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.'
#Module Scope which should carry to other called commands
$SCRIPT:ConfirmPreference = 'None'
$PSDefaultParameterValues['Install-ModuleFast:Confirm'] = $false
}

#Default Source is PWSH Gallery
$SCRIPT:DefaultSource = 'https://pwsh.gallery/index.json'

Expand All @@ -32,6 +39,8 @@ enum InstallScope {
CurrentUser
}



function Install-ModuleFast {
<#
.SYNOPSIS
Expand Down Expand Up @@ -270,7 +279,7 @@ function Install-ModuleFast {

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

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

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

if ((Get-Content -Raw $myProfile) -notmatch [Regex]::Escape($ProfileLine)) {
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 }
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 }
Write-Verbose "Adding $Destination to profile $myProfile"
Add-Content -Path $myProfile -Value "`n`n"
Add-Content -Path $myProfile -Value $ProfileLine
Expand Down Expand Up @@ -2125,6 +2134,26 @@ filter ConvertFrom-ModuleManifest {
return $moduleFastInfo
}

#Fixes an issue where ShouldProcess will not respect ConfirmPreference if -Debug is specified
function Approve-Action {
param(
[ValidateNotNullOrEmpty()][string]$Target,
[ValidateNotNullOrEmpty()][string]$Action,
$ThisCmdlet = $PSCmdlet
)
$ShouldProcessMessage = 'Performing the operation "{0}" on target "{1}"' -f $Action, $Target
if ($ENV:CI -or $CI) {
Write-Verbose "$ShouldProcessMessage (Auto-Confirmed because `$ENV:CI is specified)"
return $true
}
if ($ConfirmPreference -eq 'None') {
Write-Verbose "$ShouldProcessMessage (Auto-Confirmed because `$ConfirmPreference is set to 'None')"
return $true
}

return $ThisCmdlet.ShouldProcess($Target, $Action)
}

#endregion Helpers

### ISSUES
Expand Down

0 comments on commit 1dfe7d6

Please sign in to comment.