| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"flag" |
| 7 |
"log" |
| 8 |
"os" |
| 9 |
"path" |
| 10 |
"path/filepath" |
| 11 |
"runtime" |
| 12 |
|
| 13 |
"github.com/mmcloughlin/avo/internal/prnt" |
| 14 |
"github.com/mmcloughlin/avo/tests/thirdparty" |
| 15 |
) |
| 16 |
|
| 17 |
var ( |
| 18 |
suitefilename = flag.String("suite", "", "third-party test suite configuration") |
| 19 |
output = flag.String("output", "", "path to output file (default stdout)") |
| 20 |
) |
| 21 |
|
| 22 |
func main() { |
| 23 |
if err := mainerr(); err != nil { |
| 24 |
log.Fatal(err) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
func mainerr() error { |
| 29 |
flag.Parse() |
| 30 |
|
| 31 |
|
| 32 |
suite, err := thirdparty.LoadSuiteFile(*suitefilename) |
| 33 |
if err != nil { |
| 34 |
return err |
| 35 |
} |
| 36 |
|
| 37 |
if err := suite.Validate(); err != nil { |
| 38 |
return err |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
w := os.Stdout |
| 43 |
if *output != "" { |
| 44 |
f, err := os.Create(*output) |
| 45 |
if err != nil { |
| 46 |
return err |
| 47 |
} |
| 48 |
defer f.Close() |
| 49 |
w = f |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
b, err := GenerateWorkflow(suite) |
| 54 |
if err != nil { |
| 55 |
return err |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
if _, err := w.Write(b); err != nil { |
| 60 |
return err |
| 61 |
} |
| 62 |
|
| 63 |
return nil |
| 64 |
} |
| 65 |
|
| 66 |
func GenerateWorkflow(s *thirdparty.Suite) ([]byte, error) { |
| 67 |
g := &prnt.Generator{} |
| 68 |
g.SetIndentString(" ") |
| 69 |
|
| 70 |
_, self, _, _ := runtime.Caller(0) |
| 71 |
g.Linef("# Code generated by %s. DO NOT EDIT.", filepath.Base(self)) |
| 72 |
g.NL() |
| 73 |
|
| 74 |
|
| 75 |
g.Linef("name: packages") |
| 76 |
|
| 77 |
g.Linef("permissions:") |
| 78 |
g.Linef(" contents: read") |
| 79 |
|
| 80 |
g.Linef("on:") |
| 81 |
g.Linef(" push:") |
| 82 |
g.Linef(" branches:") |
| 83 |
g.Linef(" - master") |
| 84 |
g.Linef(" pull_request:") |
| 85 |
|
| 86 |
|
| 87 |
g.Linef("jobs:") |
| 88 |
g.Indent() |
| 89 |
for _, t := range s.Projects.Tests() { |
| 90 |
g.Linef("%s:", t.ID()) |
| 91 |
g.Indent() |
| 92 |
|
| 93 |
g.Linef("runs-on: ubuntu-latest") |
| 94 |
if t.Project.Skip() { |
| 95 |
g.Linef("if: false # skip: %s", t.Project.Reason()) |
| 96 |
} |
| 97 |
g.Linef("steps:") |
| 98 |
g.Indent() |
| 99 |
|
| 100 |
|
| 101 |
g.Linef("- name: Install Go") |
| 102 |
g.Linef(" uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f # v3.3.1") |
| 103 |
g.Linef(" with:") |
| 104 |
g.Linef(" go-version: 1.21.x") |
| 105 |
g.Linef(" check-latest: true") |
| 106 |
|
| 107 |
|
| 108 |
avodir := "avo" |
| 109 |
g.Linef("- name: Checkout avo") |
| 110 |
g.Linef(" uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0") |
| 111 |
g.Linef(" with:") |
| 112 |
g.Linef(" path: %s", avodir) |
| 113 |
g.Linef(" persist-credentials: false") |
| 114 |
|
| 115 |
|
| 116 |
pkgdir := t.Project.Repository.Name |
| 117 |
g.Linef("- name: Checkout %s", t.Project.Repository) |
| 118 |
g.Linef(" uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0") |
| 119 |
g.Linef(" with:") |
| 120 |
g.Linef(" repository: %s", t.Project.Repository) |
| 121 |
g.Linef(" ref: %s", t.Project.Version) |
| 122 |
g.Linef(" path: %s", pkgdir) |
| 123 |
g.Linef(" persist-credentials: false") |
| 124 |
|
| 125 |
|
| 126 |
c := &thirdparty.Context{ |
| 127 |
AvoDirectory: path.Join("${{ github.workspace }}", avodir), |
| 128 |
RepositoryDirectory: path.Join("${{ github.workspace }}", pkgdir), |
| 129 |
} |
| 130 |
|
| 131 |
for _, step := range t.Package.Steps(c) { |
| 132 |
g.Linef("- name: %s", step.Name) |
| 133 |
g.Linef(" working-directory: %s", path.Join(pkgdir, step.WorkingDirectory)) |
| 134 |
if len(step.Commands) == 1 { |
| 135 |
g.Linef(" run: %s", step.Commands[0]) |
| 136 |
} else { |
| 137 |
g.Linef(" run: |") |
| 138 |
for _, cmd := range step.Commands { |
| 139 |
g.Linef(" %s", cmd) |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
g.Dedent() |
| 145 |
g.Dedent() |
| 146 |
} |
| 147 |
g.Dedent() |
| 148 |
|
| 149 |
return g.Result() |
| 150 |
} |