2021-09-15 23:25:51 -07:00
|
|
|
//go:build ignore
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"path"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
|
|
"github.com/mmcloughlin/avo/internal/prnt"
|
|
|
|
|
"github.com/mmcloughlin/avo/tests/thirdparty"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2022-05-08 16:55:51 -07:00
|
|
|
suitefilename = flag.String("suite", "", "third-party test suite configuration")
|
|
|
|
|
output = flag.String("output", "", "path to output file (default stdout)")
|
2021-09-15 23:25:51 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
if err := mainerr(); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mainerr() error {
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
2022-05-08 16:55:51 -07:00
|
|
|
// Read third-party test suite.
|
|
|
|
|
suite, err := thirdparty.LoadSuiteFile(*suitefilename)
|
2021-09-15 23:25:51 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-08 16:55:51 -07:00
|
|
|
if err := suite.Validate(); err != nil {
|
2021-09-15 23:25:51 -07:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine output.
|
|
|
|
|
w := os.Stdout
|
|
|
|
|
if *output != "" {
|
|
|
|
|
f, err := os.Create(*output)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
w = f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate workflow file.
|
2022-05-08 16:55:51 -07:00
|
|
|
b, err := GenerateWorkflow(suite)
|
2021-09-15 23:25:51 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write output.
|
|
|
|
|
if _, err := w.Write(b); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-08 16:55:51 -07:00
|
|
|
func GenerateWorkflow(s *thirdparty.Suite) ([]byte, error) {
|
2021-09-15 23:25:51 -07:00
|
|
|
g := &prnt.Generator{}
|
|
|
|
|
g.SetIndentString(" ")
|
|
|
|
|
|
|
|
|
|
_, self, _, _ := runtime.Caller(0)
|
|
|
|
|
g.Linef("# Code generated by %s. DO NOT EDIT.", filepath.Base(self))
|
|
|
|
|
g.NL()
|
|
|
|
|
|
|
|
|
|
// Header.
|
|
|
|
|
g.Linef("name: packages")
|
|
|
|
|
|
|
|
|
|
g.Linef("permissions:")
|
|
|
|
|
g.Linef(" contents: read")
|
|
|
|
|
|
|
|
|
|
g.Linef("on:")
|
|
|
|
|
g.Linef(" push:")
|
|
|
|
|
g.Linef(" branches:")
|
|
|
|
|
g.Linef(" - master")
|
|
|
|
|
g.Linef(" pull_request:")
|
|
|
|
|
|
2022-04-24 20:20:11 -07:00
|
|
|
// One job per test case.
|
2021-09-15 23:25:51 -07:00
|
|
|
g.Linef("jobs:")
|
|
|
|
|
g.Indent()
|
2022-05-08 16:55:51 -07:00
|
|
|
for _, t := range s.Projects.Tests() {
|
2022-04-24 20:20:11 -07:00
|
|
|
g.Linef("%s:", t.ID())
|
2021-09-15 23:25:51 -07:00
|
|
|
g.Indent()
|
|
|
|
|
|
|
|
|
|
g.Linef("runs-on: ubuntu-latest")
|
2022-04-24 20:20:11 -07:00
|
|
|
if t.Project.Skip() {
|
|
|
|
|
g.Linef("if: false # skip: %s", t.Project.Reason())
|
2021-11-10 18:44:28 -08:00
|
|
|
}
|
2021-09-15 23:25:51 -07:00
|
|
|
g.Linef("steps:")
|
|
|
|
|
g.Indent()
|
|
|
|
|
|
|
|
|
|
// Install Go.
|
|
|
|
|
g.Linef("- name: Install Go")
|
2022-11-26 15:00:56 -08:00
|
|
|
g.Linef(" uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f # v3.3.1")
|
2021-09-15 23:25:51 -07:00
|
|
|
g.Linef(" with:")
|
2023-08-26 12:40:13 -07:00
|
|
|
g.Linef(" go-version: 1.21.x")
|
2022-04-23 21:01:50 -07:00
|
|
|
g.Linef(" check-latest: true")
|
2021-09-15 23:25:51 -07:00
|
|
|
|
|
|
|
|
// Checkout avo.
|
|
|
|
|
avodir := "avo"
|
|
|
|
|
g.Linef("- name: Checkout avo")
|
2022-11-26 15:00:56 -08:00
|
|
|
g.Linef(" uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0")
|
2021-09-15 23:25:51 -07:00
|
|
|
g.Linef(" with:")
|
|
|
|
|
g.Linef(" path: %s", avodir)
|
|
|
|
|
g.Linef(" persist-credentials: false")
|
|
|
|
|
|
|
|
|
|
// Checkout the third-party package.
|
2022-04-24 20:20:11 -07:00
|
|
|
pkgdir := t.Project.Repository.Name
|
|
|
|
|
g.Linef("- name: Checkout %s", t.Project.Repository)
|
2022-11-26 15:00:56 -08:00
|
|
|
g.Linef(" uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0")
|
2021-09-15 23:25:51 -07:00
|
|
|
g.Linef(" with:")
|
2022-04-24 20:20:11 -07:00
|
|
|
g.Linef(" repository: %s", t.Project.Repository)
|
|
|
|
|
g.Linef(" ref: %s", t.Project.Version)
|
2021-09-15 23:25:51 -07:00
|
|
|
g.Linef(" path: %s", pkgdir)
|
|
|
|
|
g.Linef(" persist-credentials: false")
|
|
|
|
|
|
|
|
|
|
// Build steps.
|
|
|
|
|
c := &thirdparty.Context{
|
|
|
|
|
AvoDirectory: path.Join("${{ github.workspace }}", avodir),
|
|
|
|
|
RepositoryDirectory: path.Join("${{ github.workspace }}", pkgdir),
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 20:20:11 -07:00
|
|
|
for _, step := range t.Package.Steps(c) {
|
2021-09-15 23:25:51 -07:00
|
|
|
g.Linef("- name: %s", step.Name)
|
|
|
|
|
g.Linef(" working-directory: %s", path.Join(pkgdir, step.WorkingDirectory))
|
|
|
|
|
if len(step.Commands) == 1 {
|
|
|
|
|
g.Linef(" run: %s", step.Commands[0])
|
|
|
|
|
} else {
|
|
|
|
|
g.Linef(" run: |")
|
|
|
|
|
for _, cmd := range step.Commands {
|
|
|
|
|
g.Linef(" %s", cmd)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g.Dedent()
|
|
|
|
|
g.Dedent()
|
|
|
|
|
}
|
|
|
|
|
g.Dedent()
|
|
|
|
|
|
|
|
|
|
return g.Result()
|
|
|
|
|
}
|