forked from foursquare/fsqio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupkeep
executable file
·143 lines (130 loc) · 4.58 KB
/
upkeep
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
#!/bin/bash
# Copyright 2015 Foursquare Labs Inc. All Rights Reserved.
# Pass variables to children
set -eao pipefail
export BUILD_ROOT="${BUILD_ROOT:-$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )}"
export UPKEEPROOT="${BUILD_ROOT}/build-support"
UPKEEP_HOME="${UPKEEPROOT}/fsqio/upkeep"
ENV_FILE="libs/env.sh"
# Symlink the proper environmental variables file.
if [[ -e "${UPKEEPROOT}/foursquare/upkeep/${ENV_FILE}" ]]; then
repo_env="${UPKEEPROOT}/foursquare/upkeep/${ENV_FILE}"
else
repo_env="${UPKEEP_HOME}/${ENV_FILE}"
fi
source "${repo_env}"
source "${UPKEEP_HOME}/libs/util.sh" && source "${UPKEEP_HOME}/libs/fetcher.sh"
if [[ -z "${FSQWORKING_XCODE}" ]]; then
# If XCode is validated, set a env to disable future checks that invocation.
validate_xcode && export FSQ_WORKING_XCODE="True"
fi
# Default to not SKIP_UPKEEP
SKIP_UPKEEP=${SKIP_UPKEEP:-0}
# Check for options.
upkeep_args=()
for opt in $@; do
case ${opt} in
"--all" )
DOWNSTREAM_TASKS="true"
task_names=( $(all_task_names) )
if [[ ! $@ =~ "--clean" ]]; then
task_names=( "${task_names[@]/clean}" )
fi
upkeep_args=( "${upkeep_args[@]}" "${task_names[@]}" )
;;
"--no-downstream" )
DOWNSTREAM_TASKS="false"
;;
"--downstream" )
DOWNSTREAM_TASKS="true"
;;
"--skip-tasks" )
SKIP_UPKEEP=1
;;
"--no-skip-tasks" )
SKIP_UPKEEP=0
;;
"--no-cache" )
# This could and probably should be per-task but the delete would have to happen in the check.sh
DELETE_UPKEEP_CACHE=1
colorized_warn "Deleting the upkeep cache...\n"
(rm -rf "${DEPENDENCIES_ROOT}" "${FS_DOWNLOAD_CACHE}")
# TODO: Don't include clean task by default.
;;
* )
# Try and catch bad options - while allowing passthrough for ./upkeep run.
if [[ "${upkeep_args[0]}" != 'run' ]] && [[ ${opt} != -*help ]] && [[ ${opt} == -* ]]; then
colorized_error "Bad task name: ${opt}"
print_help
exit_with_failure "Bad invocation: No upkeep option: ${opt}\n"
fi
upkeep_args=( "${upkeep_args[@]}" "${opt}")
;;
esac
done
function upkeep_cmd() {
# SKIP_UPKEEP_TASKS allows callers (like the git hooks) to skip upkeep tasks but still execute scripts, like ./python.
# Essentially means "void SKIP_UPKEEP for this command."
if [[ ${SKIP_UPKEEP} -eq 0 ]] || [[ $SKIP_UPKEEP_TASKS -eq 1 ]]; then
if [ ! -f "${1}" ]; then
exit_with_failure "No upkeep file found at: ${1}"
fi
echo "$@"
fi
}
# Route based on arguments.
case "${upkeep_args[0]}" in
"--help"|"help"|"-h" )
print_help
exit 0
;;
"force" )
DOWNSTREAM_TASKS=${DOWNSTREAM_TASKS:-"true"}
action="${UPKEEP_HOME}/force.sh"
args=( "${upkeep_args[@]:1}" )
;;
"check"|'')
# Check disables downstream - honestly it should probably not be surfaced as a top-level command anymore.
DOWNSTREAM_TASKS="false"
action="${UPKEEP_HOME}/check.sh"
args=( "${upkeep_args[@]:1}" )
;;
"tasks" )
DOWNSTREAM_TASKS=${DOWNSTREAM_TASKS:-"false"}
print_all_tasks "${upkeep_args[@]}"
exit 0
;;
"task-list" )
DOWNSTREAM_TASKS=${DOWNSTREAM_TASKS:-"false"}
all_task_names
exit 0
;;
"run" )
script_name="${upkeep_args[1]}"
# The script can be ${script_name}.sh under <xyz>/upkeep/scripts or be a full file path relative to the BUILD_ROOT.
action=$(find_upkeep_file "scripts" "${script_name}") || action="${BUILD_ROOT}/${script_name}"
[ -f "${action}" ] || exit_with_failure "Could not find matching script: ${script_name}"
args=( "${upkeep_args[@]:2}" )
# Execute `./upkeep check' in order to run any required tasks and then turn on SKIP for future upkeep calls.
if [[ ! SKIP_UPKEEP -eq 1 ]]; then
${UPKEEP_HOME}/check.sh
fi
# Disable task-related upkeep operations but allow others (essentially run) to continue.
SKIP_UPKEEP_TASKS=1
SKIP_UPKEEP=1
;;
*)
# This is should already default to "false", but adding a guard here. The downstream task implementation uses git
# operations and is probably not safe to automate. Until the implementation improves, the --downstream flag should
# only be invoked from the command line, akin to a `git reset` for upkeep.
DOWNSTREAM_TASKS=${DOWNSTREAM_TASKS:-"false"}
action="${UPKEEP_HOME}/execute_task.sh"
args=( "${upkeep_args[@]:0}" )
;;
esac
if [ "$0" = "$BASH_SOURCE" ]; then
# This must be two separate shell commands or else exec will return the exit code of the function's `echo` call.
cmd="$(upkeep_cmd ${action} ${args[@]})"
export SKIP_UPKEEP=$SKIP_UPKEEP
exec ${cmd}
fi