#!/usr/bin/env python3 """ TODO could load shortcuts from environment """ import os from sys import argv, exit from typing import Tuple, List, Generator, Iterable, Iterator, Sequence, Callable from itertools import chain Args = Sequence[str] StrIter = Iterable[str] DEBUG = False shortcuts = { 'c': ['clean'], 'b': ['build'], 't': ['test'], 'bt': ['compileTestKotlin'], 'ct': ['componentTest'], 'cb': ['clean', 'build'], 'cbx': ['clean', 'build', '-x', 'test'], 'cbt': ['clean', 'build', 'compileTestKotlin', '-x', 'test'], 'nd': ['-x', 'detekt'], '-nd': ['-x', 'detekt'], 'xt': ['-x', 'test'], 'br': ['bootRun'], 'rb': ['bootRun'], 'jar': ['shadowJar'], '-nc': ['--no-build-cache'], '-rr': ['--rerun-tasks'], '-wl': ['--write-locks'], '-rt': ['--rerun-tasks'], '-ef': ['-Penvironment=free'], '-el': ['-Penvironment=licensed'], '-lic': ['-Penvironment=licensed'] } optionsWithArgument = {'-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 log(name, obj): if DEBUG: print(name + str(obj)) def flat_map(func, seq): for item in seq: for transformed in func(item): yield transformed def flatten(lst): return list(chain(*lst)) def partition(predicate: Callable, seq: Iterable) -> Tuple[List, List]: true_list: List = [] false_list: List = [] for item in seq: (true_list if predicate(item) else false_list).append(item) return true_list, false_list def try_remove(lst: Sequence, value) -> Tuple[bool, Sequence]: filtered = list(filter(lambda x: x != value, lst)) return len(lst) != len(filtered), filtered def list_sub(lhs: Sequence, rhs: Sequence) -> Sequence: return [item for item in lhs if item not in rhs] def get_next(iterator: Iterator): try: item = next(iterator) return True, item except StopIteration: return False, None def pair_option_with_argument(args: StrIter) -> Generator[List[str], None, None]: iterator = iter(args) while True: has_next, option = get_next(iterator) if has_next: if option in optionsWithArgument: has_next, value = get_next(iterator) if has_next: yield [option, value] else: yield [option] return else: yield [option] else: return def mix_task_modules(modules: Sequence, tasks: Sequence) -> List[str]: return [module + ':' + task for module in modules for task in tasks] def remake(args: StrIter, modules: List[str]) -> List[str]: options, tasks = partition(lambda item: item[0].startswith('-'), args) log('options', options) log('tasks', tasks) if modules and tasks: return mix_task_modules(modules, flatten(tasks)) + flatten(options) else: return flatten(tasks + [modules] + options) def prepare_args(args: Args) -> List[str]: modules = list(filter(lambda value: value.startswith(':'), args)) log('modules', modules) _args = list_sub(args, modules) log('stage 1', _args) _args = list(flat_map(lambda arg: shortcuts.get(arg, [arg]), _args)) log('stage 2', _args) _args = list(pair_option_with_argument(_args)) log('stage 3', _args) _args = remake(_args, modules) log('stage 4', _args) 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() global DEBUG DEBUG, args = try_remove(args, '--debug') 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:])