This repository has been archived by the owner on Feb 21, 2021. It is now read-only.
/
command.go
75 lines (61 loc) · 1.61 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package dev
import (
"context"
"fmt"
"github.com/wawandco/oxplugins/plugins"
"golang.org/x/sync/errgroup"
)
var _ plugins.Command = (*Command)(nil)
// Command is the dev command, it runs the dev plugins, each one on a different
// go routine. the detail to what happen on each of these plugins is up to
// each of the Developer plugins.
type Command struct {
developers []Developer
beforeDevs []BeforeDeveloper
}
func (d Command) Name() string {
return "dev"
}
func (d Command) ParentName() string {
return ""
}
//HelpText returns the help Text of build function
func (d Command) HelpText() string {
return "calls NPM or yarn to start webpack watching the assetst"
}
// Run calls each of the beforedeveloper plugins and then
// executes Developer plugins in parallel.
func (d *Command) Run(ctx context.Context, root string, args []string) error {
for _, bd := range d.beforeDevs {
err := bd.BeforeDevelop(ctx, root)
if err != nil {
return err
}
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
wg := &errgroup.Group{}
for _, d := range d.developers {
g := d.Develop
wg.Go(func() error {
err := g(ctx, root)
if err != nil {
fmt.Println(err)
}
return nil
})
}
return wg.Wait()
}
// Receive Developer and BeforeDeveloper plugins and store these
// in the Command to be used when the command is invoked.
func (d *Command) Receive(plugins []plugins.Plugin) {
for _, tool := range plugins {
if ptool, ok := tool.(Developer); ok {
d.developers = append(d.developers, ptool)
}
if bdev, ok := tool.(BeforeDeveloper); ok {
d.beforeDevs = append(d.beforeDevs, bdev)
}
}
}