Skip to content

Commit

Permalink
Merge pull request #581 from flcdrg/Vsts-cli
Browse files Browse the repository at this point in the history
Initial support for vsts-cli
  • Loading branch information
dahlbyk authored May 23, 2018
2 parents f9f2b4b + e60cbb8 commit b428c3c
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 8 deletions.
24 changes: 24 additions & 0 deletions src/GitParamTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ $longGitParams = @{
whatchanged = 'since'
}

$shortVstsGlobal = 'h o'
$shortVstsParams = @{
abandon = "i $shortVstsGlobal"
create = "d i p r s t $shortVstsGlobal"
complete = "i $shortVstsGlobal"
list = "i p r s t $shortVstsGlobal"
reactivate = "i $shortVstsGlobal"
'set-vote' = "i $shortVstsGlobal"
show = "i $shortVstsGlobal"
update = "d i $shortVstsGlobal"
}

$longVstsGlobal = 'debug help output query verbose'
$longVstsParams = @{
abandon = "id detect instance $longVstsGlobal"
create = "auto-complete delete-source-branch work-items bypass-policy bypass-policy-reason description detect instance merge-commit-message open project repository reviewers source-branch squash target-branch title $longVstsGlobal"
complete = "id detect instance $longVstsGlobal"
list = " $longVstsGlobal"
reactivate = " $longVstsGlobal"
'set-vote' = " $longVstsGlobal"
show = " $longVstsGlobal"
update = " $longVstsGlobal"
}

# Variable is used in GitTabExpansion.ps1
$gitParamValues = @{
blame = @{
Expand Down
46 changes: 38 additions & 8 deletions src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

$Global:GitTabSettings = New-Object PSObject -Property @{
AllCommands = $false
KnownAliases = @{
'!f() { exec vsts code pr "$@"; }; f' = 'vsts.pr'
}
}

$subcommands = @{
bisect = "start bad good skip reset visualize replay log run"
notes = 'add append copy edit get-ref list merge prune remove show'
'vsts.pr' = 'create update show list complete abandon reactivate reviewers work-items set-vote policies'
reflog = "show delete expire"
remote = "
add rename remove set-head set-branches
Expand Down Expand Up @@ -44,7 +48,7 @@ $gitflowsubcommands = @{
}

function script:gitCmdOperations($commands, $command, $filter) {
$commands.$command.Trim() -split '\s+' | Where-Object { $_ -like "$filter*" }
$commands[$command].Trim() -split '\s+' | Where-Object { $_ -like "$filter*" }
}

$script:someCommands = @('add','am','annotate','archive','bisect','blame','branch','bundle','checkout','cherry',
Expand All @@ -56,6 +60,8 @@ $script:someCommands = @('add','am','annotate','archive','bisect','blame','branc
$script:gitCommandsWithLongParams = $longGitParams.Keys -join '|'
$script:gitCommandsWithShortParams = $shortGitParams.Keys -join '|'
$script:gitCommandsWithParamValues = $gitParamValues.Keys -join '|'
$script:vstsCommandsWithShortParams = $shortVstsParams.Keys -join '|'
$script:vstsCommandsWithLongParams = $longVstsParams.Keys -join '|'

try {
if ($null -ne (git help -a 2>&1 | Select-String flow)) {
Expand Down Expand Up @@ -210,30 +216,36 @@ function script:gitAliases($filter) {

function script:expandGitAlias($cmd, $rest) {
$alias = git config "alias.$cmd"

if ($alias) {
$known = $Global:GitTabSettings.KnownAliases[$alias]
if ($known) {
return "git $known$rest"
}

return "git $alias$rest"
}
else {
return "git $cmd$rest"
}
}

function script:expandLongParams($cmd, $filter) {
$longGitParams[$cmd] -split ' ' |
function script:expandLongParams($hash, $cmd, $filter) {
$hash[$cmd].Trim() -split ' ' |
Where-Object { $_ -like "$filter*" } |
Sort-Object |
ForEach-Object { -join ("--", $_) }
}

function script:expandShortParams($cmd, $filter) {
$shortGitParams[$cmd] -split ' ' |
function script:expandShortParams($hash, $cmd, $filter) {
$hash[$cmd].Trim() -split ' ' |
Where-Object { $_ -like "$filter*" } |
Sort-Object |
ForEach-Object { -join ("-", $_) }
}

function script:expandParamValues($cmd, $param, $filter) {
$gitParamValues[$cmd][$param] -split ' ' |
$gitParamValues[$cmd][$param].Trim() -split ' ' |
Where-Object { $_ -like "$filter*" } |
Sort-Object |
ForEach-Object { -join ("--", $param, "=", $_) }
Expand Down Expand Up @@ -400,13 +412,31 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) {

# Handles git <cmd> --<param>
"^(?<cmd>$gitCommandsWithLongParams).* --(?<param>\S*)$" {
expandLongParams $matches['cmd'] $matches['param']
expandLongParams $longGitParams $matches['cmd'] $matches['param']
}

# Handles git <cmd> -<shortparam>
"^(?<cmd>$gitCommandsWithShortParams).* -(?<shortparam>\S*)$" {
expandShortParams $matches['cmd'] $matches['shortparam']
expandShortParams $shortGitParams $matches['cmd'] $matches['shortparam']
}

# Handles git pr alias
"vsts\.pr\s+(?<op>\S*)$" {
gitCmdOperations $subcommands 'vsts.pr' $matches['op']
}

# Handles git pr <cmd> --<param>
"vsts\.pr\s+(?<cmd>$vstsCommandsWithLongParams).*--(?<param>\S*)$"
{
expandLongParams $longVstsParams $matches['cmd'] $matches['param']
}

# Handles git pr <cmd> -<shortparam>
"vsts\.pr\s+(?<cmd>$vstsCommandsWithShortParams).*-(?<shortparam>\S*)$"
{
expandShortParams $shortVstsParams $matches['cmd'] $matches['shortparam']
}

}
}

Expand Down
57 changes: 57 additions & 0 deletions test/GitParamTabExpansionVsts.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
. $PSScriptRoot\Shared.ps1

Describe 'ParamsTabExpansion VSTS Tests' {
Context 'Push Parameters TabExpansion Tests' {
# Create a git alias for 'pr', as if we'd installed vsts-cli
BeforeEach {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoPath = NewGitTempRepo

# Test with non-standard vsts pr alias name
&$gitbin config alias.test-vsts-pr "!f() { exec vsts code pr \`"`$`@\`"; }; f"
}
AfterEach {
RemoveGitTempRepo $repoPath
}

It 'Tab completes empty for git pr oops parameters values' {
$result = & $module GitTabExpansionInternal 'git test-vsts-pr oops --'
$result | Should Be @()
}

It 'Tab completes empty for git pr oops short parameter values' {
$result = & $module GitTabExpansionInternal 'git test-vsts-pr oops -'
$result | Should Be @()
}

It 'Tab completes git pr create parameters values' {
$result = & $module GitTabExpansionInternal 'git test-vsts-pr create --'
$result -contains '--auto-complete' | Should Be $true
}
It 'Tab completes git pr create auto-complete parameters values' {
$result = & $module GitTabExpansionInternal 'git test-vsts-pr create --auto-complete --'
$result -contains '--delete-source-branch' | Should Be $true
}

It 'Tab completes git pr show all parameters values' {
$result = & $module GitTabExpansionInternal 'git test-vsts-pr show --'
$result -contains '--' | Should Be $false
$result -contains '--debug' | Should Be $true
$result -contains '--help' | Should Be $true
$result -contains '--output' | Should Be $true
$result -contains '--query' | Should Be $true
$result -contains '--verbose' | Should Be $true
}

It 'Tab completes git pr create all short push parameters' {
$result = & $module GitTabExpansionInternal 'git test-vsts-pr create -'
$result -contains '-d' | Should Be $true
$result -contains '-i' | Should Be $true
$result -contains '-p' | Should Be $true
$result -contains '-r' | Should Be $true
$result -contains '-s' | Should Be $true
$result -contains '-h' | Should Be $true
$result -contains '-o' | Should Be $true
}
}
}
17 changes: 17 additions & 0 deletions test/TabExpansion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,23 @@ Describe 'TabExpansion Tests' {
}
}

Context 'Vsts' {
BeforeEach {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoPath = NewGitTempRepo

# Test with non-standard vsts pr alias name
&$gitbin config alias.test-vsts-pr "!f() { exec vsts code pr \`"`$`@\`"; }; f"
}
AfterEach {
RemoveGitTempRepo $repoPath
}
It 'Tab completes pr options' {
$result = & $module GitTabExpansionInternal 'git test-vsts-pr '
$result -contains 'abandon' | Should Be $true
}
}

Context 'Add/Reset/Checkout TabExpansion Tests' {
BeforeEach {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
Expand Down

0 comments on commit b428c3c

Please sign in to comment.