Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
81 lines (70 sloc) 2.21 KB
#!/bin/bash
set -e
get_external_modifiers() {
local modifiers=""
for file in \
"any.proto" \
"api.proto" \
"duration.proto" \
"empty.proto" \
"field_mask.proto" \
"source_context.proto" \
"struct.proto" \
"timestamp.proto" \
"type.proto" \
"wrappers.proto"; do
local modifiers="${modifiers}Mgoogle/protobuf/${file}=go.pedge.io/google-protobuf,"
done
local modifiers="${modifiers}Mgoogle/api/annotations.proto=github.com/gengo/grpc-gateway/third_party/googleapis/google/api,"
local modifiers="${modifiers}Mgoogle/api/http.proto=github.com/gengo/grpc-gateway/third_party/googleapis/google/api,"
echo "${modifiers}Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor"
}
get_googleapi_files() {
find . -name *\.proto | sed "s/\.\///g"
}
get_googleapi_dirs() {
find . -name *\.proto -exec bash -c 'dirname {}' \; | sort | uniq | sed "s/\.\///g"
}
# arg1: dir being currently compiled
get_googleapi_modifiers() {
local modifiers=""
for file in $(get_googleapi_files); do
if ! echo "${file}" | grep "${1}" > /dev/null; then
local modifiers="${modifiers},M${file}=go.pedge.io/googleapis/$(dirname "${file}")"
fi
done
echo ${modifiers}
}
copy_googleapi_files_into_cwd() {
if [ -z "${DRY_RUN}" ]; then
rm -rf /tmp/googleapis
git clone https://github.com/google/googleapis.git /tmp/googleapis
rm -rf google
cp -R /tmp/googleapis/google .
rm -rf google/api
fi
}
# arg1: dir
get_protoc_cmd() {
echo protoc --proto_path=. --proto_path=${GOPATH}/src/github.com/gengo/grpc-gateway/third_party/googleapis --go_out=plugins=grpc,$(get_external_modifiers)$(get_googleapi_modifiers "${1}"):. ${1}/*\.proto
}
# arg1: dir
dir_to_go_package() {
echo "${1}" | sed "s/\//_/g"
}
# arg1: dir
create_go_package_file() {
local dir="${1}"
local go_package="$(dir_to_go_package "${dir}")"
rm -f "${dir}/${go_package}.go"
echo "package ${go_package} // import \"go.pedge.io/googleapis/${dir}\"" > "${dir}/${go_package}.pb.go"
}
copy_googleapi_files_into_cwd
for dir in $(get_googleapi_dirs); do \
protoc_cmd="$(get_protoc_cmd "${dir}")"
echo "${protoc_cmd}"
if [ -z "${DRY_RUN}" ]; then
${protoc_cmd}
create_go_package_file "${dir}"
fi
done