Skip to content

Commit

Permalink
chore: deal with code-generator breaking changes
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Sep 27, 2024
1 parent e6601e1 commit b950fd2
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 60 deletions.
12 changes: 6 additions & 6 deletions api/rbac/v1alpha1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 22 additions & 22 deletions api/v1alpha1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 59 additions & 16 deletions hack/codegen/proto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,84 @@ readonly API_PKGS=(
)

readonly APIMACHINERY_PKGS=(
"k8s.io/apimachinery/pkg/util/intstr"
"+k8s.io/apimachinery/pkg/api/resource"
"+k8s.io/apimachinery/pkg/runtime/schema"
"+k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/api/core/v1,k8s.io/api/batch/v1,k8s.io/api/rbac/v1"
"-k8s.io/api/core/v1"
"-k8s.io/api/batch/v1"
"-k8s.io/api/rbac/v1"
"-k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"-k8s.io/apimachinery/pkg/util/intstr"
"-k8s.io/apimachinery/pkg/api/resource"
"-k8s.io/apimachinery/pkg/runtime/schema"
"-k8s.io/apimachinery/pkg/runtime"
"-k8s.io/apimachinery/pkg/apis/meta/v1"
)

# Change working directory to the root of the repository
work_dir=$(dirname "${0}")
work_dir=$(readlink -f "${work_dir}/../..")

# Create a temporary build directory
build_dir=$(mktemp -d)

function clean() {
echo "Clean up intermediate resources..."
rm -r "${build_dir}" || true
rm -r "${work_dir}/pkg/api/v1alpha1" || true
rm -r "${work_dir}/pkg/api/rbac" || true
rm -r "${work_dir}/vendor" || true
}
trap 'clean' EXIT

function main() {
echo "Change working directory to temporary build directory..."
cd "${build_dir}"

echo "Prepare build directory..."
# Initialize dummy module inside build directory
go mod init github.com/akuity
go work init

# Copy source files to build directory
local build_src_dir
build_src_dir="${build_dir}/src/github.com/akuity/kargo"
mkdir -p "${build_src_dir}"

# Use find to locate and copy .go files, go.mod, and go.sum
# while preserving the directory structure
find "$work_dir" \( \
-name '*.go' -o \
-name 'go.mod' -o \
-name 'go.sum' \
\) -type f | while read -r file; do
rel_path="${file#$work_dir/}"
dest_file="$build_src_dir/$rel_path"
dest_dir=$(dirname "$dest_file")
mkdir -p "$dest_dir"
cp "$file" "$dest_file"
done
go work use ./src/github.com/akuity/kargo

echo "Vendor dependencies for protobuf code generation..."
go mod tidy
go mod vendor
go work sync
go work vendor

echo "Generate protobuf code from Kubebuilder structs..."
GOPATH=${GOPATH} go-to-protobuf \
--go-header-file=./hack/boilerplate.go.txt \
go-to-protobuf \
--go-header-file="${work_dir}/hack/boilerplate.go.txt" \
--packages="$(IFS=, ; echo "${API_PKGS[*]}")" \
--apimachinery-packages="$(IFS=, ; echo "${APIMACHINERY_PKGS[*]}")" \
--proto-import "${work_dir}/hack/include" \
--proto-import "${work_dir}/vendor"
--proto-import="${work_dir}/hack/include" \
--proto-import="${build_dir}/vendor" \
--output-dir="${build_dir}/src"

echo "Copy generated code to the working directory..."
cp -R "${GOPATH}/src/github.com/akuity/kargo/api" "${work_dir}"
cp -R "${GOPATH}/src/github.com/akuity/kargo/internal" "${work_dir}"
cp -R "${build_src_dir}/api" "${work_dir}"
cp -R "${build_src_dir}/internal" "${work_dir}"

echo "Change working directory to repository directory..."
cd "${work_dir}"

echo "Vendor dependencies for protobuf code generation..."
go mod vendor

echo "Generate protobuf code (Go)..."
buf generate api
Expand All @@ -63,7 +105,8 @@ function main() {
}

(
# Include local binaries in the PATH
export PATH="${work_dir}/hack/bin:${PATH}"
cd "${work_dir}"
# cd "${work_dir}"
main
)
Loading

0 comments on commit b950fd2

Please sign in to comment.