Skip to content

Commit 2a4a107

Browse files
committed
feat: a few fixes and improvements on task --init
* Fixed check for an existing Taskfile: look for all possibilities, and not only `Taskfile.yml` specifically. * Changed default extension from `.yml` to `.yaml`. * Added a description (`desc`) to the `default` task. Important to at lest `task --list` work by default (a core feature). * Added `yaml-language-server` comment.
1 parent 67a0225 commit 2a4a107

File tree

8 files changed

+44
-32
lines changed

8 files changed

+44
-32
lines changed
File renamed without changes.

init.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66

77
"github.com/go-task/task/v3/errors"
88
"github.com/go-task/task/v3/internal/filepathext"
9+
"github.com/go-task/task/v3/taskfile"
910
)
1011

11-
const defaultTaskFilename = "Taskfile.yml"
12+
const defaultFilename = "Taskfile.yaml"
1213

13-
//go:embed taskfile/templates/default.yml
14+
//go:embed taskfile/templates/default.yaml
1415
var DefaultTaskfile string
1516

1617
// InitTaskfile creates a new Taskfile at path.
@@ -20,22 +21,30 @@ var DefaultTaskfile string
2021
//
2122
// The final file path is always returned and may be different from the input path.
2223
func InitTaskfile(path string) (string, error) {
23-
fi, err := os.Stat(path)
24-
if err == nil && !fi.IsDir() {
24+
info, err := os.Stat(path)
25+
if err == nil && !info.IsDir() {
2526
return path, errors.TaskfileAlreadyExistsError{}
2627
}
2728

28-
if fi != nil && fi.IsDir() {
29-
path = filepathext.SmartJoin(path, defaultTaskFilename)
30-
// path was a directory, so check if Taskfile.yml exists in it
31-
if _, err := os.Stat(path); err == nil {
29+
if info != nil && info.IsDir() {
30+
// path was a directory, check if there is a Taskfile already
31+
if hasDefaultTaskfile(path) {
3232
return path, errors.TaskfileAlreadyExistsError{}
3333
}
34+
path = filepathext.SmartJoin(path, defaultFilename)
3435
}
3536

3637
if err := os.WriteFile(path, []byte(DefaultTaskfile), 0o644); err != nil {
3738
return path, err
3839
}
39-
4040
return path, nil
4141
}
42+
43+
func hasDefaultTaskfile(dir string) bool {
44+
for _, name := range taskfile.DefaultTaskfiles {
45+
if _, err := os.Stat(filepathext.SmartJoin(dir, name)); err == nil {
46+
return true
47+
}
48+
}
49+
return false
50+
}

init_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,29 @@ func TestInitDir(t *testing.T) {
1212
t.Parallel()
1313

1414
const dir = "testdata/init"
15-
file := filepathext.SmartJoin(dir, "Taskfile.yml")
15+
file := filepathext.SmartJoin(dir, "Taskfile.yaml")
16+
defer os.Remove(file)
1617

1718
_ = os.Remove(file)
1819
if _, err := os.Stat(file); err == nil {
19-
t.Errorf("Taskfile.yml should not exist")
20+
t.Errorf("Taskfile.yaml should not exist")
2021
}
2122

2223
if _, err := task.InitTaskfile(dir); err != nil {
2324
t.Error(err)
2425
}
2526

2627
if _, err := os.Stat(file); err != nil {
27-
t.Errorf("Taskfile.yml should exist")
28+
t.Errorf("Taskfile.yaml should exist")
2829
}
29-
30-
_ = os.Remove(file)
3130
}
3231

3332
func TestInitFile(t *testing.T) {
3433
t.Parallel()
3534

3635
const dir = "testdata/init"
3736
file := filepathext.SmartJoin(dir, "Tasks.yml")
37+
defer os.Remove(file)
3838

3939
_ = os.Remove(file)
4040
if _, err := os.Stat(file); err == nil {
@@ -48,5 +48,4 @@ func TestInitFile(t *testing.T) {
4848
if _, err := os.Stat(file); err != nil {
4949
t.Errorf("Tasks.yml should exist")
5050
}
51-
_ = os.Remove(file)
5251
}

internal/fsext/fs_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ func TestSearch(t *testing.T) {
114114
name: "find ../../Taskfile.yml using no entrypoint or dir by walking",
115115
entrypoint: "",
116116
dir: "",
117-
possibleFilenames: []string{"Taskfile.yml"},
118-
expectedEntrypoint: filepath.Join(wd, "..", "..", "Taskfile.yml"),
117+
possibleFilenames: []string{"Taskfile.yaml"},
118+
expectedEntrypoint: filepath.Join(wd, "..", "..", "Taskfile.yaml"),
119119
},
120120
{
121121
name: "find foo.txt first if listed first in possible filenames",

taskfile/node_file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type FileNode struct {
1919

2020
func NewFileNode(entrypoint, dir string, opts ...NodeOption) (*FileNode, error) {
2121
// Find the entrypoint file
22-
resolvedEntrypoint, err := fsext.Search(entrypoint, dir, defaultTaskfiles)
22+
resolvedEntrypoint, err := fsext.Search(entrypoint, dir, DefaultTaskfiles)
2323
if err != nil {
2424
return nil, err
2525
}

taskfile/taskfile.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
)
1313

1414
var (
15-
defaultTaskfiles = []string{
15+
// DefaultTaskfiles is the list of Taskfile file names supported by default.
16+
DefaultTaskfiles = []string{
1617
"Taskfile.yml",
1718
"taskfile.yml",
1819
"Taskfile.yaml",
@@ -66,7 +67,7 @@ func RemoteExists(ctx context.Context, u url.URL) (*url.URL, error) {
6667

6768
// If the request was not successful, append the default Taskfile names to
6869
// the URL and return the URL of the first successful request
69-
for _, taskfile := range defaultTaskfiles {
70+
for _, taskfile := range DefaultTaskfiles {
7071
// Fixes a bug with JoinPath where a leading slash is not added to the
7172
// path if it is empty
7273
if u.Path == "" {

taskfile/templates/default.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# https://taskfile.dev
2+
#
3+
# yaml-language-server: $schema=https://taskfile.dev/schema.json
4+
5+
version: '3'
6+
7+
vars:
8+
GREETING: Hello, world!
9+
10+
tasks:
11+
default:
12+
desc: Print a greeting message
13+
cmds:
14+
- echo "{{.GREETING}}"
15+
silent: true

taskfile/templates/default.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)