diff --git a/README.md b/README.md index 0888c18..6a01920 100644 --- a/README.md +++ b/README.md @@ -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" @@ -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. diff --git a/changelog.md b/changelog.md index c8af9fa..040b223 100644 --- a/changelog.md +++ b/changelog.md @@ -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. diff --git a/groom.toml b/groom.toml index 747d304..3bcd383 100644 --- a/groom.toml +++ b/groom.toml @@ -1,5 +1,6 @@ name = "groom" + [variables] version = "0.1.1" build-dir = "build" diff --git a/pkg/config/parse.go b/pkg/config/parse.go index a96c2ed..ea4e4df 100644 --- a/pkg/config/parse.go +++ b/pkg/config/parse.go @@ -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 } diff --git a/pkg/tasks/tasks.go b/pkg/tasks/tasks.go index 429a408..4988fad 100644 --- a/pkg/tasks/tasks.go +++ b/pkg/tasks/tasks.go @@ -1,6 +1,7 @@ package tasks import ( + "os" "slices" "github.com/buildkite/shellwords" @@ -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] { @@ -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)