From fe2b8c8afa4642ba1614a7e7155e03daad7f9774 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sat, 15 Jun 2019 21:12:54 -0300 Subject: [PATCH] Post-fixes to #211 --- CHANGELOG.md | 5 ++++ internal/taskfile/task.go | 23 ----------------- task.go | 25 ++++++++++++++++--- .../explicit_exists/exists/{.keepme => .keep} | 0 4 files changed, 27 insertions(+), 26 deletions(-) rename testdata/dir/explicit_exists/exists/{.keepme => .keep} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6facc17d..de23253c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Unreleased + +- Create directory informed on `dir:` if it doesn't exist + ([#209](https://github.com/go-task/task/issues/209), [#211](https://github.com/go-task/task/pull/211)). + ## v2.5.2 - 2019-05-11 - Reverted YAML upgrade due issues with CRLF on Windows diff --git a/internal/taskfile/task.go b/internal/taskfile/task.go index a686f04b..1afcbfa3 100644 --- a/internal/taskfile/task.go +++ b/internal/taskfile/task.go @@ -1,8 +1,5 @@ package taskfile -import "os" -import "sync" - // Tasks represents a group of tasks type Tasks map[string]*Task @@ -17,7 +14,6 @@ type Task struct { Generates []string Status []string Dir string - mkdirMutex sync.Mutex Vars Vars Env Vars Silent bool @@ -25,22 +21,3 @@ type Task struct { Prefix string IgnoreError bool `yaml:"ignore_error"` } - -// Mkdir creates the directory Task.Dir. -// Safe to be called concurrently. -func (t *Task) Mkdir() error { - if t.Dir == "" { - // No "dir:" attribute, so we do nothing. - return nil - } - - t.mkdirMutex.Lock() - defer t.mkdirMutex.Unlock() - - if _, err := os.Stat(t.Dir); os.IsNotExist(err) { - if err := os.MkdirAll(t.Dir, 0755); err != nil { - return err - } - } - return nil -} diff --git a/task.go b/task.go index bca22c4d..e0688983 100644 --- a/task.go +++ b/task.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "sync" "sync/atomic" "github.com/go-task/task/v2/internal/compiler" @@ -51,6 +52,7 @@ type Executor struct { taskvars taskfile.Vars taskCallCount map[string]*int32 + mkdirMutexMap map[string]*sync.Mutex } // Run runs Task @@ -167,8 +169,10 @@ func (e *Executor) Setup() error { } e.taskCallCount = make(map[string]*int32, len(e.Taskfile.Tasks)) + e.mkdirMutexMap = make(map[string]*sync.Mutex, len(e.Taskfile.Tasks)) for k := range e.Taskfile.Tasks { e.taskCallCount[k] = new(int32) + e.mkdirMutexMap[k] = &sync.Mutex{} } return nil } @@ -200,9 +204,7 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error { } } - // When using the "dir:" attribute it can happen that the directory doesn't exist. - // If so, we create it. - if err := t.Mkdir(); err != nil { + if err := e.mkdir(t); err != nil { e.Logger.Errf("task: cannot make directory %q: %v", t.Dir, err) } @@ -223,6 +225,23 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error { return nil } +func (e *Executor) mkdir(t *taskfile.Task) error { + if t.Dir == "" { + return nil + } + + mutex := e.mkdirMutexMap[t.Task] + mutex.Lock() + defer mutex.Unlock() + + if _, err := os.Stat(t.Dir); os.IsNotExist(err) { + if err := os.MkdirAll(t.Dir, 0755); err != nil { + return err + } + } + return nil +} + func (e *Executor) runDeps(ctx context.Context, t *taskfile.Task) error { g, ctx := errgroup.WithContext(ctx) diff --git a/testdata/dir/explicit_exists/exists/.keepme b/testdata/dir/explicit_exists/exists/.keep similarity index 100% rename from testdata/dir/explicit_exists/exists/.keepme rename to testdata/dir/explicit_exists/exists/.keep