1
0
mirror of https://github.com/go-task/task.git synced 2025-11-23 22:24:45 +02:00

feat: add option to declare an included Taskfile as flatten (#1704)

This commit is contained in:
Valentin Maerten
2024-08-26 23:17:39 +02:00
committed by GitHub
parent f302b50519
commit ef3b853728
15 changed files with 272 additions and 38 deletions

View File

@@ -8,7 +8,7 @@ toc_max_heading_level: 5
# Schema Reference
| Attribute | Type | Default | Description |
| ---------- | ---------------------------------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|------------|------------------------------------|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `version` | `string` | | Version of the Taskfile. The current version is `3`. |
| `output` | `string` | `interleaved` | Output mode. Available options: `interleaved`, `group` and `prefixed`. |
| `method` | `string` | `checksum` | Default method in this Taskfile. Can be overridden in a task by task basis. Available options: `checksum`, `timestamp` and `none`. |
@@ -26,10 +26,11 @@ toc_max_heading_level: 5
## Include
| Attribute | Type | Default | Description |
| ---------- | --------------------- | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|------------|-----------------------|-------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `taskfile` | `string` | | The path for the Taskfile or directory to be included. If a directory, Task will look for files named `Taskfile.yml` or `Taskfile.yaml` inside that directory. If a relative path, resolved relative to the directory containing the including Taskfile. |
| `dir` | `string` | The parent Taskfile directory | The working directory of the included tasks when run. |
| `optional` | `bool` | `false` | If `true`, no errors will be thrown if the specified file does not exist. |
| `flatten` | `bool` | `false` | If `true`, the tasks from the included Taskfile will be available in the including Taskfile without a namespace. If a task with the same name already exists in the including Taskfile, an error will be thrown. |
| `internal` | `bool` | `false` | Stops any task in the included Taskfile from being callable on the command line. These commands will also be omitted from the output when used with `--list`. |
| `aliases` | `[]string` | | Alternative names for the namespace of the included Taskfile. |
| `vars` | `map[string]Variable` | | A set of variables to apply to the included Taskfile. |

View File

@@ -334,6 +334,117 @@ includes:
internal: true
```
### Flatten includes
You can flatten the included Taskfile tasks into the main Taskfile by using the `flatten` option.
It means that the included Taskfile tasks will be available without the namespace.
<Tabs defaultValue="1"
values={[
{label: 'Taskfile.yml', value: '1'},
{label: 'Included.yml', value: '2'}
]}>
<TabItem value="1">
```yaml
version: '3'
includes:
lib:
taskfile: ./Included.yml
flatten: true
tasks:
greet:
cmds:
- echo "Greet"
- task: foo
```
</TabItem>
<TabItem value="2">
```yaml
version: '3'
tasks:
foo:
cmds:
- echo "Foo"
```
</TabItem></Tabs>
If you run `task -a` it will print :
```sh
task: Available tasks for this project:
* greet:
* foo
```
You can run `task foo` directly without the namespace.
You can also reference the task in other tasks without the namespace. So if you run `task greet` it will run `greet` and `foo` tasks and the output will be :
```text
```
If multiple tasks have the same name, an error will be thrown:
<Tabs defaultValue="1"
values={[
{label: 'Taskfile.yml', value: '1'},
{label: 'Included.yml', value: '2'}
]}>
<TabItem value="1">
```yaml
version: '3'
includes:
lib:
taskfile: ./Included.yml
flatten: true
tasks:
greet:
cmds:
- echo "Greet"
- task: foo
```
</TabItem>
<TabItem value="2">
```yaml
version: '3'
tasks:
greet:
cmds:
- echo "Foo"
```
</TabItem></Tabs>
If you run `task -a` it will print:
```text
task: Found multiple tasks (greet) included by "lib"
```
### Vars of included Taskfiles
You can also specify variables when including a Taskfile. This may be useful for

View File

@@ -610,6 +610,10 @@
"description": "If `true`, no errors will be thrown if the specified file does not exist.",
"type": "boolean"
},
"flatten": {
"description": "If `true`, the tasks from the included Taskfile will be available in the including Taskfile without a namespace. If a task with the same name already exists in the including Taskfile, an error will be thrown.",
"type": "boolean"
},
"internal": {
"description": "Stops any task in the included Taskfile from being callable on the command line. These commands will also be omitted from the output when used with `--list`.",
"type": "boolean"