Skip to content

Commit

Permalink
Merge pull request #504 from dahlbyk/AllUsers
Browse files Browse the repository at this point in the history
Add `Add-PoshGitToProfile -AllUsers` support
  • Loading branch information
dahlbyk authored Jan 1, 2018
2 parents 2ecf05a + 08d73ec commit bf0ec4a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
11 changes: 1 addition & 10 deletions src/GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,7 @@ $global:GitPromptSettings = [pscustomobject]@{
TruncatedBranchSuffix = '...'
}

# PowerShell 5.x only runs on Windows so use .NET types to determine isAdminProcess
# Or if we are on v6 or higher, check the $IsWindows pre-defined variable.
if (($PSVersionTable.PSVersion.Major -le 5) -or $IsWindows) {
$currentUser = [Security.Principal.WindowsPrincipal]([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdminProcess = $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
else {
# Must be Linux or OSX, so use the id util. Root has userid of 0.
$isAdminProcess = 0 -eq (id -u)
}
$isAdminProcess = Test-Administrator

$adminHeader = if ($isAdminProcess) { 'Administrator: ' } else { '' }

Expand Down
37 changes: 33 additions & 4 deletions src/Utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ function Invoke-Utf8ConsoleCommand([ScriptBlock]$cmd) {
}
}

function Test-Administrator {
# PowerShell 5.x only runs on Windows so use .NET types to determine isAdminProcess
# Or if we are on v6 or higher, check the $IsWindows pre-defined variable.
if (($PSVersionTable.PSVersion.Major -le 5) -or $IsWindows) {
$currentUser = [Security.Principal.WindowsPrincipal]([Security.Principal.WindowsIdentity]::GetCurrent())
return $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}

# Must be Linux or OSX, so use the id util. Root has userid of 0.
return 0 -eq (id -u)
}

<#
.SYNOPSIS
Configures your PowerShell profile (startup) script to import the posh-git
Expand All @@ -65,7 +77,12 @@ function Invoke-Utf8ConsoleCommand([ScriptBlock]$cmd) {
.PARAMETER AllHosts
By default, this command modifies the CurrentUserCurrentHost profile
script. By specifying the AllHosts switch, the command updates the
CurrentUserAllHosts profile.
CurrentUserAllHosts profile (or AllUsersAllHosts, given -AllUsers).
.PARAMETER AllUsers
By default, this command modifies the CurrentUserCurrentHost profile
script. By specifying the AllUsers switch, the command updates the
AllUsersCurrentHost profile (or AllUsersAllHosts, given -AllHosts).
Requires elevated permissions.
.PARAMETER Force
Do not check if the specified profile script is already importing
posh-git. Just add Import-Module posh-git command.
Expand All @@ -76,7 +93,7 @@ function Invoke-Utf8ConsoleCommand([ScriptBlock]$cmd) {
Updates your profile script for the current PowerShell host to import the
posh-git module when the current PowerShell host starts.
.EXAMPLE
PS C:\> Add-PoshGitToProfile -AllHost
PS C:\> Add-PoshGitToProfile -AllHosts
Updates your profile script for all PowerShell hosts to import the posh-git
module whenever any PowerShell host starts.
.INPUTS
Expand All @@ -91,6 +108,10 @@ function Add-PoshGitToProfile {
[switch]
$AllHosts,

[Parameter()]
[switch]
$AllUsers,

[Parameter()]
[switch]
$Force,
Expand All @@ -104,9 +125,18 @@ function Add-PoshGitToProfile {
$TestParams
)

if ($AllUsers -and !(Test-Administrator)) {
throw 'Adding posh-git to an AllUsers profile requires an elevated host.'
}

$underTest = $false

$profilePath = if ($AllHosts) { $PROFILE.CurrentUserAllHosts } else { $PROFILE.CurrentUserCurrentHost }
$profileName = $(if ($AllUsers) { 'AllUsers' } else { 'CurrentUser' }) `
+ $(if ($AllHosts) { 'AllHosts' } else { 'CurrentHost' })
Write-Verbose "`$profileName = '$profileName'"

$profilePath = $PROFILE.$profileName
Write-Verbose "`$profilePath = '$profilePath'"

# Under test, we override some variables using $args as a backdoor.
if (($TestParams.Count -gt 0) -and ($TestParams[0] -is [string])) {
Expand Down Expand Up @@ -149,7 +179,6 @@ function Add-PoshGitToProfile {

if (!$profilePath) {
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)'"
Write-Verbose "CurrentUserAllHosts = '$($PROFILE.CurrentUserAllHosts)'"
Expand Down

0 comments on commit bf0ec4a

Please sign in to comment.