Skip to content

Commit

Permalink
allow specifying existing JAR for open api client build
Browse files Browse the repository at this point in the history
  • Loading branch information
dansan committed Nov 25, 2019
1 parent 72d7492 commit 5bb114a
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions update_openapi_client
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ JAVA_COMPILE_CMD = f"java -jar {{jar}} {OPENAPI_GENERATE_COMMAND_COMMON} -i {{sc
OPENAPI_SCHEMA_URL = "https://{host}/univention/udm/openapi.json"


class OpenAPILibGenerationError(Exception):
pass


def coro(f):
"""asyncio for click (https://github.com/pallets/click/issues/85)"""

Expand Down Expand Up @@ -109,9 +113,10 @@ def download_jar(target_path: Path):
print_error_and_exit(str(exc))


def run_openapi_generator_local_java(target_dir, quiet):
jar_path = Path(target_dir, "openapi-generator.jar")
download_jar(jar_path)
def run_openapi_generator_local_java(target_dir: str, quiet: bool, jar_path: Path = None):
if not jar_path:
jar_path = Path(target_dir, "openapi-generator.jar")
download_jar(jar_path)
cmd = JAVA_COMPILE_CMD.format(
jar=jar_path,
schema=str(Path(target_dir, TARGET_SCHEMA_FILENAME)),
Expand All @@ -120,7 +125,9 @@ def run_openapi_generator_local_java(target_dir, quiet):
click.echo(f"Starting to generate Python code...")
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = process.communicate()
if not quiet:
if process.returncode:
print_error_and_exit(stdout.decode())
elif not quiet:
click.echo(stdout.decode())


Expand Down Expand Up @@ -161,6 +168,12 @@ def get_openapi_schema(host: str, secure: bool) -> str:
"or as a JAR file. Requires either a running Docker daemon or a local"
" Java (>=8) installation.",
)
@click.option(
"--jar",
type=click.Path(exists=True, readable=True),
help="OpenAPI Generator JAR file to use with '--generator java'. If not"
"given, the JAR file will be downloaded automatically.",
)
@click.option(
"--keep-image/--dont-keep-image",
default=False,
Expand All @@ -179,10 +192,12 @@ def get_openapi_schema(host: str, secure: bool) -> str:
)
@coro
async def update_openapi_client(
host: str, generator: str, keep_image: bool, secure: bool, quiet: bool
host: str, generator: str, jar: str, keep_image: bool, secure: bool, quiet: bool
):
if not host:
print_error_and_exit("Address (FQDN or IP address) of UCS host required.")
if jar and generator != "java":
print_error_and_exit("The --jar option can only be used together with '--generator java'.")
txt = get_openapi_schema(host, secure)
with TemporaryDirectory() as temp_dir, open(
Path(temp_dir, TARGET_SCHEMA_FILENAME), "w"
Expand All @@ -197,7 +212,7 @@ async def update_openapi_client(
if generator == "docker":
run_openapi_generator_docker_container(temp_dir, quiet, keep_image)
else:
run_openapi_generator_local_java(temp_dir, quiet)
run_openapi_generator_local_java(temp_dir, quiet, Path(jar))
click.echo("Installing package via pip...")
pip_main.main(
["install", "--compile", "--upgrade", str(Path(temp_dir, "python/"))]
Expand Down

0 comments on commit 5bb114a

Please sign in to comment.