Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DojoCLI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dojo Project (HES-SO)
Projects
UI
DojoCLI
Commits
aac13e56
Verified
Commit
aac13e56
authored
1 year ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
refactored. removed useless code
parent
46e3ed77
No related branches found
No related tags found
1 merge request
!8
Draft: Resolve "Add bash completion helper function"
Pipeline
#29380
failed
1 year ago
Stage: code_quality
Stage: test
Changes
2
Pipelines
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
NodeApp/src/commander/CommanderApp.ts
+2
-3
2 additions, 3 deletions
NodeApp/src/commander/CommanderApp.ts
NodeApp/src/helpers/AutoCompletionHelper.ts
+177
-187
177 additions, 187 deletions
NodeApp/src/helpers/AutoCompletionHelper.ts
with
179 additions
and
190 deletions
NodeApp/src/commander/CommanderApp.ts
+
2
−
3
View file @
aac13e56
...
@@ -9,7 +9,7 @@ import { stateConfigFile } from '../config/ConfigFiles';
...
@@ -9,7 +9,7 @@ import { stateConfigFile } from '../config/ConfigFiles';
import
semver
from
'
semver/preload
'
;
import
semver
from
'
semver/preload
'
;
import
{
version
}
from
'
../config/Version
'
;
import
{
version
}
from
'
../config/Version
'
;
import
Config
from
'
../config/Config
'
;
import
Config
from
'
../config/Config
'
;
import
{
getBashCompletion
,
getNew
BashCompletion
}
from
'
../helpers/AutoCompletionHelper
'
;
import
{
write
BashCompletion
}
from
'
../helpers/AutoCompletionHelper
'
;
class
CommanderApp
{
class
CommanderApp
{
...
@@ -44,8 +44,7 @@ class CommanderApp {
...
@@ -44,8 +44,7 @@ class CommanderApp {
this
.
registerCommands
();
this
.
registerCommands
();
getBashCompletion
(
this
.
program
,
"
bash_completion.sh
"
)
writeBashCompletion
(
this
.
program
,
"
bash_completion.sh
"
)
getNewBashCompletion
(
this
.
program
,
"
new_bash_completion.sh
"
)
this
.
program
.
parse
();
this
.
program
.
parse
();
}
}
...
...
This diff is collapsed.
Click to expand it.
NodeApp/src/helpers/AutoCompletionHelper.ts
+
177
−
187
View file @
aac13e56
import
{
Command
}
from
'
commander
'
;
import
{
Command
}
from
'
commander
'
;
import
{
writeFileSync
}
from
'
fs
'
;
import
{
writeFileSync
}
from
'
fs
'
;
function
isLeaf
(
cmd
:
Command
):
boolean
{
return
cmd
.
commands
.
length
==
0
}
function
computeDepth
(
cmd
:
Command
|
undefined
):
number
{
if
(
cmd
===
undefined
)
{
return
0
}
else
{
return
1
+
cmd
.
commands
.
map
(
subCmd
=>
computeDepth
(
subCmd
)).
reduce
((
acc
,
depth
)
=>
depth
>
acc
?
depth
:
acc
,
0
)
}
}
function
getOptions
(
cmd
:
Command
):
string
{
// we remove <args>, [command], and , from option lines
return
cmd
.
options
.
filter
(
opt
=>
!
opt
.
hidden
).
map
(
opt
=>
opt
.
flags
.
replace
(
/<.*
?
>/
,
''
).
replace
(
/
\[
.*
?\]
/
,
''
).
replace
(
'
,
'
,
''
).
trimEnd
()
).
join
(
"
"
)
}
function
commandsAndOptionsToString
(
cmd
:
Command
):
string
{
return
cmd
.
commands
.
map
(
c
=>
c
.
name
()).
join
(
"
"
).
concat
(
'
'
+
getOptions
(
cmd
)).
trim
().
concat
(
'
--help -h
'
).
trim
()
}
function
addLine
(
identLevel
:
number
,
pattern
:
string
):
string
{
return
`
${
'
'
.
repeat
(
identLevel
)}${
pattern
}
\n`
}
function
generateSubCommands
(
cmd
:
Command
,
current
:
number
,
maxDepth
:
number
,
ident
:
number
):
string
{
if
(
current
==
maxDepth
)
{
return
addLine
(
ident
,
`case "
\$
{COMP_WORDS[$COMP_CWORD -
${
maxDepth
-
current
+
1
}
]}" in`
)
+
addLine
(
ident
+
1
,
`
${
cmd
.
name
()}
)`
)
+
addLine
(
ident
+
2
,
`words="
${
commandsAndOptionsToString
(
cmd
)}
"`
)
+
addLine
(
ident
+
1
,
'
;;
'
)
+
addLine
(
ident
+
1
,
'
*)
'
)
+
addLine
(
ident
+
1
,
'
;;
'
)
+
addLine
(
ident
,
'
esac
'
)
}
else
{
let
data
=
addLine
(
ident
,
`case "
\$
{COMP_WORDS[$COMP_CWORD -
${
maxDepth
-
current
+
1
}
]}" in`
)
+
addLine
(
ident
+
1
,
`
${
cmd
.
name
()}
)`
)
cmd
.
commands
.
forEach
(
subCmd
=>
{
data
+=
generateSubCommands
(
subCmd
,
current
+
1
,
maxDepth
,
ident
+
2
)
})
data
+=
addLine
(
ident
+
1
,
'
;;
'
)
+
addLine
(
ident
+
1
,
'
*)
'
)
+
addLine
(
ident
+
1
,
'
;;
'
)
+
addLine
(
ident
,
'
esac
'
)
return
data
}
}
export
function
writeBashCompletion
(
root
:
Command
,
filename
:
string
)
{
const
depth
=
computeDepth
(
root
)
let
data
=
addLine
(
0
,
'
#/usr/bin/env bash
\n
function _dojo_completions()
'
)
+
addLine
(
0
,
'
{
'
)
+
addLine
(
1
,
'
latest="${COMP_WORDS[$COMP_CWORD]}"
'
)
for
(
let
i
=
1
;
i
<=
depth
;
i
++
)
{
data
+=
addLine
(
1
,
`
${
i
==
1
?
'
if
'
:
'
elif
'
}
[ $COMP_CWORD -eq
${
depth
-
i
+
1
}
]`
)
+
addLine
(
1
,
'
then
'
)
data
+=
generateSubCommands
(
root
,
i
,
depth
,
2
)
}
data
+=
addLine
(
1
,
'
fi
'
)
+
addLine
(
1
,
'
COMPREPLY=($(compgen -W "$words" -- $latest))
'
)
+
addLine
(
1
,
'
return 0
'
)
+
addLine
(
0
,
'
}
'
)
+
addLine
(
0
,
'
complete -F _dojo_completions dojo
'
)
writeFileSync
(
filename
,
data
);
}
// The following code should create a bash completion automatically from the Commander
// The following code should create a bash completion automatically from the Commander
// CLI library. The file should look something like that (it looks at the time
// CLI library. The file should look something like that (it looks at the time
// this comment is written).
// this comment is written).
...
@@ -9,226 +83,142 @@ import { writeFileSync } from 'fs';
...
@@ -9,226 +83,142 @@ import { writeFileSync } from 'fs';
// function _dojo_completions()
// function _dojo_completions()
// {
// {
// latest="${COMP_WORDS[$COMP_CWORD]}"
// latest="${COMP_WORDS[$COMP_CWORD]}"
// parent="${COMP_WORDS[$COMP_CWORD - 1]}"
// if [ $COMP_CWORD -eq 3 ]
// grand_parent="${COMP_WORDS[$COMP_CWORD - 2]}"
// then
// words=""
// case "${COMP_WORDS[$COMP_CWORD - 3]}" in
// case "${parent}" in
// dojo)
// dojo)
// words="session assignment exercise -V --version -H --host --help -h"
// case "${COMP_WORDS[$COMP_CWORD - 2]}" in
// ;;
// session)
// session)
// words="login logout test --help -h"
// case "${COMP_WORDS[$COMP_CWORD - 1]}" in
// ;;
// login)
// login)
// words="-c --cli --help -h"
// words="-c --cli --help -h"
// ;;
// ;;
// *)
// ;;
// esac
// case "${COMP_WORDS[$COMP_CWORD - 1]}" in
// logout)
// logout)
// words="-f --force --help -h"
// words="-f --force --help -h"
// ;;
// ;;
// assignment)
// *)
// words="create check run publish unpublish --help -h"
// ;;
// ;;
//
create)
//
esac
//
case "${grand_parent
}" in
//
case "${COMP_WORDS[$COMP_CWORD - 1]
}" in
//
assignmen
t)
//
tes
t)
//
words="-n --name -i --members_id -u --members_username -t --template -c --clone
--help -h"
//
words="
--help -h"
// ;;
// ;;
// *)
// *)
// ;;
// ;;
// esac
// esac
// case "${grand_parent}" in
// exercise)
// words="-a --assignment -i --members_id -u --members_username -c --clone --help -h"
// ;;
// ;;
// *)
// *)
// ;;
// ;;
// esac
// esac
// case "${COMP_WORDS[$COMP_CWORD - 2]}" in
// assignment)
// case "${COMP_WORDS[$COMP_CWORD - 1]}" in
// create)
// words="-n --name -i --members_id -u --members_username -t --template -c --clone --help -h"
// ;;
// ;;
// *)
// ;;
// esac
// case "${COMP_WORDS[$COMP_CWORD - 1]}" in
// check)
// check)
// words="-p --path -v --verbose -w --super-verbose --help -h"
// words="-p --path -v --verbose -w --super-verbose --help -h"
// ;;
// ;;
// *)
// ;;
// esac
// case "${COMP_WORDS[$COMP_CWORD - 1]}" in
// run)
// run)
// case "${grand_parent}" in
// assignment)
// words="-p --path -v --verbose -w --super-verbose --help -h"
// words="-p --path -v --verbose -w --super-verbose --help -h"
// ;;
// ;;
// *)
// *)
// ;;
// ;;
// esac
// esac
// case "${grand_parent}" in
// case "${COMP_WORDS[$COMP_CWORD - 1]}" in
// publish)
// words="-f --force --help -h"
// ;;
// *)
// ;;
// esac
// case "${COMP_WORDS[$COMP_CWORD - 1]}" in
// unpublish)
// words="-f --force --help -h"
// ;;
// *)
// ;;
// esac
// ;;
// *)
// ;;
// esac
// case "${COMP_WORDS[$COMP_CWORD - 2]}" in
// exercise)
// exercise)
// case "${COMP_WORDS[$COMP_CWORD - 1]}" in
// create)
// words="-a --assignment -i --members_id -u --members_username -c --clone --help -h"
// ;;
// *)
// ;;
// esac
// case "${COMP_WORDS[$COMP_CWORD - 1]}" in
// run)
// words="-p --path -v --verbose -w --super-verbose --help -h"
// words="-p --path -v --verbose -w --super-verbose --help -h"
// ;;
// ;;
// *)
// *)
// ;;
// ;;
// esac
// esac
// ;;
// ;;
// publish)
// *)
// words="-f --force --help -h"
// ;;
// ;;
// unpublish)
// esac
// words="-f --force --help -h"
// ;;
// *)
// ;;
// ;;
// esac
// elif [ $COMP_CWORD -eq 2 ]
// then
// case "${COMP_WORDS[$COMP_CWORD - 2]}" in
// dojo)
// case "${COMP_WORDS[$COMP_CWORD - 1]}" in
// session)
// words="login logout test --help -h"
// ;;
// *)
// ;;
// esac
// case "${COMP_WORDS[$COMP_CWORD - 1]}" in
// assignment)
// words="create check run publish unpublish --help -h"
// ;;
// *)
// ;;
// esac
// case "${COMP_WORDS[$COMP_CWORD - 1]}" in
// exercise)
// exercise)
// words="create run --help -h"
// words="create run --help -h"
// ;;
// ;;
// *)
// *)
// ;;
// ;;
// esac
// esac
// ;;
// *)
// ;;
// esac
// elif [ $COMP_CWORD -eq 1 ]
// then
// case "${COMP_WORDS[$COMP_CWORD - 1]}" in
// dojo)
// words="session assignment exercise -V --version -H --host --help -h"
// ;;
// *)
// ;;
// esac
// fi
// COMPREPLY=($(compgen -W "$words" -- $latest))
// COMPREPLY=($(compgen -W "$words" -- $latest))
// return 0
// return 0
// }
// }
// complete -F _dojo_completions dojo
function
isLeaf
(
cmd
:
Command
):
boolean
{
return
cmd
.
commands
.
length
==
0
}
function
computeDepth
(
cmd
:
Command
|
undefined
):
number
{
if
(
cmd
===
undefined
)
{
return
0
}
else
{
return
1
+
cmd
.
commands
.
map
(
subCmd
=>
computeDepth
(
subCmd
)).
reduce
((
acc
,
depth
)
=>
depth
>
acc
?
depth
:
acc
,
0
)
}
}
function
search
(
cmd
:
Command
,
cmdName
:
string
):
Array
<
Command
>
{
if
(
isLeaf
(
cmd
))
{
if
(
cmd
.
name
()
!=
cmdName
)
{
return
[]
}
else
{
return
[
cmd
]
}
}
else
if
(
cmd
.
name
()
==
cmdName
)
{
return
cmd
.
commands
.
flatMap
(
st
=>
search
(
st
,
cmdName
)).
concat
(
cmd
)
}
else
{
return
cmd
.
commands
.
flatMap
(
st
=>
search
(
st
,
cmdName
))
}
}
function
flatten
(
cmd
:
Command
):
Array
<
Command
>
{
if
(
isLeaf
(
cmd
))
{
return
[
cmd
]
}
else
{
return
cmd
.
commands
.
map
(
child
=>
flatten
(
child
))
.
reduce
((
acc
,
cmd
)
=>
acc
.
concat
(
cmd
),
[
cmd
])
}
}
function
addWordsGrandParents
(
cmd
:
Command
):
string
{
let
parentWords
=
""
let
ident
=
3
if
(
cmd
.
parent
!==
null
)
{
parentWords
+=
openCase
(
ident
,
'
grand_parent
'
)
ident
+=
1
parentWords
+=
addLine
(
ident
,
`
${
cmd
.
parent
.
name
()}
)`
)
ident
+=
1
}
parentWords
+=
addLine
(
ident
,
`words="
${
commandsAndOptionsToString
(
cmd
)}
"`
)
ident
-=
1
parentWords
+=
addLine
(
ident
,
'
;;
'
)
if
(
cmd
.
parent
!==
null
)
{
ident
-=
1
parentWords
+=
`
${
closeCase
(
ident
)}
`
}
return
parentWords
}
function
getOptions
(
cmd
:
Command
):
string
{
// we remove <args>, [command], and , from option lines
return
cmd
.
options
.
filter
(
opt
=>
!
opt
.
hidden
).
map
(
opt
=>
opt
.
flags
.
replace
(
/<.*
?
>/
,
''
).
replace
(
/
\[
.*
?\]
/
,
''
).
replace
(
'
,
'
,
''
).
trimEnd
()
).
join
(
"
"
)
}
function
commandsAndOptionsToString
(
cmd
:
Command
):
string
{
return
cmd
.
commands
.
map
(
c
=>
c
.
name
()).
join
(
"
"
).
concat
(
'
'
+
getOptions
(
cmd
)).
trim
().
concat
(
'
--help -h
'
).
trim
()
}
function
openCase
(
identLevel
:
number
,
pattern
:
string
):
string
{
return
`
${
'
'
.
repeat
(
identLevel
)}
case "
\$
{
${
pattern
}
}" in\n`
}
function
closeCase
(
identLevel
:
number
):
string
{
return
`
${
'
'
.
repeat
(
identLevel
+
1
)}
*)\n`
+
`
${
'
'
.
repeat
(
identLevel
+
1
)}
;;\n`
+
`
${
'
'
.
repeat
(
identLevel
)}
esac\n`
}
function
addLine
(
identLevel
:
number
,
pattern
:
string
):
string
{
return
`
${
'
'
.
repeat
(
identLevel
)}${
pattern
}
\n`
}
export
function
getBashCompletion
(
root
:
Command
,
filename
:
string
)
{
let
data
=
`
${
addLine
(
0
,
'
#/usr/bin/env bash
\n
function _dojo_completions()
'
)}
`
+
`
${
addLine
(
0
,
'
{
'
)}
`
+
`
${
addLine
(
1
,
'
latest="${COMP_WORDS[$COMP_CWORD]}"
'
)}
`
+
`
${
addLine
(
1
,
'
parent="${COMP_WORDS[$COMP_CWORD - 1]}"
'
)}
`
+
`
${
addLine
(
1
,
'
grand_parent="${COMP_WORDS[$COMP_CWORD - 2]}"
'
)}
`
+
`
${
addLine
(
1
,
'
words=""
'
)}
`
+
`
${
openCase
(
1
,
"
parent
"
)}
`
const
commands
=
Array
.
from
(
new
Set
(
flatten
(
root
).
map
(
cmd
=>
cmd
.
name
()))).
map
(
name
=>
search
(
root
,
name
))
for
(
const
cmdNode
of
commands
)
{
const
cmd
=
cmdNode
[
0
]
data
+=
addLine
(
2
,
`
${
cmd
.
name
()}
)`
)
if
(
cmd
!==
undefined
&&
cmdNode
.
length
==
1
)
{
data
+=
addLine
(
3
,
`words="
${
commandsAndOptionsToString
(
cmd
)}
"`
)
}
else
if
(
cmd
!==
undefined
&&
cmdNode
.
length
>
1
)
{
cmdNode
.
forEach
(
n
=>
data
+=
addWordsGrandParents
(
n
))
}
data
+=
addLine
(
2
,
'
;;
'
)
}
data
+=
`
${
closeCase
(
1
)}
`
+
`
${
addLine
(
1
,
'
COMPREPLY=($(compgen -W "$words" -- $latest))
'
)}
`
+
`
${
addLine
(
1
,
'
return 0
'
)}
`
+
`
${
addLine
(
0
,
'
}
'
)}
`
+
`
${
addLine
(
0
,
'
complete -F _dojo_completions dojo
'
)}
`
writeFileSync
(
filename
,
data
);
}
function
generateSubCommands
(
cmd
:
Command
,
current
:
number
,
maxDepth
:
number
):
string
{
if
(
current
==
maxDepth
)
{
return
`
${
addLine
(
current
,
`case "
\$
{COMP_WORDS[$COMP_CWORD -
${
maxDepth
-
current
+
1
}
]}" in`
)}
`
+
`
${
addLine
(
current
+
1
,
`
${
cmd
.
name
()}
)`
)}
`
+
`
${
addLine
(
current
+
2
,
`words="
${
commandsAndOptionsToString
(
cmd
)}
"`
)}
`
+
`
${
addLine
(
current
+
1
,
'
;;
'
)}
`
+
`
${
addLine
(
current
+
1
,
'
*)
'
)}
`
+
`
${
addLine
(
current
,
'
;;
'
)}
`
+
`
${
addLine
(
current
,
'
esac
'
)}
`
}
else
{
let
data
=
`
${
addLine
(
current
,
`case "
\$
{COMP_WORDS[$COMP_CWORD -
${
maxDepth
-
current
+
1
}
]}" in`
)}
`
+
`
${
addLine
(
current
+
1
,
`
${
cmd
.
name
()}
)`
)}
`
cmd
.
commands
.
forEach
(
subCmd
=>
{
data
+=
generateSubCommands
(
subCmd
,
current
+
1
,
maxDepth
)
})
data
+=
`
${
addLine
(
current
,
'
;;
'
)}
`
+
`
${
addLine
(
current
,
'
*)
'
)}
`
+
`
${
addLine
(
current
,
'
;;
'
)}
`
+
`
${
addLine
(
current
,
'
esac
'
)}
`
return
data
}
}
export
function
getNewBashCompletion
(
root
:
Command
,
filename
:
string
)
{
const
depth
=
computeDepth
(
root
)
let
data
=
`
${
addLine
(
0
,
'
#/usr/bin/env bash
\n
function _dojo_completions()
'
)}
`
+
`
${
addLine
(
0
,
'
{
'
)}
`
+
`
${
addLine
(
1
,
'
latest="${COMP_WORDS[$COMP_CWORD]}"
'
)}
`
for
(
let
i
=
1
;
i
<=
depth
;
i
++
)
{
data
+=
`
${
addLine
(
1
,
`
${
i
==
1
?
'
if
'
:
'
elif
'
}
[ $COMP_CWORD -eq
${
depth
-
i
+
1
}
]; then`
)}
`
data
+=
generateSubCommands
(
root
,
i
,
depth
)
}
data
+=
`
${
addLine
(
1
,
'
fi
'
)}
`
+
`
${
addLine
(
1
,
'
COMPREPLY=($(compgen -W "$words" -- $latest))
'
)}
`
+
`
${
addLine
(
1
,
'
return 0
'
)}
`
+
`
${
addLine
(
0
,
'
}
'
)}
`
+
`
${
addLine
(
0
,
'
complete -F _dojo_completions dojo
'
)}
`
writeFileSync
(
filename
,
data
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment