-
-
Notifications
You must be signed in to change notification settings - Fork 825
Add the Remove-PoshGitFromProfile function
#877
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cfd0bf4
Add the `Remove-PoshGitFromProfile` function
dscho 8fe7e5a
Adjust warnings
dahlbyk 1296dd3
Drop self-import
dahlbyk 9b14103
Merge branch 'master' into pr/877
dahlbyk 5454d1c
Split out new Context
dahlbyk 8ed98be
Format
dahlbyk c6f4a64
Try to simplify assert
dahlbyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -216,6 +216,123 @@ function Add-PoshGitToProfile { | |||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| <# | ||||||||||||||||||||||||||||||||||||||||
| .SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||
| Modifies your PowerShell profile (startup) script so that it does not import | ||||||||||||||||||||||||||||||||||||||||
| the posh-git module when PowerShell starts. | ||||||||||||||||||||||||||||||||||||||||
| .DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||
| Checks if your PowerShell profile script is importing posh-git and if it does, | ||||||||||||||||||||||||||||||||||||||||
| removes the command to import the posh-git module. This will cause PowerShell | ||||||||||||||||||||||||||||||||||||||||
| to no longer load posh-git whenever PowerShell starts. | ||||||||||||||||||||||||||||||||||||||||
| .PARAMETER AllHosts | ||||||||||||||||||||||||||||||||||||||||
| By default, this command modifies the CurrentUserCurrentHost profile | ||||||||||||||||||||||||||||||||||||||||
| script. By specifying the AllHosts switch, the command updates the | ||||||||||||||||||||||||||||||||||||||||
| 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. | ||||||||||||||||||||||||||||||||||||||||
| .EXAMPLE | ||||||||||||||||||||||||||||||||||||||||
| PS C:\> Remove-PoshGitFromProfile | ||||||||||||||||||||||||||||||||||||||||
| Updates your profile script for the current PowerShell host to stop importing | ||||||||||||||||||||||||||||||||||||||||
| the posh-git module when the current PowerShell host starts. | ||||||||||||||||||||||||||||||||||||||||
| .EXAMPLE | ||||||||||||||||||||||||||||||||||||||||
| PS C:\> Remove-PoshGitFromProfile -AllHosts | ||||||||||||||||||||||||||||||||||||||||
| Updates your profile script for all PowerShell hosts to no longer import the | ||||||||||||||||||||||||||||||||||||||||
| posh-git module whenever any PowerShell host starts. | ||||||||||||||||||||||||||||||||||||||||
| .INPUTS | ||||||||||||||||||||||||||||||||||||||||
| None. | ||||||||||||||||||||||||||||||||||||||||
| .OUTPUTS | ||||||||||||||||||||||||||||||||||||||||
| None. | ||||||||||||||||||||||||||||||||||||||||
| #> | ||||||||||||||||||||||||||||||||||||||||
| function Remove-PoshGitFromProfile { | ||||||||||||||||||||||||||||||||||||||||
| [CmdletBinding(SupportsShouldProcess)] | ||||||||||||||||||||||||||||||||||||||||
| param( | ||||||||||||||||||||||||||||||||||||||||
| [Parameter()] | ||||||||||||||||||||||||||||||||||||||||
| [switch] | ||||||||||||||||||||||||||||||||||||||||
| $AllHosts, | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| [Parameter()] | ||||||||||||||||||||||||||||||||||||||||
| [switch] | ||||||||||||||||||||||||||||||||||||||||
| $AllUsers, | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| [Parameter(ValueFromRemainingArguments)] | ||||||||||||||||||||||||||||||||||||||||
| [psobject[]] | ||||||||||||||||||||||||||||||||||||||||
| $TestParams | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if ($AllUsers -and !(Test-Administrator)) { | ||||||||||||||||||||||||||||||||||||||||
| throw 'Removing posh-git from an AllUsers profile requires an elevated host.' | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| $underTest = $false | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| $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])) { | ||||||||||||||||||||||||||||||||||||||||
| $profilePath = [string]$TestParams[0] | ||||||||||||||||||||||||||||||||||||||||
| $underTest = $true | ||||||||||||||||||||||||||||||||||||||||
| if ($TestParams.Count -gt 1) { | ||||||||||||||||||||||||||||||||||||||||
| $ModuleBasePath = [string]$TestParams[1] | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (!$profilePath) { $profilePath = $PROFILE } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (!$profilePath) { | ||||||||||||||||||||||||||||||||||||||||
| Write-Warning "Skipping add of posh-git import to profile; no profile found." | ||||||||||||||||||||||||||||||||||||||||
| Write-Verbose "`$PROFILE = '$PROFILE'" | ||||||||||||||||||||||||||||||||||||||||
| Write-Verbose "CurrentUserCurrentHost = '$($PROFILE.CurrentUserCurrentHost)'" | ||||||||||||||||||||||||||||||||||||||||
| Write-Verbose "CurrentUserAllHosts = '$($PROFILE.CurrentUserAllHosts)'" | ||||||||||||||||||||||||||||||||||||||||
| Write-Verbose "AllUsersCurrentHost = '$($PROFILE.AllUsersCurrentHost)'" | ||||||||||||||||||||||||||||||||||||||||
| Write-Verbose "AllUsersAllHosts = '$($PROFILE.AllUsersAllHosts)'" | ||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (Test-Path -LiteralPath $profilePath) { | ||||||||||||||||||||||||||||||||||||||||
| # If the profile script exists and is signed, then we should not modify it | ||||||||||||||||||||||||||||||||||||||||
| if (!(Get-Command Get-AuthenticodeSignature -ErrorAction SilentlyContinue)) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| Write-Verbose "Platform doesn't support script signing, skipping test for signed profile." | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| else { | ||||||||||||||||||||||||||||||||||||||||
| $sig = Get-AuthenticodeSignature $profilePath | ||||||||||||||||||||||||||||||||||||||||
| if ($null -ne $sig.SignerCertificate) { | ||||||||||||||||||||||||||||||||||||||||
| 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." | ||||||||||||||||||||||||||||||||||||||||
|
dahlbyk marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| $oldProfile = @(Get-Content $profilePath) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| . $currentVersionPath\src\Utils.ps1 | ||||||||||||||||||||||||||||||||||||||||
| $oldProfileEncoding = Get-FileEncoding $profilePath | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| $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 *\posh-git.psd1*') { | ||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| $newProfile += $line | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| Set-Content -path $profilePath -value $newProfile -Force -Encoding $oldProfileEncoding | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+314
to
+330
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noting this is pulled from: posh-git/chocolatey/tools/chocolateyUninstall.ps1 Lines 7 to 25 in dc2b636
Will be nice to use this function there. |
||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| <# | ||||||||||||||||||||||||||||||||||||||||
| .SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||
| Gets the file encoding of the specified file. | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.