-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinstall.ps1
More file actions
249 lines (205 loc) · 8.13 KB
/
install.ps1
File metadata and controls
249 lines (205 loc) · 8.13 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
<#
.SYNOPSIS
Initializes the development environment for the Google Ads API Developer Assistant on Windows.
.DESCRIPTION
This script performs the following steps:
1. Verifies that required tools (git) are installed.
2. Clones or updates the selected Google Ads client libraries into a specified directory.
3. Updates the '.gemini/settings.json' file to include the project's API examples,
saved code, and the cloned client libraries in the context.
.PARAMETER Python
Include google-ads-python.
.PARAMETER Php
Include google-ads-php.
.PARAMETER Ruby
Include google-ads-ruby.
.PARAMETER Java
Include google-ads-java.
.PARAMETER Dotnet
Include google-ads-dotnet.
.EXAMPLE
.\install.ps1 -Java
Installs Java and Python libraries.
.EXAMPLE
.\install.ps1
Installs only the Python library.
.EXAMPLE
.\install.ps1 -Java
Installs Java and Python libraries.
#>
param(
[switch]$Php,
[switch]$Ruby,
[switch]$Java,
[switch]$Dotnet
)
$ErrorActionPreference = "Stop"
# --- Project Directory Resolution ---
# Determine the root directory of the current git repository.
try {
$ProjectDirAbs = git rev-parse --show-toplevel 2>$null
if (-not $ProjectDirAbs) { throw "Not in a git repo" }
# Normalize path separator
$ProjectDirAbs = (Get-Item -LiteralPath $ProjectDirAbs).FullName
}
catch {
Write-Error "ERROR: This script must be run from within the google-ads-api-developer-assistant git repository."
exit 1
}
Write-Host "Detected project root: $ProjectDirAbs"
# --- Configuration ---
$DefaultParentDir = Join-Path $ProjectDirAbs "client_libs"
$AllLangs = @("python", "php", "ruby", "java", "dotnet")
# Helper to get repo config
function Get-RepoConfig {
param([string]$Lang)
switch ($Lang) {
"python" { return @{ Name = "google-ads-python"; Url = "https://github.com/googleads/google-ads-python.git" } }
"php" { return @{ Name = "google-ads-php"; Url = "https://github.com/googleads/google-ads-php.git" } }
"ruby" { return @{ Name = "google-ads-ruby"; Url = "https://github.com/googleads/google-ads-ruby.git" } }
"java" { return @{ Name = "google-ads-java"; Url = "https://github.com/googleads/google-ads-java.git" } }
"dotnet" { return @{ Name = "google-ads-dotnet"; Url = "https://github.com/googleads/google-ads-dotnet.git" } }
}
}
# --- Defaults ---
$Python = $true
$AnySelected = $false
if ($Php -or $Ruby -or $Java -or $Dotnet) {
$AnySelected = $true
}
# If no specific languages selected, default to Python only
if (-not $AnySelected) {
Write-Host "No additional languages selected. Defaulting to Python only."
}
# --- Dependency Check ---
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
Write-Error "ERROR: git is not installed. Please install it to continue."
exit 1
}
# --- Path Resolution and Validation ---
Write-Host "Ensuring default library directory exists: $DefaultParentDir"
if (-not (Test-Path -LiteralPath $DefaultParentDir)) {
New-Item -ItemType Directory -Force -LiteralPath $DefaultParentDir | Out-Null
}
# Helper to check if enabled
function Test-Enabled {
param([string]$Lang)
switch ($Lang) {
"python" { return $Python }
"php" { return $Php }
"ruby" { return $Ruby }
"java" { return $Java }
"dotnet" { return $Dotnet }
default { return $false }
}
}
$LibPaths = @{}
foreach ($Lang in $AllLangs) {
if (Test-Enabled -Lang $Lang) {
$Config = Get-RepoConfig -Lang $Lang
$RepoPath = Join-Path $DefaultParentDir $Config.Name
$LibPaths[$Lang] = $RepoPath
}
}
# --- Clone/Update Repositories ---
foreach ($Lang in $AllLangs) {
if (Test-Enabled -Lang $Lang) {
$Config = Get-RepoConfig -Lang $Lang
$RepoPath = $LibPaths[$Lang]
$RepoUrl = $Config.Url
Write-Host "Managing repository $($Config.Name) in $RepoPath"
if (Test-Path -LiteralPath (Join-Path $RepoPath ".git")) {
Write-Host "Directory $RepoPath already exists. Updating..."
Push-Location $RepoPath
try {
git pull
if ($LASTEXITCODE -eq 0) {
Write-Host "Successfully updated $($Config.Name)."
} else {
Write-Warning "Failed to update $($Config.Name). Continuing..."
}
}
finally {
Pop-Location
}
}
elseif (Test-Path -LiteralPath $RepoPath) {
Write-Warning "Directory $RepoPath exists but is not a git repo. Skipping."
}
else {
Write-Host "Cloning $RepoUrl into $RepoPath"
git clone $RepoUrl $RepoPath
if ($LASTEXITCODE -ne 0) {
Write-Error "ERROR: Failed to clone $RepoUrl"
exit 1
}
Write-Host "Successfully cloned $($Config.Name)."
}
}
}
# --- Modify settings.json ---
$SettingsFile = Join-Path $ProjectDirAbs ".gemini\settings.json"
if (-not (Test-Path -LiteralPath $SettingsFile)) {
Write-Error "ERROR: Settings file not found: $SettingsFile"
exit 1
}
Write-Host "Updating $SettingsFile with context paths..."
$ContextPathExamples = Join-Path $ProjectDirAbs "api_examples"
$ContextPathSaved = Join-Path $ProjectDirAbs "saved/code"
try {
$SettingsJson = Get-Content -LiteralPath $SettingsFile -Raw | ConvertFrom-Json
# Initialize array with default paths
$NewPaths = @($ContextPathExamples, $ContextPathSaved)
# Add enabled lib paths
foreach ($Lang in $AllLangs) {
if (Test-Enabled -Lang $Lang) {
$NewPaths += $LibPaths[$Lang]
}
}
# Update the object
if (-not $SettingsJson.context) {
$SettingsJson | Add-Member -MemberType NoteProperty -Name "context" -Value @{}
}
# Note: If context is a PSCustomObject, we can just assign.
if (-not $SettingsJson.context.PSObject.Properties["includeDirectories"]) {
$SettingsJson.context | Add-Member -MemberType NoteProperty -Name "includeDirectories" -Value $NewPaths
} else {
$SettingsJson.context.includeDirectories = $NewPaths
}
# Save back to file
$SettingsJson | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath $SettingsFile -Encoding UTF8
Write-Host "Successfully updated $SettingsFile"
Write-Host "New contents of context.includeDirectories:"
Write-Host ($SettingsJson.context.includeDirectories | Out-String)
Write-Host "Registering Google Ads API Developer Assistant as a Gemini extension..."
if (Get-Command gemini -ErrorAction SilentlyContinue) {
try {
$InstallOutput = "Y" | & gemini extensions install https://github.com/googleads/google-ads-api-developer-assistant.git 2>&1 | Out-String
if ($LASTEXITCODE -ne 0) {
if ($InstallOutput -match "already installed") {
Write-Host "Extension already installed. Reinstalling..."
gemini extensions uninstall "google-ads-api-developer-assistant" 2>&1 | Out-Null
$InstallOutput = "Y" | & gemini extensions install https://github.com/googleads/google-ads-api-developer-assistant.git 2>&1 | Out-String
} else {
Write-Warning $InstallOutput
Write-Warning "Failed to register extension automatically. You may need to run 'gemini extensions install https://github.com/googleads/google-ads-api-developer-assistant.git' manually."
}
} else {
Write-Host $InstallOutput
}
}
catch {
Write-Warning "An unexpected error occurred during extension registration: $_"
}
} else {
Write-Warning "'gemini' command not found. Skipping extension registration."
}
}
catch {
Write-Error "ERROR: Failed to update settings file: $_"
exit 1
}
Write-Host "Installation complete."
Write-Host ""
Write-Host "IMPORTANT: You must manually configure a development environment for each language you wish to use."
Write-Host " (e.g., run 'pip install google-ads' for Python, run 'composer install' for PHP, etc.)"