Skip to content

Commit

Permalink
feat: add bash linting (#134)
Browse files Browse the repository at this point in the history
* feat: add bash linting

* don't fail fast

* fix replace

* fix sed options
  • Loading branch information
averikitsch committed Jan 30, 2023
1 parent 6b4fcf0 commit f1f3db8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 26 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Lint
on: [pull_request]

jobs:
lint:
runs-on: ubuntu-latest
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: true

steps:
- name: Checkout Repository
uses: actions/checkout@v3

- name: Install ShellCheck
run: sudo apt install shellcheck

- name: Lint Shell Scripts
run: |
set +e
shellcheck tools/*.sh # See all warnings
shellcheck --severity error -f quiet tools/*.sh # Fail on errors only
57 changes: 31 additions & 26 deletions tools/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,64 +43,69 @@ mkdir -p $PROTOC_OUT
rm -rf $TEST_GEN_OUT
mkdir -p $TEST_GEN_OUT

# Set sed options for Ubuntu vs MacOs
SED="sed -i"

# Ensure clean source for local builds
BUILD_LOCALLY="${BUILD_LOCALLY:-false}"
if [[ "$BUILD_LOCALLY" == "true" ]]; then
MACOS='.bak'
SED="sed -i .bak"
current=$(pwd)
cd $DATA_SOURCE_PATH
git restore --staged $PROTOBUF_SRC $THIRDPARTY
git restore $PROTOBUF_SRC $THIRDPARTY
cd "$DATA_SOURCE_PATH"
git restore --staged "$PROTOBUF_SRC" "$THIRDPARTY"
git restore "$PROTOBUF_SRC" "$THIRDPARTY"
git clean -f
cd $current
fi
cd "$current"
fi

# Assemble protoc plugin to generate tests
mvn clean package assembly:single -f protoc-gen-java-snowpea/

# Setup monitored resource proto with Java options
MonitoredResourceProto=$(find $THIRDPARTY -name "monitored_resource.proto")
echo -en "\noption java_multiple_files = true;" >> $MonitoredResourceProto
echo -en "\noption java_outer_classname = \"MonitoredResourceProto\";" >> $MonitoredResourceProto
echo -en "\noption java_package = \"com.google.api\";" >> $MonitoredResourceProto
MonitoredResourceProto=$(find "$THIRDPARTY" -name "monitored_resource.proto")
{
echo -en "\noption java_multiple_files = true;"
echo -en "\noption java_outer_classname = \"MonitoredResourceProto\";"
echo -en "\noption java_package = \"com.google.api\";"
} >> "$MonitoredResourceProto"

echo
echo "########################################################"
for proto_src in $(find "${PROTOBUF_SRC}" -type f -name data.proto); do
echo
echo "# Generating proto bindings - $proto_src"
echo "# Generating proto bindings - ${proto_src}"

# Comment out preset Java options
sed -i $MACOS 's/option java/\/\/option java/' $proto_src
$SED 's/option java/\/\/option java/' "$proto_src"
# Set Java option: multiple files
echo -en "\noption java_multiple_files = true;" >> $proto_src
echo -en "\noption java_multiple_files = true;" >> "$proto_src"
# Set Java option: package name
awk '/^package/,/;/' $proto_src > PROTO_PACKAGE; # Ex. package google.events.cloud.pubsub.v1; => google.events.cloud.pubsub.v1
sed -i $MACOS 's/package /option java_package = \"com./' PROTO_PACKAGE
sed -i $MACOS 's/;/"/' PROTO_PACKAGE
echo -en "\n$(cat PROTO_PACKAGE);" >> $proto_src
awk '/^package/,/;/' "$proto_src" > PROTO_PACKAGE; # Ex. package google.events.cloud.pubsub.v1; => google.events.cloud.pubsub.v1
$SED 's/package /option java_package = \"com./' PROTO_PACKAGE
$SED 's/;/"/' PROTO_PACKAGE
echo -en "\n$(cat PROTO_PACKAGE);" >> "$proto_src"

$PROTOC_PATH \
-I=$PROTOBUF_SRC \
-I=$THIRDPARTY \
-I="$PROTOBUF_SRC" \
-I="$THIRDPARTY" \
--java_out=$PROTOC_OUT \
$proto_src
"$proto_src"
done

echo
echo "########################################################"
for proto_src in $(find "${PROTOBUF_SRC}" -type f -name events.proto); do
echo
echo "# Generating validation tests - $proto_src"
echo "# Generating validation tests - ${proto_src}"
# Note: --java-snowpea_opt is a list tests to skip due to imcompatibility
$PROTOC_PATH \
--plugin=protoc-gen-java-snowpea=protoc-gen-java-snowpea/startup-script.sh \
--java-snowpea_out $TEST_GEN_OUT \
--java-snowpea_opt "MessagePublishedData,LogEntryData#strict" \
-I $PROTOBUF_SRC \
-I $THIRDPARTY \
-I "$PROTOBUF_SRC" \
-I "$THIRDPARTY" \
--experimental_allow_proto3_optional \
$proto_src
"$proto_src"
done

YEAR=$(date +'%Y')
Expand All @@ -110,8 +115,8 @@ FILE_HEAD="// Generated by the protocol buffer compiler."
echo
echo "########################################################"
for i in $(find "${PROTOC_OUT}" -type f -name "*.java"); do
echo "# Generating license header - $i"
sed -i $MACOS "s|$FILE_HEAD|$LICENSE\n$FILE_HEAD|" $i
echo "# Generating license header - ${i}"
$SED "s|$FILE_HEAD|$LICENSE\n$FILE_HEAD|" "$i"
done

echo
Expand Down

0 comments on commit f1f3db8

Please sign in to comment.