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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Allow shorter syntax for tasks with default configuration #240

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion internal/taskfile/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ type Task struct {
Silent bool
Method string
Prefix string
IgnoreError bool `yaml:"ignore_error"`
Copy link
Member

Choose a reason for hiding this comment

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

Given the suggestion made on func (t *Task) UnmarshalYAML, we should re-add this reflect tag.

IgnoreError bool
}
58 changes: 58 additions & 0 deletions internal/taskfile/taskfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,61 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
}
return nil
}

func (t *Task) UnmarshalYAML(unmarshal func(interface{}) error) error {
var task struct {
Copy link
Member

Choose a reason for hiding this comment

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

Hey @jaedle,

IMHO you don't need to copy the entire task struct into this function. Just try to marshal the string and []string types first, and none work then unmarshal to the t variable directly. 🙂

Copy link
Member

Choose a reason for hiding this comment

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

Oops, I just remembered that we also have alternative command syntaxes, like { cmd: "echo foo", silent: true } for example.

That's easy to do, though. Just check for taskfile.Cmd and []taskfile.Cmd instead of for string and []string. All variations should just work.

Task string
Cmds []*Cmd
Deps []*Dep
Desc string
Summary string
Sources []string
Generates []string
Status []string
Preconditions []*Precondition
Dir string
Vars Vars
Env Vars
Silent bool
Method string
Prefix string
IgnoreError bool `yaml:"ignore_error"`
}

err := unmarshal(&task)
if err != nil {
var short []string
err := unmarshal(&short)
if err != nil {
return err
}

for _, cmd := range short {
t.Cmds = append(t.Cmds, &Cmd{
Cmd: cmd,
})

}

return nil
}

t.Task = task.Task
t.Cmds = task.Cmds
t.Deps = task.Deps
t.Desc = task.Desc
t.Summary = task.Summary
t.Sources = task.Sources
t.Generates = task.Generates
t.Status = task.Status
t.Preconditions = task.Preconditions
t.Dir = task.Dir
t.Vars = task.Vars
t.Env = task.Env
t.Silent = task.Silent
t.Method = task.Method
t.Prefix = task.Prefix
t.IgnoreError = task.IgnoreError

return nil
}
15 changes: 14 additions & 1 deletion task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,20 @@ func TestWhenDirAttributeItCreatesMissingAndRunsInThatDir(t *testing.T) {
_ = os.Remove(toBeCreated)
}

func TestShortTaskNotation(t *testing.T) {
const dir = "testdata/short_task_notation/"

var buff bytes.Buffer
e := task.Executor{
Dir: dir,
Stdout: &buff,
Stderr: &buff,
}
assert.NoError(t, e.Setup())
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "run"}))
assert.Equal(t, readTestFixture(t, dir, "expected_output.txt"), buff.String())
}

func TestDisplaysErrorOnUnsupportedVersion(t *testing.T) {
e := task.Executor{
Dir: "testdata/version/v1",
Expand All @@ -650,5 +664,4 @@ func TestDisplaysErrorOnUnsupportedVersion(t *testing.T) {
err := e.Setup()
assert.Error(t, err)
assert.Equal(t, "task: Taskfile versions prior to v2 are not supported anymore", err.Error())

}
16 changes: 16 additions & 0 deletions testdata/short_task_notation/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '3'

tasks:

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

run:
cmds:
- task: single-cmd
- task: multiple-cmds

single-cmd:
cmds:
- echo "single-line-short-task"

multiple-cmds:
- echo "multiple-cmds-short-tasks-1"
- echo "multiple-cmds-short-tasks-2"
6 changes: 6 additions & 0 deletions testdata/short_task_notation/expected_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
task: echo "single-line-short-task"
single-line-short-task
task: echo "multiple-cmds-short-tasks-1"
multiple-cmds-short-tasks-1
task: echo "multiple-cmds-short-tasks-2"
multiple-cmds-short-tasks-2