File: make_workflow.go

package info (click to toggle)
golang-github-mmcloughlin-avo 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,024 kB
  • sloc: xml: 71,029; asm: 14,862; sh: 194; makefile: 21; ansic: 11
file content (151 lines) | stat: -rw-r--r-- 3,263 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//go:build ignore
// +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 (
	suitefilename = flag.String("suite", "", "third-party test suite configuration")
	output        = flag.String("output", "", "path to output file (default stdout)")
)

func main() {
	if err := mainerr(); err != nil {
		log.Fatal(err)
	}
}

func mainerr() error {
	flag.Parse()

	// Read third-party test suite.
	suite, err := thirdparty.LoadSuiteFile(*suitefilename)
	if err != nil {
		return err
	}

	if err := suite.Validate(); err != nil {
		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.
	b, err := GenerateWorkflow(suite)
	if err != nil {
		return err
	}

	// Write output.
	if _, err := w.Write(b); err != nil {
		return err
	}

	return nil
}

func GenerateWorkflow(s *thirdparty.Suite) ([]byte, error) {
	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:")

	// One job per test case.
	g.Linef("jobs:")
	g.Indent()
	for _, t := range s.Projects.Tests() {
		g.Linef("%s:", t.ID())
		g.Indent()

		g.Linef("runs-on: ubuntu-latest")
		if t.Project.Skip() {
			g.Linef("if: false # skip: %s", t.Project.Reason())
		}
		g.Linef("steps:")
		g.Indent()

		// Install Go.
		g.Linef("- name: Install Go")
		g.Linef("  uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f # v3.3.1")
		g.Linef("  with:")
		g.Linef("    go-version: 1.19.x")
		g.Linef("    check-latest: true")

		// Checkout avo.
		avodir := "avo"
		g.Linef("- name: Checkout avo")
		g.Linef("  uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0")
		g.Linef("  with:")
		g.Linef("    path: %s", avodir)
		g.Linef("    persist-credentials: false")

		// Checkout the third-party package.
		pkgdir := t.Project.Repository.Name
		g.Linef("- name: Checkout %s", t.Project.Repository)
		g.Linef("  uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0")
		g.Linef("  with:")
		g.Linef("    repository: %s", t.Project.Repository)
		g.Linef("    ref: %s", t.Project.Version)
		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),
		}

		for _, step := range t.Package.Steps(c) {
			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()
}