feat: implement task sorting with --sort
flag
#1105
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #946
This PR implements custom sorting for tasks using a new
--sort
flag. This will work with any of the commands that output a list of tasks. i.e.task --list
task --list --silent
task --list --json
task --list-all
The
--sort
flag will accept a string that specifies the sort order. The following values are supported:default
- The same asalphanumeric
, but root tasks (with no namespace) will be always appear first.none
- No sorting. Tasks will appear in the order they are defined in the taskfilealphanumeric
- Sorts tasks alphabetically by name ignoring the files they are defined in.The implementation for this was quite complex so here is a bullet point summary of the changes:
copy.go
has been moved to a new package ininternal/deepcopy
so that it can be more easily reusedinternal/sort
- Contains the task sorting interface and implementationsinternal/orderedmap
- Contains a generic ordered map implementationtaskfile.Tasks
to use the orderedmap implementationtaskfile.Vars
to use the orderedmap implementationtaskfile.Vars
use a pointer. When this pointer isnil
, the method on the underlyingOrderedMap
cannot be called (because of how embedding works in Go). I've simply added some wrapper methods ontaskfile.Vars
to handle these cases which donil
checks before calling the underlying method. This is not an issue withtaskfile.Tasks
because they aren't implemented with a pointer.Side note... this would be so much nicer if we had golang/go#56413 in Go 😛