Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/onPush.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ jobs:
. ./cd-extras/public/_Classes.ps1
md ./_reports
Import-Module Pester
Invoke-Pester ./tests/cd-extras.Tests.ps1 -EnableExit -CodeCoverage ./cd-extras/private/*.ps1,./cd-extras/public/*.ps1 -CodeCoverageOutputFile ./_reports/coverage.xml -OutputFile ./_reports/testresults.xml
cd tests
Invoke-Pester ./cd-extras.Tests.ps1 -EnableExit -CodeCoverage ../cd-extras/private/*.ps1,../cd-extras/public/*.ps1 -CodeCoverageOutputFile ../_reports/coverage.xml -OutputFile ../_reports/testresults.xml
shell: pwsh

# run the tests again on WIndows, this time in PowerShell v5
# run the tests again on Windows, this time in PowerShell v5
- name: Run the tests (Powershell v5)
if: matrix.os == 'windows-latest'
run: |
Expand Down
17 changes: 17 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Publish to PowerShell Gallery

on: workflow_dispatch

jobs:
publish:
name: Publish to PSGallery
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Publish
env:
PSNugetKey: ${{ secrets.PSNugetKey }}
run: ./publishme.ps1 -Confirm:$false
shell: pwsh
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ _reports/
ps_modules
out/
cd-extras/about_Cd-Extras.help.txt
coverage.xml
5 changes: 4 additions & 1 deletion PSScriptAnalyzerSettings.psd1
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
@{
ExcludeRules = @('PSAvoidUsingCmdletAliases')
ExcludeRules = @(
'PSAvoidUsingCmdletAliases',
'PSAvoidGlobalVars',
'PSUseShouldProcessForStateChangingFunctions')
}
5 changes: 3 additions & 2 deletions cd-extras/cd-extras.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@{
RootModule = 'cd-extras.psm1'
ModuleVersion = '2.9.4'
ModuleVersion = '3.0.0'
GUID = '206fccbd-dc96-4b23-908c-5ac821372e16'

Author = 'Nick Cox'
Expand All @@ -15,7 +15,8 @@

PrivateData = @{
PSData = @{
ReleaseNotes = 'Fix an issue where UNC paths not expanded properly.'
Prerelease = 'beta2'
ReleaseNotes = 'Adds Set-Recent and Set-Frecent functionality'
Tags = @('cd+', 'cd-', 'AUTO_CD', 'CD_PATH', 'CDABLE_VARS', 'bash', 'zsh')
LicenseUri = 'https://github.com/nickcox/cd-extras/blob/master/LICENSE'
ProjectUri = 'https://github.com/nickcox/cd-extras'
Expand Down
22 changes: 11 additions & 11 deletions cd-extras/cd-extras.psm1
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
$cdAlias = if ($x = (Get-Alias -Name 'cd' -ErrorAction ignore)) { $x.Definition }

Get-ChildItem $PSScriptRoot/private/*.ps1 | % { . $_.FullName }
Get-ChildItem $PSScriptRoot/public/*.ps1 | % { . $_.FullName }
Get-ChildItem -File -Filter *.ps1 $PSScriptRoot/private, $PSScriptRoot/public | % {
. $_.FullName
}

# remove stupid phantom module
Get-Module | ? Path -eq ("$PSScriptRoot/public/_Classes.ps1" | Resolve-Path) | Remove-Module
Get-Module | Where Path -eq ("$PSScriptRoot/public/_Classes.ps1" | Resolve-Path) | Remove-Module

$global:cde = if ((Test-Path variable:cde) -and $cde -is [System.Collections.IDictionary]) {
New-Object -Type CdeOptions -Property $global:cde
}
else {
New-Object -Type CdeOptions
}
$global:cde = [CdeOptions]::new()
(Get-Variable cde).Attributes.Add([ValidateScript]::new( { Set-CdExtrasOption -Validate } ))

# some set up happens in Set-Option so make sure to call it here
Set-CdExtrasOption -Option 'AUTO_CD' -Value $global:cde.AUTO_CD
RegisterCompletions @('Step-Up') 'n' { CompleteAncestors @args }
RegisterCompletions @('Undo-Location', 'Redo-Location') 'n' { CompleteStack @args }
RegisterCompletions @('Set-RecentLocation') 'Terms' { CompleteRecent @args }
RegisterCompletions @('Set-FrecentLocation') 'Terms' { CompleteFrecent @args }

$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
if ($background) { $background.Dispose() }
$ExecutionContext.SessionState.InvokeCommand.CommandNotFoundAction = $null
Set-Item Alias:cd $cdAlias
Remove-Variable cde -Scope Global -ErrorAction Ignore
Expand Down
7 changes: 2 additions & 5 deletions cd-extras/private/CommandNotFound.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ function CommandNotFound($actions, $isUnderTest) {
if ($CommandLookupEventArgs.CommandOrigin -ne 'Runspace' -and !(&$isUnderTest)) { return }
$invocation = if ($isUnderTest) { $CommandName } else { $MyInvocation.Line }

# don't run as part of pipeline
if ($invocation -match "$([regex]::Escape($CommandName))\s*\|") { return }

# don't run if no word characters given
if ($invocation -notmatch '\w|^\.{3,}$') { return }
# only match stuff that looks AUTO_CDish
if ($invocation -notmatch '^[\w~/\\\.]*$|^\.{3,}$') { return }

$actions | % { &$_ $CommandName $CommandLookupEventArgs }
}.GetNewClosure()
Expand Down
12 changes: 5 additions & 7 deletions cd-extras/private/CompleteAncestors.ps1
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
function CompleteAncestors {
param($commandName, $parameterName, $wordToComplete)
$ups = Get-Ancestors
if (!$ups) { return }

$valueToMatch = $wordToComplete | RemoveSurroundingQuotes
$normalised = $valueToMatch | NormaliseAndEscape

if (!$ups) { return }

$ups | where Path -eq $valueToMatch |
DefaultIfEmpty { $ups | where Name -match $normalised } |
DefaultIfEmpty { $ups | where Path -match $normalised } |
IndexedComplete |
DefaultIfEmpty { $null }
$ups | Where Path -eq $valueToMatch |
DefaultIfEmpty { $ups | Where Name -match $normalised } |
DefaultIfEmpty { $ups | Where Path -match $normalised } |
IndexedComplete
}
11 changes: 11 additions & 0 deletions cd-extras/private/CompleteFrecent.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function CompleteFrecent {
param($commandName, $parameterName, $wordToComplete)

$recents = Get-FrecentLocation $wordToComplete

if (!$recents) { return }

@($recents) | Where Path -match ($wordToComplete | RemoveSurroundingQuotes | RemoveTrailingSeparator | Escape) |
IndexedComplete -indexed $false |
DefaultIfEmpty { $null }
}
20 changes: 7 additions & 13 deletions cd-extras/private/CompletePaths.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,10 @@ function CompletePaths {
Process {
$fullPath = $_ | Convert-Path

$completionText = if ($wordToComplete -match '^\.{1,2}$') {
$wordToComplete
}
elseif (!($wordToComplete | IsRooted) -and ($_ | Resolve-Path -Relative | IsDescendedFrom ..)) {
$_ | Resolve-Path -Relative
}
else {
$fullPath -replace "^$($HOME | NormaliseAndEscape)", "~"
}
$completionText =
if ($wordToComplete -in '.', '..') { $wordToComplete }
elseif (!($wordToComplete | IsRooted) -and ($relative = $_ | Resolve-Path -Relative) -and ($relative | IsDescendedFrom ..)) { $relative }
else { $fullPath -replace "^$($HOME | NormaliseAndEscape)", "~" }

# add normalised trailing directory separator; quote if contains spaces
$trailChar = if ($_.PSIsContainer) { ${/} }
Expand All @@ -62,7 +57,7 @@ function CompletePaths {
if ($n -le 1) { $_ } else { "$_ ($n)" }
}

$tooltip = if ($cde.ToolTip) { &$cde.ToolTip $_ $isListTruncated } else { $_ }
$tooltip = if ($cde.ToolTip -and ($tooltip = &$cde.ToolTip $_ $isListTruncated)) { $tooltip } else { $_ }

[Management.Automation.CompletionResult]::new(
$completionText,
Expand Down Expand Up @@ -110,18 +105,17 @@ function CompletePaths {
$cde.CDABLE_VARS -and
$completions.Length -lt $maxCompletions -and
$wordToComplete -match '[^/\\]+' -and # separate variable from slashes before or after it
($maybeVar = Get-Variable "$($Matches[0])*" -ValueOnly | where { Test-Path $_ -PathType Container })
($maybeVar = Get-Variable "$($Matches[0])*" -ValueOnly | Where { Test-Path $_ -PathType Container })
) {
Expand-Path @switches ($wordToExpand -replace $Matches[0], $maybeVar)
}

$allCompletions = @($completions) + @($variableCompletions) | ? { $_ }
$allCompletions = (@($completions) + @($variableCompletions)).Where{$_} | select -Unique
$isListTruncated = if ($allCompletions.Length -gt $maxCompletions) { $true }

if (!$allCompletions) { return }

$allCompletions |
Select -Unique |
Sort-Object { !$_.PSIsContainer, $_.PSChildName } |
Select -First $maxCompletions |
CompletionResult $isListTruncated
Expand Down
11 changes: 11 additions & 0 deletions cd-extras/private/CompleteRecent.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function CompleteRecent {
param($commandName, $parameterName, $wordToComplete)

$recents = Get-RecentLocation $wordToComplete

if (!$recents) { return }

@($recents) | Where Path -match ($wordToComplete | RemoveSurroundingQuotes | RemoveTrailingSeparator | Escape) |
IndexedComplete $false |
DefaultIfEmpty { $null }
}
9 changes: 3 additions & 6 deletions cd-extras/private/CompleteStack.ps1
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
function CompleteStack {
param($commandName, $parameterName, $wordToComplete, $commandAst, $boundParameters)
param($commandName, $parameterName, $wordToComplete)

$stack = if (
$commandName -and
$commandName -match 'Redo' -or
(
$aliased = (Get-Alias $commandName -ea Ignore).ResolvedCommandName -and
$aliased -match 'Redo'
)
($aliased = (Get-Alias $commandName -ea Ignore).ResolvedCommandName -and $aliased -match 'Redo')
) { (Get-Stack -Redo) }
else { (Get-Stack -Undo) }

if (!$stack) { return }

@($stack) | Where Path -match ($wordToComplete | RemoveSurroundingQuotes | RemoveTrailingSeparator | Escape) |
IndexedComplete |
Select -First $cde.MaxRecentCompletions |
DefaultIfEmpty { $null }
}
Loading