mirror of
https://github.com/go-task/task.git
synced 2025-01-22 05:10:17 +02:00
Merge pull request #292 from evg4b/included_tasks
Added option to make included Taskfile run commands on its own directory
This commit is contained in:
commit
5e75639244
@ -6,6 +6,8 @@
|
||||
([#252](https://github.com/go-task/task/issues/252)).
|
||||
- Implement short task syntax
|
||||
([#194](https://github.com/go-task/task/issues/194), [#240](https://github.com/go-task/task/pull/240)).
|
||||
- Added option to make included Taskfile run commands on its own directory
|
||||
([#260](https://github.com/go-task/task/issues/260), [#144](https://github.com/go-task/task/issues/144))
|
||||
|
||||
# v3.0.0 - Preview 2
|
||||
|
||||
|
40
internal/taskfile/included_taskfile.go
Normal file
40
internal/taskfile/included_taskfile.go
Normal file
@ -0,0 +1,40 @@
|
||||
package taskfile
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrCantUnmarshalIncludedTaskfile is returned for invalid var YAML.
|
||||
ErrCantUnmarshalIncludedTaskfile = errors.New("task: can't unmarshal included value")
|
||||
)
|
||||
|
||||
// IncludedTaskfile represents information about included tasksfile
|
||||
type IncludedTaskfile struct {
|
||||
Taskfile string
|
||||
Dir string
|
||||
AdvancedImport bool
|
||||
}
|
||||
|
||||
// IncludedTaskfiles represents information about included tasksfiles
|
||||
type IncludedTaskfiles = map[string]IncludedTaskfile
|
||||
|
||||
// UnmarshalYAML implements yaml.Unmarshaler interface
|
||||
func (it *IncludedTaskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var str string
|
||||
if err := unmarshal(&str); err == nil {
|
||||
it.Taskfile = str
|
||||
return nil
|
||||
}
|
||||
|
||||
var includedTaskfile struct {
|
||||
Taskfile string
|
||||
Dir string
|
||||
}
|
||||
if err := unmarshal(&includedTaskfile); err == nil {
|
||||
it.Dir = includedTaskfile.Dir
|
||||
it.Taskfile = includedTaskfile.Taskfile
|
||||
it.AdvancedImport = true
|
||||
return nil
|
||||
}
|
||||
|
||||
return ErrCantUnmarshalIncludedTaskfile
|
||||
}
|
@ -22,7 +22,7 @@ func Merge(t1, t2 *Taskfile, namespaces ...string) error {
|
||||
}
|
||||
|
||||
if t1.Includes == nil {
|
||||
t1.Includes = make(map[string]string)
|
||||
t1.Includes = make(IncludedTaskfiles)
|
||||
}
|
||||
for k, v := range t2.Includes {
|
||||
t1.Includes[k] = v
|
||||
|
@ -28,8 +28,13 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for namespace, path := range t.Includes {
|
||||
path = filepath.Join(dir, path)
|
||||
for namespace, includedTask := range t.Includes {
|
||||
if filepath.IsAbs(includedTask.Taskfile) {
|
||||
path = includedTask.Taskfile
|
||||
} else {
|
||||
path = filepath.Join(dir, includedTask.Taskfile)
|
||||
}
|
||||
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -44,6 +49,15 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
|
||||
if len(includedTaskfile.Includes) > 0 {
|
||||
return nil, ErrIncludedTaskfilesCantHaveIncludes
|
||||
}
|
||||
|
||||
if includedTask.AdvancedImport {
|
||||
for _, task := range includedTaskfile.Tasks {
|
||||
if !filepath.IsAbs(task.Dir) {
|
||||
task.Dir = filepath.Join(includedTask.Dir, task.Dir)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err = taskfile.Merge(t, includedTaskfile, namespace); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ type Taskfile struct {
|
||||
Expansions int
|
||||
Output string
|
||||
Method string
|
||||
Includes map[string]string
|
||||
Includes IncludedTaskfiles
|
||||
Vars Vars
|
||||
Env Vars
|
||||
Tasks Tasks
|
||||
@ -20,7 +20,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
Expansions int
|
||||
Output string
|
||||
Method string
|
||||
Includes map[string]string
|
||||
Includes IncludedTaskfiles
|
||||
Vars Vars
|
||||
Env Vars
|
||||
Tasks Tasks
|
||||
|
8
task.go
8
task.go
@ -207,6 +207,14 @@ func (e *Executor) Setup() error {
|
||||
}
|
||||
}
|
||||
|
||||
if v < 3 {
|
||||
for _, taskfile := range e.Taskfile.Includes {
|
||||
if taskfile.AdvancedImport {
|
||||
return errors.New(`task: Import with additional parameters is only available starting on Taskfile version v3`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
24
task_test.go
24
task_test.go
@ -540,14 +540,32 @@ func TestIncludes(t *testing.T) {
|
||||
Target: "default",
|
||||
TrimSpace: true,
|
||||
Files: map[string]string{
|
||||
"main.txt": "main",
|
||||
"included_directory.txt": "included_directory",
|
||||
"included_taskfile.txt": "included_taskfile",
|
||||
"main.txt": "main",
|
||||
"included_directory.txt": "included_directory",
|
||||
"included_directory_without_dir.txt": "included_directory_without_dir",
|
||||
"included_taskfile_without_dir.txt": "included_taskfile_without_dir",
|
||||
"./module2/included_directory_with_dir.txt": "included_directory_with_dir",
|
||||
"./module2/included_taskfile_with_dir.txt": "included_taskfile_with_dir",
|
||||
},
|
||||
}
|
||||
tt.Run(t)
|
||||
}
|
||||
|
||||
func TestIncorrectVersionIncludes(t *testing.T) {
|
||||
const dir = "testdata/incorrect_includes"
|
||||
expectedError := "task: Import with additional parameters is only available starting on Taskfile version v3"
|
||||
|
||||
var buff bytes.Buffer
|
||||
e := task.Executor{
|
||||
Dir: dir,
|
||||
Stdout: &buff,
|
||||
Stderr: &buff,
|
||||
Silent: true,
|
||||
}
|
||||
|
||||
assert.EqualError(t, e.Setup(), expectedError)
|
||||
}
|
||||
|
||||
func TestIncludesEmptyMain(t *testing.T) {
|
||||
tt := fileContentTest{
|
||||
Dir: "testdata/includes_empty",
|
||||
|
16
testdata/includes/Taskfile.yml
vendored
16
testdata/includes/Taskfile.yml
vendored
@ -1,8 +1,18 @@
|
||||
version: '2'
|
||||
version: '3'
|
||||
|
||||
includes:
|
||||
included: ./included
|
||||
included_taskfile: ./Taskfile2.yml
|
||||
included_without_dir:
|
||||
taskfile: ./module1
|
||||
included_taskfile_without_dir:
|
||||
taskfile: ./module1/Taskfile.yml
|
||||
included_with_dir:
|
||||
taskfile: ./module2
|
||||
dir: ./module2
|
||||
included_taskfile_with_dir:
|
||||
taskfile: ./module2/Taskfile.yml
|
||||
dir: ./module2
|
||||
|
||||
tasks:
|
||||
default:
|
||||
@ -10,6 +20,10 @@ tasks:
|
||||
- task: gen
|
||||
- task: included:gen
|
||||
- task: included_taskfile:gen
|
||||
- task: included_without_dir:gen_file
|
||||
- task: included_taskfile_without_dir:gen_dir
|
||||
- task: included_with_dir:gen_file
|
||||
- task: included_taskfile_with_dir:gen_dir
|
||||
|
||||
gen:
|
||||
cmds:
|
||||
|
2
testdata/includes/Taskfile2.yml
vendored
2
testdata/includes/Taskfile2.yml
vendored
@ -1,4 +1,4 @@
|
||||
version: '2'
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
gen:
|
||||
|
2
testdata/includes/included/Taskfile.yml
vendored
2
testdata/includes/included/Taskfile.yml
vendored
@ -1,4 +1,4 @@
|
||||
version: '2'
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
gen:
|
||||
|
10
testdata/includes/module1/Taskfile.yml
vendored
Normal file
10
testdata/includes/module1/Taskfile.yml
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
gen_dir:
|
||||
cmds:
|
||||
- echo included_directory_without_dir > included_directory_without_dir.txt
|
||||
|
||||
gen_file:
|
||||
cmds:
|
||||
- echo included_taskfile_without_dir > included_taskfile_without_dir.txt
|
10
testdata/includes/module2/Taskfile.yml
vendored
Normal file
10
testdata/includes/module2/Taskfile.yml
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
gen_dir:
|
||||
cmds:
|
||||
- echo included_directory_with_dir > included_directory_with_dir.txt
|
||||
|
||||
gen_file:
|
||||
cmds:
|
||||
- echo included_taskfile_with_dir > included_taskfile_with_dir.txt
|
10
testdata/incorrect_includes/Taskfile.yml
vendored
Normal file
10
testdata/incorrect_includes/Taskfile.yml
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
version: '2.6'
|
||||
|
||||
includes:
|
||||
included:
|
||||
taskfile: ./included
|
||||
|
||||
tasks:
|
||||
default:
|
||||
cmds:
|
||||
- task: gen
|
6
testdata/incorrect_includes/included/Taskfile.yml
vendored
Normal file
6
testdata/incorrect_includes/included/Taskfile.yml
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
version: '2.6'
|
||||
|
||||
tasks:
|
||||
gen:
|
||||
cmds:
|
||||
- echo incorrect includes test
|
Loading…
x
Reference in New Issue
Block a user