From a6b1672413656072442caa0cc9ec5c0e5eb4786f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=A3=20Bida=20Vacaro?= Date: Sat, 18 Mar 2023 14:25:52 -0300 Subject: [PATCH] fix(groups): change groups to accept dict instead of list && improve help cmd (#28) * fix(groups): change groups to accept dict instead of list && improve help cmd * linter * Changes requests in the review * Update __main__.py --- .makim.yaml | 8 ++++---- makim/__main__.py | 16 +++++++--------- makim/makim.py | 16 ++++++---------- tests/.makim-bash-group-scope.yaml | 4 ++-- tests/.makim-bash-main-scope.yaml | 2 +- tests/.makim-bash-target-scope.yaml | 2 +- tests/.makim-complex.yaml | 18 +++++++++--------- tests/.makim-simple.yaml | 2 +- tests/.makim-unittest.yaml | 2 +- 9 files changed, 32 insertions(+), 38 deletions(-) diff --git a/.makim.yaml b/.makim.yaml index fd1b3c2..652151f 100644 --- a/.makim.yaml +++ b/.makim.yaml @@ -1,6 +1,6 @@ version: 1.0.0 groups: - - name: default + default: targets: clean: help: Clean unnecessary temporary files @@ -18,7 +18,7 @@ groups: rm -fr htmlcov/ rm -fr .pytest_cache - - name: docs + docs: env-file: .env targets: build: @@ -30,7 +30,7 @@ groups: help: Preview documentation page locally run: mkdocs serve --watch docs --config-file docs/mkdocs.yaml - - name: release + release: env-file: .env vars: app: | @@ -54,7 +54,7 @@ groups: help: Run semantic release in dry-run mode run: {{ app }} '--dry-run' - - name: tests + tests: targets: lint: help: Run linter tools diff --git a/makim/__main__.py b/makim/__main__.py index 794b345..f9dc462 100644 --- a/makim/__main__.py +++ b/makim/__main__.py @@ -53,7 +53,6 @@ def _get_args(): add_help=False, formatter_class=CustomHelpFormatter, ) - parser.add_argument( '--help', '-h', @@ -93,14 +92,12 @@ def _get_args(): makim_file = makim_file_default makim.load(makim_file) - target_help = [] - - for group in makim.config_data['groups']: - for target_name, target_data in group['targets'].items(): - target_name_qualified = f"{group['name']}.{target_name}" + groups = makim.config_data.get('groups', []) + for group in groups: + for target_name, target_data in groups[group]['targets'].items(): + target_name_qualified = f'{group}.{target_name}' help_text = target_data['help'] if 'help' in target_data else '' - target_help.append(f' {target_name_qualified} => {help_text}') if 'args' in target_data: @@ -111,7 +108,6 @@ def _get_args(): f' --{arg_name}: ({arg_data["type"]}) ' f'{arg_data["help"]}' ) - target_help.append("NOTE: 'default.' prefix is optional.") parser.add_argument( @@ -180,6 +176,9 @@ def app(): args_parser = _get_args() args = args_parser.parse_args() + if not args.target or args.help: + return args_parser.print_help() + if args.help: return args_parser.print_help() @@ -188,7 +187,6 @@ def app(): makim.load(args.makim_file) makim_args.update(dict(args._get_kwargs())) - return makim.run(makim_args) diff --git a/makim/makim.py b/makim/makim.py index f583c85..a82fd2c 100644 --- a/makim/makim.py +++ b/makim/makim.py @@ -130,21 +130,19 @@ def _change_group_data(self, group_name=None): if group_name is not None: self.group_name = group_name - shell_app_default = self.config_data.get('shell', 'xonsh') - if self.group_name == 'default' and len(groups) == 1: - self.group_data = groups[0] - self.group_name = groups[0]['name'] + group = list(groups)[0] + self.group_data = groups[group] shell_app = self.group_data.get('shell', shell_app_default) self._load_shell_app(shell_app) return - for g in groups: - if g['name'] == self.group_name: - self.group_data = g - shell_app = g.get('shell', shell_app_default) + for group in groups: + if group == self.group_name: + self.group_data = groups[group] + shell_app = groups[group].get('shell', shell_app_default) self._load_shell_app(shell_app) return @@ -162,7 +160,6 @@ def _load_shell_args(self): def _run_dependencies(self, args: dict): if not self.target_data.get('dependencies'): return - makim_dep = deepcopy(self) args_dep_original = { 'makim_file': args['makim_file'], @@ -222,7 +219,6 @@ def _run_dependencies(self, args: dict): def _run_command(self, args: dict): cmd = self.target_data.get('run', '').strip() - if 'vars' not in self.group_data: self.group_data['vars'] = {} diff --git a/tests/.makim-bash-group-scope.yaml b/tests/.makim-bash-group-scope.yaml index e213afb..eb6e130 100644 --- a/tests/.makim-bash-group-scope.yaml +++ b/tests/.makim-bash-group-scope.yaml @@ -1,7 +1,7 @@ version: 1.0.0 env-file: .env groups: - - name: group-scope + group-scope: shell: bash targets: test: @@ -17,7 +17,7 @@ groups: export MAKIM_TEST=$(pwd) echo ${MAKIM_TEST} - - name: group-deps + group-deps: targets: dep: help: dependency using xonsh diff --git a/tests/.makim-bash-main-scope.yaml b/tests/.makim-bash-main-scope.yaml index 82dfe09..df5c595 100644 --- a/tests/.makim-bash-main-scope.yaml +++ b/tests/.makim-bash-main-scope.yaml @@ -2,7 +2,7 @@ version: 1.0.0 env-file: .env shell: bash groups: - - name: main-scope + main-scope: targets: test: help: Test bash defined in the main scope diff --git a/tests/.makim-bash-target-scope.yaml b/tests/.makim-bash-target-scope.yaml index c18320f..d2573ad 100644 --- a/tests/.makim-bash-target-scope.yaml +++ b/tests/.makim-bash-target-scope.yaml @@ -1,7 +1,7 @@ version: 1.0.0 env-file: .env groups: - - name: target-scope + target-scope: targets: dep: help: dependency using xonsh diff --git a/tests/.makim-complex.yaml b/tests/.makim-complex.yaml index 4b04e8b..12a9053 100644 --- a/tests/.makim-complex.yaml +++ b/tests/.makim-complex.yaml @@ -1,13 +1,13 @@ version: 1.0.0 env-file: .env groups: - - name: default + default: targets: lint: help: Run linter tools run: echo "[II] Run linter" - - name: build + build: targets: clean-gcda: help: Remove temporary gcda files @@ -71,19 +71,19 @@ groups: clean: {{ args.clean }} asan-options: "fast_unwind_on_malloc=0" - - name: env + env: targets: create-file: help: Create a dot env file run: echo "[II] Create a dot env file" - - name: conda + conda: targets: build: help: Create the conda package run: echo "[II] Create the conda package" - - name: release + release: vars: app: echo targets: @@ -95,7 +95,7 @@ groups: help: Run semantic-release on CI for tests in dry-run mode. run: {{ app }} --dry-run - - name: docs + docs: targets: api: help: Build API docs @@ -119,7 +119,7 @@ groups: - target: docs.api run: echo "[II] Preview documentation result locally" - - name: tests + tests: targets: sanitizer: help: Run sanitizer tests @@ -154,13 +154,13 @@ groups: - target: tests.sanitizer - target: tests.examples - - name: debug + debug: targets: fibonacci: help: Debug package via an example file (fibonacci) run: echo "[II] Debug package via an example file" - - name: print + print: targets: local-env-vars: help: Print environment variables diff --git a/tests/.makim-simple.yaml b/tests/.makim-simple.yaml index 93198d1..1e32db9 100644 --- a/tests/.makim-simple.yaml +++ b/tests/.makim-simple.yaml @@ -1,7 +1,7 @@ version: 1.0.0 env-file: .env groups: - - name: default + default: targets: clean: help: Use this target to clean up temporary files diff --git a/tests/.makim-unittest.yaml b/tests/.makim-unittest.yaml index 8ac010f..35bf6b4 100644 --- a/tests/.makim-unittest.yaml +++ b/tests/.makim-unittest.yaml @@ -1,7 +1,7 @@ version: 1.0.0 env-file: .env groups: - - name: tests + tests: targets: test-1: help: test-1 args `all` should be false