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

Chocolatey Simplification + Uninstall #358

Merged
merged 28 commits into from
Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
297e07a
Remove Chocolatey prompt override
dahlbyk Jan 5, 2017
48f61c9
Strip Chocolatey prompt override on install
dahlbyk Jan 5, 2017
35c57a9
Trivial cleanup
dahlbyk Jan 27, 2017
c1c9748
Refactor/simplify check for existing profile import
dahlbyk Jan 27, 2017
8248f03
Update Chocolatey to fix Import-Module path, too
dahlbyk Jan 30, 2017
e1d10ac
Preserve $PROFILE encoding on Chocolatey install
dahlbyk Jan 27, 2017
6abca6a
Clone Chocolatey install as uninstall
dahlbyk Jan 5, 2017
962d38b
Add Chocolatey uninstall
dahlbyk Jan 5, 2017
1c77805
Pin Choclatey install to version tag
dahlbyk Jan 30, 2017
7e42038
Update Chocolatey to 0.7.0-pre1
dahlbyk Jan 30, 2017
7fd9638
Suppress Chocolatey installation location
dahlbyk Jan 30, 2017
9f6b256
Add script to streamline local pack/install loop
dahlbyk Jan 30, 2017
6fd6a31
Merge branch 'master' into chocolatey-uninstall
dahlbyk Jan 30, 2017
9d559c4
Bump to 0.7.0-pre2
dahlbyk Jan 30, 2017
3755191
Warn/quit if $profilePath is missing
dahlbyk Jan 30, 2017
5f8c899
Add verbose logging around Add-PoshGitToProfile
dahlbyk Jan 30, 2017
a4edddd
Bump to 0.7.0-pre3
dahlbyk Jan 30, 2017
6bdac52
Add $PROFILE to verbose output
dahlbyk Jan 30, 2017
77aaa9c
Fix poorly formed interpolation
dahlbyk Jan 31, 2017
5c8e048
Bump to 0.7.0-pre4
dahlbyk Jan 31, 2017
9a1d91d
Specify local version to install
dahlbyk Jan 31, 2017
8161c8f
Fail if the version tag doesn't exist
dahlbyk Jan 31, 2017
c9c1b52
Confirm remote tag, with -Force option to create
dahlbyk Jan 31, 2017
4c8cbc6
Add handling for non-standard $PROFILE
dahlbyk Jan 31, 2017
7620aaf
Force should always update remote tag
dahlbyk Jan 31, 2017
04c2415
Create local tag, too
dahlbyk Jan 31, 2017
f8d9639
Merge branch 'master' into chocolatey-uninstall
dahlbyk Feb 3, 2017
8a4207d
Bump to 0.7.0-pre5
dahlbyk Feb 3, 2017
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
4 changes: 4 additions & 0 deletions chocolatey/packAndLocalInstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pushd $PSScriptRoot
choco pack poshgit.nuspec
choco install -f -y poshgit -pre -s .
popd
2 changes: 1 addition & 1 deletion chocolatey/poshgit.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<metadata>
<id>poshgit</id>
<title>posh-git</title>
<version>0.6.1.20160330</version>
<version>0.7.0-pre3</version>
<authors>Keith Dahlby, Mark Embling, Jeremy Skinner</authors>
<owners>Keith Dahlby</owners>
<description>### posh-git
Expand Down
56 changes: 23 additions & 33 deletions chocolatey/tools/chocolateyInstall.ps1
Original file line number Diff line number Diff line change
@@ -1,58 +1,48 @@
function Insert-Script([ref]$originalScript, $script) {
if(!($originalScript.Value -Contains $script)) { $originalScript.Value += $script }
}

try {
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."
Write-Host "Attempting to remove existing `'$poshgitPath`'."
remove-item $poshgitPath -recurse -force
}
} catch {
Write-Host 'Could not remove poshgit folder'
Write-Host "Could not remove `'$poshgitPath`'"
}

$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
$version = "v$Env:chocolateyPackageVersion"
if ($version -eq 'v') { $version = 'master' }
$poshGitInstall = if ($env:poshGit ) { $env:poshGit } else { "https://github.com/dahlbyk/posh-git/zipball/$version" }
$zip = Install-ChocolateyZipPackage 'poshgit' $poshGitInstall $poshgitPath
$currentVersionPath = Get-ChildItem "$poshgitPath\*posh-git*\" | Sort-Object -Property LastWriteTime | Select-Object -Last 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if it would be better to sort on the directory name if it includes a lexicographic sortable version number?

Copy link
Owner Author

Choose a reason for hiding this comment

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

This is really just defensive programming. A few lines up we try to remove-item $poshgitPath, so this folder will almost always have a single subfolder.


if(Test-Path $PROFILE) {
$oldProfile = @(Get-Content $PROFILE)

. $currentVersionPath\src\Utils.ps1
$oldProfileEncoding = Get-FileEncoding $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 -like '*PoshGitPrompt*') { continue; }

if($line -like '. *posh-git*profile.example.ps1*') {
$line = ". '$currentVersionPath\profile.example.ps1'"
}
if($line -like 'Import-Module *\src\posh-git.psd1*') {
$line = "Import-Module '$currentVersionPath\src\posh-git.psd1'"
}
if($line.Trim().Length -gt 0) { $newProfile += $line }
$newProfile += $line
}
# Save any previous Prompt logic
Insert-Script ([REF]$newProfile) $oldPromptOverride
Set-Content -path $profile -value $newProfile -Force
Set-Content -path $profile -value $newProfile -Force -Encoding $oldProfileEncoding
}

$installer = Join-Path $pgitDir 'install.ps1'
$installer = Join-Path $currentVersionPath '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 }
if($oldProfile){ Set-Content -path $PROFILE -value $oldProfile -Force -Encoding $oldProfileEncoding }
}
catch {}
throw
Expand Down
44 changes: 44 additions & 0 deletions chocolatey/tools/chocolateyUninstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
try {
$binRoot = Get-BinRoot
$poshgitPath = join-path $binRoot 'poshgit'

$currentVersionPath = Get-ChildItem "$poshgitPath\*posh-git*\" | Sort-Object -Property LastWriteTime | Select-Object -Last 1

if(Test-Path $PROFILE) {
$oldProfile = @(Get-Content $PROFILE)

. $currentVersionPath\src\Utils.ps1
$oldProfileEncoding = Get-FileEncoding $PROFILE

$newProfile = @()
foreach($line in $oldProfile) {
if ($line -like '*PoshGitPrompt*') { continue; }
if ($line -like '*Load posh-git example profile*') { continue; }

if($line -like '. *posh-git*profile.example.ps1*') {
continue;
}
if($line -like 'Import-Module *\src\posh-git.psd1*') {
continue;
}
$newProfile += $line
}
Set-Content -path $profile -value $newProfile -Force -Encoding $oldProfileEncoding
}

try {
if (test-path($poshgitPath)) {
Write-Host "Attempting to remove existing `'$poshgitPath`'."
remove-item $poshgitPath -recurse -force
}
} catch {
Write-Host "Could not remove `'$poshgitPath`'"
}
} catch {
try {
if($oldProfile){ Set-Content -path $PROFILE -value $oldProfile -Force -Encoding $oldProfileEncoding }
}
catch {}
throw
}

15 changes: 14 additions & 1 deletion src/Utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ function Add-PoshGitToProfile([switch]$AllHosts, [switch]$Force, [switch]$WhatIf
}
}

if (!$profilePath) {
Write-Warning "Skipping add of posh-git import; no profile found."
Write-Verbose "`$profilePath = '$profilePath'"
Write-Verbose "`$PROFILE = '$PROFILE'"
Write-Verbose "CurrentUserCurrentHost = '${PROFILE.CurrentUserCurrentHost}'"
Write-Verbose "CurrentUserAllHosts = '${PROFILE.CurrentUserAllHosts}'"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here you want "... '$($Profile.CurrentUserAllHosts)'" ${} is handy when you want to separate the variable from the next text e.g. "${foo}${bar}".

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto for the other two below.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Good catch, not sure what I was thinking.

Write-Verbose "AllUsersCurrentHost = '${PROFILE.AllUsersCurrentHost}'"
Write-Verbose "AllUsersAllHosts = '${PROFILE.AllUsersAllHosts}'"
return
}

# Check if the location of this module file is in the PSModulePath
if (Test-InPSModulePath $ModuleBasePath) {
$profileContent = "`nImport-Module posh-git"
Expand Down Expand Up @@ -215,7 +226,9 @@ function Test-PoshGitImportedInScript {
return $false
}

(@(Get-Content $Path -ErrorAction SilentlyContinue) -match 'posh-git').Count -gt 0
$match = (@(Get-Content $Path -ErrorAction SilentlyContinue) -match 'posh-git').Count -gt 0
if ($match) { Write-Verbose "posh-git found in '$Path'" }
$match
}

function dbg($Message, [Diagnostics.Stopwatch]$Stopwatch) {
Expand Down