Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Add support for running one off protobuf compiles
Browse files Browse the repository at this point in the history
Add support for running a single one off protobuf compile run of all
proto files of an input directory, using the renamed protobuf-compiler
image (previously protobuf-watcher).
  • Loading branch information
spaulg committed Sep 22, 2020
1 parent 95e79df commit 7530c67
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 30 deletions.
3 changes: 3 additions & 0 deletions grpc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,7 @@ RUN chmod a+x /usr/local/bin/entrypoint.sh
COPY watch.sh /usr/local/bin/watch.sh
RUN chmod a+x /usr/local/bin/watch.sh

COPY compile.sh /usr/local/bin/compile.sh
RUN chmod a+x /usr/local/bin/compile.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
37 changes: 37 additions & 0 deletions grpc/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/sh
set -e

INPUT_DIRECTORY=""
OUTPUT_DIRECTORY=""
ROOT_NS_DIRECTORY=""

print_help() {
echo "Usage: $0 -i INPUT_DIRECTORY -o OUTPUT_DIRECTORY [-n ROOT_NS_DIRECTORY]"
exit 2
}

while getopts "i:o:n:" c; do
case $c in
i) INPUT_DIRECTORY=$OPTARG ;;
o) OUTPUT_DIRECTORY=$OPTARG ;;
n) ROOT_NS_DIRECTORY=$OPTARG ;;
*) print_help ;;
esac
done

# Generate temp path for file generation
TMP_OUTPUT_DIRECTORY=$(mktemp -d)

# Generate files
find "$INPUT_DIRECTORY" -name "*.proto" -type f -prune -printf 'Building %p\n' -exec protoc \
--proto_path="$INPUT_DIRECTORY" \
--php_out="$TMP_OUTPUT_DIRECTORY" \
--grpc_out="$TMP_OUTPUT_DIRECTORY" \
--plugin=protoc-gen-grpc=/usr/bin/grpc_php_plugin \
{} \;

# Relocate files, accounting for root namespace
rsync -a "$TMP_OUTPUT_DIRECTORY/$ROOT_NS_DIRECTORY" "$OUTPUT_DIRECTORY"

# Delete any temp files left behind
rm -rf "$TMP_OUTPUT_DIRECTORY"
33 changes: 26 additions & 7 deletions grpc/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,33 @@
set -e

WATCHER_PID=""
WATCH=0

term_handler() {
if [ "$WATCHER_PID" != "" ]; then
kill -TERM "$WATCHER_PID"
fi
print_help() {
echo "Usage: $0 [-w]"
exit 2
}

trap term_handler TERM
while getopts "w" c; do
case $c in
w) WATCH=1 ;;
*) print_help ;;
esac
done

bash -l -c '/usr/local/bin/watch.sh' & WATCHER_PID=$!
wait "$WATCHER_PID"
if [ $WATCH -eq 1 ]; then
# If watching, start the watch process and listen for term signals
term_handler() {
if [ "$WATCHER_PID" != "" ]; then
kill -TERM "$WATCHER_PID"
fi
}

trap term_handler TERM

bash -l -c "/usr/local/bin/watch.sh -i \"$INPUT_DIRECTORY\" -o \"$OUTPUT_DIRECTORY\" -n \"$ROOT_NS_DIRECTORY\"" & WATCHER_PID=$!
wait "$WATCHER_PID"
else
# If not watching, run a compile once directly
/usr/local/bin/compile.sh -i "$INPUT_DIRECTORY" -o "$OUTPUT_DIRECTORY" -n "$ROOT_NS_DIRECTORY"
fi
43 changes: 20 additions & 23 deletions grpc/watch.sh
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
#!/bin/sh
set -e

VERBOSE=0
if [ "$VERBOSE" = "true" ]; then
VERBOSE=1
fi

# Default root namespace is empty
ROOT_NS_DIRECTORY=${ROOT_NS_DIRECTORY:-""}
INPUT_DIRECTORY=""
OUTPUT_DIRECTORY=""
ROOT_NS_DIRECTORY=""

print_help() {
echo "Usage: $0 -i INPUT_DIRECTORY -o OUTPUT_DIRECTORY [-n ROOT_NS_DIRECTORY]"
exit 2
}

while getopts "i:o:n:" c; do
case $c in
i) INPUT_DIRECTORY=$OPTARG ;;
o) OUTPUT_DIRECTORY=$OPTARG ;;
n) ROOT_NS_DIRECTORY=$OPTARG ;;
*) print_help ;;
esac
done

echo "Watch directory: $INPUT_DIRECTORY"
echo "Build directory: $OUTPUT_DIRECTORY"
echo "Root directory: $ROOT_NS_DIRECTORY"

compile_files() {
# Generate temp path for file generation
TMP_OUTPUT_DIRECTORY=$(mktemp -d)

# Generate files
find "$INPUT_DIRECTORY" -name "*.proto" -type f -prune -printf 'Building %p\n' -exec protoc \
--proto_path="$INPUT_DIRECTORY" \
--php_out="$TMP_OUTPUT_DIRECTORY" \
--grpc_out="$TMP_OUTPUT_DIRECTORY" \
--plugin=protoc-gen-grpc=/usr/bin/grpc_php_plugin \
{} \;

# Relocate files, accounting for root namespace
rsync -a "$TMP_OUTPUT_DIRECTORY/$ROOT_NS_DIRECTORY" "$OUTPUT_DIRECTORY"

# Delete any temp files left behind
rm -rf "$TMP_OUTPUT_DIRECTORY"
}

# First time compile
compile_files
/usr/local/bin/compile.sh -i "$INPUT_DIRECTORY" -o "$OUTPUT_DIRECTORY" -n "$ROOT_NS_DIRECTORY"

# Then watch for changes
inotifywait -q -m -e modify -e attrib -e move -e create -e delete -e delete_self -e unmount "$INPUT_DIRECTORY" |
Expand All @@ -41,5 +38,5 @@ while read -r directory events filename; do
echo "directory: $directory, events: $events, filename: $filename";
fi

compile_files
/usr/local/bin/compile.sh -i "$INPUT_DIRECTORY" -o "$OUTPUT_DIRECTORY" -n "$ROOT_NS_DIRECTORY"
done

0 comments on commit 7530c67

Please sign in to comment.