7 7 import importlib.util
15 16 from pathlib
import Path
16 17 from types
import ModuleType
373 374 def pi_generate(prompt:
str, model:
str | None = None, label:
str = "agent") ->
str:
375 + pi_bin
= shutil.which(
"pi")
or shutil.which(
"pi.cmd")
377 + raise RuntimeError(
"pi executable not found on PATH")
378 383 "--no-extensions",
385 390 cmd.extend([
"--model", model])
393 + tempfile.TemporaryDirectory(prefix
="pi-ext-prompt-") as prompt_dir,
389 394 tempfile.TemporaryFile(
"w+", encoding
="utf-8") as out,
390 395 tempfile.TemporaryFile(
"w+", encoding
="utf-8") as err_out,
392 - proc
= subprocess.Popen(cmd, cwd
=ROOT, text
=True, stdout
=out, stderr
=err_out)
397 + prompt_path
= Path(prompt_dir)
/ "prompt.md" 398 + prompt_path.write_text(prompt, encoding
="utf-8")
399 + proc
= subprocess.Popen(
400 + [
*cmd,
f"@{prompt_path}
"],
393 406 frames
= "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏" 394 407 start
= time.monotonic()
461 474 return out.rstrip()
+ "\n" 477 +def wrap_generated_markdown(text:
str, width:
int = 80) ->
str:
478 + lines:
list[
str]
= []
480 + for raw_line
in text.replace(
"\r\n",
"\n").replace(
"\r",
"\n").split(
"\n"):
481 + line
= raw_line.rstrip()
482 + stripped
= line.lstrip()
483 + if stripped.startswith(
"```")
or stripped.startswith(
"~~~"):
484 + in_fence
= not in_fence
487 + if in_fence
or not line
or stripped.startswith((
"#",
"<!--",
"!["))
or "|" in line:
491 + bullet
= re.match(
r"^(\s*[-*]\s+)(.+)$", line)
493 + prefix, body
= bullet.groups()
498 + initial_indent
=prefix,
499 + subsequent_indent
=" " * len(prefix),
500 + break_long_words
=False,
501 + break_on_hyphens
=False,
506 + numbered
= re.match(
r"^(\s*\d+[.)]\s+)(.+)$", line)
508 + prefix, body
= numbered.groups()
513 + initial_indent
=prefix,
514 + subsequent_indent
=" " * len(prefix),
515 + break_long_words
=False,
516 + break_on_hyphens
=False,
521 + if line.startswith(
" "):
529 + break_long_words
=False,
530 + break_on_hyphens
=False,
533 + return "\n".join(lines).rstrip()
+ "\n" 536 +def write_repo_text(path: Path, text:
str) ->
None:
537 + path.write_text(wrap_generated_markdown(text), encoding
="utf-8", newline
="\n")
464 540 def update_changelog(ext: Path, model:
str | None = None) ->
str:
465 541 path
= ext
/ "CHANGELOG.md" 466 542 entry
= generate_changelog_entry(ext, model)
467 543 heading
= f"## {root_version()}
" 468 544 if not path.exists():
469 - path.write_text(
f"# Changelog\n\n{entry}
")
545 + write_repo_text(path,
f"# Changelog\n\n{entry}
")
470 546 print(
f"OK changelog {ext.name}
")
478 554 (i
for i
in range(start
+ 1,
len(lines))
if lines[i].startswith(
"## ")),
len(lines)
480 - path.write_text(
"".join(lines[:start])
+ entry
+ "\n" + "".join(lines[end:]).lstrip(
"\n"))
558 + "".join(lines[:start])
+ entry
+ "\n" + "".join(lines[end:]).lstrip(
"\n"),
481 560 elif text.startswith(
"# Changelog"):
482 561 _, rest
= text.split(
"\n",
1)
483 - path.write_text(
"# Changelog\n\n" + entry
+ "\n" + rest.lstrip(
"\n"))
562 + write_repo_text(path,
"# Changelog\n\n" + entry
+ "\n" + rest.lstrip(
"\n"))
485 - path.write_text(
f"# Changelog\n\n{entry}
\n{text}
")
564 + write_repo_text(path,
f"# Changelog\n\n{entry}
\n{text}
")
486 565 print(
f"OK changelog {ext.name}
")
525 604 path
= ROOT
/ "CHANGELOG.md" 526 605 heading
= f"## {root_version()}
" 527 606 if not path.exists():
528 - path.write_text(
f"# Changelog\n\n{entry}
")
607 + write_repo_text(path,
f"# Changelog\n\n{entry}
")
529 608 print(
"OK changelog root")
531 610 text
= path.read_text()
536 615 (i
for i
in range(start
+ 1,
len(lines))
if lines[i].startswith(
"## ")),
len(lines)
538 - path.write_text(
"".join(lines[:start])
+ entry
+ "\n" + "".join(lines[end:]).lstrip(
"\n"))
619 + "".join(lines[:start])
+ entry
+ "\n" + "".join(lines[end:]).lstrip(
"\n"),
539 621 elif text.startswith(
"# Changelog"):
540 622 _, rest
= text.split(
"\n",
1)
541 - path.write_text(
"# Changelog\n\n" + entry
+ "\n" + rest.lstrip(
"\n"))
623 + write_repo_text(path,
"# Changelog\n\n" + entry
+ "\n" + rest.lstrip(
"\n"))
543 - path.write_text(
f"# Changelog\n\n{entry}
\n{text}
")
625 + write_repo_text(path,
f"# Changelog\n\n{entry}
\n{text}
")
544 626 print(
"OK changelog root")
586 668 def update_readme(model:
str | None = None) ->
str:
587 669 text
= generate_readme(model)
588 - (ROOT
/ "README.md").write_text(text)
670 + write_repo_text(ROOT
/ "README.md", text)
589 671 print(
"OK README.md")
635 717 def update_ext_readme(ext: Path, model:
str | None = None) ->
str:
636 718 text
= generate_ext_readme(ext, model)
637 - (ext
/ "README.md").write_text(text)
719 + write_repo_text(ext
/ "README.md", text)
638 720 print(
f"OK README {ext.name}
")
869 +def release_tag() ->
str:
870 + return f"v{root_version()}
" 873 +def tag_exists(tag:
str) ->
bool:
874 + return git(
"rev-parse",
"--verify",
"--quiet",
f"refs/tags/{tag}
", check
=False).returncode
== 0 787 877 def create_tag() ->
str:
788 - tag
= f"v{root_version()}
" 790 - git(
"rev-parse",
"--verify",
"--quiet",
f"refs/tags/{tag}
", check
=False).returncode
== 0 878 + tag
= release_tag()
879 + exists
= tag_exists(tag)
793 881 print(
f"OK tag {tag}
exists")
805 -def commit_release(paths_to_add:
list[Path]) ->
None:
893 +def commit_release(paths_to_add:
list[Path]) ->
bool:
806 894 assert_only_release_changes(paths_to_add)
808 896 if jj(
"diff",
"--quiet", check
=False).returncode
== 0:
809 897 print(
"OK commit none")
811 899 jj(
"commit",
"-m",
f"release: v{root_version()}
")
812 900 bookmark
= release_bookmark()
813 901 jj(
"bookmark",
"set", bookmark,
"-r",
"@-")
814 902 print(
f"OK commit v{root_version()}
({bookmark}
)")
817 905 paths
= sorted({
str(path.relative_to(ROOT))
for path
in paths_to_add
if path.exists()})
818 906 git(
"add",
*paths)
819 907 if git(
"diff",
"--cached",
"--quiet",
"--",
*paths, check
=False).returncode
== 0:
820 908 print(
"OK commit none")
822 910 git(
"commit",
"-m",
f"release: v{root_version()}
",
"--",
*paths)
823 911 print(
f"OK commit v{root_version()}
")
826 915 def push_release(tag:
str) ->
None:
907 -def ensure_ci() ->
None:
908 - if os.environ.get(
"PI_EXT_CI_DONE")
== "1":
910 - subprocess.run([
"mise",
"run",
"ci"], cwd
=ROOT, check
=True)
911 - os.environ[
"PI_EXT_CI_DONE"]
= "1" 915 - selected:
list[
str],
*, dry_run:
bool, model:
str | None, skip_demo:
bool, no_push:
bool 918 - dirs
= ext_dirs(selected)
921 - demos
= demos_to_update(installed_drivers_for(dirs))
922 - print(
"DRY RUN publish")
923 - print(
f"version: v{root_version()}
")
924 - print(
"demos: " + (
", ".join(name
for name, _targets
in demos)
if demos
else "none"))
926 - print(generate_readme(model).rstrip())
927 - print(
"repo changelog:")
928 - print(generate_repo_changelog_entry(dirs, model).rstrip())
930 - print(generate_changelog_entry(ext, model).rstrip())
931 - print(
"skip commit/tag/push")
934 - demo_paths
= []
if skip_demo
else update_demos(dirs)
935 - update_all_readmes(dirs, model)
936 - update_repo_changelog(dirs, model)
938 - update_changelog(ext, model)
996 +def release_paths(dirs:
list[Path]) ->
list[Path]:
998 + EXT_ROOT
/ target
for _driver, targets
in installed_drivers_for(dirs)
for target
in targets
944 1004 ROOT
/ "README.md",
945 1005 ROOT
/ "CHANGELOG.md",
946 1006 ROOT
/ "package.json",
947 1007 ROOT
/ "package-lock.json",
1012 +def release_markdown_paths(dirs:
list[Path]) ->
list[Path]:
1013 + paths
= [ROOT
/ "README.md", ROOT
/ "CHANGELOG.md"]
1015 + paths.extend([ext
/ "README.md", ext
/ "CHANGELOG.md"])
1016 + return [path
for path
in paths
if path.exists()]
1019 +def format_release_artifacts(dirs:
list[Path]) ->
None:
1020 + dprint
= shutil.which(
"dprint")
or shutil.which(
"dprint.cmd")
1022 + raise SystemExit(
"dprint not found; cannot format release artifacts")
1023 + paths
= [
str(path.relative_to(ROOT))
for path
in release_markdown_paths(dirs)]
1025 + subprocess.run([dprint,
"fmt",
*paths], cwd
=ROOT, check
=True)
1028 +def cmd_release_plan(selected:
list[
str], model:
str | None) ->
int:
1029 + dirs
= ext_dirs(selected)
1030 + demos
= demos_to_update(installed_drivers_for(dirs))
1031 + print(
"RELEASE PLAN")
1032 + print(
f"version: v{root_version()}
")
1033 + print(
"demos: " + (
", ".join(name
for name, _targets
in demos)
if demos
else "none"))
1035 + print(generate_readme(model).rstrip())
1036 + print(
"repo changelog:")
1037 + print(generate_repo_changelog_entry(dirs, model).rstrip())
1039 + print(generate_changelog_entry(ext, model).rstrip())
1040 + print(
"skip write/commit/tag/push")
1044 +def cmd_release_prepare(selected:
list[
str], model:
str | None, skip_demo:
bool) ->
int:
1045 + dirs
= ext_dirs(selected)
1047 + update_demos(dirs)
1048 + update_all_readmes(dirs, model)
1049 + update_repo_changelog(dirs, model)
1051 + update_changelog(ext, model)
1052 + format_release_artifacts(dirs)
1053 + print(
f"OK release prepared v{root_version()}
")
1057 +def cmd_publish(selected:
list[
str]) ->
int:
1058 + dirs
= ext_dirs(selected)
1059 + tag
= release_tag()
1060 + committed
= commit_release(release_paths(dirs))
1061 + if not committed
and not tag_exists(tag):
1063 + "no prepared release changes to publish; run mise run release-prepare first" 950 1065 tag
= create_tag()
952 - print(
"OK push skipped")
974 1086 readme_p.add_argument(
"--write", action
="store_true")
975 1087 demo_p
= sub.add_parser(
"demo")
976 1088 demo_p.add_argument(
"drivers", nargs
="*")
1089 + release_plan_p
= sub.add_parser(
"release-plan")
1090 + release_plan_p.add_argument(
"extensions", nargs
="*")
1091 + release_plan_p.add_argument(
"--model")
1092 + release_prepare_p
= sub.add_parser(
"release-prepare")
1093 + release_prepare_p.add_argument(
"extensions", nargs
="*")
1094 + release_prepare_p.add_argument(
"--model")
1095 + release_prepare_p.add_argument(
"--skip-demo", action
="store_true")
977 1096 publish_p
= sub.add_parser(
"publish")
978 1097 publish_p.add_argument(
"extensions", nargs
="*")
979 - publish_p.add_argument(
"--dry-run", action
="store_true")
980 - publish_p.add_argument(
"--model")
981 - publish_p.add_argument(
"--skip-demo", action
="store_true")
982 - publish_p.add_argument(
"--no-push", action
="store_true")
983 1098 args
= parser.parse_args()
985 1100 if args.command
== "list":
994 1109 return cmd_readme(args.extensions, args.model, args.write)
995 1110 if args.command
== "demo":
996 1111 return cmd_demo(args.drivers)
1112 + if args.command
== "release-plan":
1113 + return cmd_release_plan(args.extensions, args.model)
1114 + if args.command
== "release-prepare":
1115 + return cmd_release_prepare(args.extensions, args.model, args.skip_demo)
997 1116 if args.command
== "publish":
998 - return cmd_publish(
1000 - dry_run
=args.dry_run,
1002 - skip_demo
=args.skip_demo,
1003 - no_push
=args.no_push,
1117 + return cmd_publish(args.extensions)
1005 1118 raise AssertionError(args.command)