-
Notifications
You must be signed in to change notification settings - Fork 11
/
buckw
executable file
·240 lines (209 loc) · 7.2 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
#!/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 setab 1 2>/dev/null || true`
yellow=`tput setab 3 2>/dev/null || true`
green=`tput setab 2 2>/dev/null || true`
blue=`tput setab 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'
ensure ant 'http://ant.apache.org/'
INSTALLED_WATCHMAN=`command -v watchman`
DEFAULT_BUCK_REPO="https://github.com/facebook/buck.git"
DEFAULT_BUCK_INSTALL_DIR="$HOME/.gradle/caches/okbuck/buck"
CUSTOM_BUCK_REPO=""
CUSTOM_REMOTE_NAME="custom"
OKBUCK_SUCCESS="$SCRIPT_DIR/build/okbuck.success"
OKBUCK_DIR="$SCRIPT_DIR/.okbuck"
MAX_DISPLAY_CHANGES=10
ensureWatch ( ) {
watchman watch-project $SCRIPT_DIR >/dev/null 2>&1
}
getChanges ( ) {
ensureWatch
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", "gradle"],
["suffix", "map"],
["suffix", "pro"],
["name", ["lint.xml", "gradle-wrapper.properties"]],
["match", "**/src/**/AndroidManifest.xml", "wholename"]
],
["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": ["allof",
["type", "d"],
["anyof",
["match", "**/src/**/java", "wholename"],
["match", "**/src/**/res", "wholename"],
["match", "**/src/**/resources", "wholename"]
],
["not",
["dirname", ".okbuck"]
]
],
"fields": ["new", "exists", "name"]
}]
EOT`
}
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
# 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
}
setupBuckBinary ( ) {
# If no explicit buck binary is set
if [[ -z "$BUCK_BINARY" ]] ; then
# If no buck installation directory is set
if [[ -z "$BUCK_HOME" ]]; then
BUCK_HOME=$DEFAULT_BUCK_INSTALL_DIR
fi
# Install buck from source if not already available
if [[ ! -d "$BUCK_HOME" ]]; then
warn "BUCK NOT FOUND IN '$BUCK_HOME'. INSTALLING BUCK..."
git clone $DEFAULT_BUCK_REPO $BUCK_HOME || die "FAILED TO CLONE $DEFAULT_BUCK_REPO"
fi
# Add custom buck remote
if [[ ! -z $CUSTOM_BUCK_REPO ]]; then
cd $BUCK_HOME
REMOTE_EXISTS=$(git remote -v | grep "$CUSTOM_REMOTE_NAME")
if [[ -z "$REMOTE_EXISTS" ]]; then
git remote add $CUSTOM_REMOTE_NAME $CUSTOM_BUCK_REPO || die "FAILED TO ADD REMOTE $CUSTOM_BUCK_REPO"
fi
cd -
fi
# Check for current buck version
BUCK_VERSION=$(cat $SCRIPT_DIR/.buckversion)
if [[ ! -z "$BUCK_VERSION" ]]; then
pushd "$BUCK_HOME" >/dev/null
CURRENT_BUCK_VERSION=$(git rev-parse HEAD)
if [[ "$BUCK_VERSION" != "$CURRENT_BUCK_VERSION" ]]; then
info "BUCK VERSION IS AT $CURRENT_BUCK_VERSION , BUT IT SHOULD BE $BUCK_VERSION. UPDATING BUCK"
git clean -fdx --quiet && git reset --hard --quiet && git fetch --all && git checkout "$BUCK_VERSION" --quiet && success "UPDATED BUCK TO $BUCK_VERSION" || die "FAILED TO UPDATE BUCK TO $BUCK_VERSION"
ps -ef | grep "[c]om.facebook.buck.cli.Main\$DaemonBootstrap" | awk '{print $2}' | xargs kill -9 && success "STOPPED ACTIVE BUCK DAEMONS"
fi
popd >/dev/null
fi
# Build buck if needed
if [[ ! -f "$BUCK_HOME/build/successful-build" ]]; then
echo "Buck does not appear to have been built -- building Buck!"
pushd "$BUCK_HOME" >/dev/null
mkdir -p build && ant > build/ant.log && success "BUCK BUILT SUCCESSFULLY" || die "FAILED TO BUILD BUCK"
popd >/dev/null
fi
BUCK_BINARY="$BUCK_HOME/bin/buck"
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
}
# Handle parameters and flags
handleParams ( ) {
# Go directly to the kill command, help command, or --help option. Do not run okbuck.
if [[ "kill" == $1 || "help" == $1 || $@ == *"--help"* || $@ == *"-h"* ]]; then
SKIP_OKBUCK=true
fi
}
handleParams "$@"
setupBuckRun
# Invoke buck binary with arguments
exec "$BUCK_BINARY" "$@"