|
72 | 72 | ) |
73 | 73 |
|
74 | 74 |
|
| 75 | +def _replace_url_in_text(text: str, old_url: str, new_url: str) -> str: |
| 76 | + """ |
| 77 | + Replace old_url with new_url in .ps1 text. |
| 78 | +
|
| 79 | + If new_url contains a PowerShell subexpression ``$(...)`` and the URL |
| 80 | + was wrapped in single quotes, re-quote that token with double quotes so |
| 81 | + the subexpression actually expands at runtime. Single-quoted strings in |
| 82 | + PowerShell are literal — ``$()`` is never evaluated inside them. |
| 83 | + """ |
| 84 | + if "$(" not in new_url: |
| 85 | + return text.replace(old_url, new_url) |
| 86 | + # Single-quoted token: 'https://...' → "$(Split-Path ...)\files\foo.exe" |
| 87 | + text = text.replace(f"'{old_url}'", f'"{new_url}"') |
| 88 | + # Unquoted or already double-quoted occurrences |
| 89 | + text = text.replace(old_url, new_url) |
| 90 | + return text |
| 91 | + |
| 92 | + |
75 | 93 | # ── Helpers ─────────────────────────────────────────────────────────────────── |
76 | 94 |
|
77 | 95 |
|
@@ -195,7 +213,7 @@ def _rewrite_ps1( |
195 | 213 | if mappings: |
196 | 214 | new_text = text |
197 | 215 | for m in mappings: |
198 | | - new_text = new_text.replace(m["old_url"], m["new_url"]) |
| 216 | + new_text = _replace_url_in_text(new_text, m["old_url"], m["new_url"]) |
199 | 217 | if new_text != text: |
200 | 218 | ps1.write_text(new_text, encoding="utf-8") |
201 | 219 | vlog(f"Rewrote {len(mappings)} URL(s) in {ps1.name} [{mode}]") |
@@ -328,7 +346,7 @@ def build_nupkg( |
328 | 346 | # Rewrite URLs |
329 | 347 | text = ps1.read_text(encoding="utf-8", errors="replace") |
330 | 348 | for m in ps1_maps: |
331 | | - text = text.replace(m["old_url"], m["new_url"]) |
| 349 | + text = _replace_url_in_text(text, m["old_url"], m["new_url"]) |
332 | 350 | ps1.write_text(text, encoding="utf-8") |
333 | 351 | # Embed installer files (internalize only) |
334 | 352 | if mode == "internalize": |
|
0 commit comments