|
| 1 | +""" |
| 2 | +Claude Code namespaced tags |
| 3 | +
|
| 4 | +Helpers for getting Claude Code set up nicely with gopls |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import json |
| 10 | +import os |
| 11 | + |
| 12 | +from invoke import task |
| 13 | + |
| 14 | +from tasks.build_tags import build_tags, compute_config_build_tags |
| 15 | +from tasks.flavor import AgentFlavor |
| 16 | +from tasks.libs.types.version import Version |
| 17 | + |
| 18 | +CLAUDE_PLUGINS_DIR = ".claude-plugins" |
| 19 | +PLUGIN_NAME = "datadog-agent-gopls" |
| 20 | +PLUGIN_DIR = os.path.join(CLAUDE_PLUGINS_DIR, PLUGIN_NAME, ".claude-plugin") |
| 21 | +PLUGIN_FILE = "plugin.json" |
| 22 | +DEFAULT_VERSION = "1.0.0" |
| 23 | + |
| 24 | + |
| 25 | +def _read_existing_plugin() -> tuple[Version, str | None]: |
| 26 | + """Read the existing plugin.json if it exists, returning (version, build_flags_tag).""" |
| 27 | + fullpath = os.path.join(PLUGIN_DIR, PLUGIN_FILE) |
| 28 | + try: |
| 29 | + with open(fullpath) as f: |
| 30 | + plugin = json.load(f) |
| 31 | + except (FileNotFoundError, json.JSONDecodeError): |
| 32 | + return Version.from_tag(DEFAULT_VERSION), None |
| 33 | + version = Version.from_tag(plugin.get("version", DEFAULT_VERSION)) |
| 34 | + flags = plugin.get("lspServers", {}).get("gopls", {}).get("initializationOptions", {}).get("build.buildFlags", []) |
| 35 | + tags_flag = next((f for f in flags if f.startswith("-tags=")), None) |
| 36 | + return version, tags_flag |
| 37 | + |
| 38 | + |
| 39 | +@task( |
| 40 | + help={ |
| 41 | + "targets": f"Comma separated list of targets to include. Possible values: all, {', '.join(build_tags[AgentFlavor.base].keys())}. Default: all", |
| 42 | + "flavor": f"Agent flavor to use. Possible values: {', '.join(AgentFlavor.__members__.keys())}. Default: {AgentFlavor.base.name}", |
| 43 | + } |
| 44 | +) |
| 45 | +def set_buildtags( |
| 46 | + _: object, |
| 47 | + targets: str = "all", |
| 48 | + build_include: str | None = None, |
| 49 | + build_exclude: str | None = None, |
| 50 | + flavor: str = AgentFlavor.base.name, |
| 51 | +) -> None: |
| 52 | + """ |
| 53 | + Create/update Claude Code gopls plugin configuration with correct build tags |
| 54 | + """ |
| 55 | + use_tags = compute_config_build_tags( |
| 56 | + targets=targets, |
| 57 | + build_include=build_include, |
| 58 | + build_exclude=build_exclude, |
| 59 | + flavor=flavor, |
| 60 | + ) |
| 61 | + |
| 62 | + if not os.path.exists(PLUGIN_DIR): |
| 63 | + os.makedirs(PLUGIN_DIR) |
| 64 | + |
| 65 | + new_tags_flag = f"-tags={','.join(sorted(use_tags))}" |
| 66 | + version, old_tags_flag = _read_existing_plugin() |
| 67 | + if old_tags_flag is not None and old_tags_flag != new_tags_flag: |
| 68 | + version = version.next_version(bump_patch=True) |
| 69 | + |
| 70 | + plugin = { |
| 71 | + "name": PLUGIN_NAME, |
| 72 | + "version": str(version), |
| 73 | + "description": "Go LSP (gopls) pre-configured for the datadog-agent repository with build tags and performance flags", |
| 74 | + "author": { |
| 75 | + "name": "Lénaïc Huard", |
| 76 | + "email": "lenaic.huard@datadoghq.com", |
| 77 | + }, |
| 78 | + "lspServers": { |
| 79 | + "gopls": { |
| 80 | + "command": "gopls", |
| 81 | + "extensionToLanguage": { |
| 82 | + ".go": "go", |
| 83 | + }, |
| 84 | + "initializationOptions": { |
| 85 | + "build.buildFlags": [ |
| 86 | + new_tags_flag, |
| 87 | + "-buildvcs=false", |
| 88 | + ], |
| 89 | + "formatting.local": "github.com/DataDog/datadog-agent", |
| 90 | + }, |
| 91 | + } |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + fullpath = os.path.join(PLUGIN_DIR, PLUGIN_FILE) |
| 96 | + with open(fullpath, "w") as f: |
| 97 | + json.dump(plugin, f, indent=2) |
| 98 | + f.write("\n") |
0 commit comments