Completion: add completion generator from oclif.manifest.json

Change-type: patch
This commit is contained in:
Pierre Kancir 2021-04-29 23:49:41 +02:00
parent 9283f83356
commit bb503a0322
6 changed files with 170 additions and 0 deletions

2
.gitignore vendored
View File

@ -34,3 +34,5 @@
/package-lock.json
/resinrc.yml
/tmp/
/completion/bash/
/completion/zsh/

View File

@ -0,0 +1,30 @@
#!/bin/bash
_balena_complete()
{
local cur prev
# Valid top-level completions
$main_commands$
# Sub-completions
$sub_cmds$
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [ $COMP_CWORD -eq 1 ]
then
COMPREPLY=( $(compgen -W "${main_commands}" -- $cur) )
elif [ $COMP_CWORD -eq 2 ]
then
case "$prev" in
$sub_cmds_prev$
"*")
;;
esac
fi
}
complete -F _balena_complete balena

View File

@ -0,0 +1,33 @@
#compdef balena
#autoload
_balena() {
typeset -A opt_args
local context state line curcontext="$curcontext"
# Valid top-level completions
$main_commands$
# Sub-completions
$sub_cmds$
_arguments -C \
'(- 1 *)--version[show version and exit]' \
'(- 1 *)'{-h,--help}'[show help options and exit]' \
'1:first command:_balena_main_cmds' \
'2:second command:_balena_sec_cmds' \
&& ret=0
}
(( $+functions[_balena_main_cmds] )) ||
_balena_main_cmds() {
_describe -t main_commands 'command' main_commands "$@" && ret=0
}
(( $+functions[_balena_sec_cmds] )) ||
_balena_sec_cmds() {
case $line[1] in
$sub_cmds_prev$
esac
}
_balena "$@"

View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
_BALENA_COMPLETION_DIR=$(builtin cd "`dirname "${BASH_SOURCE[0]}"`" > /dev/null && pwd)
source "$_BALENA_COMPLETION_DIR/bash/balena_comp"

View File

@ -0,0 +1,6 @@
#!/usr/bin/env zsh
_BALENA_COMPLETION_DIR=$(builtin cd -q "`dirname "$0"`" > /dev/null && pwd)
fpath+=$_BALENA_COMPLETION_DIR/zsh/

View File

@ -0,0 +1,95 @@
const path = require('path');
const rootDir = path.join(__dirname, '..');
const fs = require('fs');
commandsFilePath = path.join(rootDir, 'oclif.manifest.json')
if (fs.existsSync(commandsFilePath)) {
console.log('Found file');
} else {
console.error('Not Found file');
return;
}
const commands_json = JSON.parse(fs.readFileSync(commandsFilePath, 'utf8'));
var mainCommands = [];
var additionalCommands = [];
for (const key in commands_json.commands) {
const cmd = key.split(":");
if (cmd.length > 1) {
additionalCommands.push(cmd);
if (!mainCommands.includes(cmd[0])) {
mainCommands.push(cmd[0]);
}
} else {
mainCommands.push(cmd[0]);
}
}
const mainCommandsStr = mainCommands.join(" ");
// GENERATE BASH COMPLETION FILE
bashFilePathIn = path.join(__dirname, "balena_bash_template");
bashFilePathOut = path.join(__dirname, "bash/balena_comp");
fs.readFile(bashFilePathIn, 'utf8', function (err,data) {
if (err) {
return console.error(err);
}
data = data.replace(/\$main_commands\$/g, 'main_commands=\"' + mainCommandsStr + '\"');
var subCommands = [];
var prevElement = additionalCommands[0][0];
additionalCommands.forEach(function(element) {
if (element[0] == prevElement) {
subCommands.push(element[1]);
} else {
const prevElement2 = prevElement.replace(/-/g, "_") + "_cmds";
data = data.replace(/\$sub_cmds\$/g, " " + prevElement2 + '=\"' + subCommands.join(" ") + '\"\n\$sub_cmds\$');
data = data.replace(/\$sub_cmds_prev\$/g, " " + prevElement + ")\n COMPREPLY=( $(compgen -W \"$" + prevElement2 + "\" -- $cur) )\n ;;\n\$sub_cmds_prev\$");
prevElement = element[0];
subCommands = [];
subCommands.push(element[1]);
}
});
// cleanup placeholders
data = data.replace(/\$sub_cmds\$/g, "");
data = data.replace(/\$sub_cmds_prev\$/g, "");
fs.writeFile(bashFilePathOut, data, 'utf8', function (err) {
if (err) {
return console.error(err);
}
});
});
// GENERATE ZSH COMPLETION FILE
zshFilePathIn = path.join(__dirname, "balena_zsh_template");
zshFilePathOut = path.join(__dirname, "zsh/_balena");
fs.readFile(zshFilePathIn, 'utf8', function (err,data) {
if (err) {
return console.error(err);
}
data = data.replace(/\$main_commands\$/g, 'main_commands=( ' + mainCommandsStr + ' )');
var subCommands = [];
var prevElement = additionalCommands[0][0];
additionalCommands.forEach(function(element) {
if (element[0] == prevElement) {
subCommands.push(element[1]);
} else {
const prevElement2 = prevElement.replace(/-/g, "_") + "_cmds";
data = data.replace(/\$sub_cmds\$/g, " " + prevElement2 + '=( ' + subCommands.join(" ") + ' )\n\$sub_cmds\$');
data = data.replace(/\$sub_cmds_prev\$/g, " \"" + prevElement + "\")\n _describe -t " + prevElement2 + " \'" + prevElement + '_cmd\' ' + prevElement2 + " \"$@\" && ret=0\n ;;\n\$sub_cmds_prev\$");
prevElement = element[0];
subCommands = [];
subCommands.push(element[1]);
}
});
// cleanup placeholders
data = data.replace(/\$sub_cmds\$/g, "");
data = data.replace(/\$sub_cmds_prev\$/g, "");
fs.writeFile(zshFilePathOut, data, 'utf8', function (err) {
if (err) {
return console.error(err);
}
});
});