diff --git a/src/Utils.ps1 b/src/Utils.ps1 index 3a9aa4c..48c7fb6 100644 --- a/src/Utils.ps1 +++ b/src/Utils.ps1 @@ -82,18 +82,32 @@ function Invoke-Utf8ConsoleCommand([ScriptBlock]$cmd) { .OUTPUTS None. #> -function Add-PoshGitToProfile([switch]$AllHosts, [switch]$Force, [switch]$WhatIf) { +function Add-PoshGitToProfile { + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter()] + [switch] + $AllHosts, + + [Parameter()] + [switch] + $Force, + + [Parameter(ValueFromRemainingArguments)] + [psobject[]] + $TestParams + ) + $underTest = $false $profilePath = if ($AllHosts) { $PROFILE.CurrentUserAllHosts } else { $PROFILE.CurrentUserCurrentHost } # Under test, we override some variables using $args as a backdoor. - # TODO: Can we just turn these into optional parameters with well-defined behavior? - if (($args.Count -gt 0) -and ($args[0] -is [string])) { - $profilePath = [string]$args[0] + if (($TestParams.Count -gt 0) -and ($TestParams[0] -is [string])) { + $profilePath = [string]$TestParams[0] $underTest = $true - if ($args.Count -gt 1) { - $ModuleBasePath = [string]$args[1] + if ($TestParams.Count -gt 1) { + $ModuleBasePath = [string]$TestParams[1] } } @@ -138,6 +152,16 @@ function Add-PoshGitToProfile([switch]$AllHosts, [switch]$Force, [switch]$WhatIf return } + # If the profile script exists and is signed, then we should not modify it + if (Test-Path -LiteralPath $profilePath) { + $sig = Get-AuthenticodeSignature $profilePath + if ($sig.Status -eq [System.Management.Automation.SignatureStatus]::Valid) { + Write-Warning "The profile script '$profilePath' is signed and cannot be updated." + Write-Warning "Add the command 'Import-Module posh-git' to your profile and resign it." + return + } + } + # Check if the location of this module file is in the PSModulePath if (Test-InPSModulePath $ModuleBasePath) { $profileContent = "`nImport-Module posh-git" @@ -146,7 +170,9 @@ function Add-PoshGitToProfile([switch]$AllHosts, [switch]$Force, [switch]$WhatIf $profileContent = "`nImport-Module '$ModuleBasePath\posh-git.psd1'" } - Add-Content -LiteralPath $profilePath -Value $profileContent -Encoding UTF8 -WhatIf:$WhatIf + if ($PSCmdlet.ShouldProcess($profilePath, "Add 'Import-Module posh-git' to profile")) { + Add-Content -LiteralPath $profilePath -Value $profileContent -Encoding UTF8 + } } <#