Skip to content

Commit

Permalink
feat: Add support for more shell apps (osl-incubator#84)
Browse files Browse the repository at this point in the history
Add support for more shell apps, allowing the usage of flags/parameters in the `shell` attribute, for example: `shell: "my-command -f"`.
  • Loading branch information
xmnlab authored Jan 18, 2024
1 parent b3326ba commit bd75fad
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ jobs:
if: ${{ matrix.os != 'macos' }}
run: makim smoke-tests.containers

- name: Run smoke tests using different interpreters
run: makim smoke-tests.shell-app

- name: Run smoke tests using unittest makim file
run: makim smoke-tests.unittest

Expand Down
15 changes: 15 additions & 0 deletions .makim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ groups:
cd ./tests/
makim $VERBOSE_FLAG --file $MAKIM_FILE containers.run
shell-app:
help: Test makim with working-directory for global no-path and its various combinations with group and target working-directory
args:
verbose-mode:
help: Run the all the tests in verbose mode
type: bool
action: store_true
env:
MAKIM_FILE: tests/.makim-interpreters.yaml
shell: bash
run: |
export VERBOSE_FLAG='{{ "--verbose" if args.verbose_mode else "" }}'
export MAKIM_FILE="$(pwd)/${MAKIM_FILE}"
makim $VERBOSE_FLAG --file $MAKIM_FILE main.all
unittest:
help: Test makim using a unittest makimfile
args:
Expand Down
9 changes: 9 additions & 0 deletions conda/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,14 @@ channels:
- nodefaults
- conda-forge
dependencies:
- bash
- python 3.8.1 # min version supported
- poetry >=1.5
- nodejs
- perl
- r-base
- sh
- pip
- pip:
# note: workaround ci issue for macos
- setuptools >= 40.8.0
32 changes: 21 additions & 11 deletions src/makim/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
the way to define targets and dependencies. Instead of using the
`Makefile` format, it uses `yaml` format.
"""
from __future__ import annotations

import io
import os
import pprint
Expand All @@ -14,7 +16,7 @@

from copy import deepcopy
from pathlib import Path
from typing import Dict, Optional, Tuple, Union
from typing import Optional, Union

import dotenv
import sh
Expand All @@ -30,6 +32,12 @@
SCOPE_TARGET = 2


KNOWN_SHELL_APP_ARGS = {
'bash': ['-e'],
'php': ['-f'],
}


def escape_template_tag(v: str) -> str:
"""Escape template tag when processing the template config file."""
return v.replace('{{', r'\{\{').replace('}}', r'\}\}')
Expand Down Expand Up @@ -61,6 +69,7 @@ class Makim(PrintPlugin):
verbose: bool = False
global_data: dict = {}
shell_app: sh.Command = sh.xonsh
shell_args: list[str] = []

# temporary variables
env: dict = {} # initial env
Expand All @@ -84,6 +93,7 @@ def __init__(self):
self.file = '.makim.yaml'
self.dry_run = False
self.verbose = False
self.shell_args: list[str] = []

def _call_shell_app(self, cmd):
fd, filepath = tempfile.mkstemp(suffix='.makim', text=True)
Expand Down Expand Up @@ -228,10 +238,17 @@ def update_working_directory(

return working_dir

def _load_shell_app(self, shell_app: str = ''):
def _load_shell_app(self, shell_app: str = '') -> None:
if not shell_app:
shell_app = self.global_data.get('shell', 'xonsh')
self.shell_app = getattr(sh, shell_app)
return

cmd = shell_app.split(' ')
cmd_name = cmd[0]
self.shell_app = getattr(sh, cmd_name)

args: list[str] = KNOWN_SHELL_APP_ARGS.get(cmd_name, [])
self.shell_args = args + cmd[1:]

def _load_dotenv(self, data_scope: dict) -> dict:
env_file = data_scope.get('env-file')
Expand All @@ -251,7 +268,7 @@ def _load_dotenv(self, data_scope: dict) -> dict:

def _load_scoped_data(
self, scope: str
) -> Tuple[Dict[str, str], Dict[str, str]]:
) -> tuple[dict[str, str], dict[str, str]]:
scope_options = ('global', 'group', 'target')
if scope not in scope_options:
raise Exception(f'The given scope `{scope}` is not valid.')
Expand Down Expand Up @@ -332,13 +349,6 @@ def _load_target_args(self):
default if default is not None else False if is_bool else None
)

@property
def shell_args(self):
"""Return the arguments for the defined shell app."""
if self.shell_app.__dict__['__name__'].endswith('bash'):
return ['-e']
return []

# run commands

def _run_dependencies(self, args: dict):
Expand Down
43 changes: 43 additions & 0 deletions tests/.makim-interpreters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: 1.0
env-file: .env
groups:
main:
targets:
node:
help: Test using nodejs
shell: node
run: console.log("Hello, World!");
perl:
help: Test using perl
shell: perl
run: print "Hello, World!\n";

php:
help: Test using php
shell: php
run: print "Hello, World!\n";

python:
help: Test using php
shell: python
run: print("Hello, World!")

r:
help: Test using R
shell: Rscript
run: print("Hello World!")

sh:
help: Test using sh
shell: sh
run: echo "Hello, World!"

all:
dependencies:
- target: node
- target: perl
# note: php from conda-forge has conflicts with r-base
# - target: php
- target: python
- target: r
- target: sh

0 comments on commit bd75fad

Please sign in to comment.