-
Notifications
You must be signed in to change notification settings - Fork 169
/
buckw
executable file
·244 lines (214 loc) · 6.84 KB
/
buckw
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env bash
#########################################################################
##
## Buck wrapper script to invoke okbuck when needed, before running buck
##
#########################################################################
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : ".*-> \(.*\)$"`
if expr "$link" : "/.*" > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
SCRIPT_DIR="`pwd -P`"
cd "$SAVED" >/dev/null
red=`tput setaf 1 2>/dev/null || true`
yellow=`tput setaf 3 2>/dev/null || true`
green=`tput setaf 2 2>/dev/null || true`
blue=`tput setaf 4 2>/dev/null || true`
reset=`tput sgr0 2>/dev/null || true`
bold=`tput bold 2>/dev/null || true`
die ( ) {
echo
echo "${bold}${red} $* ${reset}"
echo
exit 1
}
info ( ) {
echo "${bold}${blue}$*${reset}"
}
warn ( ) {
echo "${bold}${yellow}$*${reset}"
}
success ( ) {
echo
echo "${bold}${green}$*${reset}"
echo
}
ensure ( ) {
command -v $1 >/dev/null 2>&1 || die "ERROR: "$1" could be found in your PATH. Please install $1. $2"
}
jsonq() {
python -c "import sys,json; obj=json.load(sys.stdin); print($1)"
}
ensure python "https://www.python.org"
INSTALLED_WATCHMAN=`command -v watchman`
OKBUCK_SUCCESS="$SCRIPT_DIR/build/okbuck.success"
OKBUCK_DIR="$SCRIPT_DIR/.okbuck"
MAX_DISPLAY_CHANGES=10
WATCHMAN_TIMEOUT=10
WATCHMAN_FAILED="WATCHMAN FAILED"
BUCK_BINARY_DIR="$OKBUCK_DIR/workspace/buck_binary"
export JAVA_OPTS="${JAVA_OPTS:-\"-Xmx1g\" \"-Xms1g\"}"
# Timeout a call and exit early. This can be called via timeout <time> <command>.
timeout( ) {
perl -e "alarm shift; exec @ARGV" "$@";
}
ensureWatch ( ) {
timeout $WATCHMAN_TIMEOUT watchman watch-project "$SCRIPT_DIR" >/dev/null 2>&1
}
removeWatch ( ) {
timeout $WATCHMAN_TIMEOUT watchman watch-del "$SCRIPT_DIR" >/dev/null 2>&1
}
getChanges ( ) {
ensureWatch
WATCHMAN_ERROR_CODE=$?
if [[ $WATCHMAN_ERROR_CODE -ne 0 ]]; then
WATCHED_CHANGES=$WATCHMAN_FAILED
echo "watch-project failed after $WATCHMAN_TIMEOUT seconds, error code: $WATCHMAN_ERROR_CODE"
removeWatch
WATCHDEL_ERROR_CODE=$?
if [[ $WATCHDEL_ERROR_CODE -ne 0 ]]; then
echo "watch-del failed after $WATCHMAN_TIMEOUT seconds, error code: $?"
echo "Killing all watchman processes"
killall watchman || true
fi
else
WATCHED_CHANGES=`watchman --output-encoding=json --no-pretty -j 2>&1 <<-EOT
["query", "$SCRIPT_DIR", {
"since": "n:okbuck_trig",
"expression": ["allof",
["type", "f"],
["anyof",
["suffix", "sq"],
["suffix", "gradle"],
["name", ["gradle-wrapper.properties"]]
],
["not",
["dirname", ".okbuck"]
]
],
"fields": ["name"]
}]
EOT`
SOURCE_ROOTS=`watchman --output-encoding=json --no-pretty -j 2>&1 <<-EOT
["query", "$SCRIPT_DIR", {
"since": "n:okbuck_source_roots",
"expression": ["anyof",
["allof",
["type", "d"],
["anyof",
["match", "**/src/**/java", "wholename"],
["match", "**/src/**/res", "wholename"],
["match", "**/src/**/kotlin", "wholename"],
["match", "**/src/**/resources", "wholename"]
],
["not",
["dirname", ".okbuck"]
]
],
["allof",
["type", "f"],
["anyof", ["name", "AndroidManifest.xml"]],
["not", ["dirname", ".okbuck"]]
]
],
"fields": ["new", "exists", "name"]
}]
EOT`
fi
}
updateOkBuckSuccess ( ) {
OKBUCK_SUCCESS_DIR=`dirname $OKBUCK_SUCCESS`
mkdir -p $OKBUCK_SUCCESS_DIR
touch "$OKBUCK_SUCCESS"
}
runOkBuck ( ) {
info "RUNNING OKBUCK..."
echo
rm -f $OKBUCK_SUCCESS
( "$SCRIPT_DIR/gradlew" -p "$SCRIPT_DIR" okbuck -Dokbuck.wrapper=true $EXTRA_OKBUCK_ARGS &&
updateOkBuckSuccess && success "PROCEEDING WITH BUCK" ) || die "OKBUCK FAILED"
}
watchmanWorkflow ( ) {
# Get list of changed files since last time by querying watchman
getChanges
if [[ "$WATCHED_CHANGES" = "$WATCHMAN_FAILED" ]]; then
runOkBuck
else
# Format list for simpler output
CHANGES=$(echo $WATCHED_CHANGES | jsonq '" ".join(obj["files"])')
NEW_OR_DELETED_SOURCE_ROOTS=$(echo $SOURCE_ROOTS | jsonq '" ".join([f["name"] for f in obj["files"] if (not f["exists"] or f["new"])])')
NUM_CHANGES=$(echo $CHANGES $NEW_OR_DELETED_SOURCE_ROOTS | wc -w)
if [[ $NUM_CHANGES -gt 0 ]]; then
info "CHANGES DETECTED IN:"
echo $CHANGES $NEW_OR_DELETED_SOURCE_ROOTS | tr " " "\n" | head -n $MAX_DISPLAY_CHANGES
if [[ $NUM_CHANGES -gt $MAX_DISPLAY_CHANGES ]]; then
DIFF=`expr $NUM_CHANGES - $MAX_DISPLAY_CHANGES`
echo "...and $DIFF more"
fi
echo
runOkBuck
fi
fi
}
setupBuckBinary ( ) {
BUCK_BINARY=$(find "$BUCK_BINARY_DIR" -name buck*.pex)
if [[ ! -x "$BUCK_BINARY" ]]; then
chmod +x "$BUCK_BINARY"
fi
}
# Run tasks before buck command
setupBuckRun ( ) {
if [[ ! -z "$SKIP_OKBUCK" ]]; then
:
elif [[ ! -z "$FORCE_OKBUCK" ]]; then
runOkBuck
elif [[ ! -f "$OKBUCK_SUCCESS" ]] || [[ ! -d "$OKBUCK_DIR" ]]; then
warn "NO PREVIOUS SUCCESSFUL OKBUCK RUN"
if [[ ! -z "$INSTALLED_WATCHMAN" ]]; then
getChanges # Prevent watchman from running after this run, since changes would already be accounted for
fi
runOkBuck
elif [[ ! -z "$INSTALLED_WATCHMAN" ]]; then
watchmanWorkflow
else
warn "ALWAYS RUNNING OKBUCK SINCE WATCHMAN IS NOT INSTALLED"
warn "INSTALL WATCHMAN FOR FASTER BUILDS"
warn "https://facebook.github.io/watchman"
echo
runOkBuck
fi
setupBuckBinary
# Replace kotlin_home config with full path instead of source path
local kotlin_home
kotlin_home=$("$BUCK_BINARY" audit config kotlin.kotlin_home)
if [[ "$kotlin_home" == *":kotlin_home"* ]]; then
echo "changing kotlin home"
local resolved_kotlin_home
resolved_kotlin_home=$("$BUCK_BINARY" build //.okbuck/workspace/kotlin_home:kotlin_home --show-output | awk '{print $2}')
sed -i.bak "s://.okbuck/workspace/kotlin_home\:kotlin_home:${resolved_kotlin_home}:g" .okbuck/config/okbuck.buckconfig
fi
}
# Handle parameters and flags
handleParams ( ) {
SKIP_FLAGS=""
for arg in "$@"
do
case "$arg" in
--help|-h|help|kill) export SKIP_OKBUCK=1 && SKIP_FLAGS=1 ;; # Go directly to the kill command, help command, or --help option. Do not run okbuck.
--version) export SKIP_FLAGS=1;;
esac;
done
}
handleParams "$@"
setupBuckRun
# Invoke buck binary with arguments
exec "$BUCK_BINARY" "$@" $EXTRA_BUCK_CONFIG