Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow to run codegen outside GOPATH #16511

Merged
merged 6 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ gogen: ensure-gopath
go generate ./util/argo/...

.PHONY: protogen
protogen: ensure-gopath mod-vendor-local
protogen: mod-vendor-local protogen-fast
blakepettersson marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: protogen-fast
protogen-fast:
export GO111MODULE=off
./hack/generate-proto.sh

Expand Down
41 changes: 27 additions & 14 deletions hack/generate-proto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ set -o nounset
set -o pipefail

# shellcheck disable=SC2128
PROJECT_ROOT=$(cd "$(dirname "${BASH_SOURCE}")"/..; pwd)
PROJECT_ROOT=$(
cd "$(dirname "${BASH_SOURCE}")"/..
pwd
)
PATH="${PROJECT_ROOT}/dist:${PATH}"
GOPATH=$(go env GOPATH)
GOPATH_PROJECT_ROOT="${GOPATH}/src/github.com/argoproj/argo-cd"

# output tool versions
go version
Expand Down Expand Up @@ -41,6 +45,7 @@ APIMACHINERY_PKGS=(

export GO111MODULE=on
[ -e ./v2 ] || ln -s . v2
[ -e "${GOPATH_PROJECT_ROOT}" ] || (mkdir -p "$(dirname "${GOPATH_PROJECT_ROOT}")" && ln -s "${PROJECT_ROOT}" "${GOPATH_PROJECT_ROOT}")

# protoc_include is the include directory containing the .proto files distributed with protoc binary
if [ -d /dist/protoc-include ]; then
Expand All @@ -53,10 +58,17 @@ fi

go-to-protobuf \
--go-header-file="${PROJECT_ROOT}"/hack/custom-boilerplate.go.txt \
--packages="$(IFS=, ; echo "${PACKAGES[*]}")" \
--apimachinery-packages="$(IFS=, ; echo "${APIMACHINERY_PKGS[*]}")" \
--proto-import=./vendor \
--proto-import="${protoc_include}"
--packages="$(
IFS=,
echo "${PACKAGES[*]}"
)" \
--apimachinery-packages="$(
IFS=,
echo "${APIMACHINERY_PKGS[*]}"
)" \
--proto-import="${PROJECT_ROOT}"/vendor \
--proto-import="${protoc_include}" \
--output-base="${GOPATH}/src/"

# Either protoc-gen-go, protoc-gen-gofast, or protoc-gen-gogofast can be used to build
# server/*/<service>.pb.go from .proto files. golang/protobuf and gogo/protobuf can be used
Expand Down Expand Up @@ -86,9 +98,11 @@ for i in ${PROTO_FILES}; do
--${GOPROTOBINARY}_out=plugins=grpc:"$GOPATH"/src \
--grpc-gateway_out=logtostderr=true:"$GOPATH"/src \
--swagger_out=logtostderr=true:. \
$i
"$i"
done
[ -e ./v2 ] && rm -rf v2

[ -L "${GOPATH_PROJECT_ROOT}" ] && rm -rf v2 "${GOPATH_PROJECT_ROOT}"
[ -L ./v2 ] && rm -rf v2

# collect_swagger gathers swagger files into a subdirectory
collect_swagger() {
Expand All @@ -97,7 +111,7 @@ collect_swagger() {
PRIMARY_SWAGGER=$(mktemp)
COMBINED_SWAGGER=$(mktemp)

cat <<EOF > "${PRIMARY_SWAGGER}"
cat <<EOF >"${PRIMARY_SWAGGER}"
{
"swagger": "2.0",
"info": {
Expand All @@ -111,7 +125,7 @@ EOF

rm -f "${SWAGGER_OUT}"

find "${SWAGGER_ROOT}" -name '*.swagger.json' -exec swagger mixin --ignore-conflicts "${PRIMARY_SWAGGER}" '{}' \+ > "${COMBINED_SWAGGER}"
find "${SWAGGER_ROOT}" -name '*.swagger.json' -exec swagger mixin --ignore-conflicts "${PRIMARY_SWAGGER}" '{}' \+ >"${COMBINED_SWAGGER}"
jq -r 'del(.definitions[].properties[]? | select(."$ref"!=null and .description!=null).description) | del(.definitions[].properties[]? | select(."$ref"!=null and .title!=null).title) |
# The "array" and "map" fields have custom unmarshaling. Modify the swagger to reflect this.
.definitions.v1alpha1ApplicationSourcePluginParameter.properties.array = {"description":"Array is the value of an array type parameter.","type":"array","items":{"type":"string"}} |
Expand All @@ -120,10 +134,10 @@ EOF
del(.definitions.v1alpha1OptionalMap) |
# Output for int64 is incorrect, because it is based on proto definitions, where int64 is a string. In our JSON API, we expect int64 to be an integer. https://github.com/grpc-ecosystem/grpc-gateway/issues/219
(.definitions[]?.properties[]? | select(.type == "string" and .format == "int64")) |= (.type = "integer")
' "${COMBINED_SWAGGER}" | \
jq '.definitions.v1Time.type = "string" | .definitions.v1Time.format = "date-time" | del(.definitions.v1Time.properties)' | \
jq '.definitions.v1alpha1ResourceNode.allOf = [{"$ref": "#/definitions/v1alpha1ResourceRef"}] | del(.definitions.v1alpha1ResourceNode.properties.resourceRef) ' \
> "${SWAGGER_OUT}"
' "${COMBINED_SWAGGER}" |
jq '.definitions.v1Time.type = "string" | .definitions.v1Time.format = "date-time" | del(.definitions.v1Time.properties)' |
jq '.definitions.v1alpha1ResourceNode.allOf = [{"$ref": "#/definitions/v1alpha1ResourceRef"}] | del(.definitions.v1alpha1ResourceNode.properties.resourceRef) ' \
>"${SWAGGER_OUT}"

/bin/rm "${PRIMARY_SWAGGER}" "${COMBINED_SWAGGER}"
}
Expand All @@ -139,4 +153,3 @@ clean_swagger server
clean_swagger reposerver
clean_swagger controller
clean_swagger cmpserver

Loading