-
-
Notifications
You must be signed in to change notification settings - Fork 801
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
During install create the profile dir if it does not already exist #449
Conversation
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.
Just a few minor issues to fix but otherwise looks good.
src/Utils.ps1
Outdated
@@ -176,6 +176,14 @@ function Add-PoshGitToProfile { | |||
$profileContent = "`nImport-Module '$ModuleBasePath\posh-git.psd1'" | |||
} | |||
|
|||
# Make sure the profile path exists | |||
$profileDir = Split-Path -Parent $profilePath | |||
if (-not (Test-Path -LiteralPath $profileDir)) { |
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.
In the code base we tend not to use -not
in favor of !
. We're not 100% there yet but I don't want to introduce more script that needs to be changed.
src/Utils.ps1
Outdated
$profileDir = Split-Path -Parent $profilePath | ||
if (-not (Test-Path -LiteralPath $profileDir)) { | ||
if ($PSCmdlet.ShouldProcess($profileDir, "Create current user Powershell profile directory")) { | ||
New-Item $profileDir -Type Directory |
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.
Two things. Better to use the actual parameter name ItemType
than the alias Type
. Also, this command will output the created directory. Better to just redirect the output to $null. Then add support for -verbose e.g.:
New-Item $profileDir -ItemType Directory -Verbose:$VerbosePreference > $null
src/Utils.ps1
Outdated
# Make sure the profile path exists | ||
$profileDir = Split-Path -Parent $profilePath | ||
if (-not (Test-Path -LiteralPath $profileDir)) { | ||
if ($PSCmdlet.ShouldProcess($profileDir, "Create current user Powershell profile directory")) { |
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.
PowerShell is properly spelled with a capital S
- PowerShell
.
Great callouts! Thanks. Amended the suggested changes and retested. (Both with $VerbosePreference = "SilentlyContinue" and "Continue"). Tests looked good. |
src/Utils.ps1
Outdated
@@ -176,6 +176,14 @@ function Add-PoshGitToProfile { | |||
$profileContent = "`nImport-Module '$ModuleBasePath\posh-git.psd1'" | |||
} | |||
|
|||
# Make sure the profile path exists | |||
$profileDir = Split-Path -Parent $profilePath |
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.
Sorry missed one other minor issue (which I myself forget from time-to-time). Can you switch the order on params here and use -LiteralPath
e.g. $profileDir = Split-Path -LiteralPath $profilePath -Parent
. Thanks.
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.
No problem, I moved the -Parent parameter to the end.
I also looked at using -LiteralPath with Split-Path, but it does not have an explicit -LiteralPath parameter, however based on the below test it looks like it already implicitly treats the path literally.
C:\test[1]> Get-Item -LiteralPath c:\test[1]\File[1].txt
Directory: C:\test[1]
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 3/2/2017 3:18 AM 0 File[1].txt
C:\test[1]> Get-Item -Path c:\test[1]\File[1].txt
C:\test[1]> Split-Path -Path c:\test[1]\File[1].txt -Parent
c:\test[1]
I created a directory and file with names that both need to be interpreted literally.
-
Get-Item with -LiteralPath works fine
-
Get-Item with -Path fails to retrieve the file
-
Split-Path with -Path however still correctly parses the path to retrieve the parent
So, I think Split-Path is good with just -Path. Looking through the other instances of Split-Path in the code, I figured I would keep it consistent and not explicitly state -Path, but let me know if you want me to change it. It's no problem.
You should also be able to call |
LGTM, but care to add a test for this scenario? |
@tolgabalci OK for the ParentSet parameter set, there is no -LiteralPath parameter. Looks good. I'm going to merge this and I'll add a test later tonight. If you're really inspired to add the test, you can always open a separate PR. |
It just dawned on me that there's a much simpler fix for this: |
Do you think it is worth changing this? I'm not sure that it is. |
Just a little less path-splitting logic to maintain. And thinking through edge cases, it's technically possible (though unlikely with defaults) that more than |
@rkeithhill Thank you for adding the test and showing me how to do it! @dahlbyk New-Item will actually create the full parent directory tree without -Force when used with -ItemType Directory. I do not think -Force is really intended for use when creating directories. I was trying it out, it does have the interesting side effect of suppressing errors when one of the directories in the directory tree being created is a file and it fails to create the path. @rkeithhill @dahlbyk, thank you so much for being so responsive! |
Interesting… I suppose the nested directory variations are all things we could test for, if you are interested in exploring publicly. 😁 |
While installing posh-git using install.ps1 on Windows 7 Enterprise SP1 laptops for users running Powershell 4.0 or 5.0 had some users receive this error:
Nothing had previously been added to the user's Profile scripts AND the folder "WindowsPowerShell" did not exists under the user's documents. This caused Add-Content to fail.
The pull request is to create the Powershell profile directory for the current user if it does not already exists.
Tested both with and without -WhatIf and it works good.