gws refactoring
type tips string formatting
This commit is contained in:
34
bin/gws
34
bin/gws
@@ -7,6 +7,9 @@ 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'],
|
||||
@@ -30,7 +33,7 @@ Use cases
|
||||
gws cbx :microservices:afm-exec-api
|
||||
|
||||
Options
|
||||
--dry doesn't start gw app
|
||||
--show doesn't start gw app
|
||||
"""
|
||||
|
||||
|
||||
@@ -44,14 +47,19 @@ def show_help():
|
||||
def show_shortcuts():
|
||||
print('Shortcuts')
|
||||
for key, value in shortcuts.items():
|
||||
print(' {} ... {}'.format(key, ' '.join(value)))
|
||||
print(f" {key} ... {' '.join(value)}")
|
||||
|
||||
|
||||
def flat_map(f, xs):
|
||||
def flat_map(f: Callable, xs: Sequence) -> Iterable:
|
||||
return (y for ys in xs for y in f(ys))
|
||||
|
||||
|
||||
def split_options(args):
|
||||
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:
|
||||
@@ -64,11 +72,11 @@ def split_options(args):
|
||||
break
|
||||
|
||||
|
||||
def mix_task_modules(modules, tasks):
|
||||
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, modules):
|
||||
def remake(args: Args, modules: List[str]) -> List[str]:
|
||||
tasks = []
|
||||
options = []
|
||||
for item in split_options(args):
|
||||
@@ -82,26 +90,28 @@ def remake(args, modules):
|
||||
return tasks + modules + options
|
||||
|
||||
|
||||
def prepare_args(args):
|
||||
def prepare_args(args: Args) -> List[str]:
|
||||
modules = list(filter(lambda value: value.startswith(':'), args))
|
||||
args = (arg for arg in argv[1:] if arg not in modules)
|
||||
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):
|
||||
def prepare_command(args: Args) -> str:
|
||||
args = prepare_args(args)
|
||||
return 'gw ' + ' '.join(args)
|
||||
|
||||
|
||||
def main(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 '--dry' not in args:
|
||||
print(cmd)
|
||||
if not just_show:
|
||||
print()
|
||||
os.system(cmd)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user