Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,36 @@ func TestIncludesWithExclude(t *testing.T) {
assert.Equal(t, "foo\n", buff.String())
}

func TestIncludesWithOnly(t *testing.T) {
t.Parallel()

var buff bytes.Buffer
e := task.NewExecutor(
task.WithDir("testdata/includes_with_only"),
task.WithSilent(true),
task.WithStdout(&buff),
task.WithStderr(&buff),
)
require.NoError(t, e.Setup())

err := e.Run(t.Context(), &task.Call{Task: "included:foo"})
require.NoError(t, err)
assert.Equal(t, "foo\n", buff.String())
buff.Reset()

err = e.Run(t.Context(), &task.Call{Task: "included:bar"})
require.Error(t, err)
buff.Reset()

err = e.Run(t.Context(), &task.Call{Task: "bar"})
require.NoError(t, err)
assert.Equal(t, "bar\n", buff.String())
buff.Reset()

err = e.Run(t.Context(), &task.Call{Task: "foo"})
require.Error(t, err)
}

func TestIncludedTaskfileVarMerging(t *testing.T) {
t.Parallel()

Expand Down
4 changes: 4 additions & 0 deletions taskfile/ast/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
Internal bool
Aliases []string
Excludes []string
Only []string
AdvancedImport bool
Vars *Vars
Flatten bool
Expand Down Expand Up @@ -165,6 +166,7 @@ func (include *Include) UnmarshalYAML(node *yaml.Node) error {
Flatten bool
Aliases []string
Excludes []string
Only []string
Vars *Vars
Checksum string
}
Expand All @@ -177,6 +179,7 @@ func (include *Include) UnmarshalYAML(node *yaml.Node) error {
include.Internal = includedTaskfile.Internal
include.Aliases = includedTaskfile.Aliases
include.Excludes = includedTaskfile.Excludes
include.Only = includedTaskfile.Only
include.AdvancedImport = true
include.Vars = includedTaskfile.Vars
include.Flatten = includedTaskfile.Flatten
Expand All @@ -200,6 +203,7 @@ func (include *Include) DeepCopy() *Include {
Optional: include.Optional,
Internal: include.Internal,
Excludes: deepcopy.Slice(include.Excludes),
Only: deepcopy.Slice(include.Only),
AdvancedImport: include.AdvancedImport,
Vars: include.Vars.DeepCopy(),
Flatten: include.Flatten,
Expand Down
5 changes: 5 additions & 0 deletions taskfile/ast/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ func (t1 *Tasks) Merge(t2 *Tasks, include *Include, includedTaskfileVars *Vars)
continue
}

// if an only list is set and the task is not in it, skip it
if len(include.Only) > 0 && !slices.Contains(include.Only, name) {
continue
}

if !include.Flatten {
// Add namespaces to task dependencies
for _, dep := range task.Deps {
Expand Down
1 change: 1 addition & 0 deletions taskfile/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ func (r *Reader) include(ctx context.Context, node Node) error {
Aliases: include.Aliases,
AdvancedImport: include.AdvancedImport,
Excludes: include.Excludes,
Only: include.Only,
Vars: include.Vars,
Checksum: include.Checksum,
}
Expand Down
17 changes: 17 additions & 0 deletions testdata/includes_with_only/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: '3'

includes:
included:
taskfile: ./included/Taskfile.yml
only:
- foo
included_flatten:
taskfile: ./included/Taskfile.yml
flatten: true
only:
- bar

tasks:
default:
cmds:
- echo "default"
5 changes: 5 additions & 0 deletions testdata/includes_with_only/included/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: '3'

tasks:
foo: echo foo
bar: echo bar
39 changes: 39 additions & 0 deletions website/src/docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,45 @@ tasks:

It's compatible with the `flatten` option.

### Include only specific tasks

You can include only specific tasks by using the `only` option. This is the
opposite of `excludes` — only the listed tasks will be included and all others
will be ignored.

::: code-group

```yaml [Taskfile.yml]
version: '3'

includes:
included:
taskfile: ./Included.yml
only: [foo]
```

```yaml [Included.yml]
version: '3'

tasks:
foo: echo "Foo"
bar: echo "Bar"
```

:::

`task included:bar` will throw an error because only `foo` is included but
`task included:foo` will work and display `Foo`.

::: info

When `only` is set, `excludes` is redundant — any task not in the `only` list
is already excluded.

:::

It's compatible with the `flatten` option.

### Vars of included Taskfiles

You can also specify variables when including a Taskfile. This may be useful for
Expand Down
15 changes: 15 additions & 0 deletions website/src/docs/reference/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ includes:
internal: false
aliases: [api]
excludes: [internal-task]
only: [public-task]
vars:
SERVICE_NAME: backend
checksum: abc123...
Expand Down Expand Up @@ -306,6 +307,20 @@ includes:
excludes: [internal-setup, debug-only]
```

### `only`

- **Type**: `[]string`
- **Description**: Tasks to include. If set, only these tasks will be included
and all others will be ignored. Opposite of `excludes`. When `only` is set,
`excludes` is redundant.

```yaml
includes:
shared:
taskfile: ./shared.yml
only: [build, test]
```

### `vars`

- **Type**: `map[string]Variable`
Expand Down
7 changes: 7 additions & 0 deletions website/src/public/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,13 @@
"type": "string"
}
},
"only": {
"description": "A list of tasks to include. If set, only these tasks will be included and all others will be ignored.",
"type": "array",
"items": {
"type": "string"
}
},
"vars": {
"description": "A set of variables to apply to the included Taskfile.",
"$ref": "#/definitions/vars"
Expand Down