Skip to content

Commit c6004a8

Browse files
jimschubertwing328
authored andcommitted
Adds a simple bash completion script (#277)
* Adds a simple bash completion script This works with any loading script named openapi-generator-cli. That is, if you've installed via homebrew or created a script similar to https://gist.github.com/jimschubert/ce241b0c78140e364f46914ef8ec4103 This script is relatively simple, relying on fallback to the recently add "completion" command to the CLI project. The script includes a possible extension to allow for per-language options to autocomplete when the user is applying additional properties. This work is currently commented out, as it may be simplified a bit in the CLI first. * Add launcher script and "install" instructions
1 parent 85f0909 commit c6004a8

3 files changed

Lines changed: 149 additions & 0 deletions

File tree

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,49 @@ export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
167167
export PATH=${JAVA_HOME}/bin:$PATH
168168
```
169169

170+
### Launcher Script
171+
172+
One downside to manual jar downloads is that you don't keep up-to-date with the latest released version. We have a Bash launcher script at [bin/utils/openapi-generator.cli.sh](./bin/utils/openapi-generator.cli.sh) which resolves this issue.
173+
174+
To install the launcher script, copy the contents of the script to a location on your path and make the script executable.
175+
176+
An example of setting this up (NOTE: Always evaluate scripts curled from external systems before executing them).
177+
178+
```
179+
mkdir -p ~/bin/openapitools
180+
curl https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/bin/utils/openapi-generator.cli.sh > ~/bin/openapitools/openapi-generator-cli
181+
chmod u+x ~/bin/openapitools/openapi-generator-cli
182+
export PATH=$PATH:~/bin/openapitools/
183+
```
184+
185+
Now, `openapi-generator-cli` is "installed". On invocation, it will query the GitHub repository for the most recently released version. If this matches the last downloaded jar,
186+
it will execute as normal. If a newer version is found, the script will download the latest release and execute it.
187+
188+
If you need to invoke an older version of the generator, you can define the variable `OPENAPI_GENERATOR_VERSION` either ad hoc or globally. You can export this variable if you'd like to persist a specific release version.
189+
190+
Examples:
191+
192+
```
193+
# Execute latest released openapi-generator-cli
194+
openapi-generator-cli version
195+
196+
# Execute version 3.1.0 for the current invocation, regardless of the latest released version
197+
OPENAPI_GENERATOR_VERSION=3.1.0 openapi-generator-cli version
198+
199+
# Execute version 3.1.0-SNAPSHOT for the current invocation
200+
OPENAPI_GENERATOR_VERSION=3.1.0-SNAPSHOT openapi-generator-cli version
201+
202+
# Execute version 3.0.2 for every invocation in the current shell session
203+
export OPENAPI_GENERATOR_VERSION=3.0.2
204+
openapi-generator-cli version # is 3.0.2
205+
openapi-generator-cli version # is also 3.0.2
206+
207+
# To "install" a specific version, set the variable in .bashrc/.bash_profile
208+
echo "export OPENAPI_GENERATOR_VERSION=3.0.2" >> ~/.bashrc
209+
source ~/.bashrc
210+
openapi-generator-cli version # is always 3.0.2, unless any of the above overrides are done ad hoc
211+
```
212+
170213
### [1.4 - Build Projects](#table-of-contents)
171214

172215
To build from source, you need the following installed and available in your `$PATH:`

bin/utils/openapi-generator-cli.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env bash
2+
####
3+
# Save as openapi-generator-cli on your PATH. chmod u+x. Enjoy.
4+
#
5+
# This script will query github on every invocation to pull the latest released version
6+
# of openapi-generator.
7+
#
8+
# If you want repeatable executions, you can explicitly set a version via
9+
# OPENAPI_GENERATOR_VERSION
10+
# e.g. (in Bash)
11+
# export OPENAPI_GENERATOR_VERSION=3.1.0
12+
# openapi-generator-cli.sh
13+
# or
14+
# OPENAPI_GENERATOR_VERSION=3.1.0 openapi-generator-cli.sh
15+
#
16+
# This is also helpful, for example, if you want want to evaluate a SNAPSHOT version.
17+
#
18+
# NOTE: Jars are downloaded on demand from maven into the same directory as this script
19+
# for every 'latest' version pulled from github. Consider putting this under its own directory.
20+
####
21+
set -o pipefail
22+
23+
for cmd in {mvn,python,curl}; do
24+
if ! command -v ${cmd} > /dev/null; then
25+
>&2 echo "This script requires '${cmd}' to be installed."
26+
exit 1
27+
fi
28+
done
29+
30+
function latest.tag {
31+
local uri="https://api.github.com/repos/${1}/tags"
32+
curl -s ${uri} | python -c "import sys, json; print json.load(sys.stdin)[0]['name'][1:]"
33+
}
34+
35+
ghrepo=openapitools/openapi-generator
36+
groupid=org.openapitools
37+
artifactid=openapi-generator-cli
38+
ver=${OPENAPI_GENERATOR_VERSION:-$(latest.tag $ghrepo)}
39+
40+
jar=${artifactid}-${ver}.jar
41+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
42+
43+
if [ ! -f ${DIR}/${jar} ]; then
44+
repo="central::default::https://repo1.maven.apache.org/maven2"
45+
if [[ ${ver} =~ ^.*-SNAPSHOT$ ]]; then
46+
repo="central::default::https://oss.sonatype.org/content/repositories/snapshots"
47+
fi
48+
mvn org.apache.maven.plugins:maven-dependency-plugin:2.9:get \
49+
-DremoteRepositories=${repo} \
50+
-Dartifact=${groupid}:${artifactid}:${ver} \
51+
-Dtransitive=false \
52+
-Ddest=${DIR}/${jar}
53+
fi
54+
55+
java -ea \
56+
${JAVA_OPTS} \
57+
-Xms512M \
58+
-Xmx1024M \
59+
-server \
60+
-jar ${DIR}/${jar} "$@"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
3+
###
4+
# Provides completion assistance for openapi-generator-cli
5+
# Install
6+
# Mac:
7+
# brew install bash-completion
8+
# cp openapi-generator-cli-completion.bash `brew --prefix`/etc/bash_completion.d
9+
# Linux: many distributions include this automatically. Search for your distro-specific instructions.
10+
# When in doubt, try sourcing this file:
11+
# type complete && source openapi-generator-cli
12+
#
13+
# see http://tldp.org/LDP/abs/html/tabexpansion.html
14+
###
15+
16+
_openapi_generator_cli_completions() {
17+
COMPREPLY=()
18+
local IFS=$' \t\n'
19+
local options=()
20+
21+
options+=("$($1 completion ${COMP_WORDS[@]:1})")
22+
23+
case "${COMP_WORDS[1]}" in
24+
generate)
25+
case "${COMP_WORDS[@]:2}" in
26+
-l|--lang|-g|--generator-name)
27+
# TODO: This is a nice-to-have and not required.
28+
# Apply generator-specific options to additional properties. These can be queried via:
29+
# openapi-generator-cli config-help -l YOUR_LANG | grep '^\t' | grep -v '^\t\s\s\s\s' | tr -d '\t'
30+
# where YOUR_LANG would need to be evaluated as the value after the current switch.
31+
# but rather than switching on 'generate' maybe switch on --additional-properties?
32+
;;
33+
esac
34+
;;
35+
*)
36+
# ignore
37+
;;
38+
esac
39+
40+
# printf '%s\n' "${options[@]}"
41+
if [[ -n "${options[@]}" ]]; then
42+
COMPREPLY=( $(compgen -W "${options}" -- ${2}) )
43+
fi
44+
}
45+
46+
complete -F _openapi_generator_cli_completions openapi-generator-cli

0 commit comments

Comments
 (0)