Skip to content
Snippets Groups Projects
Verified Commit ce88a755 authored by orestis.malaspin's avatar orestis.malaspin
Browse files

removed cmdNodes which were useless

parent 304e0953
No related branches found
No related tags found
1 merge request!8Draft: Resolve "Add bash completion helper function"
Pipeline #29373 passed
...@@ -79,84 +79,62 @@ import { writeFileSync } from 'fs'; ...@@ -79,84 +79,62 @@ import { writeFileSync } from 'fs';
// return 0 // return 0
// } // }
interface CmdNode { function isLeaf(cmd: Command): boolean {
readonly name: string; return cmd.commands.length == 0
readonly children: Array<CmdNode>;
readonly parent?: string;
} }
function createCmdNode(name: string, children: Array<CmdNode>, parent?: string): CmdNode { function search(cmd: Command, cmdName: string): Array<Command> {
return { if (isLeaf(cmd)) {
name, if (cmd.name() != cmdName) {
children,
parent
};
}
function isLeaf(tree: CmdNode): boolean {
return tree.children === undefined || tree.children.length == 0
}
function search(tree: CmdNode, cmdName: string): Array<CmdNode> {
if (isLeaf(tree)) {
if (tree.name != cmdName) {
return [] return []
} else { } else {
return [tree] return [cmd]
} }
} else if (tree.name == cmdName) { } else if (cmd.name() == cmdName) {
return tree.children.flatMap(st => search(st, cmdName)).concat(tree) return cmd.commands.flatMap(st => search(st, cmdName)).concat(cmd)
} else { } else {
return tree.children.flatMap(st => search(st, cmdName)) return cmd.commands.flatMap(st => search(st, cmdName))
} }
} }
function flatten(tree: CmdNode): Array<CmdNode> { function flatten(cmd: Command): Array<Command> {
if (isLeaf(tree)) { if (isLeaf(cmd)) {
return [tree] return [cmd]
} else { } else {
return tree.children return cmd.commands
.map(child => flatten(child)) .map(child => flatten(child))
.reduce((acc, cmd) => acc.concat(cmd), [tree]) .reduce((acc, cmd) => acc.concat(cmd), [cmd])
} }
} }
function addWordsGrandParents(cmd: CmdNode): string { function addWordsGrandParents(cmd: Command): string {
let parentWords = "" let parentWords = ""
let ident = 3 let ident = 3
if (cmd.parent !== undefined) { if (cmd.parent !== null) {
parentWords += openCase(ident, 'grand_parent') parentWords += openCase(ident, 'grand_parent')
ident += 1 ident += 1
parentWords += addLine(ident, `${cmd.parent})`) parentWords += addLine(ident, `${cmd.parent.name()})`)
ident += 1 ident += 1
} }
parentWords += addLine(ident, `words="${cmd.children.map(c => c.name).join(" ")} --help -h"`) parentWords += addLine(ident, `words="${commandsAndOptionsToString(cmd)}"`)
ident -= 1 ident -= 1
parentWords += addLine(ident, ';;') parentWords += addLine(ident, ';;')
if (cmd.parent !== undefined) { if (cmd.parent !== null) {
ident -= 1 ident -= 1
parentWords += `${closeCase(ident)}` parentWords += `${closeCase(ident)}`
} }
return parentWords return parentWords
} }
function getOptions(cmd: Command): Array<CmdNode> { function getOptions(cmd: Command): string {
// we remove <args>, [command], and , from option lines // we remove <args>, [command], and , from option lines
return cmd.options.filter(opt => !opt.hidden).map(opt => return cmd.options.filter(opt => !opt.hidden).map(opt =>
createCmdNode(opt.flags.replace(/<.*?>/, '').replace(/\[.*?\]/, '').replace(',', '').trimEnd(), [], cmd.name()) opt.flags.replace(/<.*?>/, '').replace(/\[.*?\]/, '').replace(',', '').trimEnd()
) ).join(" ")
} }
function getSubCmdNode(cmd: Command, parent: Command): CmdNode { function commandsAndOptionsToString(cmd: Command): string {
if (cmd.commands.length > 0) { return cmd.commands.map(c => c.name()).join(" ").concat(' ' + getOptions(cmd)).trim().concat(' --help -h').trim()
return createCmdNode(cmd.name(), cmd.commands.map(subCmd => getSubCmdNode(subCmd, cmd)).concat(getOptions(cmd)), parent.name())
} else {
return createCmdNode(cmd.name(), getOptions(cmd), parent.name())
}
}
function buildCmdNode(root: Command): CmdNode {
return createCmdNode(root.name(), root.commands.map(subCmd => getSubCmdNode(subCmd, root)).concat(getOptions(root)))
} }
function openCase(identLevel: number, pattern: string): string { function openCase(identLevel: number, pattern: string): string {
...@@ -174,20 +152,19 @@ function addLine(identLevel: number, pattern: string): string { ...@@ -174,20 +152,19 @@ function addLine(identLevel: number, pattern: string): string {
} }
export function getBashCompletion(root: Command, filename: string) { export function getBashCompletion(root: Command, filename: string) {
const tree = buildCmdNode(root)
let data = let data =
`${addLine(0, '#/usr/bin/env bash\nfunction _dojo_completions()')}` `${addLine(0, '#/usr/bin/env bash\nfunction _dojo_completions()')}`
+ `${addLine(0, '{')}${addLine(1, 'latest="${COMP_WORDS[$COMP_CWORD]}"')}` + `${addLine(0, '{')}${addLine(1, 'latest="${COMP_WORDS[$COMP_CWORD]}"')}`
+ `${addLine(1, 'parent="${COMP_WORDS[$COMP_CWORD - 1]}"')}` + `${addLine(1, 'parent="${COMP_WORDS[$COMP_CWORD - 1]}"')}`
+ `${addLine(1, 'grand_parent="${COMP_WORDS[$COMP_CWORD - 2]}"')}` + `${addLine(1, 'grand_parent="${COMP_WORDS[$COMP_CWORD - 2]}"')}`
+ `${addLine(1, 'words=""')}${openCase(1, "parent")}` + `${addLine(1, 'words=""')}`
const commands = Array.from(new Set(flatten(tree).filter(cmd => !isLeaf(cmd)).map(cmd => cmd.name))).map(name => search(tree, name)) + `${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) { for (const cmdNode of commands) {
const cmd = cmdNode[0] const cmd = cmdNode[0]
data += addLine(2, `${cmd.name})`) data += addLine(2, `${cmd.name()})`)
if (cmd !== undefined && cmdNode.length == 1) { if (cmd !== undefined && cmdNode.length == 1) {
data += addLine(3, `words="${cmd.children.map(c => c.name).join(" ")} --help -h"`) data += addLine(3, `words="${commandsAndOptionsToString(cmd)}"`)
} else if (cmd !== undefined && cmdNode.length > 1) { } else if (cmd !== undefined && cmdNode.length > 1) {
cmdNode.forEach(n => data += addWordsGrandParents(n)) cmdNode.forEach(n => data += addWordsGrandParents(n))
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment