Skip to content

Commit 81a927c

Browse files
adityatapsclaude
andcommitted
feat(scripts): auto-patch Makefile and tf-all.sh when scaffolding new modules
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c8a50de commit 81a927c

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

scripts/create-aws-account.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,58 @@ if changed:
247247
open(path, 'w').write(content)
248248
PYEOF
249249

250+
# ── 3. Patch Makefile + tf-all.sh ─────────────────────────────────────────────
251+
python3 - "$REPO_ROOT/Makefile" "$REPO_ROOT/scripts/tf-all.sh" "$OU" "$ACCOUNT_NAME" <<'PYEOF'
252+
import sys, re
253+
254+
makefile_path, tf_all_path, ou, account_name = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
255+
256+
target = f"aws-{ou}-{account_name}"
257+
module_path = f"providers/aws/accounts/{ou}/{account_name}"
258+
259+
content = open(makefile_path).read()
260+
261+
if target in content:
262+
print(f"⚠ {target} already in Makefile — skipping")
263+
sys.exit(0)
264+
265+
# 1. Add to .PHONY (before the closing " ci-plan" line)
266+
content = content.replace(
267+
' ci-plan',
268+
f' {target} \\\n ci-plan'
269+
)
270+
271+
# 2. Add to github-sync prerequisites (before its recipe line)
272+
content = re.sub(
273+
r'(github-sync:.*?)(\n\t@\$\(RUN\) providers/github)',
274+
lambda m: m.group(1) + f' \\\n {target}' + m.group(2),
275+
content,
276+
flags=re.DOTALL
277+
)
278+
279+
# 3. Insert target block in Tier 3 section
280+
new_block = f'\n{target}: pagerduty aws-org github-init aws-management\n\t@$(RUN) {module_path}\n'
281+
content = content.replace('# ── Tier 2:', new_block + '# ── Tier 2:')
282+
283+
open(makefile_path, 'w').write(content)
284+
print(f"✓ Added {target} to Makefile")
285+
286+
# 4. Increment TOTAL_MODULES in tf-all.sh
287+
tf_all = open(tf_all_path).read()
288+
m = re.search(r'TOTAL_MODULES=(\d+)', tf_all)
289+
if m:
290+
old, new = int(m.group(1)), int(m.group(1)) + 1
291+
open(tf_all_path, 'w').write(tf_all.replace(f'TOTAL_MODULES={old}', f'TOTAL_MODULES={new}'))
292+
print(f"✓ Updated TOTAL_MODULES: {old} → {new}")
293+
PYEOF
294+
250295
# ── Done ───────────────────────────────────────────────────────────────────────
251296
echo ""
252297
echo "✓ '$ACCOUNT_NAME' scaffolded. Files changed:"
253298
echo " + providers/aws/accounts/$OU/$ACCOUNT_NAME/ (new)"
254299
echo " ~ providers/github/main.tf"
300+
echo " ~ Makefile"
301+
echo " ~ scripts/tf-all.sh"
255302
echo ""
256303
echo "Next steps:"
257304
echo ""

scripts/create-gcp-project.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,55 @@ labels = {
214214
EOF
215215
fi
216216

217+
# ── Patch Makefile + tf-all.sh ────────────────────────────────────────────────
218+
python3 - "$REPO_ROOT/Makefile" "$REPO_ROOT/scripts/tf-all.sh" "$FOLDER" "$PROJECT_ID" <<'PYEOF'
219+
import sys, re
220+
221+
makefile_path, tf_all_path, folder, project_id = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
222+
223+
target = f"gcp-{folder}-{project_id}"
224+
module_path = f"providers/gcp/projects/{folder}/{project_id}"
225+
226+
content = open(makefile_path).read()
227+
228+
if target in content:
229+
print(f"⚠ {target} already in Makefile — skipping")
230+
sys.exit(0)
231+
232+
# 1. Add to .PHONY (before the closing " ci-plan" line)
233+
content = content.replace(
234+
' ci-plan',
235+
f' {target} \\\n ci-plan'
236+
)
237+
238+
# 2. Add to github-sync prerequisites (before its recipe line)
239+
content = re.sub(
240+
r'(github-sync:.*?)(\n\t@\$\(RUN\) providers/github)',
241+
lambda m: m.group(1) + f' \\\n {target}' + m.group(2),
242+
content,
243+
flags=re.DOTALL
244+
)
245+
246+
# 3. Insert target block in Tier 3 section
247+
new_block = f'\n{target}: pagerduty gcp-org github-init gcp-management\n\t@$(RUN) {module_path}\n'
248+
content = content.replace('# ── Tier 2:', new_block + '# ── Tier 2:')
249+
250+
# 4. Append to ci-plan (GCP projects are accessible via GCP WIF auth in CI)
251+
content = content.rstrip('\n') + f'\n\t@$(RUN) {module_path}\n'
252+
253+
open(makefile_path, 'w').write(content)
254+
print(f"✓ Added {target} to Makefile")
255+
print(f"✓ Added {module_path} to ci-plan")
256+
257+
# 5. Increment TOTAL_MODULES in tf-all.sh
258+
tf_all = open(tf_all_path).read()
259+
m = re.search(r'TOTAL_MODULES=(\d+)', tf_all)
260+
if m:
261+
old, new = int(m.group(1)), int(m.group(1)) + 1
262+
open(tf_all_path, 'w').write(tf_all.replace(f'TOTAL_MODULES={old}', f'TOTAL_MODULES={new}'))
263+
print(f"✓ Updated TOTAL_MODULES: {old} → {new}")
264+
PYEOF
265+
217266
# ── Done ───────────────────────────────────────────────────────────────────────
218267
echo ""
219268
echo "✓ Project scaffolded at: providers/gcp/projects/$FOLDER/$PROJECT_ID/"

0 commit comments

Comments
 (0)