Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions github/gen-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ package main

import (
"bytes"
"errors"
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/fs"
"log"
"os"
"slices"
Expand All @@ -40,6 +42,7 @@ const (

var (
verbose = flag.Bool("v", false, "Print verbose log messages")
check = flag.Bool("check", false, "Check whether generated files are up to date")
Comment thread
Not-Dhananjay-Mishra marked this conversation as resolved.
Outdated

sourceTmpl = template.Must(template.New("source").Parse(source))
testTmpl = template.Must(template.New("test").Parse(test))
Expand Down Expand Up @@ -67,6 +70,10 @@ var (
}
)

func isCheck() bool {
return *check || os.Getenv("CHECK") == "1"
}

func logf(fmt string, args ...any) {
if *verbose {
log.Printf(fmt, args...)
Expand Down Expand Up @@ -233,6 +240,22 @@ func (t *templateData) dump() error {
return fmt.Errorf("format.Source:\n%v\n%v", buf.String(), err)
}

if isCheck() {
logf("Checking %v...", filename)
old, err := os.ReadFile(filename)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("Missing file: %v\n", filename)
}
return err
}

if !bytes.Equal(old, clean) {
return fmt.Errorf("Generated files are out of date. Please run go generate ./... and commit the results")
}
return nil
}

logf("Writing %v...", filename)
if err := os.Chmod(filename, 0o644); err != nil {
return fmt.Errorf("os.Chmod(%q, 0644): %v", filename, err)
Expand Down
26 changes: 25 additions & 1 deletion github/gen-iterators.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ package main

import (
"bytes"
"errors"
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/fs"
"log"
"os"
"reflect"
Expand All @@ -35,6 +37,7 @@ const (

var (
verbose = flag.Bool("v", false, "Print verbose log messages")
check = flag.Bool("check", false, "Check whether generated files are up to date")
Comment thread
Not-Dhananjay-Mishra marked this conversation as resolved.
Outdated

sourceTmpl = template.Must(template.New("source").Funcs(template.FuncMap{
"hasPrefix": strings.HasPrefix,
Expand All @@ -43,6 +46,10 @@ var (
testTmpl = template.Must(template.New("test").Parse(test))
)

func isCheck() bool {
return *check || os.Getenv("CHECK") == "1"
}

func logf(fmt string, args ...any) {
if *verbose {
log.Printf(fmt, args...)
Expand Down Expand Up @@ -599,8 +606,25 @@ func (t *templateData) dump() error {
if err != nil {
return fmt.Errorf("format.Source: %v\n%s", err, buf.String())
}
if isCheck() {
logf("Checking %v...", filename)
old, err := os.ReadFile(filename)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("Missing file: %v\n", filename)
}
return err
}

if !bytes.Equal(old, clean) {
return fmt.Errorf("Generated files are out of date. Please run go generate ./... and commit the results")
}
return nil
}

logf("Writing %v...", filename)
return os.WriteFile(filename, clean, 0o644)
err = os.WriteFile(filename, clean, 0o644)
return err
Comment thread
Not-Dhananjay-Mishra marked this conversation as resolved.
Outdated
}

if err := processTemplate(sourceTmpl, t.filename); err != nil {
Expand Down
23 changes: 23 additions & 0 deletions github/gen-stringify-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ package main

import (
"bytes"
"errors"
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/fs"
"log"
"os"
"strings"
Expand All @@ -37,6 +39,7 @@ const (

var (
verbose = flag.Bool("v", false, "Print verbose log messages")
check = flag.Bool("check", false, "Check whether generated files are up to date")
Comment thread
Not-Dhananjay-Mishra marked this conversation as resolved.
Outdated

// skipStructMethods lists "struct.method" combos to skip.
skipStructMethods = map[string]bool{}
Expand Down Expand Up @@ -83,6 +86,10 @@ var (
sourceTmpl = template.Must(template.New("source").Funcs(funcMap).Parse(source))
)

func isCheck() bool {
return *check || os.Getenv("CHECK") == "1"
}

func main() {
flag.Parse()
fset := token.NewFileSet()
Expand Down Expand Up @@ -353,6 +360,22 @@ func (t *templateData) dump() error {
return err
}

if isCheck() {
logf("Checking %v...", t.filename)
old, err := os.ReadFile(t.filename)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("Missing file: %v\n", t.filename)
}
return err
}

if !bytes.Equal(old, clean) {
return fmt.Errorf("Generated files are out of date. Please run go generate ./... and commit the results")
}
return nil
}

logf("Writing %v...", t.filename)
if err := os.Chmod(t.filename, 0o644); err != nil {
return fmt.Errorf("os.Chmod(%q, 0644): %v", t.filename, err)
Expand Down
51 changes: 16 additions & 35 deletions script/generate.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
#/ `script/generate.sh` runs `go generate` on all modules in this repo.
#/ `script/generate.sh` runs `go generate` on repo.
#/ It also runs `script/run-check-structfield-settings.sh -fix` to keep linter
#/ exceptions in `.golangci.yml` up to date.
#/ `script/generate.sh --check` checks that the generated files are up to date.
Expand All @@ -8,47 +8,28 @@ set -e

CDPATH="" cd -- "$(dirname -- "$0")/.."

CHECK_MODE=0
if [ "$1" = "--check" ]; then
GENTEMP="$(mktemp -d)"
git worktree add -q --detach "$GENTEMP"
trap 'git worktree remove -f "$GENTEMP"; rm -rf "$GENTEMP"' EXIT
git diff --name-only --diff-filter=D --no-renames HEAD | while read -r f; do
rm -f "$GENTEMP/$f"
done
git ls-files -com --exclude-standard | while read -r f; do
target="$GENTEMP/$f"
mkdir -p "$(dirname -- "$target")"
cp "$f" "$target"
done
if [ -f "$(pwd)"/bin ]; then
ln -s "$(pwd)"/bin "$GENTEMP"/bin
fi
(
cd "$GENTEMP"
git add .
git -c user.name='bot' -c user.email='bot@localhost' -c commit.gpgsign=false commit -m "generate" -q --allow-empty
script/generate.sh
[ -z "$(git status --porcelain)" ] || {
msg="Generated files are out of date. Please run script/generate.sh and commit the results"
if [ -n "$GITHUB_ACTIONS" ]; then
echo "::error ::$msg"
else
echo "$msg" 1>&2
fi
git diff
exit 1
}
)
exit 0
export CHECK=1
CHECK_MODE=1
fi

go generate ./...

MOD_DIRS="$(git ls-files '*go.mod' | xargs dirname | sort)"
Comment thread
Not-Dhananjay-Mishra marked this conversation as resolved.

for dir in $MOD_DIRS; do
(
cd "$dir"
go generate ./...
go mod tidy
if [ "$CHECK_MODE" = "1" ]; then
if ! go mod tidy -diff; then
echo "go.mod/go.sum are out of date in $dir"
exit 1
fi
else
go mod tidy
fi
)
done
script/run-check-structfield-settings.sh -fix

script/run-check-structfield-settings.sh -fix
Comment thread
Not-Dhananjay-Mishra marked this conversation as resolved.
Outdated