Skip to content

Commit 8734edc

Browse files
committed
chore(regen): automate ClientOptions patch reapply
1 parent d9cd1b1 commit 8734edc

3 files changed

Lines changed: 114 additions & 3 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env python3
2+
3+
import json
4+
import re
5+
import sys
6+
from pathlib import Path
7+
8+
9+
CLIENT_OPTIONS_PATH = Path("src/main/java/com/deepgram/core/ClientOptions.java")
10+
CLIENT_OPTIONS_BAK_PATH = Path("src/main/java/com/deepgram/core/ClientOptions.java.bak")
11+
BUILD_GRADLE_PATH = Path("build.gradle")
12+
METADATA_PATH = Path(".fern/metadata.json")
13+
SDK_NAME = "com.deepgram:deepgram-java-sdk"
14+
15+
16+
def load_sdk_version(text: str) -> str:
17+
if CLIENT_OPTIONS_BAK_PATH.exists():
18+
bak_text = CLIENT_OPTIONS_BAK_PATH.read_text()
19+
match = re.search(r'put\("X-Fern-SDK-Version",\s*"([^"]+)"\)', bak_text)
20+
if match:
21+
return match.group(1)
22+
23+
match = re.search(r'put\("User-Agent",\s*"[^"]*/([^"]+)"\)', bak_text)
24+
if match:
25+
return match.group(1)
26+
27+
if BUILD_GRADLE_PATH.exists():
28+
build_gradle = BUILD_GRADLE_PATH.read_text()
29+
match = re.search(r"^version\s*=\s*'([^']+)'", build_gradle, re.MULTILINE)
30+
if match:
31+
return match.group(1)
32+
33+
match = re.search(r'put\("X-Fern-SDK-Version",\s*"([^"]+)"\)', text)
34+
if match:
35+
return match.group(1)
36+
37+
match = re.search(r'put\("User-Agent",\s*"[^"]*/([^"]+)"\)', text)
38+
if match:
39+
return match.group(1)
40+
41+
if METADATA_PATH.exists():
42+
metadata = json.loads(METADATA_PATH.read_text())
43+
sdk_version = metadata.get("sdkVersion")
44+
if sdk_version:
45+
return sdk_version
46+
47+
raise RuntimeError("Unable to determine SDK version for ClientOptions.java")
48+
49+
50+
def replace_header_line(
51+
text: str, key: str, value: str, add_release_please: bool
52+
) -> str:
53+
pattern = re.compile(
54+
rf'^(?P<indent>\s*)put\("{re.escape(key)}",\s*"[^"]*"\);(?:\s*// x-release-please-version)?\s*$',
55+
re.MULTILINE,
56+
)
57+
58+
def repl(match: re.Match[str]) -> str:
59+
suffix = " // x-release-please-version" if add_release_please else ""
60+
return f'{match.group("indent")}put("{key}", "{value}");{suffix}'
61+
62+
updated_text, count = pattern.subn(repl, text, count=1)
63+
if count != 1:
64+
raise RuntimeError(f"Unable to locate header line for {key}")
65+
return updated_text
66+
67+
68+
def main() -> int:
69+
if not CLIENT_OPTIONS_PATH.exists():
70+
raise RuntimeError(f"File not found: {CLIENT_OPTIONS_PATH}")
71+
72+
original_text = CLIENT_OPTIONS_PATH.read_text()
73+
sdk_version = load_sdk_version(original_text)
74+
75+
updated_text = original_text
76+
updated_text = replace_header_line(
77+
updated_text,
78+
"User-Agent",
79+
f"{SDK_NAME}/{sdk_version}",
80+
add_release_please=True,
81+
)
82+
updated_text = replace_header_line(
83+
updated_text,
84+
"X-Fern-SDK-Name",
85+
SDK_NAME,
86+
add_release_please=False,
87+
)
88+
updated_text = replace_header_line(
89+
updated_text,
90+
"X-Fern-SDK-Version",
91+
sdk_version,
92+
add_release_please=True,
93+
)
94+
95+
if updated_text != original_text:
96+
CLIENT_OPTIONS_PATH.write_text(updated_text)
97+
print(f"Updated {CLIENT_OPTIONS_PATH} to {SDK_NAME}/{sdk_version}")
98+
else:
99+
print(
100+
f"{CLIENT_OPTIONS_PATH} already has the expected Deepgram header constants"
101+
)
102+
103+
return 0
104+
105+
106+
if __name__ == "__main__":
107+
try:
108+
raise SystemExit(main())
109+
except Exception as exc:
110+
print(f"Error: {exc}", file=sys.stderr)
111+
raise SystemExit(1)

.claude/skills/review-regen.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Read AGENTS.md for full context on the regeneration workflow and freeze classifi
1414
- Patches still needed (must be re-applied)
1515
- New changes from the generator worth noting
1616
5. Wait for user direction on which patches to re-apply.
17-
6. Re-apply confirmed patches to the generated files.
17+
6. Re-apply confirmed patches to the generated files. If `src/main/java/com/deepgram/core/ClientOptions.java` was regenerated, run `python3 .claude/scripts/fix_clientoptions.py` to restore the Deepgram SDK header constants and `// x-release-please-version` markers.
1818
7. In `.fernignore`, replace each `.bak` path back to the original path for files that still need patches.
1919
8. Remove `.fernignore` entries entirely for files where patches are no longer needed.
2020
9. Delete all `.bak` files.

AGENTS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ How to identify:
4747

4848
Current temporarily frozen files:
4949

50-
- `src/main/java/com/deepgram/core/ClientOptions.java` - preserves release-please version markers and correct SDK header constants that Fern currently overwrites
50+
- `src/main/java/com/deepgram/core/ClientOptions.java` - preserves release-please version markers and correct SDK header constants that Fern currently overwrites; re-apply with `python3 .claude/scripts/fix_clientoptions.py` during review regen
5151

5252
### Prepare repo for regeneration
5353

@@ -66,7 +66,7 @@ Current temporarily frozen files:
6666
The `.bak` files are our manually patched versions protected by `.fernignore`. The original paths now contain the freshly generated versions. By comparing the two, we can see what the generator now produces versus what we had patched.
6767

6868
1. Diff each `.bak` file against the new generated version to understand what changed and whether our patches are still needed.
69-
2. Re-apply any patches that are still necessary to the newly generated files.
69+
2. Re-apply any patches that are still necessary to the newly generated files. For `src/main/java/com/deepgram/core/ClientOptions.java`, run `python3 .claude/scripts/fix_clientoptions.py` to restore the Deepgram SDK header constants and `// x-release-please-version` markers.
7070
3. In `.fernignore`, replace each `.bak` path back to the original path for files that still need patches.
7171
4. Remove `.fernignore` entries entirely for any files where the generator now produces correct output.
7272
5. Delete all `.bak` files once review is complete.

0 commit comments

Comments
 (0)