-
-
Notifications
You must be signed in to change notification settings - Fork 825
Install module to 'Program Files' and remove modifying $PROFILE #578
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
base: v0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| $ErrorActionPreference = 'Stop' | ||
|
|
||
| $moduleName = 'posh-git' # this could be different from package name | ||
| Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,35 @@ | ||
| try { | ||
| $poshgitPath = join-path (Get-ToolsLocation) 'poshgit' | ||
|
|
||
| try { | ||
| if (test-path($poshgitPath)) { | ||
| Write-Host "Attempting to remove existing `'$poshgitPath`'." | ||
| remove-item $poshgitPath -recurse -force | ||
| } | ||
| } catch { | ||
| Write-Host "Could not remove `'$poshgitPath`'" | ||
| } | ||
| $ErrorActionPreference = 'Stop' | ||
|
|
||
| $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 | ||
|
|
||
| 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 '. *posh-git*profile.example.ps1*') { | ||
| $line = ". '$currentVersionPath\profile.example.ps1' choco" | ||
| } | ||
| if($line -like 'Import-Module *\src\posh-git.psd1*') { | ||
| $line = "Import-Module '$currentVersionPath\src\posh-git.psd1'" | ||
| } | ||
| $newProfile += $line | ||
| } | ||
| Set-Content -path $profile -value $newProfile -Force -Encoding $oldProfileEncoding | ||
| } | ||
| $toolsDir = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)" | ||
| $moduleName = 'posh-git' # this may be different from the package name and different case | ||
|
|
||
| # module may already be installed outside of Chocolatey | ||
| Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue | ||
|
|
||
| $sourcePath = Join-Path -Path $toolsDir -ChildPath "$modulename\*" | ||
| $destPath = Join-Path -Path $env:ProgramFiles -ChildPath "WindowsPowerShell\Modules\$moduleName" | ||
|
|
||
| if ($PSVersionTable.PSVersion.Major -ge 5) { | ||
| $manifestFile = Join-Path -Path $toolsDir -ChildPath "$moduleName\$moduleName.psd1" | ||
| $manifest = Test-ModuleManifest -Path $manifestFile -WarningAction Ignore -ErrorAction Stop | ||
| $destPath = Join-Path -Path $destPath -ChildPath $manifest.Version.ToString() | ||
| } | ||
|
|
||
| Write-Verbose "Creating destination directory '$destPath' for module." | ||
| New-Item -Path $destPath -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null | ||
|
|
||
| Write-Verbose "Moving '$moduleName' files from '$sourcePath' to '$destPath'." | ||
| Move-Item -Path $sourcePath -Destination $destPath -Force | ||
|
|
||
| $installer = Join-Path $currentVersionPath 'install.ps1' | ||
| & $installer | ||
| } catch { | ||
| try { | ||
| if($oldProfile){ Set-Content -path $PROFILE -value $oldProfile -Force -Encoding $oldProfileEncoding } | ||
| } | ||
| catch {} | ||
| throw | ||
| if ($PSVersionTable.PSVersion.Major -lt 4) { | ||
| $modulePaths = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') -split ';' | ||
|
Collaborator
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. Can we assume no one will try to use Chocolatey to install this on Linux or macOS? If not, you should use
Author
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. For the time being we can. As per the next comment this is only for v0 so we don't need to worry about PS Core. |
||
| if ($modulePaths -notcontains $destPath) { | ||
| Write-Verbose "Adding '$destPath' to PSModulePath." | ||
| $newModulePath = @($destPath, $modulePaths) -join ';' | ||
|
|
||
| [Environment]::SetEnvironmentVariable('PSModulePath', $newModulePath, 'Machine') | ||
| $env:PSModulePath = $newModulePath | ||
| } | ||
| } | ||
|
|
||
| Write-Host "To add posh-git to your profile use 'Import-Module posh-git; Add-PoshGitToProfile'." | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,17 @@ | ||
| try { | ||
| $poshgitPath = join-path (Get-ToolsLocation) 'poshgit' | ||
| $ErrorActionPreference = 'Stop' | ||
|
|
||
| $currentVersionPath = Get-ChildItem "$poshgitPath\*posh-git*\" | Sort-Object -Property LastWriteTime | Select-Object -Last 1 | ||
| $moduleName = 'posh-git' | ||
| $sourcePath = Join-Path -Path $env:ProgramFiles -ChildPath "WindowsPowerShell\Modules\$moduleName" | ||
|
|
||
| if(Test-Path $PROFILE) { | ||
| $oldProfile = @(Get-Content $PROFILE) | ||
| Write-Verbose "Removing all version of '$moduleName' from '$sourcePath'." | ||
| Remove-Item -Path $sourcePath -Recurse -Force -ErrorAction SilentlyContinue | ||
|
|
||
| . $currentVersionPath\src\Utils.ps1 | ||
| $oldProfileEncoding = Get-FileEncoding $PROFILE | ||
| if ($PSVersionTable.PSVersion.Major -lt 4) { | ||
| $modulePaths = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') -split ';' | ||
|
|
||
| $newProfile = @() | ||
| foreach($line in $oldProfile) { | ||
| if ($line -like '*PoshGitPrompt*') { continue; } | ||
| if ($line -like '*Load posh-git example profile*') { continue; } | ||
| if ($line -like '*Start-SshAgent*') { 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 | ||
| } | ||
| Write-Verbose "Removing '$sourcePath' from PSModulePath." | ||
| $newModulePath = $modulePaths | Where-Object { $_ -ne $sourcePath } | ||
|
|
||
| [Environment]::SetEnvironmentVariable('PSModulePath', $newModulePath, 'Machine') | ||
| $env:PSModulePath = $newModulePath | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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.
A big part of the v1 release is to support PowerShell Core. If the user has PS Core installed, we should also install into
"PowerShell\Modules\$moduleName"if the$env:ProgramFiles\PowerShelldir exists.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 agree. I have been wondering what to do about whether you run Chocolatey under Core or not and where you should put the modules. However this is purely for the v0 branch so PS Core wasn't a consideration.