diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4cf76996..da07b768 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 @@ -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 diff --git a/codegen/format.sh b/codegen/format.sh new file mode 100755 index 00000000..0d09c196 --- /dev/null +++ b/codegen/format.sh @@ -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)" . + diff --git a/codegen/gen.sh b/codegen/gen.sh new file mode 100755 index 00000000..338ef8f6 --- /dev/null +++ b/codegen/gen.sh @@ -0,0 +1,6 @@ +#!/bin/bash +# Runs codegen and docgen tools +set -e +cd "$(dirname "$0")" +./gen_code.sh +./gen_docs.sh diff --git a/codegen/gen_code.sh b/codegen/gen_code.sh new file mode 100755 index 00000000..d21a3347 --- /dev/null +++ b/codegen/gen_code.sh @@ -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 \ No newline at end of file diff --git a/codegen/gen_docs.sh b/codegen/gen_docs.sh new file mode 100755 index 00000000..1bccee79 --- /dev/null +++ b/codegen/gen_docs.sh @@ -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 \ No newline at end of file diff --git a/codegen/install.sh b/codegen/install.sh new file mode 100755 index 00000000..67c10ebc --- /dev/null +++ b/codegen/install.sh @@ -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 diff --git a/codegen/rm_gen.sh b/codegen/rm_gen.sh new file mode 100755 index 00000000..bebf5fa4 --- /dev/null +++ b/codegen/rm_gen.sh @@ -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 {} + diff --git a/justfile b/justfile index 90fd2240..bee56388 100644 --- a/justfile +++ b/justfile @@ -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 . @@ -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