Skip to content
Open
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
25 changes: 17 additions & 8 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (

"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/filepathext"
"github.com/go-task/task/v3/taskfile"
)

const defaultTaskFilename = "Taskfile.yml"
const defaultFilename = "Taskfile.yml"

//go:embed taskfile/templates/default.yml
var DefaultTaskfile string
Expand All @@ -20,22 +21,30 @@ var DefaultTaskfile string
//
// The final file path is always returned and may be different from the input path.
func InitTaskfile(path string) (string, error) {
fi, err := os.Stat(path)
if err == nil && !fi.IsDir() {
info, err := os.Stat(path)
if err == nil && !info.IsDir() {
return path, errors.TaskfileAlreadyExistsError{}
}

if fi != nil && fi.IsDir() {
path = filepathext.SmartJoin(path, defaultTaskFilename)
// path was a directory, so check if Taskfile.yml exists in it
if _, err := os.Stat(path); err == nil {
if info != nil && info.IsDir() {
// path was a directory, check if there is a Taskfile already
if hasDefaultTaskfile(path) {
return path, errors.TaskfileAlreadyExistsError{}
}
path = filepathext.SmartJoin(path, defaultFilename)
}

if err := os.WriteFile(path, []byte(DefaultTaskfile), 0o644); err != nil {
return path, err
}

return path, nil
}

func hasDefaultTaskfile(dir string) bool {
for _, name := range taskfile.DefaultTaskfiles {
if _, err := os.Stat(filepathext.SmartJoin(dir, name)); err == nil {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion taskfile/node_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type FileNode struct {

func NewFileNode(entrypoint, dir string, opts ...NodeOption) (*FileNode, error) {
// Find the entrypoint file
resolvedEntrypoint, err := fsext.Search(entrypoint, dir, defaultTaskfiles)
resolvedEntrypoint, err := fsext.Search(entrypoint, dir, DefaultTaskfiles)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions taskfile/taskfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

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

// If the request was not successful, append the default Taskfile names to
// the URL and return the URL of the first successful request
for _, taskfile := range defaultTaskfiles {
for _, taskfile := range DefaultTaskfiles {
// Fixes a bug with JoinPath where a leading slash is not added to the
// path if it is empty
if u.Path == "" {
Expand Down
3 changes: 2 additions & 1 deletion taskfile/templates/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
version: '3'
Copy link
Member

Choose a reason for hiding this comment

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

We could consider setting this to the current version of the binary creating the file. This ensures that the same features will be available for the person initialising the file and the people reading it.

It would also encourage the use of this feature which I think is underused. Most people just put 3 despite requiring a much more recent version.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is an interesting idea.

Would we want to add as 3.45.4 or 3.45?

Copy link
Member

Choose a reason for hiding this comment

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

3.45 is probably enough since we only add features on minor revisions anyway. The chances are that anyone getting a message to upgrade will immediately install the latest version instead of the minimum version anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

A different perspective: unless a taskfile needs a specific version, then doing this will become an annoyance when people are working/sharing/using different versions of task/taskfiles. We don't use the feature because its not very useful for us ... the version of Task in use is already managed, which is sufficient.

Copy link
Member

Choose a reason for hiding this comment

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

The version key is not intended to dictate a specific version. It is a minimum version required for the features in the Taskfile. The vast majority of Taskfiles will require a minimum version since they'll use features introduced after v3.0.0. For instance, any Taskfile that uses aliases would need to have a minimum version of 3.17.

This is extremely useful as instead of receiving a confusing error when they use a version of Task that is too old, they'll receive a message that tells them to update.

I think the main reason this is underused is because it didn't work this way until v3.34 and is underdocumented. None of our examples/tests use it and the tool itself doesn't advertise the feature. Adding this to the --init flag is analogous to go mod init adding the current version of Go to the file.


vars:
GREETING: Hello, World!
GREETING: Hello, world!

tasks:
default:
desc: Print a greeting message
cmds:
- echo "{{.GREETING}}"
silent: true
2 changes: 2 additions & 0 deletions website/src/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ vars:

tasks:
default:
desc: Print a greeting message
cmds:
- echo "{{.GREETING}}"
silent: true
Expand Down Expand Up @@ -111,6 +112,7 @@ vars:

tasks:
default:
desc: Print a greeting message
cmds:
- echo "{{.GREETING}}"
silent: true
Expand Down
Loading