Skip to content

Commit

Permalink
Merge pull request #376 from dahlbyk/rkeithhill/migrate-core-files-to…
Browse files Browse the repository at this point in the history
…-src-dir

Simplify install.ps1 and move core module files under src/
  • Loading branch information
rkeithhill authored Jan 26, 2017
2 parents 8851db9 + fa7941b commit a77ae39
Show file tree
Hide file tree
Showing 17 changed files with 26 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# To use this file you must have EditorConfig installed for your editor.
# For Visual Studio Code, install the extension: "EditorConfig for VS Code"
# https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig
root = true

[*]
Expand Down
5 changes: 0 additions & 5 deletions chocolatey/tools/chocolateyInstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ try {
Set-Content -path $profile -value $newProfile -Force
}

$subfolder = get-childitem $poshgitPath -recurse -include 'dahlbyk-posh-git-*' | select -First 1
write-debug "Found and using folder `'$subfolder`'"
$installer = Join-Path $subfolder 'install.ps1'
& $installer

$newProfile = @(Get-Content $PROFILE)
Insert-Script ([REF]$newProfile) "Rename-Item Function:\Prompt PoshGitPrompt -Force"

Expand Down
40 changes: 2 additions & 38 deletions install.ps1
Original file line number Diff line number Diff line change
@@ -1,42 +1,6 @@
param([switch]$WhatIf = $false)

# Dot source for Get-FileEncoding
$installDir = Split-Path $MyInvocation.MyCommand.Path -Parent
. $installDir\Utils.ps1

if($PSVersionTable.PSVersion.Major -lt 2) {
Write-Warning "posh-git requires PowerShell 2.0 or better; you have version $($Host.Version)."
return
}

if(!(Test-Path $PROFILE)) {
Write-Host "Creating PowerShell profile...`n$PROFILE"
New-Item $PROFILE -Force -Type File -ErrorAction Stop -WhatIf:$WhatIf > $null
}

if(!(Get-Command git -ErrorAction SilentlyContinue)) {
Write-Warning 'Could not find git command. Please create a git alias or add %ProgramFiles%\Git\cmd to PATH.'
return
}

if(!(. (Join-Path $installDir "CheckVersion.ps1"))) {
return
}

$profileLine = ". '$installDir\profile.example.ps1'"
if(Select-String -Path $PROFILE -Pattern $profileLine -Quiet -SimpleMatch) {
Write-Host "It seems posh-git is already installed..."
return
}

Write-Host "Adding posh-git to profile..."
@"
# Load posh-git example profile
$profileLine
"@ | Out-File $PROFILE -Append -WhatIf:$WhatIf -Encoding (Get-FileEncoding $PROFILE)

Write-Host 'posh-git sucessfully installed!'
Write-Host 'Please reload your profile for the changes to take effect:'
Write-Host ' . $PROFILE'
Import-Module $installDir\src\posh-git.psd1
Add-PoshGitToProfile -WhatIf:$WhatIf
4 changes: 3 additions & 1 deletion profile.example.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $poshGitModule = Get-Module posh-git -ListAvailable | Sort-Object Version -Desce
if ($poshGitModule) {
$poshGitModule | Import-Module
}
elseif (Test-Path -LiteralPath ($modulePath = Join-Path (Split-Path $MyInvocation.MyCommand.Path -Parent) 'posh-git.psd1')) {
elseif (Test-Path -LiteralPath ($modulePath = Join-Path (Split-Path $MyInvocation.MyCommand.Path -Parent) (Join-Path src 'posh-git.psd1'))) {
Import-Module $modulePath
}
else {
Expand All @@ -16,3 +16,5 @@ else {
# $Global:GitPromptSettings.BranchBehindAndAheadDisplay = "Compact"

Start-SshAgent -Quiet

Write-Warning "posh-git's profile.example.ps1 will be removed in a future version. To avoid a change in behavior, copy its contents into your $PROFILE."
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 3 additions & 4 deletions Utils.ps1 → src/Utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ function Invoke-Utf8ConsoleCommand([ScriptBlock]$cmd) {
Checks if your PowerShell profile script is not already importing posh-git
and if not, adds a command to import the posh-git module. This will cause
PowerShell to load posh-git whenever PowerShell starts.
imprt
.PARAMETER AllHosts
By default, this command modifies the CurrentUserCurrentHost profile
script. By specifying the AllHosts switch, the command updates the
Expand All @@ -79,7 +78,7 @@ function Invoke-Utf8ConsoleCommand([ScriptBlock]$cmd) {
.OUTPUTS
None.
#>
function Add-PoshGitToProfile([switch]$AllHosts, [switch]$Force) {
function Add-PoshGitToProfile([switch]$AllHosts, [switch]$Force, [switch]$WhatIf) {
$underTest = $false

$profilePath = if ($AllHosts) { $PROFILE.CurrentUserAllHosts } else { $PROFILE.CurrentUserCurrentHost }
Expand Down Expand Up @@ -126,7 +125,7 @@ function Add-PoshGitToProfile([switch]$AllHosts, [switch]$Force) {
$profileContent = "`nImport-Module '$ModuleBasePath\posh-git.psd1'"
}

Add-Content -LiteralPath $profilePath -Value $profileContent -Encoding UTF8
Add-Content -LiteralPath $profilePath -Value $profileContent -Encoding UTF8 -WhatIf:$WhatIf
}

<#
Expand Down Expand Up @@ -215,7 +214,7 @@ function Test-PoshGitImportedInScript {
return $false
}

@((Get-Content $Path -ErrorAction SilentlyContinue) -match 'posh-git').Count -gt 0
(@(Get-Content $Path -ErrorAction SilentlyContinue) -match 'posh-git').Count -gt 0
}

function dbg($Message, [Diagnostics.Stopwatch]$Stopwatch) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/Get-GitStatus.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# For info on Pester mocking see - http://www.powershellmagazine.com/2014/09/30/pester-mock-and-testdrive/
. $PSScriptRoot\Shared.ps1

Describe 'Get-GitStatus Tests' {
Context 'Get-GitStatus Working Directory Tests' {
Expand Down
5 changes: 1 addition & 4 deletions test/ModuleManifest.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
. $PSScriptRoot\Shared.ps1

Describe 'Module Manifest Tests' {
BeforeAll {
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$moduleManifestPath = "$PSScriptRoot\..\posh-git.psd1"
}
It 'Passes Test-ModuleManifest' {
Test-ModuleManifest -Path $moduleManifestPath | Should Not BeNullOrEmpty
$? | Should Be $true
Expand Down
4 changes: 3 additions & 1 deletion test/Shared.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
$module = Import-Module $PSScriptRoot\..\posh-git.psd1 -PassThru
$modulePath = Convert-Path $PSScriptRoot\..\src
$moduleManifestPath = "$modulePath\posh-git.psd1"
$module = Import-Module $moduleManifestPath -PassThru

function MakeNativePath([string]$Path) {
$Path -replace '\\|/', [System.IO.Path]::DirectorySeparatorChar
Expand Down
12 changes: 10 additions & 2 deletions test/Utils.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
. $PSScriptRoot\..\Utils.ps1
. $PSScriptRoot\Shared.ps1
. $modulePath\Utils.ps1

Describe 'Utils Function Tests' {
Context 'Add-PoshGitToProfile Tests' {
Expand All @@ -12,6 +13,9 @@ Describe 'Utils Function Tests' {
Remove-Item $profilePath -ErrorAction SilentlyContinue
}
It 'Creates profile file if it does not exist that imports absolute path' {
Mock Get-PSModulePath {
return @()
}
Remove-Item -LiteralPath $profilePath
Test-Path -LiteralPath $profilePath | Should Be $false

Expand All @@ -21,7 +25,6 @@ Describe 'Utils Function Tests' {
Get-FileEncoding $profilePath | Should Be 'utf8'
$content = Get-Content $profilePath
$content.Count | Should Be 2
$modulePath = Resolve-Path $PSScriptRoot\..
@($content)[1] | Should BeExactly "Import-Module '$modulePath\posh-git.psd1'"
}
It 'Creates profile file if it does not exist that imports from module path' {
Expand Down Expand Up @@ -96,6 +99,11 @@ New-Alias pscore C:\Users\Keith\GitHub\rkeithhill\PowerShell\src\powershell-win-
Set-Content $profilePath -Value $profileContent -Encoding Unicode
Test-PoshGitImportedInScript $profilePath | Should Be $true
}
It 'Returns false when one-line profile script does not import posh-git' {
$profileContent = "# Test"
Set-Content $profilePath -Value $profileContent -Encoding Unicode
Test-PoshGitImportedInScript $profilePath | Should Be $false
}
It 'Returns false when profile script does not import posh-git' {
$profileContent = "Import-Module Pscx`nImport-Module platyPS`nImport-Module Plaster"
Set-Content $profilePath -Value $profileContent -Encoding Unicode
Expand Down

0 comments on commit a77ae39

Please sign in to comment.