@@ -35,18 +35,97 @@ Write-Host "Detected project root: $ProjectDirAbs"
3535
3636# --- Update Assistant Repo ---
3737Write-Host " Updating google-ads-api-developer-assistant..."
38+
39+ $SettingsFile = Join-Path $ProjectDirAbs " .gemini\settings.json"
40+ $TempSettingsFile = [System.IO.Path ]::GetTempFileName()
41+
3842try {
43+ # 1. Backup existing settings if they exist
44+ if (Test-Path - LiteralPath $SettingsFile ) {
45+ Write-Host " Backing up $SettingsFile ..."
46+ Copy-Item - LiteralPath $SettingsFile - Destination $TempSettingsFile - Force
47+
48+ # 2. Reset local changes to settings.json to allow git pull
49+ # Check if file is tracked by git
50+ $GitStatus = git ls- files -- error- unmatch $SettingsFile 2> $null
51+ if ($LASTEXITCODE -eq 0 ) {
52+ Write-Host " Resetting $SettingsFile to avoid merge conflicts..."
53+ git checkout $SettingsFile
54+ }
55+ }
56+
57+ # 3. Update Repo
3958 git pull
4059 if ($LASTEXITCODE -ne 0 ) {
41- Write-Error " ERROR: Failed to update google-ads-api-developer-assistant."
42- exit 1
60+ throw " Failed to update google-ads-api-developer-assistant."
4361 }
4462 Write-Host " Successfully updated google-ads-api-developer-assistant."
63+
64+ # 4. Restore/Merge settings
65+ if ((Test-Path - LiteralPath $TempSettingsFile ) -and (Get-Item $TempSettingsFile ).Length -gt 0 ) {
66+ Write-Host " Merging preserved settings with new defaults..."
67+
68+ # Read contents
69+ $UserContent = Get-Content - LiteralPath $TempSettingsFile - Raw | ConvertFrom-Json
70+ $RepoContent = Get-Content - LiteralPath $SettingsFile - Raw | ConvertFrom-Json
71+
72+ # Merge Logic: User overrides Repo
73+ # Helper function for recursive merge could go here, but for now we do specific top-level merge
74+ # replicating jq * behavior for simple objects.
75+ # Actually, let's just use strict property copy from User to Repo for top-level keys
76+ # If deeply nested merge is needed, valid for context.includeDirectories?
77+ # Usually settings.json is flat or 1-level deep.
78+
79+ # Simple Merge: Add/Overwrite properties from User to Repo object
80+ foreach ($Prop in $UserContent.PSObject.Properties ) {
81+ if ($Prop.Name -eq " context" ) {
82+ # Special handling for context if needed, or just overwrite?
83+ # jq * merges recursively.
84+ # Let's try to merge context if both have it.
85+ if ($RepoContent.PSObject.Properties [" context" ]) {
86+ foreach ($CtxProp in $Prop.Value.PSObject.Properties ) {
87+ # e.g. includeDirectories
88+ if (-not $RepoContent.context.PSObject.Properties [$CtxProp.Name ]) {
89+ $RepoContent.context | Add-Member - MemberType NoteProperty - Name $CtxProp.Name - Value $CtxProp.Value
90+ } else {
91+ $RepoContent.context .$ ($CtxProp.Name ) = $CtxProp.Value
92+ }
93+ }
94+ } else {
95+ $RepoContent | Add-Member - MemberType NoteProperty - Name " context" - Value $Prop.Value
96+ }
97+ } else {
98+ if (-not $RepoContent.PSObject.Properties [$Prop.Name ]) {
99+ $RepoContent | Add-Member - MemberType NoteProperty - Name $Prop.Name - Value $Prop.Value
100+ } else {
101+ $RepoContent .$ ($Prop.Name ) = $Prop.Value
102+ }
103+ }
104+ }
105+
106+ # Save merged
107+ $RepoContent | ConvertTo-Json - Depth 10 | Set-Content - LiteralPath $SettingsFile - Encoding UTF8
108+ Write-Host " Settings restored and merged successfully."
109+ }
110+
45111}
46112catch {
47- Write-Error " ERROR: Failed to update google-ads-api-developer-assistant: $_ "
113+ Write-Error " ERROR: $_ "
114+ # Restore backup if pull failed or something went wrong involving the file
115+ if ((Test-Path - LiteralPath $TempSettingsFile ) -and (Get-Item $TempSettingsFile ).Length -gt 0 ) {
116+ if (-not (Test-Path - LiteralPath $SettingsFile ) -or (Get-Item $SettingsFile ).Length -eq 0 ) {
117+ Write-Host " Restoring original settings after failure..."
118+ Copy-Item - LiteralPath $TempSettingsFile - Destination $SettingsFile - Force
119+ }
120+ }
48121 exit 1
49122}
123+ finally {
124+ if (Test-Path - LiteralPath $TempSettingsFile ) {
125+ Remove-Item - LiteralPath $TempSettingsFile - Force - ErrorAction SilentlyContinue
126+ }
127+ }
128+
50129
51130# --- Locate and Update Client Libraries ---
52131$SettingsFile = Join-Path $ProjectDirAbs " .gemini\settings.json"
0 commit comments