You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
All experimental features are subject to breaking changes and/or removal at any time. We strongly recommend that you do not use these features in a production environment. They are intended for testing and feedback only.
Context
This experiment attempts to solve the problems originally described by #140. It is a follow-up to the "Any Variables" experiment (#1415) which was merged without support for maps.
Currently, all variable types are allowed except for maps. This is because there is some debate around the syntax that should be used to define them. The proposals for these syntaxes are described below.
Proposal 1
Warning
This experiment proposal breaks the following functionality:
Dynamically defined variables (using the sh keyword)
Note
To enable this experiment, set the environment variable: TASK_X_MAP_VARIABLES=1. Check out our guide to enabling experiments for more information.
This proposal removes support for the sh keyword in favour of a new syntax for
dynamically defined variables, This allows you to define a map directly as you
would for any other type:
version: 3tasks:
foo:
vars:
FOO: {a: 1, b: 2, c: 3} # <-- Directly defined map on the `FOO` keycmds:
- 'echo {{.FOO.a}}'
Migration
Taskfiles with dynamically defined variables via the sh subkey will no longer
work with this experiment enabled. In order to keep using dynamically defined
variables, you will need to migrate your Taskfile to use the new syntax.
Previously, you might have defined a dynamic variable like this:
With this experiment enabled, you will need to remove the sh subkey and define
your command as a string that begins with a $. This will instruct Task to
interpret the string as a command instead of a literal value and the variable
will be populated with the output of the command. For example:
If your current Taskfile contains a string variable that begins with a $, you
will now need to escape the $ with a backslash (\) to stop Task from
executing it as a command.
Proposal 2
Note
To enable this experiment, set the environment variable: TASK_X_MAP_VARIABLES=2. Check out our guide to enabling experiments for more information.
This proposal maintains backwards-compatibility and the sh subkey and adds another new map subkey for defining map variables:
version: 3tasks:
foo:
vars:
FOO:
map: {a: 1, b: 2, c: 3} # <-- Defined using the `map' subkey instead of directly on 'FOO'BAR: true # <-- Other types of variables are still defined directly on the keyBAZ:
sh: 'echo Hello Task'# <-- The `sh` subkey is still supportedcmds:
- 'echo {{.FOO.a}}'
Parsing JSON and YAML
Note
This part of the proposal has been removed as the same functionality is achievable using the ref keyword
See #1585 (comment).
In addition to the new map keyword, this proposal also adds support for the json and yaml keywords for parsing JSON and YAML strings into real objects/arrays. This is similar to the fromJSON template function, but means that you only have to parse the JSON/YAML once when you declare the variable, instead of every time you want to access a value.
Before:
version: 3tasks:
foo:
vars:
FOO: '{"a": 1, "b": 2, "c": 3}'# <-- JSON stringcmds:
- 'echo {{(fromJSON .FOO).a}}'# <-- Parse JSON string every time you want to access a value
- 'echo {{(fromJSON .FOO).b}}'
This experiment also adds support for looping over maps using the for keyword, just like arrays. In addition to the {{.ITEM}} variable being populated when looping over a map, we also make an additional {{.KEY}} variable available that holds the string value of the map key.
Warning
All experimental features are subject to breaking changes and/or removal at any time. We strongly recommend that you do not use these features in a production environment. They are intended for testing and feedback only.
Context
This experiment attempts to solve the problems originally described by #140. It is a follow-up to the "Any Variables" experiment (#1415) which was merged without support for maps.
Currently, all variable types are allowed except for maps. This is because there is some debate around the syntax that should be used to define them. The proposals for these syntaxes are described below.
Proposal 1
Warning
This experiment proposal breaks the following functionality:
shkeyword)Note
To enable this experiment, set the environment variable:
TASK_X_MAP_VARIABLES=1. Check out our guide to enabling experiments for more information.This proposal removes support for the
shkeyword in favour of a new syntax fordynamically defined variables, This allows you to define a map directly as you
would for any other type:
Migration
Taskfiles with dynamically defined variables via the
shsubkey will no longerwork with this experiment enabled. In order to keep using dynamically defined
variables, you will need to migrate your Taskfile to use the new syntax.
Previously, you might have defined a dynamic variable like this:
With this experiment enabled, you will need to remove the
shsubkey and defineyour command as a string that begins with a
$. This will instruct Task tointerpret the string as a command instead of a literal value and the variable
will be populated with the output of the command. For example:
If your current Taskfile contains a string variable that begins with a
$, youwill now need to escape the
$with a backslash (\) to stop Task fromexecuting it as a command.
Proposal 2
Note
To enable this experiment, set the environment variable:
TASK_X_MAP_VARIABLES=2. Check out our guide to enabling experiments for more information.This proposal maintains backwards-compatibility and the
shsubkey and adds another newmapsubkey for defining map variables:Parsing JSON and YAML
Note
This part of the proposal has been removed as the same functionality is achievable using the
refkeywordSee #1585 (comment).
In addition to the newmapkeyword, this proposal also adds support for thejsonandyamlkeywords for parsing JSON and YAML strings into real objects/arrays. This is similar to thefromJSONtemplate function, but means that you only have to parse the JSON/YAML once when you declare the variable, instead of every time you want to access a value.Before:After:Looping over maps (Both proposals)
This experiment also adds support for looping over maps using the
forkeyword, just like arrays. In addition to the{{.ITEM}}variable being populated when looping over a map, we also make an additional{{.KEY}}variable available that holds the string value of the map key.Proposal 1
Proposal 2
Note
Remember that maps are unordered, so the order in which the items are looped over is random.