mirror of
https://github.com/go-task/task.git
synced 2025-08-10 22:42:19 +02:00
Improvements + CHANGELOG for #677
This commit is contained in:
@@ -1,5 +1,12 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
- Add ability to specify vars when including a Taskfile.
|
||||||
|
[Check out the documentation](http://localhost:3000/#/usage?id=vars-of-included-taskfiles)
|
||||||
|
for more information.
|
||||||
|
([#677](https://github.com/go-task/task/pull/677)).
|
||||||
|
|
||||||
## v3.11.0 - 2022-01-19
|
## v3.11.0 - 2022-01-19
|
||||||
|
|
||||||
- Task now supports printing begin and end messages when using the `group`
|
- Task now supports printing begin and end messages when using the `group`
|
||||||
|
@@ -187,6 +187,31 @@ tasks:
|
|||||||
- echo "This command can still be successfully executed if ./tests/Taskfile.yml does not exist"
|
- echo "This command can still be successfully executed if ./tests/Taskfile.yml does not exist"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Vars of included Taskfiles
|
||||||
|
|
||||||
|
You can also specify variables when including a Taskfile. This may be useful
|
||||||
|
for having reusable Taskfile that can be tweaked or even included more than once:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
version: '3'
|
||||||
|
|
||||||
|
includes:
|
||||||
|
backend:
|
||||||
|
taskfile: ./taskfiles/Docker.yml
|
||||||
|
vars:
|
||||||
|
DOCKER_IMAGE: backend_image
|
||||||
|
|
||||||
|
frontend:
|
||||||
|
taskfile: ./taskfiles/Docker.yml
|
||||||
|
vars:
|
||||||
|
DOCKER_IMAGE: frontend_image
|
||||||
|
```
|
||||||
|
|
||||||
|
> NOTE: Vars declared in the included Taskfile have preference over the
|
||||||
|
included ones! If you want a variable in an included Taskfile to be overridable
|
||||||
|
use the [default function](https://go-task.github.io/slim-sprig/defaults.html):
|
||||||
|
`MY_VAR: '{{.MY_VAR | default "my-default-value"}}'`.
|
||||||
|
|
||||||
## Task directory
|
## Task directory
|
||||||
|
|
||||||
By default, tasks will be executed in the directory where the Taskfile is
|
By default, tasks will be executed in the directory where the Taskfile is
|
||||||
@@ -535,6 +560,8 @@ They are listed below in order of importance (e.g. most important first):
|
|||||||
- Variables declared in the task definition
|
- Variables declared in the task definition
|
||||||
- Variables given while calling a task from another
|
- Variables given while calling a task from another
|
||||||
(See [Calling another task](#calling-another-task) above)
|
(See [Calling another task](#calling-another-task) above)
|
||||||
|
- Variables of the [included Taskfile](#including-other-taskfiles) (when the task is included)
|
||||||
|
- Variables of the [inclusion of the Taskfile](#vars-of-included-taskfiles) (when the task is included)
|
||||||
- Global variables (those declared in the `vars:` option in the Taskfile)
|
- Global variables (those declared in the `vars:` option in the Taskfile)
|
||||||
- Environment variables
|
- Environment variables
|
||||||
|
|
||||||
|
@@ -74,21 +74,8 @@ func (c *CompilerV3) getVariables(t *taskfile.Task, call *taskfile.Call, evaluat
|
|||||||
}
|
}
|
||||||
rangeFunc := getRangeFunc(c.Dir)
|
rangeFunc := getRangeFunc(c.Dir)
|
||||||
|
|
||||||
if err := c.TaskfileEnv.Range(rangeFunc); err != nil {
|
var taskRangeFunc func(k string, v taskfile.Var) error
|
||||||
return nil, err
|
if t != nil {
|
||||||
}
|
|
||||||
if err := c.TaskfileVars.Range(rangeFunc); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if t == nil || call == nil {
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := call.Vars.Range(rangeFunc); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE(@andreynering): We're manually joining these paths here because
|
// NOTE(@andreynering): We're manually joining these paths here because
|
||||||
// this is the raw task, not the compiled one.
|
// this is the raw task, not the compiled one.
|
||||||
tr := templater.Templater{Vars: result, RemoveNoValue: true}
|
tr := templater.Templater{Vars: result, RemoveNoValue: true}
|
||||||
@@ -99,8 +86,31 @@ func (c *CompilerV3) getVariables(t *taskfile.Task, call *taskfile.Call, evaluat
|
|||||||
if !filepath.IsAbs(dir) {
|
if !filepath.IsAbs(dir) {
|
||||||
dir = filepath.Join(c.Dir, dir)
|
dir = filepath.Join(c.Dir, dir)
|
||||||
}
|
}
|
||||||
taskRangeFunc := getRangeFunc(dir)
|
taskRangeFunc = getRangeFunc(dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.TaskfileEnv.Range(rangeFunc); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := c.TaskfileVars.Range(rangeFunc); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if t != nil {
|
||||||
|
if err := t.IncludedTaskfileVars.Range(taskRangeFunc); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := t.IncludeVars.Range(rangeFunc); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if t == nil || call == nil {
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := call.Vars.Range(rangeFunc); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if err := t.Vars.Range(taskRangeFunc); err != nil {
|
if err := t.Vars.Range(taskRangeFunc); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
12
task_test.go
12
task_test.go
@@ -1232,16 +1232,16 @@ func TestIncludedVars(t *testing.T) {
|
|||||||
expectedOutputOrder := strings.TrimSpace(`
|
expectedOutputOrder := strings.TrimSpace(`
|
||||||
task: [included1:task1] echo "VAR_1 is included1-var1"
|
task: [included1:task1] echo "VAR_1 is included1-var1"
|
||||||
VAR_1 is included1-var1
|
VAR_1 is included1-var1
|
||||||
task: [included1:task1] echo "VAR_2 is incldued-default-var2"
|
task: [included1:task1] echo "VAR_2 is included-default-var2"
|
||||||
VAR_2 is incldued-default-var2
|
VAR_2 is included-default-var2
|
||||||
task: [included2:task1] echo "VAR_1 is included2-var1"
|
task: [included2:task1] echo "VAR_1 is included2-var1"
|
||||||
VAR_1 is included2-var1
|
VAR_1 is included2-var1
|
||||||
task: [included2:task1] echo "VAR_2 is incldued-default-var2"
|
task: [included2:task1] echo "VAR_2 is included-default-var2"
|
||||||
VAR_2 is incldued-default-var2
|
VAR_2 is included-default-var2
|
||||||
task: [included3:task1] echo "VAR_1 is included-default-var1"
|
task: [included3:task1] echo "VAR_1 is included-default-var1"
|
||||||
VAR_1 is included-default-var1
|
VAR_1 is included-default-var1
|
||||||
task: [included3:task1] echo "VAR_2 is incldued-default-var2"
|
task: [included3:task1] echo "VAR_2 is included-default-var2"
|
||||||
VAR_2 is incldued-default-var2
|
VAR_2 is included-default-var2
|
||||||
`)
|
`)
|
||||||
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "task1"}))
|
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "task1"}))
|
||||||
t.Log(buff.String())
|
t.Log(buff.String())
|
||||||
|
@@ -110,15 +110,11 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, task := range includedTaskfile.Tasks {
|
for _, task := range includedTaskfile.Tasks {
|
||||||
if includedTask.Vars != nil {
|
|
||||||
if task.Vars == nil {
|
|
||||||
task.Vars = &taskfile.Vars{}
|
|
||||||
}
|
|
||||||
task.Vars.Merge(includedTask.Vars)
|
|
||||||
}
|
|
||||||
if !filepath.IsAbs(task.Dir) {
|
if !filepath.IsAbs(task.Dir) {
|
||||||
task.Dir = filepath.Join(includedTask.Dir, task.Dir)
|
task.Dir = filepath.Join(includedTask.Dir, task.Dir)
|
||||||
}
|
}
|
||||||
|
task.IncludeVars = includedTask.Vars
|
||||||
|
task.IncludedTaskfileVars = includedTaskfile.Vars
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -24,6 +24,8 @@ type Task struct {
|
|||||||
Prefix string
|
Prefix string
|
||||||
IgnoreError bool
|
IgnoreError bool
|
||||||
Run string
|
Run string
|
||||||
|
IncludeVars *Vars
|
||||||
|
IncludedTaskfileVars *Vars
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) Name() string {
|
func (t *Task) Name() string {
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
version: "3"
|
version: "3"
|
||||||
|
|
||||||
vars:
|
vars:
|
||||||
VAR_1: included-default-var1
|
VAR_1: '{{.VAR_1 | default "included-default-var1"}}'
|
||||||
VAR_2: incldued-default-var2
|
VAR_2: '{{.VAR_2 | default "included-default-var2"}}'
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
task1:
|
task1:
|
||||||
|
@@ -61,6 +61,8 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
|
|||||||
Prefix: r.Replace(origTask.Prefix),
|
Prefix: r.Replace(origTask.Prefix),
|
||||||
IgnoreError: origTask.IgnoreError,
|
IgnoreError: origTask.IgnoreError,
|
||||||
Run: r.Replace(origTask.Run),
|
Run: r.Replace(origTask.Run),
|
||||||
|
IncludeVars: origTask.IncludeVars,
|
||||||
|
IncludedTaskfileVars: origTask.IncludedTaskfileVars,
|
||||||
}
|
}
|
||||||
new.Dir, err = execext.Expand(new.Dir)
|
new.Dir, err = execext.Expand(new.Dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Reference in New Issue
Block a user