Skip to content

Commit f04dac5

Browse files
committed
refactor(ParallelRunner): support command execution
1 parent 9677527 commit f04dac5

14 files changed

+348
-335
lines changed

add/add_react_component.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ import (
1212
)
1313

1414
type AddReactComponent struct {
15-
recipe *util.GenerationRecipe
16-
gen core.Generator
15+
composer *util.TaskComposer
16+
runner core.Generator
1717
}
1818

1919
var _ core.Executor = AddReactComponent{}
2020

2121
func NewAddReactComponent(name string) *AddReactComponent {
22-
recipe := util.NewGenerationRecipe("",
23-
&util.GenerationMaterial{
22+
composer := util.NewTaskComposer("",
23+
&core.FileDesc{
2424
Path: fmt.Sprintf("%s.%s", name, "tsx"),
25-
Provider: func() []byte {
25+
Data: func() []byte {
2626
var buf bytes.Buffer
2727

2828
templates.WriteReactComponent(&buf, name)
2929
return buf.Bytes()
3030
},
3131
},
3232
)
33-
return &AddReactComponent{recipe: recipe}
33+
return &AddReactComponent{composer: composer}
3434
}
3535

3636
func (atn AddReactComponent) Run() error {
37-
log.Debugf("generation tree:\n%s", atn.recipe.GetGenerationTree())
38-
gen := atn.recipe.MakeGenerator()
39-
atn.gen = gen
40-
return errors.Wrap(gen.Run(), "failed to generate react component")
37+
log.Debugf("runnereration tree:\n%s", atn.composer.GetGenerationTree())
38+
runner := atn.composer.MakeRunner()
39+
atn.runner = runner
40+
return errors.Wrap(runner.Run(), "failed to runnererate react component")
4141
}

add/add_react_hook.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ import (
1212
)
1313

1414
type AddReactHook struct {
15-
recipe *util.GenerationRecipe
16-
gen core.Generator
15+
composer *util.TaskComposer
16+
runner core.Generator
1717
}
1818

1919
var _ core.Executor = AddReactHook{}
2020

2121
func NewAddReactHook(name string) *AddReactHook {
22-
recipe := util.NewGenerationRecipe("",
23-
&util.GenerationMaterial{
22+
composer := util.NewTaskComposer("",
23+
&core.FileDesc{
2424
Path: fmt.Sprintf("%s.%s", name, "ts"),
25-
Provider: func() []byte {
25+
Data: func() []byte {
2626
var buf bytes.Buffer
2727

2828
templates.WriteReactHook(&buf, name)
2929
return buf.Bytes()
3030
},
3131
},
3232
)
33-
return &AddReactHook{recipe: recipe}
33+
return &AddReactHook{composer: composer}
3434
}
3535

3636
func (atn AddReactHook) Run() error {
37-
log.Debugf("generation tree:\n%s", atn.recipe.GetGenerationTree())
38-
gen := atn.recipe.MakeGenerator()
39-
atn.gen = gen
40-
return errors.Wrap(gen.Run(), "failed to generate react hook")
37+
log.Debugf("runnereration tree:\n%s", atn.composer.GetGenerationTree())
38+
runner := atn.composer.MakeRunner()
39+
atn.runner = runner
40+
return errors.Wrap(runner.Run(), "failed to runnererate react hook")
4141
}

add/add_typescript_node.go

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,55 @@ import (
1111
)
1212

1313
type AddTypescriptToNode struct {
14-
recipe *util.GenerationRecipe
15-
gen core.Generator
14+
composer *util.TaskComposer
15+
runner core.Generator
1616
}
1717

18-
var _ core.Executor = AddTypescriptToNode{}
18+
var _ core.Generator = AddTypescriptToNode{}
1919

2020
func NewAddTypescriptToNode() *AddTypescriptToNode {
21-
recipe := util.NewGenerationRecipe("",
22-
&util.GenerationMaterial{
21+
composer := util.NewTaskComposer("",
22+
&core.FileDesc{
2323
Path: ".eslintrc.js",
24-
Provider: func() []byte {
24+
Data: func() []byte {
2525
var buf bytes.Buffer
2626

2727
templates.WriteNodeEslintrc(&buf)
2828
return buf.Bytes()
2929
},
3030
},
31-
&util.GenerationMaterial{
31+
&core.FileDesc{
3232
Path: "tsconfig.json",
33-
Provider: func() []byte {
33+
Data: func() []byte {
3434
var buf bytes.Buffer
3535

3636
templates.WriteNodeTsConfig(&buf)
3737
return buf.Bytes()
3838
},
3939
},
4040
)
41-
return &AddTypescriptToNode{recipe: recipe}
41+
composer.AddCommand(&core.Command{
42+
Bin: "npm",
43+
Args: []string{"i", "-D",
44+
"typescript",
45+
"eslint",
46+
"@typescript-eslint/eslint-plugin",
47+
"eslint-plugin-prettier",
48+
"@typescript-eslint/parser",
49+
"eslint-config-prettier",
50+
"eslint-plugin-import",
51+
},
52+
})
53+
return &AddTypescriptToNode{composer: composer}
4254
}
4355

4456
func (atn AddTypescriptToNode) Run() error {
45-
cmd := core.NewCmdExecutor("npm", "i", "-D",
46-
"typescript",
47-
"eslint",
48-
"@typescript-eslint/eslint-plugin",
49-
"eslint-plugin-prettier",
50-
"@typescript-eslint/parser",
51-
"eslint-config-prettier",
52-
"eslint-plugin-import",
53-
)
54-
55-
if err := cmd.Run(); err != nil {
56-
return errors.Wrap(err, "failed to install dependencies")
57-
}
57+
log.Debugf("generation tree:\n%s", atn.composer.GetGenerationTree())
58+
runner := atn.composer.MakeRunner()
59+
atn.runner = runner
60+
return errors.Wrap(runner.Run(), "failed to generate typescript config")
61+
}
5862

59-
log.Debugf("generation tree:\n%s", atn.recipe.GetGenerationTree())
60-
gen := atn.recipe.MakeGenerator()
61-
atn.gen = gen
62-
return errors.Wrap(gen.Run(), "failed to generate typescript config")
63+
func (atn AddTypescriptToNode) Cleanup() error {
64+
return atn.runner.Cleanup()
6365
}

add/add_typescript_react.go

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,60 +11,62 @@ import (
1111
)
1212

1313
type AddTypescriptToReact struct {
14-
recipe *util.GenerationRecipe
15-
gen core.Generator
14+
composer *util.TaskComposer
15+
runner core.Generator
1616
}
1717

18-
var _ core.Executor = AddTypescriptToReact{}
18+
var _ core.Generator = AddTypescriptToReact{}
1919

2020
func NewAddTypescriptToReact() *AddTypescriptToReact {
21-
recipe := util.NewGenerationRecipe("",
22-
&util.GenerationMaterial{
21+
composer := util.NewTaskComposer("",
22+
&core.FileDesc{
2323
Path: ".eslintrc.js",
24-
Provider: func() []byte {
24+
Data: func() []byte {
2525
var buf bytes.Buffer
2626

2727
templates.WriteReactEslintrc(&buf)
2828
return buf.Bytes()
2929
},
3030
},
31-
&util.GenerationMaterial{
31+
&core.FileDesc{
3232
Path: "tsconfig.json",
33-
Provider: func() []byte {
33+
Data: func() []byte {
3434
var buf bytes.Buffer
3535

3636
templates.WriteReactTsConfig(&buf)
3737
return buf.Bytes()
3838
},
3939
},
4040
)
41-
return &AddTypescriptToReact{recipe: recipe}
41+
composer.AddCommand(&core.Command{
42+
Bin: "npm",
43+
Args: []string{"i", "-D",
44+
"@typescript-eslint/eslint-plugin",
45+
"@typescript-eslint/parser",
46+
"eslint",
47+
"eslint-config-airbnb",
48+
"eslint-config-prettier",
49+
"eslint-import-resolver-typescript",
50+
"eslint-plugin-import",
51+
"eslint-plugin-jsx-a11y",
52+
"eslint-plugin-prettier",
53+
"eslint-plugin-react",
54+
"eslint-plugin-react-hooks",
55+
"prettier",
56+
"prettier-eslint",
57+
"typescript",
58+
},
59+
})
60+
return &AddTypescriptToReact{composer: composer}
4261
}
4362

4463
func (atr AddTypescriptToReact) Run() error {
45-
cmd := core.NewCmdExecutor("npm", "i", "-D",
46-
"@typescript-eslint/eslint-plugin",
47-
"@typescript-eslint/parser",
48-
"eslint",
49-
"eslint-config-airbnb",
50-
"eslint-config-prettier",
51-
"eslint-import-resolver-typescript",
52-
"eslint-plugin-import",
53-
"eslint-plugin-jsx-a11y",
54-
"eslint-plugin-prettier",
55-
"eslint-plugin-react",
56-
"eslint-plugin-react-hooks",
57-
"prettier",
58-
"prettier-eslint",
59-
"typescript",
60-
)
61-
62-
if err := cmd.Run(); err != nil {
63-
return errors.Wrap(err, "failed to install dependencies")
64-
}
64+
log.Debugf("runnereration tree:\n%s", atr.composer.GetGenerationTree())
65+
runner := atr.composer.MakeRunner()
66+
atr.runner = runner
67+
return errors.Wrap(runner.Run(), "failed to runnererate typescript config")
68+
}
6569

66-
log.Debugf("generation tree:\n%s", atr.recipe.GetGenerationTree())
67-
gen := atr.recipe.MakeGenerator()
68-
atr.gen = gen
69-
return errors.Wrap(gen.Run(), "failed to generate typescript config")
70+
func (atr AddTypescriptToReact) Cleanup() error {
71+
return atr.runner.Cleanup()
7072
}

core/executor.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core
22

33
import (
4+
"fmt"
45
"os"
56
"os/exec"
67
"strings"
@@ -9,24 +10,30 @@ import (
910
log "github.com/sirupsen/logrus"
1011
)
1112

13+
type Command struct {
14+
Bin string
15+
Args []string
16+
}
17+
18+
func (c Command) String() string {
19+
return fmt.Sprintf("%s %s", c.Bin, strings.Join(c.Args, " "))
20+
}
21+
1222
type CmdExecutor struct {
13-
name string
14-
args []string
23+
cmd *Command
1524
}
1625

1726
var _ Executor = CmdExecutor{}
1827

19-
func NewCmdExecutor(bin string, args ...string) *CmdExecutor {
20-
return &CmdExecutor{bin, args}
28+
func NewCmdExecutor(cmd *Command) *CmdExecutor {
29+
return &CmdExecutor{cmd}
2130
}
2231

2332
func (ce CmdExecutor) Run() error {
24-
log.WithFields(log.Fields{
25-
"bin": ce.name,
26-
"args": ce.args,
27-
}).Debug("execute command")
28-
cmd := exec.Command(ce.name, ce.args...)
29-
cmd.Stdout = os.Stdout
30-
cmd.Stderr = os.Stdout
31-
return errors.Wrapf(cmd.Run(), "failed to execute command '%s %s'", ce.name, strings.Join(ce.args, " "))
33+
log.WithField("cmd", ce.cmd).Info("execute command")
34+
cmd := ce.cmd
35+
proc := exec.Command(cmd.Bin, cmd.Args...)
36+
proc.Stdout = os.Stdout
37+
proc.Stderr = os.Stdout
38+
return errors.Wrapf(proc.Run(), "failed to execute command '%s'", cmd)
3239
}

core/executor_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,30 @@ import "testing"
44

55
func TestCmdExecutor_Run(t *testing.T) {
66
type fields struct {
7-
name string
8-
args []string
7+
cmd *Command
98
}
109
tests := []struct {
1110
name string
1211
fields fields
1312
wantErr bool
1413
}{
1514
{"", fields{
16-
"ls", []string{"-ahl"},
15+
cmd: &Command{
16+
Bin: "ls",
17+
Args: []string{"-ahl"},
18+
},
1719
}, false},
1820
{"", fields{
19-
"lab", []string{"-ahl"},
21+
cmd: &Command{
22+
Bin: "lab",
23+
Args: []string{"-ahl"},
24+
},
2025
}, true},
2126
}
2227
for _, tt := range tests {
2328
t.Run(tt.name, func(t *testing.T) {
2429
ce := CmdExecutor{
25-
name: tt.fields.name,
26-
args: tt.fields.args,
30+
cmd: tt.fields.cmd,
2731
}
2832
if err := ce.Run(); (err != nil) != tt.wantErr {
2933
t.Errorf("CmdExecutor.Run() error = %v, wantErr %v", err, tt.wantErr)

core/file_generator.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ import (
1111
log "github.com/sirupsen/logrus"
1212
)
1313

14+
type FileDesc struct {
15+
Path string
16+
Data DataProvider
17+
}
18+
19+
func (fd FileDesc) String() string {
20+
return fd.Path
21+
}
22+
1423
type DataProvider = func() []byte
1524

1625
type FileGenerator struct {
@@ -19,10 +28,10 @@ type FileGenerator struct {
1928
cleaned bool
2029
}
2130

22-
func NewFileGenerator(path string, data DataProvider) Generator {
23-
file := strings.TrimPrefix(path, "/")
31+
func NewFileGenerator(fd *FileDesc) Generator {
32+
file := strings.TrimPrefix(fd.Path, "/")
2433
log.Trace("registered file: ", file)
25-
return &FileGenerator{file, data, false}
34+
return &FileGenerator{file, fd.Data, false}
2635
}
2736

2837
func (gt *FileGenerator) Run() error {

0 commit comments

Comments
 (0)