-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·387 lines (342 loc) · 12.8 KB
/
update.sh
File metadata and controls
executable file
·387 lines (342 loc) · 12.8 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/bin/bash
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Description:
# This script updates the Google Ads API Developer Assistant and its dependencies.
# It performs the following steps:
# 1. Updates the 'google-ads-api-developer-assistant' repository (git pull).
# 2. Reads '.gemini/settings.json' to locate the 'google-ads-python' repository.
# 3. Updates the 'google-ads-python' repository (git pull).
# Exit on any error, and on undefined variables.
set -eu
# Function to print errors to stderr
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}
# --- Help Function ---
usage() {
echo "Usage: $0 [OPTIONS]"
echo " Updates the Google Ads API Developer Assistant and configured client libraries."
echo ""
echo " This script performs the following actions:"
echo " 1. Updates the 'google-ads-api-developer-assistant' repository (git pull)."
echo " 2. Reads '.gemini/settings.json' to find configured client libraries."
echo " 3. Updates each found client library repository (git pull)."
echo ""
echo " Options:"
echo " -h, --help Show this help message and exit"
echo " --python Ensure google-ads-python is present and updated"
echo " --php Ensure google-ads-php is present and updated"
echo " --ruby Ensure google-ads-ruby is present and updated"
echo " --java Ensure google-ads-java is present and updated"
echo " --dotnet Ensure google-ads-dotnet is present and updated"
echo " --context_dir <dirs> Comma-separated list of directories to add to settings.json"
echo ""
echo " If flags are provided, the script will ensure those libraries are installed"
echo " (cloned) and registered in .gemini/settings.json if they weren't already."
echo ""
}
# --- Defaults ---
INSTALL_PYTHON=false
INSTALL_PHP=false
INSTALL_RUBY=false
INSTALL_JAVA=false
INSTALL_DOTNET=false
ANY_SELECTED=false
CONTEXT_DIR_ARG=""
INVALID_CONTEXT_DIRS=()
# --- Argument Parsing ---
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
--python)
INSTALL_PYTHON=true
ANY_SELECTED=true
shift
;;
--php)
INSTALL_PHP=true
ANY_SELECTED=true
shift
;;
--ruby)
INSTALL_RUBY=true
ANY_SELECTED=true
shift
;;
--java)
INSTALL_JAVA=true
ANY_SELECTED=true
shift
;;
--dotnet)
INSTALL_DOTNET=true
ANY_SELECTED=true
shift
;;
--context_dir)
shift
while [[ $# -gt 0 ]] && [[ "$1" != -* ]]; do
CONTEXT_DIR_ARG="${CONTEXT_DIR_ARG}${1},"
shift
done
if [[ -z "${CONTEXT_DIR_ARG:-}" ]]; then
err "ERROR: --context_dir requires a value"
exit 1
fi
;;
*)
# Ignore unknown options or handle them
shift
;;
esac
done
# Helper functions for repo info (Matching setup.sh)
get_repo_url() {
case "$1" in
python) echo "https://github.com/googleads/google-ads-python.git" ;;
php) echo "https://github.com/googleads/google-ads-php.git" ;;
ruby) echo "https://github.com/googleads/google-ads-ruby.git" ;;
java) echo "https://github.com/googleads/google-ads-java.git" ;;
dotnet) echo "https://github.com/googleads/google-ads-dotnet.git" ;;
esac
}
get_repo_name() {
case "$1" in
python) echo "google-ads-python" ;;
php) echo "google-ads-php" ;;
ruby) echo "google-ads-ruby" ;;
java) echo "google-ads-java" ;;
dotnet) echo "google-ads-dotnet" ;;
esac
}
is_enabled() {
case "$1" in
python) [[ "${INSTALL_PYTHON}" == "true" ]] ;;
php) [[ "${INSTALL_PHP}" == "true" ]] ;;
ruby) [[ "${INSTALL_RUBY}" == "true" ]] ;;
java) [[ "${INSTALL_JAVA}" == "true" ]] ;;
dotnet) [[ "${INSTALL_DOTNET}" == "true" ]] ;;
esac
}
# --- Dependency Check ---
if ! command -v jq &> /dev/null; then
err "ERROR: jq is not installed. Please install it to continue."
err "See: https://jqlang.github.io/jq/download/"
exit 1
fi
if ! command -v git &> /dev/null; then
err "ERROR: git is not installed. Please install it to continue."
exit 1
fi
# --- Project Directory Resolution ---
# Determine the root directory of the current git repository.
if ! PROJECT_DIR_ABS=$(git rev-parse --show-toplevel 2>/dev/null); then
err "ERROR: This script must be run from within the google-ads-api-developer-assistant git repository."
exit 1
fi
readonly PROJECT_DIR_ABS
echo "Detected project root: ${PROJECT_DIR_ABS}"
# --- Update Assistant Repo ---
echo "Updating google-ads-api-developer-assistant..."
SETTINGS_JSON=".gemini/settings.json"
TEMP_SETTINGS=$(mktemp)
CUSTOMER_ID_FILE="customer_id.txt"
TEMP_CUSTOMER_ID=$(mktemp)
# 1. Backup existing settings if they exist
if [[ -f "${SETTINGS_JSON}" ]]; then
echo "Backing up ${SETTINGS_JSON}..."
cp "${SETTINGS_JSON}" "${TEMP_SETTINGS}"
# 2. Reset local changes to settings.json to allow git pull
# Only if the file is tracked and modified (or just blindly checkout if we know it's strict)
# Safest is to just checkout it if it exists in git.
if git ls-files --error-unmatch "${SETTINGS_JSON}" &> /dev/null; then
echo "Resetting ${SETTINGS_JSON} to avoid merge conflicts..."
git checkout "${SETTINGS_JSON}"
fi
fi
# 1b. Backup customer_id.txt if it exists
if [[ -f "${CUSTOMER_ID_FILE}" ]]; then
echo "Backing up ${CUSTOMER_ID_FILE}..."
cp "${CUSTOMER_ID_FILE}" "${TEMP_CUSTOMER_ID}"
# Reset local changes to customer_id.txt to allow git pull
if git ls-files --error-unmatch "${CUSTOMER_ID_FILE}" &> /dev/null; then
echo "Resetting ${CUSTOMER_ID_FILE} to avoid merge conflicts..."
git checkout "${CUSTOMER_ID_FILE}"
fi
fi
if ! git pull; then
err "ERROR: Failed to update google-ads-api-developer-assistant."
# Attempt to restore settings if they were backed up?
# Probably safer to leave the repo state as is if pull failed,
# but strictly speaking we might want to restore the user's settings
# if we reverted them.
if [[ -f "${TEMP_SETTINGS}" ]] && [[ -s "${TEMP_SETTINGS}" ]]; then
echo "Restoring original settings after failed pull..."
mv "${TEMP_SETTINGS}" "${SETTINGS_JSON}"
fi
if [[ -f "${TEMP_CUSTOMER_ID}" ]] && [[ -s "${TEMP_CUSTOMER_ID}" ]]; then
echo "Restoring original customer_id.txt after failed pull..."
mv "${TEMP_CUSTOMER_ID}" "${CUSTOMER_ID_FILE}"
fi
exit 1
fi
# 3. Restore/Merge settings
if [[ -f "${TEMP_SETTINGS}" ]] && [[ -s "${TEMP_SETTINGS}" ]]; then
echo "Merging preserved settings with new defaults..."
# Merge: existing (backup) *over* new (repo)
# We want local user values to override repo values, but we also want
# to keep any new keys from the repo that weren't in user's file.
# Logic: .[0] is repo (new), .[1] is backup (user).
# .[0] * .[1] means backup overrides repo.
if jq -s '.[0] * .[1]' "${SETTINGS_JSON}" "${TEMP_SETTINGS}" > "${TEMP_SETTINGS}.merged"; then
mv "${TEMP_SETTINGS}.merged" "${SETTINGS_JSON}"
echo "Settings restored and merged successfully."
else
err "WARN: Failed to merge settings.json. Restoring original backup without merge."
mv "${TEMP_SETTINGS}" "${SETTINGS_JSON}"
fi
rm -f "${TEMP_SETTINGS}"
fi
# 3b. Restore customer_id.txt
if [[ -f "${TEMP_CUSTOMER_ID}" ]] && [[ -s "${TEMP_CUSTOMER_ID}" ]]; then
echo "Restoring preserved ${CUSTOMER_ID_FILE}..."
# Always overwrite with user's backup
mv "${TEMP_CUSTOMER_ID}" "${CUSTOMER_ID_FILE}"
echo "${CUSTOMER_ID_FILE} restored successfully."
rm -f "${TEMP_CUSTOMER_ID}"
fi
echo "Successfully updated google-ads-api-developer-assistant."
# --- Handle Specific Library Additions ---
readonly ALL_LANGS="python php ruby java dotnet"
readonly DEFAULT_PARENT_DIR="${PROJECT_DIR_ABS}/client_libs"
for lang in $ALL_LANGS; do
if is_enabled "$lang"; then
repo_url=$(get_repo_url "$lang")
repo_name=$(get_repo_name "$lang")
lib_path="${DEFAULT_PARENT_DIR}/${repo_name}"
if [[ ! -d "${lib_path}" ]]; then
echo "Library ${repo_name} not found. Cloning into ${lib_path}..."
mkdir -p "${DEFAULT_PARENT_DIR}"
if ! git clone "${repo_url}" "${lib_path}"; then
err "ERROR: Failed to clone ${repo_url}"
exit 1
fi
# Add to settings.json if not present
if [[ -f "${SETTINGS_JSON}" ]]; then
# Ensure path is absolute for settings.json
ABS_PATH=$(realpath "${lib_path}" 2>/dev/null || echo "${lib_path}")
echo "Registering ${ABS_PATH} in ${SETTINGS_JSON}..."
if ! jq --arg new_path "${ABS_PATH}" '
if (.context.includeDirectories | any(. == $new_path)) then
.
else
.context.includeDirectories += [$new_path]
end' "${SETTINGS_JSON}" > "${SETTINGS_JSON}.tmp"; then
err "ERROR: Failed to update ${SETTINGS_JSON}"
exit 1
fi
mv "${SETTINGS_JSON}.tmp" "${SETTINGS_JSON}"
fi
fi
fi
done
# --- Locate and Update Client Libraries ---
readonly SETTINGS_FILE="${PROJECT_DIR_ABS}/.gemini/settings.json"
if [[ ! -f "${SETTINGS_FILE}" ]]; then
err "ERROR: Settings file not found: ${SETTINGS_FILE}"
err "Please run install.sh first."
exit 1
fi
# --- Handle context_dir argument ---
if [[ -n "${CONTEXT_DIR_ARG:-}" ]]; then
# Trim spaces on either side of comma and at the end of the argument string
CONTEXT_DIR_ARG=$(echo "${CONTEXT_DIR_ARG}" | sed -e 's/[[:space:]]*,[[:space:]]*/,/g' -e 's/,,*/,/g' -e 's/,$//' -e 's/[[:space:]]*$//')
IFS=',' read -ra DIRS <<< "${CONTEXT_DIR_ARG}"
for dir in "${DIRS[@]}"; do
if [[ ! -d "$dir" ]]; then
INVALID_CONTEXT_DIRS+=("Directory not found: $dir")
continue
fi
# Resolve absolute path for consistency
if ! abs_dir=$(realpath "$dir" 2>/dev/null); then
INVALID_CONTEXT_DIRS+=("Could not resolve absolute path for: $dir")
continue
fi
echo "Adding context directory: ${abs_dir} to settings.json..."
if [[ -f "${SETTINGS_FILE}" ]]; then
if ! jq --arg new_path "${abs_dir}" '
if (.context.includeDirectories | any(. == $new_path)) then
.
else
.context.includeDirectories += [$new_path]
end' "${SETTINGS_FILE}" > "${SETTINGS_FILE}.tmp"; then
err "ERROR: Failed to update ${SETTINGS_FILE} for ${abs_dir}"
continue
fi
mv "${SETTINGS_FILE}.tmp" "${SETTINGS_FILE}"
else
err "ERROR: settings.json not found while adding context_dir"
fi
done
fi
echo "Reading ${SETTINGS_FILE} to find client libraries..."
# Read all includeDirectories
INCLUDE_DIRS=()
while IFS= read -r line; do
INCLUDE_DIRS+=("$line")
done < <(jq -r '.context.includeDirectories[]' "${SETTINGS_FILE}")
if [[ ${#INCLUDE_DIRS[@]} -eq 0 ]]; then
echo "WARN: No directories found in ${SETTINGS_FILE}."
exit 0
fi
echo "Found ${#INCLUDE_DIRS[@]} directories in settings."
for lib_path in "${INCLUDE_DIRS[@]}"; do
# Skip if path is empty
[[ -z "${lib_path}" ]] && continue
# Check if path exists
if [[ ! -d "${lib_path}" ]]; then
echo "WARN: Directory not found: ${lib_path}. Skipping."
continue
fi
# Resolve absolute path for comparison
if ! abs_lib_path=$(realpath "${lib_path}" 2>/dev/null); then
echo "WARN: Could not resolve path: ${lib_path}. Skipping."
continue
fi
# Check if it is a git repository
if [[ ! -d "${abs_lib_path}/.git" ]]; then
echo "Skipping non-git directory: ${abs_lib_path}"
continue
fi
echo "Updating repository at: ${abs_lib_path}..."
if ! (cd "${abs_lib_path}" && git pull); then
err "ERROR: Failed to update ${abs_lib_path}"
# We continue updating other libraries even if one fails?
# The prompt didn't specify, but usually best effort is good for updates.
# However, scripts usually exit on error. set -e is on.
# To fail fast:
exit 1
fi
echo "Successfully updated ${abs_lib_path}."
done
if [[ ${#INVALID_CONTEXT_DIRS[@]} -gt 0 ]]; then
for err_msg in "${INVALID_CONTEXT_DIRS[@]}"; do
err "ERROR: $err_msg"
done
fi
echo "Update complete."