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
18 changes: 11 additions & 7 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (*
return nil
}
// If the variable is dynamic, we need to resolve it first
static, err := c.HandleDynamicVar(newVar, dir, env.GetFromVars(result))

static, err := c.HandleDynamicVar(newVar, dir, env.GetFromVars(result),
[]string{}, []string{})
if err != nil {
return err
}
Expand Down Expand Up @@ -145,7 +147,7 @@ func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (*
return result, nil
}

func (c *Compiler) HandleDynamicVar(v ast.Var, dir string, e []string) (string, error) {
func (c *Compiler) HandleDynamicVar(v ast.Var, dir string, e []string, posixOpts []string, bashOpts []string) (string, error) {
c.muDynamicCache.Lock()
defer c.muDynamicCache.Unlock()

Expand All @@ -168,11 +170,13 @@ func (c *Compiler) HandleDynamicVar(v ast.Var, dir string, e []string) (string,

var stdout bytes.Buffer
opts := &execext.RunCommandOptions{
Command: *v.Sh,
Dir: dir,
Stdout: &stdout,
Stderr: c.Logger.Stderr,
Env: e,
Command: *v.Sh,
Dir: dir,
Stdout: &stdout,
Stderr: c.Logger.Stderr,
Env: e,
PosixOpts: posixOpts,
BashOpts: bashOpts,
}
if err := execext.RunCommand(context.Background(), opts); err != nil {
return "", fmt.Errorf(`task: Command "%s" failed: %s`, opts.Command, err)
Expand Down
19 changes: 13 additions & 6 deletions internal/fingerprint/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,32 @@ import (
"github.com/go-task/task/v3/internal/env"
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/internal/slicesext"
"github.com/go-task/task/v3/taskfile/ast"
)

type StatusChecker struct {
logger *logger.Logger
logger *logger.Logger
posixOpts []string
bashOpts []string
}

func NewStatusChecker(logger *logger.Logger) StatusCheckable {
func NewStatusChecker(logger *logger.Logger, posixOpts []string, bashOpts []string) StatusCheckable {
return &StatusChecker{
logger: logger,
logger: logger,
posixOpts: posixOpts,
bashOpts: bashOpts,
}
}

func (checker *StatusChecker) IsUpToDate(ctx context.Context, t *ast.Task) (bool, error) {
for _, s := range t.Status {
err := execext.RunCommand(ctx, &execext.RunCommandOptions{
Command: s,
Dir: t.Dir,
Env: env.Get(t),
Command: s,
Dir: t.Dir,
Env: env.Get(t),
PosixOpts: slicesext.UniqueJoin(checker.posixOpts, t.Set),
BashOpts: slicesext.UniqueJoin(checker.bashOpts, t.Shopt),
})
if err != nil {
checker.logger.VerboseOutf(logger.Yellow, "task: status command %s exited non-zero: %s\n", s, err)
Expand Down
18 changes: 17 additions & 1 deletion internal/fingerprint/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type (
logger *logger.Logger
statusChecker StatusCheckable
sourcesChecker SourcesCheckable
posixOpts []string
bashOpts []string
}
)

Expand Down Expand Up @@ -55,6 +57,18 @@ func WithSourcesChecker(checker SourcesCheckable) CheckerOption {
}
}

func WithPosixOpts(posixOpts []string) CheckerOption {
return func(config *CheckerConfig) {
config.posixOpts = posixOpts
}
}

func WithBashOpts(bashOpts []string) CheckerOption {
return func(config *CheckerConfig) {
config.bashOpts = bashOpts
}
}

func IsTaskUpToDate(
ctx context.Context,
t *ast.Task,
Expand All @@ -72,6 +86,8 @@ func IsTaskUpToDate(
logger: nil,
statusChecker: nil,
sourcesChecker: nil,
posixOpts: []string{},
bashOpts: []string{},
}

// Apply functional options
Expand All @@ -81,7 +97,7 @@ func IsTaskUpToDate(

// If no status checker was given, set up the default one
if config.statusChecker == nil {
config.statusChecker = NewStatusChecker(config.logger)
config.statusChecker = NewStatusChecker(config.logger, config.posixOpts, config.bashOpts)
}

// If no sources checker was given, set up the default one
Expand Down
9 changes: 6 additions & 3 deletions precondition.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/go-task/task/v3/internal/env"
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/internal/slicesext"
"github.com/go-task/task/v3/taskfile/ast"
)

Expand All @@ -16,9 +17,11 @@ var ErrPreconditionFailed = errors.New("task: precondition not met")
func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *ast.Task) (bool, error) {
for _, p := range t.Preconditions {
err := execext.RunCommand(ctx, &execext.RunCommandOptions{
Command: p.Sh,
Dir: t.Dir,
Env: env.Get(t),
Command: p.Sh,
Dir: t.Dir,
Env: env.Get(t),
PosixOpts: slicesext.UniqueJoin(e.Taskfile.Set, t.Set),
BashOpts: slicesext.UniqueJoin(e.Taskfile.Shopt, t.Shopt),
})
if err != nil {
if !errors.Is(err, context.Canceled) {
Expand Down
2 changes: 2 additions & 0 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func (e *Executor) Status(ctx context.Context, calls ...*Call) error {
fingerprint.WithTempDir(e.TempDir.Fingerprint),
fingerprint.WithDry(e.Dry),
fingerprint.WithLogger(e.Logger),
fingerprint.WithPosixOpts(e.Taskfile.Set),
fingerprint.WithBashOpts(e.Taskfile.Shopt),
)
if err != nil {
return err
Expand Down
18 changes: 12 additions & 6 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ func (e *Executor) RunTask(ctx context.Context, call *Call) error {
// Check if condition after CompiledTask so dynamic variables are resolved
if strings.TrimSpace(t.If) != "" {
if err := execext.RunCommand(ctx, &execext.RunCommandOptions{
Command: t.If,
Dir: t.Dir,
Env: env.Get(t),
Command: t.If,
Dir: t.Dir,
Env: env.Get(t),
PosixOpts: slicesext.UniqueJoin(e.Taskfile.Set, t.Set),
BashOpts: slicesext.UniqueJoin(e.Taskfile.Shopt, t.Shopt),
}); err != nil {
e.Logger.VerboseOutf(logger.Yellow, "task: if condition not met - skipped: %q\n", call.Task)
return nil
Expand Down Expand Up @@ -231,6 +233,8 @@ func (e *Executor) RunTask(ctx context.Context, call *Call) error {
fingerprint.WithTempDir(e.TempDir.Fingerprint),
fingerprint.WithDry(e.Dry),
fingerprint.WithLogger(e.Logger),
fingerprint.WithPosixOpts(e.Taskfile.Set),
fingerprint.WithBashOpts(e.Taskfile.Shopt),
)
if err != nil {
return err
Expand Down Expand Up @@ -365,9 +369,11 @@ func (e *Executor) runCommand(ctx context.Context, t *ast.Task, call *Call, i in
// Check if condition for any command type
if strings.TrimSpace(cmd.If) != "" {
if err := execext.RunCommand(ctx, &execext.RunCommandOptions{
Command: cmd.If,
Dir: t.Dir,
Env: env.Get(t),
Command: cmd.If,
Dir: t.Dir,
Env: env.Get(t),
PosixOpts: slicesext.UniqueJoin(e.Taskfile.Set, t.Set, cmd.Set),
BashOpts: slicesext.UniqueJoin(e.Taskfile.Shopt, t.Shopt, cmd.Shopt),
}); err != nil {
e.Logger.VerboseOutf(logger.Yellow, "task: [%s] if condition not met - skipped\n", t.Name())
return nil
Expand Down
5 changes: 4 additions & 1 deletion variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/filepathext"
"github.com/go-task/task/v3/internal/fingerprint"
"github.com/go-task/task/v3/internal/slicesext"
"github.com/go-task/task/v3/internal/templater"
"github.com/go-task/task/v3/taskfile/ast"
)
Expand Down Expand Up @@ -173,7 +174,9 @@ func (e *Executor) compiledTask(call *Call, evaluateShVars bool) (*ast.Task, err
new.Env.Set(k, ast.Var{Value: v.Value})
continue
}
static, err := e.Compiler.HandleDynamicVar(v, new.Dir, env.GetFromVars(new.Env))
static, err := e.Compiler.HandleDynamicVar(v, new.Dir, env.GetFromVars(new.Env),
slicesext.UniqueJoin(e.Taskfile.Set, origTask.Set),
slicesext.UniqueJoin(e.Taskfile.Shopt, origTask.Shopt))
if err != nil {
return nil, err
}
Expand Down
Loading