Skip to content

Commit

Permalink
chore(scripts): add exec command (#3598)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Aug 26, 2024
1 parent bc8b5f0 commit 6e8f846
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
20 changes: 16 additions & 4 deletions scripts/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Argument, program } from 'commander';
import semver from 'semver';

import { buildClients, buildPlaygrounds } from '../buildClients.js';
import { LANGUAGES, setVerbose } from '../common.js';
import { CLIENTS, LANGUAGES, run, setVerbose } from '../common.js';
import { ctsGenerateMany } from '../cts/generate.js';
import { runCts } from '../cts/runCts.js';
import { startTestServer } from '../cts/testServer';
Expand All @@ -20,9 +20,11 @@ import { ALL, getClientChoices, generatorList, transformSelection, PROMPT_CLIENT

const args = {
language: new Argument('[language]', 'The language').choices(PROMPT_LANGUAGES),
requiredLanguage: new Argument('language', 'The language').choices(LANGUAGES),
languages: new Argument('[language...]', 'The language').choices(PROMPT_LANGUAGES),
clients: new Argument('[client...]', 'The client').choices(getClientChoices('all')),
client: new Argument('[client]', 'The client').choices(PROMPT_CLIENTS),
requiredClient: new Argument('client', 'The client').choices(CLIENTS),
};

const flags = {
Expand Down Expand Up @@ -172,8 +174,8 @@ ctsCommand
program
.command('playground')
.description('Run the playground')
.addArgument(args.language)
.addArgument(args.client)
.addArgument(args.requiredLanguage)
.addArgument(args.requiredClient)
.action(async (langArg: LangArg, cliClient: string) => {
const { language, client } = transformSelection({
langArg,
Expand All @@ -191,7 +193,7 @@ program
program
.command('format')
.description('Format the specified folder for a specific language')
.addArgument(args.language)
.addArgument(args.requiredLanguage)
.argument('folder', 'The folder to format')
.option(flags.verbose.flag, flags.verbose.description)
.action(async (language: string, folder: string, { verbose }) => {
Expand Down Expand Up @@ -255,4 +257,14 @@ program
});
});

program
.command('exec')
.description('Executes a command inside the correct docker image')
.addArgument(args.requiredLanguage)
.argument('command...', 'The command to execute')
.action(async (language: Language, command: string[]) => {
setVerbose(true);
await run(command.join(' '), { language });
});

program.parse();
35 changes: 26 additions & 9 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,32 @@ _list_clients_for_language() {
if [[ $1 == "all" ]]; then
_list_clients
else
echo "all $(cat $ROOT/config/clients.config.json | jq -r --arg lang "$1" 'with_entries(select(.key == $lang)) | .[].clients | if (.[0] | type == "object") then .[].name else .[] end')"
echo "$(cat $ROOT/config/clients.config.json | jq -r --arg lang "$1" 'with_entries(select(.key == $lang)) | .[].clients | if (.[0] | type == "object") then .[].name else .[] end')"
fi
}

_list_clients_for_language_all() {
echo "all $(_list_clients_for_language $1)"
}

_list_clients() {
echo "all $(cat $ROOT/config/clients.config.json | jq -r 'with_entries(select(.key == "java")) | .[] | .clients[]')"
}

_apic_lang_client_complete() {
if [[ COMP_CWORD -eq $1 ]]; then
COMPREPLY=($(compgen -W "$(_list_languages_all)" -- "$cur"))
elif [[ COMP_CWORD -eq ($1 + 1) ]]; then
COMPREPLY=($(compgen -W "$(_list_clients_for_language $prev)" -- "$cur"))
elif [[ COMP_CWORD -ge ($1 + 1) ]]; then
lang="${COMP_WORDS[$1]}"
COMPREPLY=($(compgen -W "$(_list_clients_for_language_all $lang)" -- "$cur"))
fi
}

_apic_playground_complete() {
if [[ COMP_CWORD -eq 2 ]]; then
COMPREPLY=($(compgen -W "$(_list_languages)" -- "$cur"))
elif [[ COMP_CWORD -eq 3 ]]; then
COMPREPLY=($(compgen -W "$(_list_clients_for_language $lang)" -- "$cur"))
fi
}

Expand All @@ -58,7 +71,7 @@ _apic_cts_complete() {
}

_apic_build_specs_complete() {
if [[ COMP_CWORD -eq 3 ]]; then
if [[ COMP_CWORD -ge 3 ]]; then
COMPREPLY=($(compgen -W "algoliasearch $(_list_clients)" -- "$cur"))
fi
}
Expand All @@ -72,7 +85,7 @@ _apic_build_complete() {
_apic_lang_client_complete 3
elif [[ $second == "specs" ]]; then
_apic_build_specs_complete
elif [[ $second == "playground" ]]; then
elif [[ $second == "playground" && COMP_CWORD -eq 3 ]]; then
COMPREPLY=($(compgen -W "$(_list_languages_all)" -- "$cur"))
fi
fi
Expand All @@ -88,21 +101,25 @@ _apic_format_complete() {

_apic_complete() {
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
lang="${COMP_WORDS[COMP_CWORD-1]}" # initial guess, but it might be before in some commands
if [[ COMP_CWORD -eq 1 ]]; then
COMPREPLY=($(compgen -W "build cts format generate playground release snippets" -- "$cur"))
COMPREPLY=($(compgen -W "build cts exec format generate playground release snippets" -- "$cur"))
else
first="${COMP_WORDS[1]}"
if [[ $first == "generate" || $first == "playground" || $first == "snippets" ]]; then
if [[ $first == "generate" || $first == "snippets" ]]; then
_apic_lang_client_complete 2
elif [[ $first == "playground" ]]; then
_apic_playground_complete
elif [[ $first == "format" ]]; then
_apic_format_complete
elif [[ $first == "build" ]]; then
_apic_build_complete
elif [[ $first == "cts" ]]; then
_apic_cts_complete
elif [[ $first == "release" ]]; then
elif [[ $first == "release" && COMP_CWORD -eq 2 ]]; then
COMPREPLY=($(compgen -W "$(_list_languages_all)" -- "$cur"))
elif [[ $first == "exec" && COMP_CWORD -eq 2 ]]; then
COMPREPLY=($(compgen -W "$(_list_languages)" -- "$cur"))
fi
fi
}
Expand Down
4 changes: 2 additions & 2 deletions website/docs/add-new-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ You can pick a default template on the [openapi-generator's "generators" page](h
### Extract the template locally

```bash
docker exec apic_base bash -c "yarn openapi-generator-cli author template -g <templateName> -o templates/<languageName>"
apic exec java "yarn openapi-generator-cli author template -g <templateName> -o templates/<languageName>"
```

Example for the `JavaScript` client with the `typescript-node` template:

```bash
docker exec apic_base bash -c "yarn openapi-generator-cli author template -g typescript-node -o templates/javascript/"
apic exec java "yarn openapi-generator-cli author template -g typescript-node -o templates/javascript/"
```

## Update the generator config
Expand Down

0 comments on commit 6e8f846

Please sign in to comment.