diff --git a/ModuleFast.build.ps1 b/ModuleFast.build.ps1 index de97657..293ad6b 100644 --- a/ModuleFast.build.ps1 +++ b/ModuleFast.build.ps1 @@ -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 diff --git a/ModuleFast.psm1 b/ModuleFast.psm1 index 43ef790..073e639 100644 --- a/ModuleFast.psm1 +++ b/ModuleFast.psm1 @@ -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' @@ -32,6 +39,8 @@ enum InstallScope { CurrentUser } + + function Install-ModuleFast { <# .SYNOPSIS @@ -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 } } @@ -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" } @@ -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 } @@ -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 @@ -2125,6 +2134,27 @@ 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 + ) + Write-Host "Approving Action!" + $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