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

During install create the profile dir if it does not already exist #449

Merged
merged 1 commit into from
Mar 2, 2017
Merged

During install create the profile dir if it does not already exist #449

merged 1 commit into from
Mar 2, 2017

Conversation

tolgabalci
Copy link
Contributor

@tolgabalci tolgabalci commented Mar 2, 2017

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:

PS C:\tools\posh-git> .\install.ps1
Add-Content : Could not find a part of the path
'C:\Users\*username*\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'.
At C:\tools\posh-git\src\Utils.ps1:180 char:9
+         Add-Content -LiteralPath $profilePath -Value $profileContent -Encoding U ...

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.

@rkeithhill rkeithhill self-assigned this Mar 2, 2017
@rkeithhill rkeithhill added this to the v0.7.1 milestone Mar 2, 2017
@rkeithhill rkeithhill removed their assignment Mar 2, 2017
Copy link
Collaborator

@rkeithhill rkeithhill left a 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)) {
Copy link
Collaborator

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
Copy link
Collaborator

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")) {
Copy link
Collaborator

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.

@tolgabalci
Copy link
Contributor Author

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
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

@rkeithhill
Copy link
Collaborator

You should also be able to call Add-PoshGitToProfile -Verbose and see the verbose output from New-Item. BTW thanks for the PR! See the one other issue I found - it's a quick tweak.

@dahlbyk
Copy link
Owner

dahlbyk commented Mar 2, 2017

LGTM, but care to add a test for this scenario?

@rkeithhill
Copy link
Collaborator

@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.

@rkeithhill rkeithhill merged commit a3db658 into dahlbyk:master Mar 2, 2017
@dahlbyk
Copy link
Owner

dahlbyk commented Mar 2, 2017

It just dawned on me that there's a much simpler fix for this: New-Item -Force will automatically create directories as necessary. This is what install.ps1 used to do.

@rkeithhill
Copy link
Collaborator

Do you think it is worth changing this? I'm not sure that it is.

@dahlbyk
Copy link
Owner

dahlbyk commented Mar 2, 2017

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 $PROFILE's immediate parent could be missing. Of course, you could work around this with -Force when creating the parent. ¯\_(ツ)_/¯

@tolgabalci
Copy link
Contributor Author

tolgabalci commented Mar 3, 2017

@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!

@dahlbyk
Copy link
Owner

dahlbyk commented Mar 3, 2017

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.

Interesting… I suppose the nested directory variations are all things we could test for, if you are interested in exploring publicly. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants