Skip to content

Commit

Permalink
Merge pull request #428 from dahlbyk/rkeithhill/do-not-modify-signed-…
Browse files Browse the repository at this point in the history
…profile

Add-PoshGitToProfile should not modify signed prof
  • Loading branch information
dahlbyk authored Feb 20, 2017
2 parents c66402d + 857672c commit 80e62b2
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 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 @@ -128,7 +142,7 @@ function Add-PoshGitToProfile([switch]$AllHosts, [switch]$Force, [switch]$WhatIf
}

if (!$profilePath) {
Write-Warning "Skipping add of posh-git import; no profile found."
Write-Warning "Skipping add of posh-git import to profile; no profile found."
Write-Verbose "`$profilePath = '$profilePath'"
Write-Verbose "`$PROFILE = '$PROFILE'"
Write-Verbose "CurrentUserCurrentHost = '$($PROFILE.CurrentUserCurrentHost)'"
Expand All @@ -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.SignatureType -eq [System.Management.Automation.SignatureType]::Authenticode) {
Write-Warning "Skipping add of posh-git import to profile; '$profilePath' appears to be signed."
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 80e62b2

Please sign in to comment.