-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·342 lines (299 loc) · 10.7 KB
/
install.sh
File metadata and controls
executable file
·342 lines (299 loc) · 10.7 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
#!/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 initializes the development environment for the Google Ads API Developer Assistant.
# It performs the following steps:
# 1. Verifies that required tools (jq, git) are installed.
# 2. Clones or updates the 'google-ads-python' repository into a specified directory.
# 3. Updates the '.gemini/settings.json' file to include the project's API examples,
# saved code, and the cloned Python library in the context.
# 4. Registers the project as a Gemini extension.
# 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
}
# --- 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}"
# --- Configuration ---
readonly DEFAULT_PARENT_DIR="${PROJECT_DIR_ABS}/client_libs"
readonly ALL_LANGS="python php ruby java dotnet"
# Helper functions for repo info (Replacing associative arrays for Bash 3.2 compatibility)
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
}
# --- Defaults ---
# Simple variables to track selection (associative arrays not supported in Bash 3.2)
INSTALL_PYTHON=true
INSTALL_PHP=false
INSTALL_RUBY=false
INSTALL_JAVA=false
INSTALL_DOTNET=false
ANY_SELECTED=false
# --- Dependency Check ---
if ! command -v jq &> /dev/null; then
echo "jq is not installed. Attempting to install..."
if command -v brew &> /dev/null; then
echo "Homebrew detected. Installing jq..."
if brew install jq; then
echo "Successfully installed jq."
else
err "ERROR: Failed to install jq via Homebrew."
exit 1
fi
elif command -v apt-get &> /dev/null; then
if sudo apt-get update && sudo apt-get install -y jq; then
echo "Successfully installed jq."
else
err "ERROR: Failed to install jq automatically."
err "Please install jq manually to continue."
err "See: https://jqlang.github.io/jq/download/"
exit 1
fi
else
err "ERROR: jq is not installed and no supported package manager (brew/apt-get) found."
err "Please install jq manually to continue."
err "See: https://jqlang.github.io/jq/download/"
exit 1
fi
fi
if ! command -v git &> /dev/null; then
err "ERROR: git is not installed. Please install it to continue."
exit 1
fi
# --- Help Function ---
usage() {
echo "Usage: $0 [OPTIONS]"
echo " Clones/updates Google Ads client libraries and modifies the settings file."
echo ""
echo " This script initializes the development environment for the Google Ads API Developer Assistant."
echo " It clones the selected client libraries into '${DEFAULT_PARENT_DIR}'."
echo " The google-ads-python library is always installed by default."
echo ""
echo " Options:"
echo " -h, --help Show this help message and exit"
echo " --php Include google-ads-php"
echo " --ruby Include google-ads-ruby"
echo " --java Include google-ads-java"
echo " --dotnet Include google-ads-dotnet"
echo ""
echo " If no language flags are provided, only the Python library will be installed."
echo ""
echo " Example:"
echo " $0 --java (Installs Java and Python libraries)"
echo ""
}
# --- Argument Parsing ---
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
--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
;;
*)
err "ERROR: Unknown argument: $1"
usage
exit 1
;;
esac
done
# --- Language Selection Logic ---
# Python is always installed. Other languages are only installed if selected.
if [[ "${ANY_SELECTED}" == "false" ]]; then
echo "No additional languages selected. Defaulting to Python only."
fi
# --- Path Resolution and Validation ---
# Ensure default directory exists
echo "Ensuring default library directory exists: ${DEFAULT_PARENT_DIR}"
mkdir -p "${DEFAULT_PARENT_DIR}" || { err "ERROR: Failed to create ${DEFAULT_PARENT_DIR}"; exit 1; }
# Helper to check if a language is enabled
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" ]] ;;
*) return 1 ;;
esac
}
# Resolve paths
for lang in $ALL_LANGS; do
if is_enabled "$lang"; then
repo_name=$(get_repo_name "$lang")
path="${DEFAULT_PARENT_DIR}/${repo_name}"
# Resolve to absolute path
if command -v realpath &> /dev/null; then
# Try using -m if available (doesn't require existence), otherwise just path
# On macOS, realpath might not support -m or might not exist (coreutils).
# We handle missing realpath below.
ABS_PATH=$(realpath -m "$path" 2>/dev/null || realpath "$path" 2>/dev/null || echo "$path")
else
# Fallback - parent (DEFAULT_PARENT_DIR) exists now
ABS_PATH="$(cd "${DEFAULT_PARENT_DIR}" && pwd)/$(basename "$path")"
fi
# Store path in dynamic variable for later use (jq args)
# Bash 3.2 compatible way to set variable by name
eval "LIB_PATH_${lang}='${ABS_PATH}'"
fi
done
# --- Clone/Update Repositories ---
clone_or_update() {
local repo_url="$1"
local clone_path="$2"
local repo_name
repo_name=$(basename "${clone_path}")
echo "Managing repository ${repo_name} in ${clone_path}"
if [[ -d "${clone_path}/.git" ]]; then
echo "Directory ${clone_path} already exists. Updating..."
if ! (cd "${clone_path}" && git pull); then
echo "WARN: Failed to update ${repo_name}. Continuing..."
else
echo "Successfully updated ${repo_name}."
fi
elif [[ -d "${clone_path}" ]]; then
echo "WARN: Directory ${clone_path} exists but is not a git repo. Skipping."
else
echo "Cloning ${repo_url} into ${clone_path}"
if ! git clone "${repo_url}" "${clone_path}"; then
err "ERROR: Failed to clone ${repo_url}"
exit 1
fi
echo "Successfully cloned ${repo_name}."
fi
}
for lang in $ALL_LANGS; do
if is_enabled "$lang"; then
eval "path=\"\$LIB_PATH_${lang}\""
url=$(get_repo_url "$lang")
clone_or_update "$url" "$path"
fi
done
# --- Modify settings.json ---
readonly SETTINGS_FILE="${PROJECT_DIR_ABS}/.gemini/settings.json"
if [[ ! -f "${SETTINGS_FILE}" ]]; then
err "ERROR: Settings file not found: ${SETTINGS_FILE}"
exit 1
fi
echo "Updating ${SETTINGS_FILE} with context paths..."
readonly CONTEXT_PATH_EXAMPLES="${PROJECT_DIR_ABS}/api_examples"
readonly CONTEXT_PATH_SAVED="${PROJECT_DIR_ABS}/saved/code"
# Construct jq args
JQ_ARGS=(
--arg examples "${CONTEXT_PATH_EXAMPLES}"
--arg saved "${CONTEXT_PATH_SAVED}"
)
# Add each lib path as an arg
for lang in $ALL_LANGS; do
if is_enabled "$lang"; then
eval "path=\"\$LIB_PATH_${lang}\""
JQ_ARGS+=(--arg "lib_${lang}" "${path}")
fi
done
# Construct the array construction string for jq
JQ_ARRAY_STR="[\$examples, \$saved"
for lang in $ALL_LANGS; do
if is_enabled "$lang"; then
JQ_ARRAY_STR+=", \$lib_$lang"
fi
done
JQ_ARRAY_STR+="]"
# Use jq to modify the JSON file
TMP_SETTINGS_FILE=""
trap 'rm -f "${TMP_SETTINGS_FILE}"' EXIT # Cleanup tmp file on exit
if ! TMP_SETTINGS_FILE=$(mktemp "${SETTINGS_FILE}.XXXXXX"); then
err "ERROR: Failed to create temporary file."
exit 1
fi
if ! jq \
"${JQ_ARGS[@]}" \
".context.includeDirectories = ${JQ_ARRAY_STR}" \
"${SETTINGS_FILE}" > "${TMP_SETTINGS_FILE}"; then
err "ERROR: jq command failed to update ${SETTINGS_FILE}"
exit 1
fi
# Replace the original file with the modified one
if ! mv "${TMP_SETTINGS_FILE}" "${SETTINGS_FILE}"; then
err "ERROR: Failed to move temporary file to ${SETTINGS_FILE}"
exit 1
fi
echo "Registering Google Ads API Developer Assistant as a Gemini extension..."
if command -v gemini &> /dev/null; then
# Use yes Y to handle the interactive prompt as --consent is not supported in OSS
# Capture output to detect "already installed" state
if ! INSTALL_OUTPUT=$(yes Y | gemini extensions install https://github.com/googleads/google-ads-api-developer-assistant.git 2>&1); then
if [[ "${INSTALL_OUTPUT}" == *"already installed"* ]]; then
echo "Extension already installed. Reinstalling..."
gemini extensions uninstall "google-ads-api-developer-assistant" || true
yes Y | gemini extensions install https://github.com/googleads/google-ads-api-developer-assistant.git
else
echo "${INSTALL_OUTPUT}" >&2
err "WARN: Failed to register extension automatically. You may need to run 'gemini extensions install https://github.com/googleads/google-ads-api-developer-assistant.git' manually."
fi
else
echo "${INSTALL_OUTPUT}"
fi
else
echo "WARN: 'gemini' command not found. Skipping extension registration."
fi
trap - EXIT # Clear the trap
echo "Successfully updated ${SETTINGS_FILE}"
echo "New contents of context.includeDirectories:"
jq '.context.includeDirectories' "${SETTINGS_FILE}"
echo "Installation complete."
echo ""
echo "IMPORTANT: You must configure and verify the development environment for each language you wish to use."