mirror of
https://github.com/go-task/task.git
synced 2025-11-25 22:32:55 +02:00
feat: root remote taskfiles
This commit is contained in:
@@ -5,39 +5,36 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/go-task/task/v3/internal/execext"
|
||||
"github.com/go-task/task/v3/internal/filepathext"
|
||||
"github.com/go-task/task/v3/internal/logger"
|
||||
"github.com/go-task/task/v3/taskfile/ast"
|
||||
)
|
||||
|
||||
// A FileNode is a node that reads a taskfile from the local filesystem.
|
||||
type FileNode struct {
|
||||
*BaseNode
|
||||
Dir string
|
||||
Entrypoint string
|
||||
}
|
||||
|
||||
func NewFileNode(uri string, opts ...NodeOption) (*FileNode, error) {
|
||||
func NewFileNode(l *logger.Logger, entrypoint, dir string, opts ...NodeOption) (*FileNode, error) {
|
||||
var err error
|
||||
base := NewBaseNode(opts...)
|
||||
if uri == "" {
|
||||
d, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uri = d
|
||||
}
|
||||
path, err := Exists(uri)
|
||||
entrypoint, dir, err = resolveFileNodeEntrypointAndDir(l, entrypoint, dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base.dir = dir
|
||||
return &FileNode{
|
||||
BaseNode: base,
|
||||
Dir: filepath.Dir(path),
|
||||
Entrypoint: filepath.Base(path),
|
||||
Entrypoint: entrypoint,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (node *FileNode) Location() string {
|
||||
return filepathext.SmartJoin(node.Dir, node.Entrypoint)
|
||||
return node.Entrypoint
|
||||
}
|
||||
|
||||
func (node *FileNode) Remote() bool {
|
||||
@@ -53,6 +50,67 @@ func (node *FileNode) Read(ctx context.Context) ([]byte, error) {
|
||||
return io.ReadAll(f)
|
||||
}
|
||||
|
||||
func (node *FileNode) BaseDir() string {
|
||||
return node.Dir
|
||||
// resolveFileNodeEntrypointAndDir resolves checks the values of entrypoint and dir and
|
||||
// populates them with default values if necessary.
|
||||
func resolveFileNodeEntrypointAndDir(l *logger.Logger, entrypoint, dir string) (string, string, error) {
|
||||
var err error
|
||||
if entrypoint != "" {
|
||||
entrypoint, err = Exists(l, entrypoint)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if dir == "" {
|
||||
dir = filepath.Dir(entrypoint)
|
||||
}
|
||||
return entrypoint, dir, nil
|
||||
}
|
||||
if dir == "" {
|
||||
dir, err = os.Getwd()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
}
|
||||
entrypoint, err = ExistsWalk(l, dir)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
dir = filepath.Dir(entrypoint)
|
||||
return entrypoint, dir, nil
|
||||
}
|
||||
|
||||
func (node *FileNode) ResolveIncludeEntrypoint(include ast.Include) (string, error) {
|
||||
// If the file is remote, we don't need to resolve the path
|
||||
if strings.Contains(include.Taskfile, "://") {
|
||||
return include.Taskfile, nil
|
||||
}
|
||||
|
||||
path, err := execext.Expand(include.Taskfile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if filepathext.IsAbs(path) {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
// NOTE: Uses the directory of the entrypoint (Taskfile), not the current working directory
|
||||
// This means that files are included relative to one another
|
||||
entrypointDir := filepath.Dir(node.Entrypoint)
|
||||
return filepathext.SmartJoin(entrypointDir, path), nil
|
||||
}
|
||||
|
||||
func (node *FileNode) ResolveIncludeDir(include ast.Include) (string, error) {
|
||||
path, err := execext.Expand(include.Dir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if filepathext.IsAbs(path) {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
// NOTE: Uses the directory of the entrypoint (Taskfile), not the current working directory
|
||||
// This means that files are included relative to one another
|
||||
entrypointDir := filepath.Dir(node.Entrypoint)
|
||||
return filepathext.SmartJoin(entrypointDir, path), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user