@hxp-tools/codedna 0.0.0-stage → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,133 @@
1
+ #!/usr/bin/env python3
2
+ """Wire CODEDNA.md into common coding-agent instruction files."""
3
+
4
+ import argparse
5
+ import json
6
+ import re
7
+ from pathlib import Path
8
+
9
+ START = "<!-- codedna:start -->"
10
+ END = "<!-- codedna:end -->"
11
+
12
+ PLAIN_BLOCK = """<!-- codedna:start -->
13
+ ## Code and prose style
14
+
15
+ When writing or editing code or repository prose, match the conventions in [CODEDNA.md](CODEDNA.md): naming, formatting, comment and message voice, structure, and idioms, so new contributions match the existing work. Before finishing, self-check against the "AI tells" section of that file.
16
+ <!-- codedna:end -->
17
+ """
18
+
19
+ CURSOR_FRONTMATTER = """---
20
+ description: Apply the codedna style profile for this repository.
21
+ alwaysApply: true
22
+ ---
23
+
24
+ """
25
+
26
+ CASCADE_FRONTMATTER = """---
27
+ trigger: always_on
28
+ ---
29
+
30
+ """
31
+
32
+ PLAIN_TARGETS = {
33
+ "agents": Path("AGENTS.md"),
34
+ "claude": Path("CLAUDE.md"),
35
+ "gemini": Path("GEMINI.md"),
36
+ "copilot": Path(".github/copilot-instructions.md"),
37
+ }
38
+
39
+ ALL_TARGETS = ["agents", "claude", "gemini", "copilot", "cursor", "cascade"]
40
+
41
+
42
+ def block_pattern(start_marker, end_marker):
43
+ inner = "(?:(?!" + re.escape(start_marker) + ").)*?"
44
+ return re.compile(re.escape(start_marker) + inner + re.escape(end_marker), re.S)
45
+
46
+
47
+ def replace_block(text, block=PLAIN_BLOCK, start_marker=START, end_marker=END):
48
+ match = block_pattern(start_marker, end_marker).search(text)
49
+ if match:
50
+ head = text[: match.start()].rstrip()
51
+ tail = text[match.end():].lstrip("\n")
52
+ updated = (head + "\n\n" if head else "") + block.rstrip() + "\n"
53
+ if tail:
54
+ updated += "\n" + tail
55
+ return updated.rstrip() + "\n"
56
+ if text.strip():
57
+ return text.rstrip() + "\n\n" + block
58
+ return block
59
+
60
+
61
+ def write_target(path, body, prefix="", start_marker=START, end_marker=END):
62
+ existed = path.exists()
63
+ text = path.read_text(encoding="utf-8") if existed else ""
64
+ if text.strip():
65
+ updated = replace_block(text, body, start_marker, end_marker)
66
+ else:
67
+ updated = prefix + body
68
+ if updated == text:
69
+ return "unchanged"
70
+ path.parent.mkdir(parents=True, exist_ok=True)
71
+ path.write_text(updated, encoding="utf-8")
72
+ return "updated" if existed else "created"
73
+
74
+
75
+ def cascade_path(root):
76
+ devin = root / ".devin/rules"
77
+ windsurf = root / ".windsurf/rules"
78
+ if windsurf.exists() and not devin.exists():
79
+ return windsurf / "codedna.md"
80
+ return devin / "codedna.md"
81
+
82
+
83
+ def target_path(root, target):
84
+ if target in PLAIN_TARGETS:
85
+ return root / PLAIN_TARGETS[target], ""
86
+ if target == "cursor":
87
+ return root / ".cursor/rules/codedna.mdc", CURSOR_FRONTMATTER
88
+ if target == "cascade":
89
+ return cascade_path(root), CASCADE_FRONTMATTER
90
+ raise ValueError("unknown target: %s" % target)
91
+
92
+
93
+ def existing_targets(root):
94
+ targets = ["agents"]
95
+ for name, rel in PLAIN_TARGETS.items():
96
+ if name != "agents" and (root / rel).exists():
97
+ targets.append(name)
98
+ if (root / ".cursor/rules").exists():
99
+ targets.append("cursor")
100
+ if (root / ".devin/rules").exists() or (root / ".windsurf/rules").exists():
101
+ targets.append("cascade")
102
+ return targets
103
+
104
+
105
+ def wire(root, targets=None, all_targets=False):
106
+ root = Path(root)
107
+ selected = ALL_TARGETS if all_targets else (targets or existing_targets(root))
108
+ results = []
109
+ for target in dict.fromkeys(selected):
110
+ path, prefix = target_path(root, target)
111
+ action = write_target(path, PLAIN_BLOCK, prefix, START, END)
112
+ results.append({"target": target, "path": str(path), "action": action})
113
+ return results
114
+
115
+
116
+ def main(argv=None):
117
+ parser = argparse.ArgumentParser(description="Wire CODEDNA.md into agent instruction files.")
118
+ parser.add_argument("repo", nargs="?", default=".")
119
+ parser.add_argument("--agent", action="append", choices=ALL_TARGETS, help="agent target to create or update")
120
+ parser.add_argument("--all", action="store_true", help="create or update every supported target")
121
+ parser.add_argument("--json", action="store_true", help="emit JSON")
122
+ args = parser.parse_args(argv)
123
+
124
+ results = wire(args.repo, targets=args.agent, all_targets=args.all)
125
+ if args.json:
126
+ print(json.dumps(results, indent=2, sort_keys=True))
127
+ else:
128
+ for item in results:
129
+ print("%s %s: %s" % (item["action"], item["target"], item["path"]))
130
+
131
+
132
+ if __name__ == "__main__":
133
+ main()