From aac13e562ffa36f3a6c0292bdc5e1e7429004107 Mon Sep 17 00:00:00 2001 From: Orestis <orestis.malaspinas@pm.me> Date: Mon, 12 Feb 2024 23:00:44 +0100 Subject: [PATCH] refactored. removed useless code --- NodeApp/src/commander/CommanderApp.ts | 5 +- NodeApp/src/helpers/AutoCompletionHelper.ts | 364 ++++++++++---------- 2 files changed, 179 insertions(+), 190 deletions(-) diff --git a/NodeApp/src/commander/CommanderApp.ts b/NodeApp/src/commander/CommanderApp.ts index 1ddff76..4714fc0 100644 --- a/NodeApp/src/commander/CommanderApp.ts +++ b/NodeApp/src/commander/CommanderApp.ts @@ -9,7 +9,7 @@ import { stateConfigFile } from '../config/ConfigFiles'; import semver from 'semver/preload'; import { version } from '../config/Version'; import Config from '../config/Config'; -import {getBashCompletion, getNewBashCompletion} from '../helpers/AutoCompletionHelper'; +import { writeBashCompletion } from '../helpers/AutoCompletionHelper'; class CommanderApp { @@ -44,8 +44,7 @@ class CommanderApp { this.registerCommands(); - getBashCompletion(this.program, "bash_completion.sh") - getNewBashCompletion(this.program, "new_bash_completion.sh") + writeBashCompletion(this.program, "bash_completion.sh") this.program.parse(); } diff --git a/NodeApp/src/helpers/AutoCompletionHelper.ts b/NodeApp/src/helpers/AutoCompletionHelper.ts index 5ab7305..1c0b00b 100644 --- a/NodeApp/src/helpers/AutoCompletionHelper.ts +++ b/NodeApp/src/helpers/AutoCompletionHelper.ts @@ -1,84 +1,6 @@ import { Command } from 'commander'; import { writeFileSync } from 'fs'; -// 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 -// this comment is written). - -// #/usr/bin/env bash -// function _dojo_completions() -// { -// latest="${COMP_WORDS[$COMP_CWORD]}" -// parent="${COMP_WORDS[$COMP_CWORD - 1]}" -// grand_parent="${COMP_WORDS[$COMP_CWORD - 2]}" -// words="" -// case "${parent}" in -// dojo) -// words="session assignment exercise -V --version -H --host --help -h" -// ;; -// session) -// words="login logout test --help -h" -// ;; -// login) -// words="-c --cli --help -h" -// ;; -// logout) -// words="-f --force --help -h" -// ;; -// assignment) -// words="create check run publish unpublish --help -h" -// ;; -// create) -// case "${grand_parent}" in -// assignment) -// words="-n --name -i --members_id -u --members_username -t --template -c --clone --help -h" -// ;; -// *) -// ;; -// esac -// case "${grand_parent}" in -// exercise) -// words="-a --assignment -i --members_id -u --members_username -c --clone --help -h" -// ;; -// *) -// ;; -// esac -// ;; -// check) -// words="-p --path -v --verbose -w --super-verbose --help -h" -// ;; -// run) -// case "${grand_parent}" in -// assignment) -// words="-p --path -v --verbose -w --super-verbose --help -h" -// ;; -// *) -// ;; -// esac -// case "${grand_parent}" in -// exercise) -// words="-p --path -v --verbose -w --super-verbose --help -h" -// ;; -// *) -// ;; -// esac -// ;; -// publish) -// words="-f --force --help -h" -// ;; -// unpublish) -// words="-f --force --help -h" -// ;; -// exercise) -// words="create run --help -h" -// ;; -// *) -// ;; -// esac -// COMPREPLY=($(compgen -W "$words" -- $latest)) -// return 0 -// } - function isLeaf(cmd: Command): boolean { return cmd.commands.length == 0 } @@ -91,49 +13,6 @@ function computeDepth(cmd: Command | undefined): number { } } -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 => @@ -145,90 +24,201 @@ 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\nfunction _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 { +function generateSubCommands(cmd: Command, current: number, maxDepth: number, ident: 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')}` + 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(current, `case "\${COMP_WORDS[$COMP_CWORD - ${maxDepth - current + 1}]}" in`)}` - + `${addLine(current + 1, `${cmd.name()})`)}` + 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) + data += generateSubCommands(subCmd, current + 1, maxDepth, ident + 2) }) data += - `${addLine(current, ';;')}` - + `${addLine(current, '*)')}` - + `${addLine(current, ';;')}` - + `${addLine(current, 'esac')}` + addLine(ident + 1, ';;') + + addLine(ident + 1, '*)') + + addLine(ident + 1, ';;') + + addLine(ident, 'esac') return data } } -export function getNewBashCompletion(root: Command, filename: string) { +export function writeBashCompletion(root: Command, filename: string) { const depth = computeDepth(root) let data = - `${addLine(0, '#/usr/bin/env bash\nfunction _dojo_completions()')}` - + `${addLine(0, '{')}` - + `${addLine(1, 'latest="${COMP_WORDS[$COMP_CWORD]}"')}` + addLine(0, '#/usr/bin/env bash\nfunction _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, `${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')}` + 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 +// CLI library. The file should look something like that (it looks at the time +// this comment is written). + +// #/usr/bin/env bash +// function _dojo_completions() +// { +// latest="${COMP_WORDS[$COMP_CWORD]}" +// if [ $COMP_CWORD -eq 3 ] +// then +// case "${COMP_WORDS[$COMP_CWORD - 3]}" in +// dojo) +// case "${COMP_WORDS[$COMP_CWORD - 2]}" in +// session) +// case "${COMP_WORDS[$COMP_CWORD - 1]}" in +// login) +// words="-c --cli --help -h" +// ;; +// *) +// ;; +// esac +// case "${COMP_WORDS[$COMP_CWORD - 1]}" in +// logout) +// words="-f --force --help -h" +// ;; +// *) +// ;; +// esac +// case "${COMP_WORDS[$COMP_CWORD - 1]}" in +// test) +// words="--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) +// words="-p --path -v --verbose -w --super-verbose --help -h" +// ;; +// *) +// ;; +// esac +// case "${COMP_WORDS[$COMP_CWORD - 1]}" in +// run) +// words="-p --path -v --verbose -w --super-verbose --help -h" +// ;; +// *) +// ;; +// esac +// 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) +// 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" +// ;; +// *) +// ;; +// esac +// ;; +// *) +// ;; +// esac +// ;; +// *) +// ;; +// 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) +// words="create run --help -h" +// ;; +// *) +// ;; +// 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)) +// return 0 +// } +// complete -F _dojo_completions dojo -- GitLab