Remake gws

keep argument postion
simplify mixim actions with modules
This commit is contained in:
Martin Blazik
2021-11-24 10:05:34 +01:00
parent d0b87c96df
commit 4aea4e5456

133
bin/gws
View File

@@ -2,7 +2,7 @@
"""
TODO
could load shortcuts from environment
- could load shortcuts from environment
"""
import os
@@ -13,6 +13,7 @@ from itertools import chain
Args = Sequence[str]
StrIter = Iterable[str]
GwCommand = 'gw'
DEBUG = False
shortcuts = {
@@ -34,6 +35,7 @@ shortcuts = {
'br': ['bootRun'],
'rb': ['bootRun'],
'jar': ['shadowJar'],
'-nt': ['-x', 'test'],
'-nc': ['--no-build-cache'],
'-rr': ['--rerun-tasks'],
'-wl': ['--write-locks'],
@@ -42,20 +44,28 @@ shortcuts = {
'-el': ['-Penvironment=licensed'],
'-lic': ['-Penvironment=licensed']
}
optionsWithArgument = {'-x', '-b', '-c', '-g', '-p', '-D', '-I', '-P'}
actions = [
'clean',
'build',
'compileTestKotlin',
'test',
'componentTest',
'shadowJar'
]
help_text = """\
Support functionality for gw application
Use cases
gws clean build -x test :test :tools
gws cbx :tests :tools
gws cb ct
gws cbt
gws cbx :microservices:afm-exec-api
Options
--show doesn't start gw app
--debug show details about processing
"""
@@ -77,22 +87,54 @@ def log(name, obj):
print(name + str(obj))
def flat_map(func, seq):
for item in seq:
for transformed in func(item):
yield transformed
def is_module(arg: str):
return arg.startswith(':')
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 replace_shortcut(arg: str) -> List[str]:
if arg in shortcuts:
return shortcuts[arg]
else:
return [arg]
def split_on_condition(lst: Sequence, condtion):
good = []
bad = []
for item in lst:
(bad, good)[condtion(item)].append(item)
return good, bad
def pair_module_with_action(args: Args) -> List[str]:
modules, no_module = split_on_condition(args, is_module)
log("modules", modules)
log("args", no_module)
if modules:
_args = []
for arg in no_module:
if arg in actions:
for module in modules:
_args.append(module + ':' + arg)
else:
_args.append(arg)
return _args
else:
return args
def prepare_args(args: Args) -> List[str]:
args = flatten(map(replace_shortcut, args))
args = pair_module_with_action(args)
return args
def prepare_command(args: Args) -> str:
return GwCommand + ' ' + ' '.join(prepare_args(args))
def try_remove(lst: Sequence, value) -> Tuple[bool, Sequence]:
@@ -100,69 +142,6 @@ def try_remove(lst: Sequence, value) -> Tuple[bool, Sequence]:
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()