-
-
Notifications
You must be signed in to change notification settings - Fork 814
/
Copy pathchocolateyInstall.ps1
62 lines (52 loc) · 2.76 KB
/
chocolateyInstall.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function Insert-Script([ref]$originalScript, $script) {
if(!($originalScript.Value -Contains $script)) { $originalScript.Value += $script }
}
try {
$binRoot = Get-BinRoot
$oldPromptOverride = "if(Test-Path Function:\Prompt) {Rename-Item Function:\Prompt PrePoshGitPrompt -Force}"
$newPromptOverride = "function Prompt() {if(Test-Path Function:\PrePoshGitPrompt){++`$global:poshScope; New-Item function:\script:Write-host -value `"param([object] ```$object, ```$backgroundColor, ```$foregroundColor, [switch] ```$nonewline) `" -Force | Out-Null;`$private:p = PrePoshGitPrompt; if(--`$global:poshScope -eq 0) {Remove-Item function:\Write-Host -Force}}PoshGitPrompt}"
$poshgitPath = join-path $binRoot 'poshgit'
try {
if (test-path($poshgitPath)) {
Write-Host "Attempting to remove existing `'$poshgitPath`' prior to install."
remove-item $poshgitPath -recurse -force
}
} catch {
Write-Host 'Could not remove poshgit folder'
}
$poshGitInstall = if($env:poshGit -ne $null){ $env:poshGit } else {'https://github.com/dahlbyk/posh-git/zipball/master'}
Install-ChocolateyZipPackage 'poshgit' $poshGitInstall $poshgitPath
$pgitDir = Dir "$poshgitPath\*posh-git*\" | Sort-Object -Property LastWriteTime | Select -Last 1
if(Test-Path $PROFILE) {
$oldProfile = @(Get-Content $PROFILE)
$newProfile = @()
#If old profile exists replace with new one and make sure prompt preservation function is on top
$pgitExample = "$pgitDir\profile.example.ps1"
foreach($line in $oldProfile) {
if($line.ToLower().Contains("$poshgitPath".ToLower())) {
Insert-Script ([REF]$newProfile) $oldPromptOverride
$line = ". '$pgitExample'"
}
if($line.Trim().Length -gt 0) { $newProfile += $line }
}
# Save any previous Prompt logic
Insert-Script ([REF]$newProfile) $oldPromptOverride
Set-Content -path $profile -value $newProfile -Force
}
$subfolder = get-childitem $poshgitPath -recurse -include 'dahlbyk-posh-git-*' | select -First 1
write-debug "Found and using folder `'$subfolder`'"
$installer = Join-Path $subfolder 'install.ps1'
& $installer
$newProfile = @(Get-Content $PROFILE)
Insert-Script ([REF]$newProfile) "Rename-Item Function:\Prompt PoshGitPrompt -Force"
# function that will run previous prompt logic and then the poshgit logic
# all output from previous prompts will be swallowed
Insert-Script ([REF]$newProfile) $newPromptOverride
Set-Content -path $profile -value $newProfile -Force
} catch {
try {
if($oldProfile){ Set-Content -path $PROFILE -value $oldProfile -Force }
}
catch {}
throw
}