Skip to content

Commit

Permalink
Add-PoshGitToProfile should not modify signed prof
Browse files Browse the repository at this point in the history
Forgot to address this issue before shipping 0.7.0 - dahlbyk/posh-git#328 (comment)

Merge to develop.
  • Loading branch information
rkeithhill committed Feb 18, 2017
1 parent f8dbd54 commit 9a78b2f
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions src/Utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}

Expand Down Expand Up @@ -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"
Expand All @@ -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
}
}

<#
Expand Down

0 comments on commit 9a78b2f

Please sign in to comment.