-
-
Notifications
You must be signed in to change notification settings - Fork 803
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
Implement Add-PoshGitToProfile command #361
Implement Add-PoshGitToProfile command #361
Conversation
Offer to update their current/all host profile if posh-git not detected in their profile scripts. Note: we do not offer to update the "all users" profile scripts because that requires elevation and probably not worth it. Add Pester tests for the new Utils.ps1 functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking great!
# Doesn't exist, so create it | ||
New-Item -Path $profilePath -ItemType File | ||
$profileContent = @() | ||
$encoding = 'ascii' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's go with utf8
here to make sure we can handle paths with non-ASCII characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check.
|
||
# Check if the location of this module file is in the PSModulePath | ||
if (Test-InModulePath $ScriptRoot) { | ||
$profileContent += "Import-Module posh-git" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we instead check Get-Module posh-git -ListAvailable
to see if it finds this particular instance of the module? Or check that it will find any posh-git module and consider that good enough?
I'm almost tempted to just skip straight to always hard-coding the full module path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would avoid hard-coding the module path as a "bad practice" for PowerShell modules. I can switch to using Get-Module -ListAvailable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doh, that makes the Pester test a little harder. Oh well, hopefully not too much harder. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK this is making the Pester tests a lot harder. For some reason, Mock Get-Module is not working. Will look at it more tomorrow.
@@ -1,5 +1,8 @@ | |||
param([switch]$WhatIf = $false) | |||
|
|||
# Dot source for Get-FileEncoding | |||
. $PSScriptRoot\Utils.ps1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$PSScriptRoot
not supported in v2, right? Move $installDir
here from below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought I caught that. We can just use Split-Path $MyInvocation.MyCommand.Path -Parent instead.
@@ -92,6 +92,57 @@ if (!$currentPromptDef -or ($currentPromptDef -eq $defaultPromptDef)) { | |||
Set-Item Function:\prompt -Value $poshGitPromptScriptBlock | |||
} | |||
|
|||
# If running interactive, check if user want's `import-module posh-git` added to their profile | |||
if (!$NoProfileCheck -and ($MyInvocation.ScriptName.Length -eq 0)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the Length
check more idiomatic than !$MyInvocation.ScriptName
?
Also, s/want's/wants above.
@($content)[0] | Should BeExactly "Import-Module '$scriptRoot\posh-git.psd1'" | ||
} | ||
finally { | ||
Remove-Item $profilePath -ErrorAction SilentlyContinue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BeforeEach {
$profilePath = [System.IO.Path]::GetTempFileName()
}
AfterEach {
Remove-Item $profilePath -ErrorAction SilentlyContinue
}
❓
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, I started with that. Then wrote a few tests that didn't need $profilePath and removed Before/AfterEach. Then I moved those tests to a different Context. Fixed.
Remove $PSScriptRoot from install.ps1.
@@ -1,5 +1,9 @@ | |||
param([switch]$WhatIf = $false) | |||
|
|||
# Dot source for Get-FileEncoding | |||
$scriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have $installDir = Split-Path $MyInvocation.MyCommand.Path -Parent
on L22. Rename this one and delete that line? (Or keep this name and fix uses of $installDir
.)
Minor nitpick, otherwise revisions look good. Just need to fix up how we detect if we should |
However to get the Pester tests to work I had to put the Get-Module -List command in its own function and I mock that function. Mocking Get-Module was resulting in some really weird bug about a write error trying to set PSEdition.
Is this ready for a final review? (Go ahead and remove WIP from title if so.) |
Yes. Ready for final review. |
Hmm, hold off on this a bit. Joel says ScriptName is empty in implicit loading scenarios and he's right. Let's see if we can use Get-PSCallstack instead. |
Committing this so I can test it on some other systems - via remoting.
I'm becoming quite nervous about this PR. Using
And when the PowerShell console profile runs, the stack depth is three:
But then I started to notice that the PowerShell in VSCode on my machine keep stalling during init. And of course it was stalling because of the query to modify the import. Sigh... There is something about the PowerShell extension custom host that results in a stack depth one less than expected when processing profiles e.g.:
Another issue is that if posh-git hasn't been imported yet and you execute one of its commands, it will be auto-imported. That will cause the Modify Profile prompt to appear. That could be confusing. How about we rejigger this work into a command like Install-Module posh-git -Scope User
Add-PoshGitToProfile Otherwise I'm worried there will be some unanticipated scenario (other custom hosts, remoting, etc) that will result in posh-git stalling the script waiting for input. |
Looking that the Docker instructions for getting going with the posh-docker PowerShell module see the section Install-Module -Scope CurrentUser posh-docker -Force
Add-Content $PROFILE "`nImport-Module posh-docker" I like the simplicity. They could have also mentioned that if you want posh-docker loaded in all hosts, then use: Install-Module -Scope CurrentUser posh-docker -Force
Add-Content $PROFILE.CurrentUserAllHosts "`nImport-Module posh-docker" And Add-Content seems to preserve the original file's encoding. |
If you're nervous, I'm nervous. Let's skip the magic. I appreciate the simplicity of Docker's approach, but wouldn't mind being a bit more helpful. How about:
And specifying We'll still need to handle encoding (we don't today) in Chocolatey install/uninstall, but |
Agreed. Everything else sounds good as well. I'm starting on it. |
Now there is an Add-PoshGitToProfile command. I have a couple of Pester tests but I need to add a few more.
Not reliable on PS v2. Add tests.
I'm ready to merge this. @dahlbyk Do you have time to review this? |
Offer to update their current/all host profile if posh-git not detected in their profile scripts. Note: we do not offer to update the "all users" profile scripts because that requires elevation and probably not worth it.
Add Pester tests for the new Utils.ps1 functions.