Skip to content

Commit

Permalink
Check generated code is up-to-date
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Morozko committed Aug 9, 2024
1 parent 16c2772 commit 31ad9de
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 12 deletions.
90 changes: 82 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ jobs:

builds:
if: github.event_name != 'pull_request' || !github.event.pull_request.draft
strategy:
fail-fast: true
matrix:
go-version: ["1.22.x"]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
- name: Build fabric
run: go build -v -o ${{ runner.temp }}/fabric
- name: Build fabric and plugins
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
# using dev config: it skips hooks and some other stuff
args: build --config ./.goreleaser-dev.yaml --single-target --snapshot --clean

golangci-lint:
if: github.event_name != 'pull_request' || !github.event.pull_request.draft
Expand All @@ -68,3 +68,77 @@ jobs:
version: v1.59
only-new-issues: true

codegen-check:
if: github.event_name != 'pull_request' || !github.event.pull_request.draft
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.22.x"

- name: Restore cached codegen tools
id: cache-tools
uses: actions/cache/restore@v4
with:
key: codegen-tools-${{ runner.os }}-${{ hashFiles('./codegen/install.sh') }}
path: ~/go/bin/
- name: Install codegen tools
if: steps.cache-tools.outputs.cache-hit != 'true'
run: ./codegen/install.sh
- name: Cache codegen tools
if: steps.cache-tools.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
key: ${{ steps.cache-tools.outputs.cache-primary-key }}
path: ~/go/bin/

- name: Regenerate code
run: |
./codegen/rm_gen.sh
./codegen/gen.sh
- name: Get changed files
uses: tj-actions/verify-changed-files@v20
id: codegen-changed-files

- name: Format code
run: |
git reset --hard HEAD
./codegen/format.sh
- name: Get changed files
uses: tj-actions/verify-changed-files@v20
id: format-changed-files

- name: Verify that code is correctly generated and formatted
if: steps.codegen-changed-files.outputs.files_changed == 'true' || steps.format-changed-files.outputs.files_changed == 'true'
env:
CODEGEN_CHANGED_FILES: ${{ steps.codegen-changed-files.outputs.changed_files }}
FORMAT_CHANGED_FILES: ${{ steps.format-changed-files.outputs.changed_files }}
run: |
if [ -n "$CODEGEN_CHANGED_FILES" ]; then
echo "Attempted to commit non up-to-date generated files:" >> $GITHUB_STEP_SUMMARY
for file in $CODEGEN_CHANGED_FILES; do
echo "* $file" >> $GITHUB_STEP_SUMMARY
done
cat <<"EOF" >> $GITHUB_STEP_SUMMARY
Make sure that you have correct tools installed by running `./codegen/install.sh` and regenerate the files by running `./codegen/gen.sh` (`./codegen/rm_gen.sh` will remove all generated files)
EOF
echo "::error title=Need to regenerate code::Stale generated files: $CODEGEN_CHANGED_FILES"
fi
if [ -n "$FORMAT_CHANGED_FILES" ]; then
echo "Attempted to commit non-formatted files:" >> $GITHUB_STEP_SUMMARY
for file in $FORMAT_CHANGED_FILES; do
echo "* $file" >> $GITHUB_STEP_SUMMARY
done
cat <<"EOF" >> $GITHUB_STEP_SUMMARY
Format the code by running `./codegen/format.sh`
EOF
echo "::error title=Incorrect formatting::Badly formatted files: $FORMAT_CHANGED_FILES"
fi
exit 1
7 changes: 7 additions & 0 deletions codegen/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
# Formats the code
set -e
cd "$(dirname "$0")/.."
gofumpt -w .
gci write --skip-generated -s standard -s default -s "prefix(github.com/blackstork-io/fabric)" .

6 changes: 6 additions & 0 deletions codegen/gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
# Runs codegen and docgen tools
set -e
cd "$(dirname "$0")"
./gen_code.sh
./gen_docs.sh
21 changes: 21 additions & 0 deletions codegen/gen_code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
# Runs codegen tools
set -e
cd "$(dirname "$0")/.."

if [ -z "$GOBIN" ]; then
if [ -z "$GOPATH" ]; then
if [ -z "$HOME" ]; then
echo "HOME is unset"
exit 1
fi
GOBIN="$HOME/go/bin"
else
GOBIN="$GOPATH/bin"
fi
fi
PATH="$GOBIN:$PATH"

buf generate
mockery
go mod tidy
6 changes: 6 additions & 0 deletions codegen/gen_docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
# Runs codegen tools
set -e
cd "$(dirname "$0")/.."

go run ./tools/docgen --version $(git describe --tags --abbrev=0) --output ./docs/plugins
11 changes: 11 additions & 0 deletions codegen/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
# Installs tools
set -e
cd "$(dirname "$0")/.."
# Codegen tools
go install github.com/vektra/mockery/v2@v2.42.1 &
go install github.com/bufbuild/buf/cmd/buf@v1.32.2 &
# Formatting tools
go install github.com/daixiang0/gci@v0.13.4 &
go install mvdan.cc/gofumpt@v0.6.0 &
wait
9 changes: 9 additions & 0 deletions codegen/rm_gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
# Removes all generated files

cd "$(dirname "$0")/.."

grep -R --with-filename --files-with-matches --no-messages --include "*.go" -E -e '^// Code generated .* DO NOT EDIT.$' . | xargs rm
find ./mocks -type d -empty -exec rmdir {} +

find ./docs/plugins -mindepth 1 \( -maxdepth 1 -type d -o -not -name "*.md" \) -exec rm -rf {} +
7 changes: 3 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ test-run:

format:
go mod tidy
gofumpt -w .
gci write --skip-generated -s standard -s default -s "prefix(github.com/blackstork-io/fabric)" .
./gen_code.sh

format-extra: format
gofumpt -w -extra .
Expand All @@ -30,7 +29,7 @@ test-e2e:
go test -timeout 5m -race -v ./test/e2e/...

generate:
go generate ./...
./codegen/gen_code.sh

generate-docs:
go run ./tools/docgen --version v0.4.2 --output ./docs/plugins/
./codegen/gen_docs.sh

0 comments on commit 31ad9de

Please sign in to comment.