-
Notifications
You must be signed in to change notification settings - Fork 10
/
ae_protoc.sh
executable file
·82 lines (70 loc) · 2.48 KB
/
ae_protoc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
set -e
# This will make the pb only compile for appengine
AE_COPY=1
if [[ "$1" == "--ae-only" ]]; then
AE_COPY=0
shift
fi
if [[ "$#" -eq 0 ]]; then
echo "Usage: ae_protoc.sh [--ae-only] <file1.proto> [<file2.proto> ...]"
echo " This script will use the protoc in your path"
echo "to compile each proto into go source and will"
echo "edit each file to be usable on AppEngine by"
echo "removing references to the goprotobuf library."
echo
echo "Unless --ae-only is specified, the compiled protobuf"
echo "output file will be duplicated to an appengine"
echo "specific .ae.go and both will be guarded with the"
echo "appropriate +build directive."
exit 1
fi
if [[ -z "$GO_STUBS" ]]; then
export GO_STUBS="web"
fi
for FILE in "$@"; do
echo "Compiling $FILE..."
protoc --go_out=. "$FILE"
# Determine file names
PB_FILE="${FILE%.proto}.pb.go"
if [[ $AE_COPY -ne 0 ]]; then
AE_FILE="${FILE%.proto}.ae.go"
cp "$PB_FILE" "$AE_FILE"
else
AE_FILE="$PB_FILE"
fi
echo "Sanitizing $AE_FILE..."
{
echo "H" # Display human-readable errors, if any
echo "g/goprotobuf/d" # Delete goprotobuf import
echo "g/var.*Marshal$/d" # Delete Marshal placeholder
# Fix lines calling into the library
echo "g/proto1/s/proto.*CompactTextString([^)]*)/\"not available on AppEngine\"/"
echo "w" # Write
echo "q" # Quit
} | ed -s "$AE_FILE"
if [[ $AE_COPY -eq 0 ]]; then
# If we are sharing the same proto, don't insert guards
continue
fi
echo "Guarding $PB_FILE..."
{
echo "H" # Display human-readable errors, if any
echo "1i" # Insert at the beginning of the file
echo "// +build !appengine" # Don't compile this file under appengine
echo # Blank line to not confuse anything
echo "." # Exit insert mode
echo "w" # Write
echo "q" # Quit
} | ed -s "$PB_FILE"
echo "Guarding $AE_FILE..."
{
echo "H" # Display human-readable errors, if any
echo "1i" # Insert at the beginning of the file
echo "// +build appengine" # Only compile this file under appengine
echo # Blank line to not confuse anything
echo "." # Exit insert mode
echo "w" # Write
echo "q" # Quit
} | ed -s "$AE_FILE"
done