Skip to content

Commit ed37071

Browse files
authored
Merge pull request #621 from kerma/feat/support-yaml
Add support for yaml extension
2 parents d5d1984 + d73cf10 commit ed37071

File tree

11 files changed

+101
-27
lines changed

11 files changed

+101
-27
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Add support for yaml extension ([#584](https://github.com/go-task/task/issues/584))
6+
37
## v3.9.2 - 2021-12-02
48

59
- Upgrade [mvdan/sh](https://github.com/mvdan/sh) which contains a fix a for

cmd/task/task.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func main() {
7777

7878
pflag.BoolVar(&versionFlag, "version", false, "show Task version")
7979
pflag.BoolVarP(&helpFlag, "help", "h", false, "shows Task usage")
80-
pflag.BoolVarP(&init, "init", "i", false, "creates a new Taskfile.yml in the current folder")
80+
pflag.BoolVarP(&init, "init", "i", false, "creates a new Taskfile.yaml in the current folder")
8181
pflag.BoolVarP(&list, "list", "l", false, "lists tasks with description of current Taskfile")
8282
pflag.BoolVar(&status, "status", false, "exits with non-zero exit code if any of the given tasks is not up-to-date")
8383
pflag.BoolVarP(&force, "force", "f", false, "forces execution even when the task is up-to-date")
@@ -122,8 +122,6 @@ func main() {
122122
if entrypoint != "" {
123123
dir = filepath.Dir(entrypoint)
124124
entrypoint = filepath.Base(entrypoint)
125-
} else {
126-
entrypoint = "Taskfile.yml"
127125
}
128126

129127
e := task.Executor{

completion/zsh/_task

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
function __list() {
55
local -a scripts
66

7-
if [ -f Taskfile.yml ]; then
7+
if [ -f Taskfile.yml ] || [ -f Taskfile.yaml ]; then
88
scripts=($(task -l | sed '1d' | sed 's/^\* //' | awk '{ print $1 }' | sed 's/:$//' | sed 's/:/\\:/g'))
99
_describe 'script' scripts
1010
fi

init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ tasks:
2323

2424
// InitTaskfile Taskfile creates a new Taskfile
2525
func InitTaskfile(w io.Writer, dir string) error {
26-
f := filepath.Join(dir, "Taskfile.yml")
26+
f := filepath.Join(dir, "Taskfile.yaml")
2727

2828
if _, err := os.Stat(f); err == nil {
2929
return ErrTaskfileAlreadyExists
@@ -32,6 +32,6 @@ func InitTaskfile(w io.Writer, dir string) error {
3232
if err := os.WriteFile(f, []byte(defaultTaskfile), 0644); err != nil {
3333
return err
3434
}
35-
fmt.Fprintf(w, "Taskfile.yml created in the current directory\n")
35+
fmt.Fprintf(w, "Taskfile.yaml created in the current directory\n")
3636
return nil
3737
}

task.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error {
105105

106106
// Setup setups Executor's internal state
107107
func (e *Executor) Setup() error {
108-
if e.Entrypoint == "" {
109-
e.Entrypoint = "Taskfile.yml"
110-
}
111-
112108
var err error
113109
e.Taskfile, err = read.Taskfile(e.Dir, e.Entrypoint)
114110
if err != nil {

task_test.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ func init() {
2424
// fileContentTest provides a basic reusable test-case for running a Taskfile
2525
// and inspect generated files.
2626
type fileContentTest struct {
27-
Dir string
28-
Target string
29-
TrimSpace bool
30-
Files map[string]string
27+
Dir string
28+
Entrypoint string
29+
Target string
30+
TrimSpace bool
31+
Files map[string]string
3132
}
3233

3334
func (fct fileContentTest) name(file string) string {
@@ -40,9 +41,10 @@ func (fct fileContentTest) Run(t *testing.T) {
4041
}
4142

4243
e := &task.Executor{
43-
Dir: fct.Dir,
44-
Stdout: io.Discard,
45-
Stderr: io.Discard,
44+
Dir: fct.Dir,
45+
Entrypoint: fct.Entrypoint,
46+
Stdout: io.Discard,
47+
Stderr: io.Discard,
4648
}
4749
assert.NoError(t, e.Setup(), "e.Setup()")
4850
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: fct.Target}), "e.Run(target)")
@@ -550,20 +552,21 @@ func TestStatusVariables(t *testing.T) {
550552

551553
func TestInit(t *testing.T) {
552554
const dir = "testdata/init"
553-
var file = filepath.Join(dir, "Taskfile.yml")
555+
var file = filepath.Join(dir, "Taskfile.yaml")
554556

555557
_ = os.Remove(file)
556558
if _, err := os.Stat(file); err == nil {
557-
t.Errorf("Taskfile.yml should not exist")
559+
t.Errorf("Taskfile.yaml should not exist")
558560
}
559561

560562
if err := task.InitTaskfile(io.Discard, dir); err != nil {
561563
t.Error(err)
562564
}
563565

564566
if _, err := os.Stat(file); err != nil {
565-
t.Errorf("Taskfile.yml should exist")
567+
t.Errorf("Taskfile.yaml should exist")
566568
}
569+
_ = os.Remove(file)
567570
}
568571

569572
func TestCyclicDep(t *testing.T) {
@@ -790,6 +793,21 @@ func TestIncludesOptionalExplicitFalse(t *testing.T) {
790793
assert.Equal(t, "stat testdata/includes_optional_explicit_false/TaskfileOptional.yml: no such file or directory", err.Error())
791794
}
792795

796+
func TestIncludesFromCustomTaskfile(t *testing.T) {
797+
tt := fileContentTest{
798+
Dir: "testdata/includes_yaml",
799+
Entrypoint: "Custom.ext",
800+
Target: "default",
801+
TrimSpace: true,
802+
Files: map[string]string{
803+
"main.txt": "main",
804+
"included_with_yaml_extension.txt": "included_with_yaml_extension",
805+
"included_with_custom_file.txt": "included_with_custom_file",
806+
},
807+
}
808+
tt.Run(t)
809+
}
810+
793811
func TestSummary(t *testing.T) {
794812
const dir = "testdata/summary"
795813

taskfile/read/taskfile.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,26 @@ var (
1919
ErrIncludedTaskfilesCantHaveIncludes = errors.New("task: Included Taskfiles can't have includes. Please, move the include to the main Taskfile")
2020
// ErrIncludedTaskfilesCantHaveDotenvs is returned when a included Taskfile contains dotenvs
2121
ErrIncludedTaskfilesCantHaveDotenvs = errors.New("task: Included Taskfiles can't have dotenv declarations. Please, move the dotenv declaration to the main Taskfile")
22+
23+
defaultTaskfiles = []string{"Taskfile.yml", "Taskfile.yaml"}
2224
)
2325

2426
// Taskfile reads a Taskfile for a given directory
27+
// Uses current dir when dir is left empty. Uses Taskfile.yml
28+
// or Taskfile.yaml when entrypoint is left empty
2529
func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
26-
path := filepath.Join(dir, entrypoint)
27-
if _, err := os.Stat(path); err != nil {
28-
return nil, fmt.Errorf(`task: No Taskfile found on "%s". Use "task --init" to create a new one`, path)
30+
if dir == "" {
31+
d, err := os.Getwd()
32+
if err != nil {
33+
return nil, err
34+
}
35+
dir = d
2936
}
37+
path, err := exists(filepath.Join(dir, entrypoint))
38+
if err != nil {
39+
return nil, err
40+
}
41+
3042
t, err := readTaskfile(path)
3143
if err != nil {
3244
return nil, err
@@ -59,16 +71,14 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
5971
path = filepath.Join(dir, path)
6072
}
6173

62-
info, err := os.Stat(path)
74+
path, err = exists(path)
6375
if err != nil {
6476
if includedTask.Optional {
6577
return nil
6678
}
6779
return err
6880
}
69-
if info.IsDir() {
70-
path = filepath.Join(path, "Taskfile.yml")
71-
}
81+
7282
includedTaskfile, err := readTaskfile(path)
7383
if err != nil {
7484
return err
@@ -141,3 +151,22 @@ func readTaskfile(file string) (*taskfile.Taskfile, error) {
141151
var t taskfile.Taskfile
142152
return &t, yaml.NewDecoder(f).Decode(&t)
143153
}
154+
155+
func exists(path string) (string, error) {
156+
fi, err := os.Stat(path)
157+
if err != nil {
158+
return "", err
159+
}
160+
if fi.Mode().IsRegular() {
161+
return path, nil
162+
}
163+
164+
for _, n := range defaultTaskfiles {
165+
fpath := filepath.Join(path, n)
166+
if _, err := os.Stat(fpath); err == nil {
167+
return fpath, nil
168+
}
169+
}
170+
171+
return "", fmt.Errorf(`task: No Taskfile found in "%s". Use "task --init" to create a new one`, path)
172+
}

testdata/includes_yaml/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.txt

testdata/includes_yaml/Custom.ext

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: '3'
2+
3+
includes:
4+
included: ./included
5+
custom: ./included/custom.yaml
6+
7+
tasks:
8+
default:
9+
cmds:
10+
- task: gen
11+
- task: included:gen
12+
- task: custom:gen
13+
14+
gen:
15+
cmds:
16+
- echo main > main.txt
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: '3'
2+
3+
tasks:
4+
gen:
5+
cmds:
6+
- echo included_with_yaml_extension > included_with_yaml_extension.txt

0 commit comments

Comments
 (0)