Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add-PoshGitToProfile should not modify signed prof #428

Merged
merged 2 commits into from
Feb 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Always forget about this.

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