#!/usr/bin/env python3 """ TODO could load shortcuts from environment """ import os from sys import argv, exit from typing import Tuple, List, Generator, Iterable, Sequence, Callable Args = Sequence[str] shortcuts = { 'bt': ['compileTestKotlin'], 'ct': ['componentTest'], 'cb': ['clean', 'build'], 'cbx': ['clean', 'build', '-x', 'test'], 'cbt': ['clean', 'build', 'compileTestKotlin'], '-nc': ['--no-build-cache'], '-rr': ['--rerun-tasks'] } optionsWithArgument = set(['-x', '-b', '-c', '-g', '-p', '-D', '-I', '-P']) help_text = """\ Support functionality for gw application Use cases gws clean build -x test :test :tools gws cb ct gws cbt gws cbx :microservices:afm-exec-api Options --show doesn't start gw app """ def show_help(): print(help_text) show_shortcuts() print() exit(1) def show_shortcuts(): print('Shortcuts') for key, value in shortcuts.items(): print(f" {key} ... {' '.join(value)}") def flat_map(f: Callable, xs: Sequence) -> Iterable: return (y for ys in xs for y in f(ys)) def try_remove(lst: List, value) -> Tuple[bool, List]: filtered = list(filter(lambda x: x != value, lst)) return len(lst) != len(filtered), filtered def split_options(args: Args) -> Generator[List[str], None, None]: iterator = iter(args) while True: try: arg = next(iterator) if arg in optionsWithArgument: yield [arg, next(iterator)] else: yield [arg] except StopIteration: break def mix_task_modules(modules: Sequence, tasks: Sequence) -> Generator[str, None, None]: return (module + ':' + task for module in modules for task in tasks) def remake(args: Args, modules: List[str]) -> List[str]: tasks = [] options = [] for item in split_options(args): if item[0].startswith('-'): options.extend(item) else: tasks.extend(item) if modules and tasks: return list(mix_task_modules(modules, tasks)) + options else: return tasks + modules + options def prepare_args(args: Args) -> List[str]: modules = list(filter(lambda value: value.startswith(':'), args)) args = (arg for arg in args if arg not in modules) args = flat_map(lambda arg: shortcuts.get(arg, [arg]), args) args = list(args) args = remake(args, modules) return list(args) def prepare_command(args: Args) -> str: args = prepare_args(args) return 'gw ' + ' '.join(args) def main(args: List[str]): if not args: show_help() just_show, args = try_remove(args, '--show') cmd = prepare_command(args) print(cmd) if not just_show: print() os.system(cmd) if __name__ == '__main__': main(argv[1:])