Remake gws
keep argument postion simplify mixim actions with modules
This commit is contained in:
131
bin/gws
131
bin/gws
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
TODO
|
TODO
|
||||||
could load shortcuts from environment
|
- could load shortcuts from environment
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
@@ -13,6 +13,7 @@ from itertools import chain
|
|||||||
Args = Sequence[str]
|
Args = Sequence[str]
|
||||||
StrIter = Iterable[str]
|
StrIter = Iterable[str]
|
||||||
|
|
||||||
|
GwCommand = 'gw'
|
||||||
DEBUG = False
|
DEBUG = False
|
||||||
|
|
||||||
shortcuts = {
|
shortcuts = {
|
||||||
@@ -34,6 +35,7 @@ shortcuts = {
|
|||||||
'br': ['bootRun'],
|
'br': ['bootRun'],
|
||||||
'rb': ['bootRun'],
|
'rb': ['bootRun'],
|
||||||
'jar': ['shadowJar'],
|
'jar': ['shadowJar'],
|
||||||
|
'-nt': ['-x', 'test'],
|
||||||
'-nc': ['--no-build-cache'],
|
'-nc': ['--no-build-cache'],
|
||||||
'-rr': ['--rerun-tasks'],
|
'-rr': ['--rerun-tasks'],
|
||||||
'-wl': ['--write-locks'],
|
'-wl': ['--write-locks'],
|
||||||
@@ -42,20 +44,28 @@ shortcuts = {
|
|||||||
'-el': ['-Penvironment=licensed'],
|
'-el': ['-Penvironment=licensed'],
|
||||||
'-lic': ['-Penvironment=licensed']
|
'-lic': ['-Penvironment=licensed']
|
||||||
}
|
}
|
||||||
|
actions = [
|
||||||
|
'clean',
|
||||||
|
'build',
|
||||||
|
'compileTestKotlin',
|
||||||
|
'test',
|
||||||
|
'componentTest',
|
||||||
|
'shadowJar'
|
||||||
|
]
|
||||||
|
|
||||||
optionsWithArgument = {'-x', '-b', '-c', '-g', '-p', '-D', '-I', '-P'}
|
|
||||||
|
|
||||||
help_text = """\
|
help_text = """\
|
||||||
Support functionality for gw application
|
Support functionality for gw application
|
||||||
|
|
||||||
Use cases
|
Use cases
|
||||||
gws clean build -x test :test :tools
|
gws cbx :tests :tools
|
||||||
gws cb ct
|
gws cb ct
|
||||||
gws cbt
|
gws cbt
|
||||||
gws cbx :microservices:afm-exec-api
|
gws cbx :microservices:afm-exec-api
|
||||||
|
|
||||||
Options
|
Options
|
||||||
--show doesn't start gw app
|
--show doesn't start gw app
|
||||||
|
--debug show details about processing
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
@@ -77,22 +87,54 @@ def log(name, obj):
|
|||||||
print(name + str(obj))
|
print(name + str(obj))
|
||||||
|
|
||||||
|
|
||||||
def flat_map(func, seq):
|
def is_module(arg: str):
|
||||||
for item in seq:
|
return arg.startswith(':')
|
||||||
for transformed in func(item):
|
|
||||||
yield transformed
|
|
||||||
|
|
||||||
|
|
||||||
def flatten(lst):
|
def flatten(lst):
|
||||||
return list(chain(*lst))
|
return list(chain(*lst))
|
||||||
|
|
||||||
|
|
||||||
def partition(predicate: Callable, seq: Iterable) -> Tuple[List, List]:
|
def replace_shortcut(arg: str) -> List[str]:
|
||||||
true_list: List = []
|
if arg in shortcuts:
|
||||||
false_list: List = []
|
return shortcuts[arg]
|
||||||
for item in seq:
|
else:
|
||||||
(true_list if predicate(item) else false_list).append(item)
|
return [arg]
|
||||||
return true_list, false_list
|
|
||||||
|
|
||||||
|
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]:
|
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
|
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]):
|
def main(args: List[str]):
|
||||||
if not args:
|
if not args:
|
||||||
show_help()
|
show_help()
|
||||||
|
|||||||
Reference in New Issue
Block a user