Skip to content

Commit

Permalink
feat: Add working-directory to the target, group and global scope (os…
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijeetSaroha authored Dec 8, 2023
1 parent 58a6567 commit 3fbd61e
Show file tree
Hide file tree
Showing 8 changed files with 574 additions and 9 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ jobs:
brew install gnu-sed
echo 'export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"' >> ~/.bashrc
echo 'export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"' >> ~/.bash_profile
gsed -i s:/tmp:/private/tmp:g tests/.makim-working-directory-absolute-path.yaml
gsed -i s:/tmp:/private/tmp:g tests/.makim-working-directory-no-path.yaml
gsed -i s:/tmp:/private/tmp:g tests/.makim-working-directory-relative-path.yaml
- name: Prepare conda environment (windows)
if: ${{ matrix.os == 'windows' }}
Expand Down Expand Up @@ -86,6 +89,15 @@ jobs:
- name: Run smoke test for variable envs
run: makim smoke-tests.vars-env

- name: Run smoke test for working-directory-absolute-path
run: makim smoke-tests.working-directory-absolute-path

- name: Run smoke test for working-directory-no-path
run: makim smoke-tests.working-directory-no-path

- name: Run smoke test for working-directory-relative-path
run: makim smoke-tests.working-directory-relative-path

- name: Run unit tests
run: makim tests.unittest

Expand Down
94 changes: 88 additions & 6 deletions .makim.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
version: 1.0
groups:
default:
clean:
targets:
clean:
tmp:
help: Clean unnecessary temporary files
shell: bash
run: |
rm -fr build/
rm -fr dist/
rm -fr .eggs/
find . -name '*.egg-info' -exec rm -fr {} +
find . -name '*.egg' -exec rm -f {} +
find . -name '*.pyc' -exec rm -f {} +
find . -name
find . -name '__pycache__' -exec rm -fr '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +{} +
find . -name '__pycache__' -exec rm -fr {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +
rm -f .coverage
rm -fr htmlcov/
rm -fr .pytest_cache
Expand Down Expand Up @@ -75,6 +76,9 @@ groups:
- target: smoke-tests.unittest
- target: smoke-tests.vars-env
- target: smoke-tests.bash
- target: smoke-tests.working-directory-absolute-path
- target: smoke-tests.working-directory-no-path
- target: smoke-tests.working-directory-relative-path

ci:
help: Run all targets used on CI
Expand Down Expand Up @@ -213,8 +217,86 @@ groups:
makim --makim-file $MAKIM_FILE rerender-env.from-target $VERBOSE_FLAG
bash:
help: ...
help: Test makim shell attribute with bash
run: |
makim --makim-file tests/.makim-bash-main-scope.yaml main-scope.test
makim --makim-file tests/.makim-bash-group-scope.yaml group-scope.test
makim --makim-file tests/.makim-bash-target-scope.yaml target-scope.test
working-directory-absolute-path:
help: |
Test makim with working-directory absolute for global 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-working-directory-absolute-path.yaml
shell: bash
run: |
export VERBOSE_FLAG='{{ "--verbose" if args.verbose_mode else "" }}'
makim --makim-file $MAKIM_FILE --help
makim --makim-file $MAKIM_FILE --version
makim --makim-file $MAKIM_FILE group-no-path.target-no-path $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-no-path.target-absolute $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-no-path.target-relative $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-relative.target-no-path $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-relative.target-absolute $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-relative.target-relative $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-absolute.target-no-path $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-absolute.target-absolute $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-absolute.target-relative $VERBOSE_FLAG
working-directory-no-path:
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-working-directory-no-path.yaml
shell: bash
run: |
export VERBOSE_FLAG='{{ "--verbose" if args.verbose_mode else "" }}'
export MAKIM_FILE="$(pwd)/${MAKIM_FILE}"
cd /tmp
makim --makim-file $MAKIM_FILE --help
makim --makim-file $MAKIM_FILE --version
makim --makim-file $MAKIM_FILE group-no-path.target-no-path $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-no-path.target-absolute $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-no-path.target-relative $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-absolute.target-no-path $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-absolute.target-absolute $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-absolute.target-relative $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-relative.target-no-path $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-relative.target-absolute $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-relative.target-relative $VERBOSE_FLAG
working-directory-relative-path:
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-working-directory-relative-path.yaml
shell: bash
run: |
export VERBOSE_FLAG='{{ "--verbose" if args.verbose_mode else "" }}'
export MAKIM_FILE="$(pwd)/${MAKIM_FILE}"
mkdir -p /tmp/global-relative
cd /tmp
makim --makim-file $MAKIM_FILE --help
makim --makim-file $MAKIM_FILE --version
makim --makim-file $MAKIM_FILE group-no-path.target-no-path $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-no-path.target-absolute $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-no-path.target-relative $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-absolute.target-no-path $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-absolute.target-absolute $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-absolute.target-relative $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-relative.target-no-path $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-relative.target-absolute $VERBOSE_FLAG
makim --makim-file $MAKIM_FILE group-relative.target-relative $VERBOSE_FLAG
103 changes: 103 additions & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Makim Features

## Working Directory Feature

The working-directory feature in Makim allows users to define the directory
from which commands associated with specific targets or groups are executed.
This provides greater flexibility and control over the execution environment.

### Attribute: working-directory

The `working-directory` attribute can be specified at three different
scopes: global, group, and target. It allows users to set the working
directory for a specific target, a group of targets, or globally.

### Syntax and Scopes
The working-directory attribute can be applied to three different scopes:

- #### **Global Scope**
Setting the global working directory impacts all targets and groups in
the Makim configuration.

```yaml
version: 1.0
working-directory: /path/to/global/directory

# ... other configuration ...
```

- #### Group Scope

Setting the working directory at the group scope affects all targets within
that group.

```yaml
version: 1.0
groups:
my-group:
working-directory: /path/to/group/directory
targets:
target-1:
run: |
# This target will run with the working directory set to
# /path/to/group/directory
```

- #### Target Scope

Setting the working directory at the target scope allows for fine grained
control over individual targets.

```yaml
version: 1.0
groups:
my-group:
targets:
my-target:
working-directory: /path/to/target/directory
run: |
# This target will run with the working directory set to
# /path/to/target/directory
```

## Example

```yaml
version: 1.0
working-directory: /project-root
groups:
backend:
working-directory: backend
targets:
build:
help: Build the backend services
working-directory: services
run: |
echo "Building backend services..."
# Additional build commands specific to the backend
test:
help: Run backend tests
working-directory: tests
run: |
echo "Running backend tests..."
# Additional test commands specific to the backend
frontend:
working-directory: frontend
targets:
build:
help: Build the frontend application
run: |
echo "Building frontend application..."
# Additional build commands specific to the frontend
test:
help: Run frontend tests
run: |
echo "Running frontend tests..."
# Additional test commands specific to the frontend
```
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ ignore = ["RUF012"]
exclude = [
"docs",
]

[tool.ruff.lint]
select = [
"E", # pycodestyle
"F", # pyflakes
Expand All @@ -79,8 +81,6 @@ select = [
"RUF", # Ruff-specific rules
"I001", # isort
]
# fixable = ["I001"]
fix = true

[tool.ruff.pydocstyle]
convention = "numpy"
Expand Down
40 changes: 39 additions & 1 deletion src/makim/makim.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

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

import dotenv
import sh
Expand Down Expand Up @@ -63,6 +63,10 @@ class Makim(PrintPlugin):
# temporary variables
env: dict = {} # initial env
env_scoped: dict = {} # current env
# initial working directory
working_directory: Optional[Path] = None
# current working directory
working_directory_scoped: Optional[Path] = None
args: Optional[object] = None
group_name: str = 'default'
group_data: dict = {}
Expand Down Expand Up @@ -91,6 +95,7 @@ def _call_shell_app(self, cmd):
_no_err=True,
_env=os.environ,
_new_session=True,
_cwd=str(self._resolve_working_directory('target')),
)

try:
Expand Down Expand Up @@ -183,6 +188,39 @@ def _load_config_data(self):
content_io = io.StringIO(content)
self.global_data = yaml.safe_load(content_io)

def _resolve_working_directory(self, scope: str) -> Optional[Path]:
scope_options = ('global', 'group', 'target')
if scope not in scope_options:
raise Exception(f'The given scope `{scope}` is not valid.')

def update_working_directory(
current_path: Union[None, Path], new_path: str
) -> Path:
if not current_path:
return Path(new_path)
return current_path / Path(new_path)

scope_id = scope_options.index(scope)

working_dir: Optional[Path] = None

if scope_id >= SCOPE_GLOBAL:
working_dir = update_working_directory(
working_dir, self.global_data.get('working-directory', '')
)

if scope_id >= SCOPE_GROUP:
working_dir = update_working_directory(
working_dir, self.group_data.get('working-directory', '')
)

if scope_id == SCOPE_TARGET:
working_dir = update_working_directory(
working_dir, self.target_data.get('working-directory', '')
)

return working_dir

def _load_shell_app(self, shell_app: str = ''):
if not shell_app:
shell_app = self.global_data.get('shell', 'xonsh')
Expand Down
Loading

0 comments on commit 3fbd61e

Please sign in to comment.