|
1 |
| -// Build is the build pipeline for this repository. |
2 | 1 | package main
|
3 | 2 |
|
4 | 3 | import (
|
5 |
| - "fmt" |
6 | 4 | "os"
|
7 | 5 | "os/exec"
|
| 6 | + "strings" |
8 | 7 |
|
9 |
| - "github.com/goyek/goyek" |
10 |
| - "github.com/mattn/go-shellwords" |
| 8 | + "github.com/goyek/goyek/v2" |
11 | 9 | )
|
12 | 10 |
|
13 |
| -const buildDir = "build" |
14 |
| - |
15 |
| -func main() { |
16 |
| - flow().Main() |
17 |
| -} |
18 |
| - |
19 |
| -func flow() *goyek.Flow { |
20 |
| - flow := &goyek.Flow{} |
21 |
| - |
22 |
| - test := flow.Register(taskTest()) |
23 |
| - lint := flow.Register(taskLint()) |
24 |
| - misspell := flow.Register(taskMisspell()) |
25 |
| - markdownlint := flow.Register(taskMarkdownLint()) |
26 |
| - all := flow.Register(taskAll(goyek.Deps{ |
27 |
| - test, lint, misspell, markdownlint, |
28 |
| - })) |
29 |
| - |
30 |
| - flow.DefaultTask = all |
31 |
| - return flow |
32 |
| -} |
33 |
| - |
34 |
| -func taskTest() goyek.Task { |
35 |
| - return goyek.Task{ |
36 |
| - Name: "test", |
37 |
| - Usage: "go test with code covarage", |
38 |
| - Action: func(tf *goyek.TF) { |
39 |
| - Exec(tf, buildDir, "go test") |
40 |
| - Exec(tf, "", "go test -covermode=atomic -coverprofile=coverage.out ./...") |
41 |
| - }, |
42 |
| - } |
43 |
| -} |
44 |
| - |
45 |
| -func taskLint() goyek.Task { |
46 |
| - return goyek.Task{ |
47 |
| - Name: "lint", |
48 |
| - Usage: "golangci-lint", |
49 |
| - Action: func(tf *goyek.TF) { |
50 |
| - Exec(tf, buildDir, "go install github.com/golangci/golangci-lint/cmd/golangci-lint") |
51 |
| - Exec(tf, buildDir, "golangci-lint run") |
52 |
| - Exec(tf, "", "golangci-lint run") |
53 |
| - }, |
54 |
| - } |
55 |
| -} |
56 |
| - |
57 |
| -func taskMisspell() goyek.Task { |
58 |
| - return goyek.Task{ |
59 |
| - Name: "misspell", |
60 |
| - Usage: "misspell", |
61 |
| - Action: func(tf *goyek.TF) { |
62 |
| - Exec(tf, buildDir, "go install github.com/client9/misspell/cmd/misspell") |
63 |
| - Exec(tf, "", "misspell -error -locale=US *.md") |
64 |
| - }, |
65 |
| - } |
66 |
| -} |
67 |
| - |
68 |
| -func taskMarkdownLint() goyek.Task { |
69 |
| - return goyek.Task{ |
70 |
| - Name: "markdownlint", |
71 |
| - Usage: "markdownlint-cli (uses docker)", |
72 |
| - Action: func(tf *goyek.TF) { |
73 |
| - if _, err := exec.LookPath("docker"); err != nil { |
74 |
| - tf.Skip(err) |
75 |
| - } |
76 |
| - |
77 |
| - curDir, err := os.Getwd() |
78 |
| - if err != nil { |
79 |
| - tf.Fatal(err) |
80 |
| - } |
81 |
| - dockerTag := "markdownlint-cli" |
82 |
| - buildCmd := fmt.Sprintf("docker build -t %s -f build/markdownlint-cli.dockerfile .", dockerTag) |
83 |
| - Exec(tf, "", buildCmd) |
84 |
| - runCmd := fmt.Sprintf("docker run --rm -v %s:/workdir %s *.md", curDir, dockerTag) |
85 |
| - Exec(tf, "", runCmd) |
86 |
| - }, |
87 |
| - } |
88 |
| -} |
| 11 | +const ( |
| 12 | + rootDir = "." |
| 13 | + buildDir = "build" |
| 14 | + toolsDir = "tools" |
| 15 | +) |
89 | 16 |
|
90 |
| -func taskAll(deps goyek.Deps) goyek.Task { |
91 |
| - return goyek.Task{ |
| 17 | +func configure() { |
| 18 | + flow.SetDefault(flow.Define(goyek.Task{ |
92 | 19 | Name: "all",
|
93 | 20 | Usage: "build pipeline",
|
94 |
| - Deps: deps, |
95 |
| - } |
96 |
| -} |
97 |
| - |
98 |
| -// Exec runs the provided command line. |
99 |
| -// It fails the task in case of any problems. |
100 |
| -func Exec(tf *goyek.TF, workDir string, cmdLine string) { |
101 |
| - args, err := shellwords.Parse(cmdLine) |
102 |
| - if err != nil { |
103 |
| - tf.Fatalf("parse command line: %v", err) |
104 |
| - } |
105 |
| - cmd := tf.Cmd(args[0], args[1:]...) |
106 |
| - cmd.Dir = workDir |
107 |
| - if err := cmd.Run(); err != nil { |
108 |
| - tf.Fatalf("run command: %v", err) |
109 |
| - } |
| 21 | + Deps: goyek.Deps{ |
| 22 | + flow.Define(goyek.Task{ |
| 23 | + Name: "mod", |
| 24 | + Usage: "go mod tidy", |
| 25 | + Action: func(tf *goyek.TF) { |
| 26 | + Exec(tf, rootDir, "go mod tidy") |
| 27 | + Exec(tf, buildDir, "go mod tidy") |
| 28 | + Exec(tf, toolsDir, "go mod tidy") |
| 29 | + }, |
| 30 | + }), |
| 31 | + flow.Define(goyek.Task{ |
| 32 | + Name: "install", |
| 33 | + Usage: "go install tools", |
| 34 | + Action: func(tf *goyek.TF) { |
| 35 | + tools := &strings.Builder{} |
| 36 | + toolsCmd := tf.Cmd("go", "list", `-f={{ join .Imports " " }}`, "-tags=tools") |
| 37 | + toolsCmd.Dir = toolsDir |
| 38 | + toolsCmd.Stdout = tools |
| 39 | + if err := toolsCmd.Run(); err != nil { |
| 40 | + tf.Fatal(err) |
| 41 | + } |
| 42 | + Exec(tf, toolsDir, "go install "+strings.TrimSpace(tools.String())) |
| 43 | + }, |
| 44 | + }), |
| 45 | + flow.Define(goyek.Task{ |
| 46 | + Name: "golint", |
| 47 | + Usage: "golangci-lint", |
| 48 | + Action: func(tf *goyek.TF) { |
| 49 | + Exec(tf, rootDir, "golangci-lint run --fix") |
| 50 | + Exec(tf, buildDir, "golangci-lint run --fix") |
| 51 | + }, |
| 52 | + }), |
| 53 | + flow.Define(goyek.Task{ |
| 54 | + Name: "spell", |
| 55 | + Usage: "misspell", |
| 56 | + Action: func(tf *goyek.TF) { |
| 57 | + Exec(tf, rootDir, "misspell -error -locale=US -i=importas -w .") |
| 58 | + }, |
| 59 | + }), |
| 60 | + flow.Define(goyek.Task{ |
| 61 | + Name: "mdlint", |
| 62 | + Usage: "markdownlint-cli (uses docker)", |
| 63 | + Action: func(tf *goyek.TF) { |
| 64 | + if _, err := exec.LookPath("docker"); err != nil { |
| 65 | + tf.Skip(err) |
| 66 | + } |
| 67 | + curDir, err := os.Getwd() |
| 68 | + if err != nil { |
| 69 | + tf.Fatal(err) |
| 70 | + } |
| 71 | + dockerTag := "markdownlint-cli" |
| 72 | + Exec(tf, rootDir, "docker build -t "+dockerTag+" -f "+toolsDir+"/markdownlint-cli.dockerfile .") |
| 73 | + Exec(tf, rootDir, "docker run --rm -v '"+curDir+":/workdir' "+dockerTag+" *.md") |
| 74 | + }, |
| 75 | + }), |
| 76 | + flow.Define(goyek.Task{ |
| 77 | + Name: "test", |
| 78 | + Usage: "go test with code covarage", |
| 79 | + Action: func(tf *goyek.TF) { |
| 80 | + Exec(tf, rootDir, "go test -covermode=atomic -coverprofile=coverage.out ./...") |
| 81 | + }, |
| 82 | + }), |
| 83 | + }, |
| 84 | + })) |
110 | 85 | }
|
0 commit comments