|
| 1 | +"""Cline IDE integration.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import re |
| 6 | +from pathlib import Path |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from ..base import MarkdownIntegration |
| 10 | +from ..manifest import IntegrationManifest |
| 11 | + |
| 12 | + |
| 13 | +# Note injected into hook sections so Cline maps dot-notation command |
| 14 | +# names (from extensions.yml) to the hyphenated slash commands it uses. |
| 15 | +_HOOK_COMMAND_NOTE = ( |
| 16 | + "- When constructing slash commands from hook command names, " |
| 17 | + "replace dots (`.`) with hyphens (`-`). " |
| 18 | + "For example, `speckit.git.commit` → `/speckit-git-commit`.\n" |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def format_cline_command_name(cmd_name: str) -> str: |
| 23 | + """Convert command name to Cline-compatible hyphenated format. |
| 24 | +
|
| 25 | + Cline handles slash-commands optimally when they use hyphens instead of dots. |
| 26 | + This function converts dot-notation command names to hyphenated format. |
| 27 | +
|
| 28 | + The function is idempotent: already-formatted names are returned unchanged. |
| 29 | +
|
| 30 | + Examples: |
| 31 | + >>> format_cline_command_name("plan") |
| 32 | + 'speckit-plan' |
| 33 | + >>> format_cline_command_name("speckit.plan") |
| 34 | + 'speckit-plan' |
| 35 | + >>> format_cline_command_name("speckit.git.commit") |
| 36 | + 'speckit-git-commit' |
| 37 | +
|
| 38 | + Args: |
| 39 | + cmd_name: Command name in dot notation (speckit.foo.bar), |
| 40 | + hyphenated format (speckit-foo-bar), or plain name (foo) |
| 41 | +
|
| 42 | + Returns: |
| 43 | + Hyphenated command name with 'speckit-' prefix |
| 44 | + """ |
| 45 | + cmd_name = cmd_name.replace(".", "-") |
| 46 | + |
| 47 | + if not cmd_name.startswith("speckit-"): |
| 48 | + cmd_name = f"speckit-{cmd_name}" |
| 49 | + |
| 50 | + return cmd_name |
| 51 | + |
| 52 | + |
| 53 | +class ClineIntegration(MarkdownIntegration): |
| 54 | + """Integration for Cline IDE.""" |
| 55 | + |
| 56 | + key = "cline" |
| 57 | + config = { |
| 58 | + "name": "Cline", |
| 59 | + "folder": ".clinerules/", |
| 60 | + "commands_subdir": "workflows", |
| 61 | + "install_url": "https://github.com/cline/cline", |
| 62 | + "requires_cli": False, |
| 63 | + } |
| 64 | + registrar_config = { |
| 65 | + "dir": ".clinerules/workflows", |
| 66 | + "format": "markdown", |
| 67 | + "args": "$ARGUMENTS", |
| 68 | + "extension": ".md", |
| 69 | + "inject_name": True, |
| 70 | + "format_name": format_cline_command_name, |
| 71 | + "invoke_separator": "-", |
| 72 | + } |
| 73 | + context_file = ".clinerules/specify-rules.md" |
| 74 | + invoke_separator = "-" |
| 75 | + multi_install_safe = True |
| 76 | + |
| 77 | + def command_filename(self, template_name: str) -> str: |
| 78 | + """Cline uses hyphenated filenames (e.g. speckit-git-commit.md).""" |
| 79 | + return format_cline_command_name(template_name) + ".md" |
| 80 | + |
| 81 | + def process_template(self, *args, **kwargs): |
| 82 | + """Ensure shared templates render Cline command references with hyphens.""" |
| 83 | + kwargs.setdefault("invoke_separator", self.invoke_separator) |
| 84 | + return super().process_template(*args, **kwargs) |
| 85 | + |
| 86 | + @staticmethod |
| 87 | + def _inject_hook_command_note(content: str) -> str: |
| 88 | + """Insert a dot-to-hyphen note before each hook output instruction. |
| 89 | +
|
| 90 | + Targets the line ``- For each executable hook, output the following`` |
| 91 | + and inserts the note on the line before it, matching its indentation. |
| 92 | + Skips if the note is already present. |
| 93 | + """ |
| 94 | + if "replace dots" in content: |
| 95 | + return content |
| 96 | + |
| 97 | + def repl(m: re.Match[str]) -> str: |
| 98 | + indent = m.group(1) |
| 99 | + instruction = m.group(2) |
| 100 | + eol = m.group(3) |
| 101 | + return ( |
| 102 | + indent |
| 103 | + + _HOOK_COMMAND_NOTE.rstrip("\n") |
| 104 | + + eol |
| 105 | + + indent |
| 106 | + + instruction |
| 107 | + + eol |
| 108 | + ) |
| 109 | + |
| 110 | + return re.sub( |
| 111 | + r"(?m)^(\s*)(- For each executable hook, output the following[^\r\n]*)(\r\n|\n|$)", |
| 112 | + repl, |
| 113 | + content, |
| 114 | + ) |
| 115 | + |
| 116 | + @staticmethod |
| 117 | + def _rewrite_handoff_references(content: str) -> str: |
| 118 | + """Replace dot-notation agent references in handoffs with hyphens.""" |
| 119 | + return re.sub( |
| 120 | + r"(?m)^(\s*agent:\s*)(speckit\.[A-Za-z0-9-_]+(?:\.[A-Za-z0-9-_]+)*)", |
| 121 | + lambda m: f"{m.group(1)}{format_cline_command_name(m.group(2))}", |
| 122 | + content, |
| 123 | + ) |
| 124 | + |
| 125 | + def post_process_content(self, content: str) -> str: |
| 126 | + """Apply Cline-specific transformations to command content.""" |
| 127 | + updated = self._inject_hook_command_note(content) |
| 128 | + updated = self._rewrite_handoff_references(updated) |
| 129 | + return updated |
| 130 | + |
| 131 | + def setup( |
| 132 | + self, |
| 133 | + project_root: Path, |
| 134 | + manifest: IntegrationManifest, |
| 135 | + parsed_options: dict[str, Any] | None = None, |
| 136 | + **opts: Any, |
| 137 | + ) -> list[Path]: |
| 138 | + """Install Cline commands and apply post-processing transformations.""" |
| 139 | + created = super().setup(project_root, manifest, parsed_options, **opts) |
| 140 | + |
| 141 | + # Post-process generated command files |
| 142 | + dest_dir = self.commands_dest(project_root).resolve() |
| 143 | + |
| 144 | + for path in created: |
| 145 | + # Only touch .md files under the commands directory |
| 146 | + try: |
| 147 | + path.resolve().relative_to(dest_dir) |
| 148 | + except ValueError: |
| 149 | + continue |
| 150 | + if path.suffix != ".md": |
| 151 | + continue |
| 152 | + |
| 153 | + content_bytes = path.read_bytes() |
| 154 | + content = content_bytes.decode("utf-8") |
| 155 | + |
| 156 | + updated = self.post_process_content(content) |
| 157 | + |
| 158 | + if updated != content: |
| 159 | + path.write_bytes(updated.encode("utf-8")) |
| 160 | + self.record_file_in_manifest(path, project_root, manifest) |
| 161 | + |
| 162 | + return created |
0 commit comments