Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement task sorting with --sort flag #1105

Merged
merged 8 commits into from Apr 6, 2023
Merged

feat: implement task sorting with --sort flag #1105

merged 8 commits into from Apr 6, 2023

Conversation

pd93
Copy link
Member

@pd93 pd93 commented Apr 2, 2023

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 as alphanumeric, but root tasks (with no namespace) will be always appear first.
    • This is the current sorting behavior
  • none - No sorting. Tasks will appear in the order they are defined in the taskfile
    • If multiple taskfiles are used, they will be traversed in lexicographical order of the taskfile path.
  • alphanumeric - 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:

  • The contents of copy.go has been moved to a new package in internal/deepcopy so that it can be more easily reused
  • We have two other new packages:
    • internal/sort - Contains the task sorting interface and implementations
    • internal/orderedmap - Contains a generic ordered map implementation
  • Refactored taskfile.Tasks to use the orderedmap implementation
  • Refactored taskfile.Vars to use the orderedmap implementation
    • This replaces the custom ordered map that was previously used
    • I had to do some interesting things here because most uses of taskfile.Vars use a pointer. When this pointer is nil, the method on the underlying OrderedMap cannot be called (because of how embedding works in Go). I've simply added some wrapper methods on taskfile.Vars to handle these cases which do nil checks before calling the underlying method. This is not an issue with taskfile.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 馃槢

Copy link
Member

@andreynering andreynering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! 馃憦

This flag needs to be mentioned on the API page. Other than that, this is ready to be merged.

internal/orderedmap/orderedmap.go Outdated Show resolved Hide resolved
cmd/task/task.go Show resolved Hide resolved
@pd93 pd93 merged commit f22389a into master Apr 6, 2023
8 checks passed
@pd93 pd93 deleted the sorting branch April 6, 2023 11:07
@pd93 pd93 mentioned this pull request Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow for changing the order of tasks when listing tasks
2 participants