Skip to content

Commit

Permalink
feat: can change directory before executing task
Browse files Browse the repository at this point in the history
  • Loading branch information
pspiagicw committed Apr 22, 2024
1 parent d6183ea commit ff74a2a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ commands = [
# Tasks can contain dependencies, and environment variables defined
[task.test]
environment = [ "TESTS=1" ]
directory = "test"
command = "python -m unittest"
depends = [
"format"
Expand All @@ -65,6 +66,48 @@ command = "go fmt ./..."
![help](./gifs/help.gif)

### Task

A `task` in `groom` needs a name and atleast a single command.

This can be a task.

```toml
[task.some-name]
command = "echo 'Sample Task'"
```

It can contain
- Environment variables
- List of environment variables with it's values as string.
- Task dependencies
- List of dependencies which should be valid groom tasks.
- Multiple commands
- List of commands to execute `sequentially`.
- When `command` and `commands` both exist. `commands` takes precedence.
- Description
- A single line describing the purpose of the task.
- Directory
- A absolute path to change the working directory before executing the commands.

```toml
[task.build]
depends = [
"format"
]
description = "Build the project"
directory = "src"
environment = ["DEBUG_BUILD=0"]
commands = [
"gcc -c main.o main.c",
"gcc -c game.o game.c",
"gcc -o $name game.o main.o"
]

```

> Everything except `name` and `command` are optional
### List

Run `groom` without any arguments to list all configured tasks.
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added the `directory` option to change directory before executing a task.
- Help Printing.
- Added a `--simple` flag to task listing.
- Added parent directory recursion. `groom` now finds `groom.toml` in the parent directories.
Expand Down
1 change: 1 addition & 0 deletions groom.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name = "groom"


[variables]
version = "0.1.1"
build-dir = "build"
Expand Down
1 change: 1 addition & 0 deletions pkg/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Task struct {
Shell string `toml:"shell"`
Environment []string `toml:"environment"`
Depends []string `toml:"depends"`
Directory string `toml:"directory"`
Name string
}

Expand Down
33 changes: 30 additions & 3 deletions pkg/tasks/tasks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tasks

import (
"os"
"slices"

"github.com/buildkite/shellwords"
Expand Down Expand Up @@ -41,7 +42,7 @@ func sort(tasks []*config.Task, groomConfig *config.Config) []*config.Task {
dependency := getTask(dep, groomConfig)

if dependency == nil {
goreland.LogFatal("Task %s depends on %s, which does not exist", current.Name, dep)
goreland.LogFatal("Task [%s] depends on [%s], which does not exist", current.Name, dep)
}

if !seen[dependency.Name] {
Expand Down Expand Up @@ -108,14 +109,40 @@ func executeTasks(taskList []*config.Task, opts *argparse.Opts) {
for _, task := range taskList {
logTask(task)
if !opts.DryRun {
runCommand(task)
runTask(task)
}
}
}
func runCommand(task *config.Task) {
func pushDirectory(task *config.Task) {
if task.Directory != "" {
curdir, err := os.Getwd()
if err != nil {
goreland.LogFatal("Error changing directory to %s: %s", task.Directory, err)
}
err = os.Chdir(task.Directory)
if err != nil {
goreland.LogFatal("Error changing directory to %s: %s", task.Directory, err)
}
task.Directory = curdir
}
}
func popDirectory(task *config.Task) {
if task.Directory != "" {
err := os.Chdir(task.Directory)
if err != nil {
goreland.LogFatal("Error changing directory to %s: %s", task.Directory, err)
}
}
}
func runTask(task *config.Task) {

pushDirectory(task)

for _, command := range task.Commands {
run(task, command)
}

popDirectory(task)
}
func run(task *config.Task, command string) {
components, err := shellwords.Split(command)
Expand Down

0 comments on commit ff74a2a

Please sign in to comment.