Skip to content

Commit

Permalink
feat: Change dependencies to hooks (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijeetSaroha authored Oct 4, 2024
1 parent 7d617ce commit be5c25b
Show file tree
Hide file tree
Showing 17 changed files with 298 additions and 190 deletions.
55 changes: 37 additions & 18 deletions .makim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,29 @@ groups:

smoke:
help: Run smoke tests
dependencies:
- task: smoke-tests.simple
- task: smoke-tests.complex
- task: smoke-tests.containers
- task: smoke-tests.unittest
- task: smoke-tests.vars-env
- task: smoke-tests.test-vars
- task: smoke-tests.bash
- task: smoke-tests.dir-absolute-path
- task: smoke-tests.dir-no-path
- task: smoke-tests.dir-relative-path
- task: smoke-tests.interactive-args
hooks:
pre-run:
- task: smoke-tests.simple
- task: smoke-tests.complex
- task: smoke-tests.containers
- task: smoke-tests.unittest
- task: smoke-tests.vars-env
- task: smoke-tests.test-vars
- task: smoke-tests.bash
- task: smoke-tests.dir-absolute-path
- task: smoke-tests.dir-no-path
- task: smoke-tests.dir-relative-path
- task: smoke-tests.interactive-args
- task: smoke-tests.run-hooks

ci:
help: Run all tasks used on CI
dependencies:
- task: tests.linter
- task: tests.unittest
- task: tests.smoke
- task: docs.build
hooks:
pre-run:
- task: tests.linter
- task: tests.unittest
- task: tests.smoke
- task: docs.build

smoke-tests:
tasks:
Expand Down Expand Up @@ -356,7 +359,7 @@ groups:
action: store_true
env:
MAKIM_FILE: tests/smoke/.makim-interactive-args.yaml
shell: bash
backend: bash
run: |
export VERBOSE_FLAG='${{ "--verbose" if args.verbose_mode else "" }}'
makim $VERBOSE_FLAG --file $MAKIM_FILE --help
Expand All @@ -372,6 +375,22 @@ groups:
exit 1
fi
run-hooks:
help: Test makim with pre-run and post-run hooks
args:
verbose-mode:
help: Run the all the tests in verbose mode
type: bool
action: store_true
env:
MAKIM_FILE: tests/smoke/.makim-hooks.yaml
backend: bash
run: |
export VERBOSE_FLAG='${{ "--verbose" if args.verbose_mode else "" }}'
makim $VERBOSE_FLAG --file $MAKIM_FILE --help
makim $VERBOSE_FLAG --file $MAKIM_FILE --version
makim $VERBOSE_FLAG --file $MAKIM_FILE build.compile
error:
help: This group helps tests failure tasks
tasks:
Expand Down
55 changes: 29 additions & 26 deletions docs/tutorials/introduction/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@
" type: bool\n",
" action: store_true\n",
" help: if not set, the clean dependency will not be triggered.\n",
" dependencies:\n",
" - task: clean.tmp\n",
" if: ${{ args.clean == true }}\n",
" hooks:\n",
" pre-run:\n",
" - task: clean.tmp\n",
" if: ${{ args.clean == true }}\n",
" run: |\n",
" echo \"Runnint unit tests...\""
]
Expand Down Expand Up @@ -388,12 +389,13 @@
"\n",
" run-all:\n",
" help: Run tests for all the other tasks\n",
" dependencies:\n",
" - task: node\n",
" - task: perl\n",
" - task: python\n",
" - task: r\n",
" - task: sh"
" hooks:\n",
" pre-run:\n",
" - task: node\n",
" - task: perl\n",
" - task: python\n",
" - task: r\n",
" - task: sh"
]
},
{
Expand Down Expand Up @@ -702,23 +704,24 @@
" args:\n",
" i-am-sure:\n",
" type: bool\n",
" dependencies:\n",
" - task: print.name\n",
" if: ${{ args.i_am_sure == true }}\n",
" args:\n",
" name: Mary\n",
" - task: print.name\n",
" if: ${{ args.i_am_sure == true }}\n",
" args:\n",
" name: John\n",
" - task: print.name\n",
" if: ${{ args.i_am_sure == true }}\n",
" args:\n",
" name: Ellen\n",
" - task: print.name\n",
" if: ${{ args.i_am_sure == true }}\n",
" args:\n",
" name: Marc"
" hokos:\n",
" pre-run:\n",
" - task: print.name\n",
" if: ${{ args.i_am_sure == true }}\n",
" args:\n",
" name: Mary\n",
" - task: print.name\n",
" if: ${{ args.i_am_sure == true }}\n",
" args:\n",
" name: John\n",
" - task: print.name\n",
" if: ${{ args.i_am_sure == true }}\n",
" args:\n",
" name: Ellen\n",
" - task: print.name\n",
" if: ${{ args.i_am_sure == true }}\n",
" args:\n",
" name: Marc"
]
},
{
Expand Down
7 changes: 4 additions & 3 deletions docs/tutorials/makim-for-nox-users/.makim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ groups:
all:
help: Run nox tests
dependencies:
- task: nox.tests
- task: nox.lint
hooks:
pre-run:
- task: nox.tests
- task: nox.lint
7 changes: 4 additions & 3 deletions docs/tutorials/makim-for-nox-users/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,10 @@
"\n",
" all:\n",
" help: Run nox tests\n",
" dependencies:\n",
" - task: nox.tests\n",
" - task: nox.lint"
" hooks:\n",
" pre-run:\n",
" - task: nox.tests\n",
" - task: nox.lint"
]
},
{
Expand Down
42 changes: 21 additions & 21 deletions src/makim/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,18 +446,17 @@ def _load_task_args(self):
)

# run commands

def _run_dependencies(self, args: dict):
if not self.task_data.get('dependencies'):
def _run_hooks(self, args: dict, hook_type: str):
if not self.task_data.get('hooks', {}).get(hook_type):
return
makim_dep = deepcopy(self)
args_dep_original = {
makim_hook = deepcopy(self)
args_hook_original = {
'help': args.get('help', False),
'version': args.get('version', False),
'args': {},
}

makim_dep._change_group_data()
makim_hook._change_group_data()

# clean double dash prefix in args
original_args_clean = {}
Expand All @@ -470,46 +469,46 @@ def _run_dependencies(self, args: dict):
else arg_value
)

for dep_data in self.task_data['dependencies']:
env, variables = makim_dep._load_scoped_data('task')
for hook_data in self.task_data['hooks'][hook_type]:
env, variables = makim_hook._load_scoped_data('task')
for k, v in env.items():
os.environ[k] = v

makim_dep.env_scoped = deepcopy(env)
args_dep = {}
makim_hook.env_scoped = deepcopy(env)
args_hook = {}

# update the arguments
for arg_name, arg_value in dep_data.get('args', {}).items():
for arg_name, arg_value in hook_data.get('args', {}).items():
unescaped_value = (
str(arg_value)
if isinstance(arg_value, str)
else str(arg_value)
)

args_dep[f'--{arg_name}'] = yaml.safe_load(
args_hook[f'--{arg_name}'] = yaml.safe_load(
TEMPLATE.from_string(unescaped_value).render(
args=original_args_clean, env=makim_dep.env_scoped
args=original_args_clean, env=makim_hook.env_scoped
)
)

args_dep['task'] = dep_data['task']
args_dep.update(args_dep_original)
args_hook['task'] = hook_data['task']
args_hook.update(args_hook_original)

# checking for the conditional statement
if_stmt = dep_data.get('if')
if_stmt = hook_data.get('if')
if if_stmt:
result = TEMPLATE.from_string(str(if_stmt)).render(
args=original_args_clean, env=self.env_scoped
)
if not yaml.safe_load(result):
if self.verbose:
MakimLogs.print_info(
'[II] Skipping dependency: '
f'{dep_data.get("task")}'
f'[II] Skipping {hook_type} hook: '
f'{hook_data.get("task")}'
)
continue

makim_dep.run(deepcopy(args_dep))
makim_hook.run(deepcopy(args_hook))

def _run_command(self, args: dict):
cmd = self.task_data.get('run', '').strip()
Expand Down Expand Up @@ -588,7 +587,7 @@ def _run_command(self, args: dict):
os.environ.clear()
os.environ.update(self.env_scoped)

# public methods
# public methods

def load(self, file: str, dry_run: bool = False, verbose: bool = False):
"""Load makim configuration."""
Expand Down Expand Up @@ -618,5 +617,6 @@ def run(self, args: dict):
'Condition (if) not satisfied.'
)

self._run_dependencies(args)
self._run_hooks(args, 'pre-run')
self._run_command(args)
self._run_hooks(args, 'post-run')
5 changes: 3 additions & 2 deletions tests/smoke/.makim-bash-group-scope.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ groups:
type: bool
action: store_true
help: Remove all files that are tracked by git
dependencies:
- task: group-deps.dep
hooks:
pre-run:
- task: group-deps.dep
run: |
export MAKIM_TEST=$(pwd)
echo ${MAKIM_TEST}
Expand Down
5 changes: 3 additions & 2 deletions tests/smoke/.makim-bash-task-scope.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ groups:
test:
backend: bash
help: Test bash defined in the task scope
dependencies:
- task: task-scope.dep
hooks:
pre-run:
- task: task-scope.dep
run: |
export MAKIM_TEST=$(pwd)
echo ${MAKIM_TEST}
Loading

0 comments on commit be5c25b

Please sign in to comment.