@buildr-ai/buildr 0.1.0-rc.1
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.
- package/LICENSE +21 -0
- package/README.md +15 -0
- package/docs/cli-architecture.md +62 -0
- package/docs/cli-reference.md +67 -0
- package/docs/known-limitations.md +13 -0
- package/examples/minimal-workspace/README.md +24 -0
- package/package/README.md +28 -0
- package/package/bootstrap/contract.yml +122 -0
- package/package/bootstrap/guide.md +79 -0
- package/package/manifest.yml +144 -0
- package/package/targets/runtime/skills/buildr/SKILL.md +151 -0
- package/package/targets/workspace/.buildr/workspace.yml +5 -0
- package/package/targets/workspace/AGENTS.md +5 -0
- package/package/targets/workspace/commands/buildr/openspec/manifest.yml +15 -0
- package/package/targets/workspace/commands/manifest.yml +2 -0
- package/package/targets/workspace/components/buildr/openspec/component.yml +16 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/openspec-apply-sidebar.md +3 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/openspec-archive-sidebar.md +3 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/openspec-explore-sidebar.md +3 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/openspec-propose-sidebar.md +3 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/openspec-sync-sidebar.md +3 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/task-finish-post-sync.md +1 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/task-finish-pre-sync.md +1 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/task-triage-change-ready.md +10 -0
- package/package/targets/workspace/components/manifest.yml +2 -0
- package/package/targets/workspace/gitignore +13 -0
- package/package/targets/workspace/projects/AGENTS.project.md +25 -0
- package/package/targets/workspace/projects/manifest.yml +2 -0
- package/package/targets/workspace/projects/services/manifest.yml +3 -0
- package/package/targets/workspace/projects/skills/manifest.yml +2 -0
- package/package/targets/workspace/rules/buildr/core.md +63 -0
- package/package/targets/workspace/rules/manifest.yml +9 -0
- package/package/targets/workspace/skills/buildr/git-ops/SKILL.md +73 -0
- package/package/targets/workspace/skills/buildr/openspec-contract-guard/SKILL.md +67 -0
- package/package/targets/workspace/skills/buildr/task-finish/SKILL.md +104 -0
- package/package/targets/workspace/skills/buildr/task-triage/SKILL.md +103 -0
- package/package/targets/workspace/skills/buildr/task-worktree/SKILL.md +104 -0
- package/package/targets/workspace/skills/manifest.yml +38 -0
- package/package/targets/workspace/skills/openspec/openspec-apply-change/SKILL.md +159 -0
- package/package/targets/workspace/skills/openspec/openspec-archive-change/SKILL.md +117 -0
- package/package/targets/workspace/skills/openspec/openspec-explore/SKILL.md +287 -0
- package/package/targets/workspace/skills/openspec/openspec-propose/SKILL.md +111 -0
- package/package/targets/workspace/skills/openspec/openspec-sync-specs/SKILL.md +147 -0
- package/package.json +67 -0
- package/tools/buildr +11 -0
- package/tools/cli/application/cli-update.mjs +219 -0
- package/tools/cli/application/compose-runtime.mjs +38 -0
- package/tools/cli/application/doctor/runtime-diagnostics.mjs +226 -0
- package/tools/cli/application/doctor/scope-diagnostics.mjs +440 -0
- package/tools/cli/application/doctor/service-diagnostics.mjs +184 -0
- package/tools/cli/application/doctor.mjs +201 -0
- package/tools/cli/application/package-maintenance/builtin-receipts.mjs +73 -0
- package/tools/cli/application/package-maintenance/output.mjs +148 -0
- package/tools/cli/application/package-maintenance/smoke-checks.mjs +469 -0
- package/tools/cli/application/package-maintenance/static-validation.mjs +678 -0
- package/tools/cli/application/package-maintenance.mjs +538 -0
- package/tools/cli/application/runtime.mjs +118 -0
- package/tools/cli/application/workspace-operations.mjs +310 -0
- package/tools/cli/bootstrap.mjs +6 -0
- package/tools/cli/command/help.mjs +292 -0
- package/tools/cli/command/registry.mjs +97 -0
- package/tools/cli/domains/commands.mjs +661 -0
- package/tools/cli/domains/components.mjs +922 -0
- package/tools/cli/domains/openspec.mjs +603 -0
- package/tools/cli/domains/package-assets.mjs +652 -0
- package/tools/cli/domains/rules.mjs +380 -0
- package/tools/cli/domains/runtime.mjs +94 -0
- package/tools/cli/domains/skills.mjs +647 -0
- package/tools/cli/domains/workspace.mjs +571 -0
- package/tools/cli/shared/infrastructure.mjs +411 -0
- package/tools/cli/shared/json-contracts.mjs +22 -0
- package/tools/cli/shared/platform.mjs +52 -0
- package/tools/runtime/adapter-contract.mjs +330 -0
- package/tools/runtime/check-claude-code.mjs +26 -0
- package/tools/runtime/check-codex.mjs +26 -0
- package/tools/runtime/projection.mjs +163 -0
- package/tools/runtime/render-claude-code-rules.mjs +459 -0
- package/tools/runtime/render-claude-code.mjs +59 -0
- package/tools/runtime/skills/arguments.mjs +51 -0
- package/tools/runtime/skills/contributions.mjs +171 -0
- package/tools/runtime/skills/manifests.mjs +167 -0
- package/tools/runtime/skills/primitives.mjs +86 -0
- package/tools/runtime/skills/render-plan.mjs +244 -0
- package/tools/runtime/skills/sources.mjs +276 -0
- package/tools/shared/fetch-remote-text.mjs +78 -0
- package/tools/verification/integrity/managed-data-integrity.mjs +107 -0
- package/tools/verification/integrity/managed-mutations.mjs +72 -0
- package/tools/verification/openspec/contract.mjs +212 -0
- package/tools/verification/runtime/adapter-contract.mjs +102 -0
- package/tools/verification/runtime/adapter-parity.mjs +99 -0
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import process from 'node:process';
|
|
6
|
+
import { assertRuntimeTargetPath } from './adapter-contract.mjs';
|
|
7
|
+
|
|
8
|
+
const MANAGED_PREFIX = '<!-- Generated by Buildr.';
|
|
9
|
+
const MANAGED_BLOCK_START_PREFIX = '<!-- BEGIN Buildr managed Claude Code rules bridge';
|
|
10
|
+
const MANAGED_BLOCK_END = '<!-- END Buildr managed Claude Code rules bridge -->';
|
|
11
|
+
const RULE_DISCOVERY_EXCLUDED_DIRECTORIES = new Set([
|
|
12
|
+
'.git',
|
|
13
|
+
'.hg',
|
|
14
|
+
'.svn',
|
|
15
|
+
'.agents',
|
|
16
|
+
'.claude',
|
|
17
|
+
'.codex',
|
|
18
|
+
'.cursor',
|
|
19
|
+
'.trae',
|
|
20
|
+
'.worktrees',
|
|
21
|
+
'node_modules',
|
|
22
|
+
'vendor',
|
|
23
|
+
'dist',
|
|
24
|
+
'build',
|
|
25
|
+
'coverage',
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
function usage(command = 'buildr rules render claude-code') {
|
|
29
|
+
console.error(`Usage: ${command} --scope <.|projects/project[/services/service[/path...]]> --target <dir>`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function parseRenderClaudeCodeRulesArgs(argv, command) {
|
|
33
|
+
const args = {};
|
|
34
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
35
|
+
const arg = argv[i];
|
|
36
|
+
if (arg === '--scope' || arg === '--target') {
|
|
37
|
+
const value = argv[i + 1];
|
|
38
|
+
if (!value || value.startsWith('--')) {
|
|
39
|
+
throw new Error(`Missing value for ${arg}`);
|
|
40
|
+
}
|
|
41
|
+
args[arg.slice(2)] = value;
|
|
42
|
+
i += 1;
|
|
43
|
+
} else {
|
|
44
|
+
throw new Error(`Unknown argument: ${arg}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (!args.scope || !args.target) {
|
|
48
|
+
usage(command);
|
|
49
|
+
throw new Error('Missing required arguments');
|
|
50
|
+
}
|
|
51
|
+
return args;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function normalizeRelativePath(input) {
|
|
55
|
+
if (typeof input !== 'string' || !input.trim()) throw new Error('Scope must be a workspace-relative path.');
|
|
56
|
+
const normalized = path.normalize(input.trim()).replace(/^\.\//, '') || '.';
|
|
57
|
+
if (path.isAbsolute(normalized) || normalized === '..' || normalized.startsWith(`..${path.sep}`)) {
|
|
58
|
+
throw new Error(`Scope must stay inside the workspace: ${input}`);
|
|
59
|
+
}
|
|
60
|
+
return normalized;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function ensureDirectory(dir, message) {
|
|
64
|
+
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
|
|
65
|
+
throw new Error(message);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function ensureScopeStaysInsideWorkspace(repoRoot, parts, scopeInput) {
|
|
70
|
+
let current = repoRoot;
|
|
71
|
+
for (const part of parts) {
|
|
72
|
+
if (part === '.') continue;
|
|
73
|
+
current = path.join(current, part);
|
|
74
|
+
if (fs.existsSync(current) && fs.lstatSync(current).isSymbolicLink()) {
|
|
75
|
+
throw new Error(`Scope must not traverse a symbolic link: ${scopeInput}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const workspaceRealPath = fs.realpathSync(repoRoot);
|
|
79
|
+
const scopeRealPath = fs.realpathSync(current);
|
|
80
|
+
const relative = path.relative(workspaceRealPath, scopeRealPath);
|
|
81
|
+
if (relative === '..' || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
|
|
82
|
+
throw new Error(`Scope must stay inside the workspace: ${scopeInput}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function resolveRuleScope(repoRoot, scopeInput) {
|
|
87
|
+
const requestedScope = normalizeRelativePath(scopeInput);
|
|
88
|
+
let scope = requestedScope;
|
|
89
|
+
let parts = scope === '.' ? ['.'] : scope.split(path.sep);
|
|
90
|
+
const warnings = [];
|
|
91
|
+
const organizationRoot = repoRoot;
|
|
92
|
+
let projectRoot = null;
|
|
93
|
+
let serviceRoot = null;
|
|
94
|
+
let scopeRoot;
|
|
95
|
+
|
|
96
|
+
if (parts[0] === '.') {
|
|
97
|
+
scopeRoot = repoRoot;
|
|
98
|
+
} else if (parts[0] === 'projects') {
|
|
99
|
+
if (parts.length < 2 || !parts[1]) throw new Error(`Project scope must start with projects/<project>: ${scopeInput}`);
|
|
100
|
+
projectRoot = path.join(repoRoot, 'projects', parts[1]);
|
|
101
|
+
ensureDirectory(projectRoot, `Project scope does not exist: ${toPosixRelative(repoRoot, projectRoot)}`);
|
|
102
|
+
|
|
103
|
+
if (parts.length > 2 && parts[2] !== 'services') {
|
|
104
|
+
const registeredServices = readRegistryIds(path.join(projectRoot, 'services', 'manifest.yml'), 'services');
|
|
105
|
+
const legacyService = parts[2];
|
|
106
|
+
if (registeredServices.has(legacyService)) {
|
|
107
|
+
const literalRoot = path.join(repoRoot, ...parts);
|
|
108
|
+
const canonicalParts = ['projects', parts[1], 'services', ...parts.slice(2)];
|
|
109
|
+
const canonicalRoot = path.join(repoRoot, ...canonicalParts);
|
|
110
|
+
const literalExists = isDirectory(literalRoot);
|
|
111
|
+
const canonicalExists = isDirectory(canonicalRoot);
|
|
112
|
+
if (literalExists && canonicalExists) {
|
|
113
|
+
throw new Error(`Ambiguous legacy Service scope: ${scopeInput}. Use canonical workspace path ${canonicalParts.join('/')}.`);
|
|
114
|
+
}
|
|
115
|
+
if (!literalExists) {
|
|
116
|
+
scope = canonicalParts.join(path.sep);
|
|
117
|
+
parts = canonicalParts;
|
|
118
|
+
warnings.push(`旧 Service scope ${requestedScope} 已兼容解析为 ${toPosixPath(scope)};请改用 canonical workspace 相对路径。`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
scopeRoot = path.join(repoRoot, ...parts);
|
|
124
|
+
if (parts[2] === 'services' && parts[3]) {
|
|
125
|
+
serviceRoot = path.join(projectRoot, 'services', parts[3]);
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
throw new Error(`Unsupported scope. Use . or a real workspace path under projects/<project>: ${scopeInput}`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
for (const part of parts) {
|
|
132
|
+
if (RULE_DISCOVERY_EXCLUDED_DIRECTORIES.has(part)) {
|
|
133
|
+
throw new Error(`Scope enters an excluded directory: ${scopeInput}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
ensureDirectory(scopeRoot, `Scope directory does not exist: ${toPosixRelative(repoRoot, scopeRoot)}`);
|
|
137
|
+
ensureScopeStaysInsideWorkspace(repoRoot, parts, scopeInput);
|
|
138
|
+
|
|
139
|
+
return {
|
|
140
|
+
scope: toPosixPath(scope),
|
|
141
|
+
requestedScope: toPosixPath(requestedScope),
|
|
142
|
+
organizationRoot,
|
|
143
|
+
projectRoot,
|
|
144
|
+
serviceRoot,
|
|
145
|
+
scopeRoot,
|
|
146
|
+
warnings,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function toPosixPath(value) {
|
|
151
|
+
return value.split(path.sep).join('/');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function isDirectory(directory) {
|
|
155
|
+
return fs.existsSync(directory) && fs.statSync(directory).isDirectory();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function isFile(file) {
|
|
159
|
+
return fs.existsSync(file) && fs.statSync(file).isFile();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function readRegistryIds(file, collectionKey) {
|
|
163
|
+
const ids = new Set();
|
|
164
|
+
if (!isFile(file)) return ids;
|
|
165
|
+
let inCollection = false;
|
|
166
|
+
for (const rawLine of fs.readFileSync(file, 'utf8').split(/\r?\n/)) {
|
|
167
|
+
if (!inCollection) {
|
|
168
|
+
if (new RegExp(`^${collectionKey}:\\s*$`).test(rawLine)) inCollection = true;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (/^[^\s]/.test(rawLine)) break;
|
|
172
|
+
const match = rawLine.match(/^ ([A-Za-z0-9._-]+):(?:\s.*)?$/);
|
|
173
|
+
if (match) ids.add(match[1]);
|
|
174
|
+
}
|
|
175
|
+
return ids;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function managedNestedGitRoots(repoRoot) {
|
|
179
|
+
const roots = new Set();
|
|
180
|
+
const projectIds = readRegistryIds(path.join(repoRoot, 'projects', 'manifest.yml'), 'projects');
|
|
181
|
+
for (const projectId of projectIds) {
|
|
182
|
+
const projectRoot = path.join(repoRoot, 'projects', projectId);
|
|
183
|
+
roots.add(path.resolve(projectRoot));
|
|
184
|
+
const serviceIds = readRegistryIds(path.join(projectRoot, 'services', 'manifest.yml'), 'services');
|
|
185
|
+
for (const serviceId of serviceIds) {
|
|
186
|
+
roots.add(path.resolve(projectRoot, 'services', serviceId));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return roots;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function discoveryBoundary(repoRoot, directory, reason) {
|
|
193
|
+
return { path: toPosixRelative(repoRoot, directory), reason };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function discoverDescendantFiles(repoRoot, scopeInfo, basename) {
|
|
197
|
+
const files = [];
|
|
198
|
+
const boundaries = [];
|
|
199
|
+
const managedGitRoots = managedNestedGitRoots(repoRoot);
|
|
200
|
+
|
|
201
|
+
function walk(directory) {
|
|
202
|
+
if (isFile(path.join(directory, basename))) files.push(path.join(directory, basename));
|
|
203
|
+
const entries = fs.readdirSync(directory, { withFileTypes: true })
|
|
204
|
+
.sort((left, right) => left.name.localeCompare(right.name));
|
|
205
|
+
for (const entry of entries) {
|
|
206
|
+
if (!entry.isDirectory() && !entry.isSymbolicLink()) continue;
|
|
207
|
+
const child = path.join(directory, entry.name);
|
|
208
|
+
if (entry.isSymbolicLink()) {
|
|
209
|
+
boundaries.push(discoveryBoundary(repoRoot, child, 'symbolic-link'));
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (RULE_DISCOVERY_EXCLUDED_DIRECTORIES.has(entry.name)) {
|
|
213
|
+
boundaries.push(discoveryBoundary(repoRoot, child, 'excluded-directory'));
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (fs.existsSync(path.join(child, '.git')) && !managedGitRoots.has(path.resolve(child))) {
|
|
217
|
+
boundaries.push(discoveryBoundary(repoRoot, child, 'unregistered-nested-git-repository'));
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
walk(child);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
walk(scopeInfo.scopeRoot);
|
|
225
|
+
return { files, boundaries };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function ancestorDirectories(repoRoot, scopeRoot) {
|
|
229
|
+
const relative = path.relative(repoRoot, scopeRoot);
|
|
230
|
+
const directories = [repoRoot];
|
|
231
|
+
if (!relative) return directories;
|
|
232
|
+
let current = repoRoot;
|
|
233
|
+
for (const segment of relative.split(path.sep)) {
|
|
234
|
+
current = path.join(current, segment);
|
|
235
|
+
directories.push(current);
|
|
236
|
+
}
|
|
237
|
+
return directories;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function buildRuleDiscoveryPlan(repoRoot, scopeInfo) {
|
|
241
|
+
const sources = new Set();
|
|
242
|
+
for (const directory of ancestorDirectories(repoRoot, scopeInfo.scopeRoot)) {
|
|
243
|
+
const source = path.join(directory, 'AGENTS.md');
|
|
244
|
+
if (isFile(source)) sources.add(source);
|
|
245
|
+
}
|
|
246
|
+
const descendants = discoverDescendantFiles(repoRoot, scopeInfo, 'AGENTS.md');
|
|
247
|
+
for (const source of descendants.files) sources.add(source);
|
|
248
|
+
const files = [...sources].sort((left, right) => {
|
|
249
|
+
const leftRelative = path.relative(repoRoot, left);
|
|
250
|
+
const rightRelative = path.relative(repoRoot, right);
|
|
251
|
+
const depthDifference = leftRelative.split(path.sep).length - rightRelative.split(path.sep).length;
|
|
252
|
+
return depthDifference || toPosixPath(leftRelative).localeCompare(toPosixPath(rightRelative));
|
|
253
|
+
});
|
|
254
|
+
return {
|
|
255
|
+
scope: scopeInfo.scope,
|
|
256
|
+
requestedScope: scopeInfo.requestedScope,
|
|
257
|
+
files,
|
|
258
|
+
boundaries: descendants.boundaries,
|
|
259
|
+
warnings: [...scopeInfo.warnings],
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export function resolveAgentFiles(repoRoot, scopeInfo) {
|
|
264
|
+
return buildRuleDiscoveryPlan(repoRoot, scopeInfo).files;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function discoverManagedOrphanRuleBridges(repoRoot, scopeInfo) {
|
|
268
|
+
const descendants = discoverDescendantFiles(repoRoot, scopeInfo, 'CLAUDE.md');
|
|
269
|
+
const files = [];
|
|
270
|
+
for (const targetFile of descendants.files) {
|
|
271
|
+
if (isFile(path.join(path.dirname(targetFile), 'AGENTS.md'))) continue;
|
|
272
|
+
const content = fs.readFileSync(targetFile, 'utf8');
|
|
273
|
+
if (hasManagedRulesMarker(content)) files.push(targetFile);
|
|
274
|
+
}
|
|
275
|
+
return { files, boundaries: descendants.boundaries };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export function hasManagedRulesMarker(content) {
|
|
279
|
+
const firstLine = content.split(/\r?\n/)[0] ?? '';
|
|
280
|
+
return content.includes(MANAGED_BLOCK_START_PREFIX) || firstLine.startsWith(MANAGED_PREFIX) || content.includes('此文件为 Claude Code 桥接文件。');
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function buildConflictMessage(targetFile) {
|
|
284
|
+
return [
|
|
285
|
+
`CLAUDE.md already exists and is not Buildr-managed: ${targetFile}`,
|
|
286
|
+
'Choose a merge plan:',
|
|
287
|
+
'1. Add a Buildr managed block to this file, then rerun this command.',
|
|
288
|
+
'2. Move long-term rules into the relevant Buildr AGENTS.md, then rerun this command.',
|
|
289
|
+
'3. Keep Claude Code-only custom content outside the Buildr managed block.',
|
|
290
|
+
].join('\n');
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function toPosixRelative(from, to) {
|
|
294
|
+
const relative = path.relative(from, to) || path.basename(to);
|
|
295
|
+
return relative.split(path.sep).join('/');
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function buildRulesTarget(repoRoot, targetRoot, agentFile) {
|
|
299
|
+
return path.join(path.dirname(agentFile), 'CLAUDE.md');
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function buildClaudeManagedBlock(repoRoot, targetFile, agentFile) {
|
|
303
|
+
const lines = [
|
|
304
|
+
'<!-- BEGIN Buildr managed Claude Code rules bridge; type: reference; source: AGENTS.md -->',
|
|
305
|
+
'@AGENTS.md',
|
|
306
|
+
MANAGED_BLOCK_END,
|
|
307
|
+
'',
|
|
308
|
+
];
|
|
309
|
+
return lines.join('\n');
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export function buildClaudeBridge(repoRoot, targetFile, agentFile) {
|
|
313
|
+
return `# CLAUDE.md\n\n${buildClaudeManagedBlock(repoRoot, targetFile, agentFile)}`;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function replaceManagedBlock(existingContent, managedBlock) {
|
|
317
|
+
const start = existingContent.indexOf(MANAGED_BLOCK_START_PREFIX);
|
|
318
|
+
const end = existingContent.indexOf(MANAGED_BLOCK_END);
|
|
319
|
+
if (start !== -1 && end !== -1 && end > start) {
|
|
320
|
+
const afterEnd = end + MANAGED_BLOCK_END.length;
|
|
321
|
+
return `${existingContent.slice(0, start)}${managedBlock.trimEnd()}${existingContent.slice(afterEnd)}`;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const lines = existingContent.split(/\r?\n/);
|
|
325
|
+
const firstLine = lines[0] ?? '';
|
|
326
|
+
if (firstLine.startsWith(MANAGED_PREFIX) || existingContent.includes('此文件为 Claude Code 桥接文件。')) {
|
|
327
|
+
const hashLineIndex = lines.findIndex((line) => line.startsWith('<!-- Buildr source hash:'));
|
|
328
|
+
const customSuffix = hashLineIndex === -1 ? '' : lines.slice(hashLineIndex + 1).join('\n').trim();
|
|
329
|
+
return customSuffix ? `${managedBlock}\n${customSuffix}\n` : managedBlock;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return `${managedBlock}\n${existingContent}`;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function removeManagedBlock(existingContent) {
|
|
336
|
+
const start = existingContent.indexOf(MANAGED_BLOCK_START_PREFIX);
|
|
337
|
+
const end = existingContent.indexOf(MANAGED_BLOCK_END);
|
|
338
|
+
if (start === -1 || end === -1 || end <= start) return null;
|
|
339
|
+
const afterEnd = end + MANAGED_BLOCK_END.length;
|
|
340
|
+
const content = `${existingContent.slice(0, start)}${existingContent.slice(afterEnd)}`
|
|
341
|
+
.replace(/^# CLAUDE\.md\s*$/m, '')
|
|
342
|
+
.trim();
|
|
343
|
+
return content ? `${content}\n` : '';
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
export function planRulesRender(repoRoot, targetRoot, scopeInfo) {
|
|
347
|
+
const discovery = buildRuleDiscoveryPlan(repoRoot, scopeInfo);
|
|
348
|
+
const agentFiles = discovery.files;
|
|
349
|
+
if (agentFiles.length === 0) {
|
|
350
|
+
throw new Error(`No AGENTS.md files found for scope: ${scopeInfo.scope}`);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const conflicts = [];
|
|
354
|
+
const actions = [];
|
|
355
|
+
for (const agentFile of agentFiles) {
|
|
356
|
+
const targetFile = buildRulesTarget(repoRoot, targetRoot, agentFile);
|
|
357
|
+
let existingContent = null;
|
|
358
|
+
if (fs.existsSync(targetFile)) {
|
|
359
|
+
existingContent = fs.readFileSync(targetFile, 'utf8');
|
|
360
|
+
if (!hasManagedRulesMarker(existingContent)) {
|
|
361
|
+
conflicts.push(buildConflictMessage(targetFile));
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const managedBlock = buildClaudeManagedBlock(repoRoot, targetFile, agentFile);
|
|
367
|
+
const content = existingContent !== null
|
|
368
|
+
? replaceManagedBlock(existingContent, managedBlock)
|
|
369
|
+
: buildClaudeBridge(repoRoot, targetFile, agentFile);
|
|
370
|
+
actions.push({
|
|
371
|
+
action: existingContent === null ? 'created' : existingContent === content ? 'unchanged' : 'updated',
|
|
372
|
+
sourceFile: agentFile,
|
|
373
|
+
targetFile,
|
|
374
|
+
content,
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const orphanDiscovery = discoverManagedOrphanRuleBridges(repoRoot, scopeInfo);
|
|
379
|
+
for (const targetFile of orphanDiscovery.files) {
|
|
380
|
+
const existingContent = fs.readFileSync(targetFile, 'utf8');
|
|
381
|
+
const preservedContent = removeManagedBlock(existingContent);
|
|
382
|
+
actions.push({
|
|
383
|
+
action: preservedContent ? 'updated' : 'removed',
|
|
384
|
+
sourceFile: null,
|
|
385
|
+
targetFile,
|
|
386
|
+
content: preservedContent || null,
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
return {
|
|
390
|
+
targetRoot,
|
|
391
|
+
actions,
|
|
392
|
+
conflicts,
|
|
393
|
+
discovery: {
|
|
394
|
+
...discovery,
|
|
395
|
+
boundaries: [...discovery.boundaries, ...orphanDiscovery.boundaries]
|
|
396
|
+
.filter((boundary, index, all) => all.findIndex((item) => item.path === boundary.path && item.reason === boundary.reason) === index),
|
|
397
|
+
},
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export function applyRulesRenderPlan(plan) {
|
|
402
|
+
if (plan.conflicts.length > 0) {
|
|
403
|
+
throw new Error(`Rules render found ${plan.conflicts.length} conflict(s); no files were changed.\n\n${plan.conflicts.join('\n\n')}`);
|
|
404
|
+
}
|
|
405
|
+
for (const item of plan.actions) assertRuntimeTargetPath(plan.targetRoot, item.targetFile, 'Runtime Rules target');
|
|
406
|
+
for (const item of plan.actions) {
|
|
407
|
+
if (item.action === 'unchanged') continue;
|
|
408
|
+
if (item.action === 'removed') {
|
|
409
|
+
fs.rmSync(item.targetFile);
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
fs.mkdirSync(path.dirname(item.targetFile), { recursive: true });
|
|
413
|
+
fs.writeFileSync(item.targetFile, item.content, 'utf8');
|
|
414
|
+
}
|
|
415
|
+
return plan;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function renderRules(repoRoot, targetRoot, scopeInfo) {
|
|
419
|
+
return applyRulesRenderPlan(planRulesRender(repoRoot, targetRoot, scopeInfo));
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
export function renderClaudeCodeRules(argv, options = {}) {
|
|
423
|
+
const repoRoot = options.repoRoot ?? process.cwd();
|
|
424
|
+
const command = options.command ?? 'buildr rules render claude-code';
|
|
425
|
+
const args = parseRenderClaudeCodeRulesArgs(argv, command);
|
|
426
|
+
const scopeInfo = resolveRuleScope(repoRoot, args.scope);
|
|
427
|
+
const targetRoot = path.resolve(repoRoot, args.target);
|
|
428
|
+
ensureDirectory(targetRoot, `Target directory does not exist: ${targetRoot}`);
|
|
429
|
+
|
|
430
|
+
const rendered = renderRules(repoRoot, targetRoot, scopeInfo);
|
|
431
|
+
return {
|
|
432
|
+
targetRoot,
|
|
433
|
+
scope: scopeInfo.scope,
|
|
434
|
+
scopeInfo,
|
|
435
|
+
files: rendered.actions.map((item) => item.targetFile),
|
|
436
|
+
actions: rendered.actions,
|
|
437
|
+
discovery: rendered.discovery,
|
|
438
|
+
warnings: rendered.discovery.warnings,
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function main() {
|
|
443
|
+
const { targetRoot, actions, warnings } = renderClaudeCodeRules(process.argv.slice(2), {
|
|
444
|
+
command: 'node tools/runtime/render-claude-code-rules.mjs',
|
|
445
|
+
});
|
|
446
|
+
for (const warning of warnings) console.error(`Warning: ${warning}`);
|
|
447
|
+
for (const item of actions) {
|
|
448
|
+
console.log(`${item.action} ${toPosixRelative(targetRoot, item.targetFile)}`);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
453
|
+
try {
|
|
454
|
+
main();
|
|
455
|
+
} catch (error) {
|
|
456
|
+
console.error(error.message);
|
|
457
|
+
process.exit(1);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
import { parseInstallClaudeCodeBuildrSkillArgs, parseRenderClaudeCodeArgs } from './skills/arguments.mjs';
|
|
6
|
+
import { ensureDirectory, resolveSkillScope } from './skills/primitives.mjs';
|
|
7
|
+
import { resolveSkillContributions } from './skills/contributions.mjs';
|
|
8
|
+
import { resolvePackageAgentSkill, resolveSkills } from './skills/sources.mjs';
|
|
9
|
+
import { applySkillRenderPlan, buildRuntimeSkillTarget, buildSkillRenderPlan, buildSkillTarget, buildSkillContent, buildAgentInstallPlanContent, buildAgentInstallPlanTarget, hasManagedSkillMarker, resolveRenderSkills } from './skills/render-plan.mjs';
|
|
10
|
+
|
|
11
|
+
export { parseInstallClaudeCodeBuildrSkillArgs, parseRenderClaudeCodeArgs } from './skills/arguments.mjs';
|
|
12
|
+
export { resolveSkillScope } from './skills/primitives.mjs';
|
|
13
|
+
export { resolveSkillContributions } from './skills/contributions.mjs';
|
|
14
|
+
export { resolvePackageAgentSkill, resolveSkills } from './skills/sources.mjs';
|
|
15
|
+
export { applySkillRenderPlan, buildRuntimeSkillTarget, buildSkillRenderPlan, buildSkillTarget, buildSkillContent, buildAgentInstallPlanContent, buildAgentInstallPlanTarget, hasManagedSkillMarker, resolveRenderSkills } from './skills/render-plan.mjs';
|
|
16
|
+
|
|
17
|
+
function renderSkill(repoRoot, targetRoot, skill, runtime = 'claude-code') {
|
|
18
|
+
return applySkillRenderPlan(buildSkillRenderPlan(repoRoot, targetRoot, [skill], runtime), targetRoot)[0]
|
|
19
|
+
?? (skill.installMode === 'agent' ? buildAgentInstallPlanTarget(targetRoot, skill) : buildRuntimeSkillTarget(targetRoot, skill, runtime));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function renderClaudeCode(argv, options = {}) {
|
|
23
|
+
const repoRoot = options.repoRoot ?? process.cwd();
|
|
24
|
+
const command = options.command ?? 'node tools/runtime/render-claude-code.mjs';
|
|
25
|
+
const args = parseRenderClaudeCodeArgs(argv, command);
|
|
26
|
+
const targetRoot = path.resolve(repoRoot, args.target);
|
|
27
|
+
ensureDirectory(targetRoot, `Target directory does not exist: ${targetRoot}`);
|
|
28
|
+
const skills = resolveRenderSkills(repoRoot, args.scope, 'claude-code');
|
|
29
|
+
const plan = buildSkillRenderPlan(repoRoot, targetRoot, skills, 'claude-code');
|
|
30
|
+
return { targetRoot, files: options.planOnly ? [] : applySkillRenderPlan(plan, targetRoot), plan };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function installClaudeCodeBuildrSkill(argv, options = {}) {
|
|
34
|
+
const repoRoot = options.repoRoot ?? process.cwd();
|
|
35
|
+
const command = options.command ?? 'node tools/runtime/render-claude-code.mjs install';
|
|
36
|
+
const args = parseInstallClaudeCodeBuildrSkillArgs(argv, command);
|
|
37
|
+
const targetRoot = path.resolve(repoRoot, args.target);
|
|
38
|
+
ensureDirectory(targetRoot, `Target directory does not exist: ${targetRoot}`);
|
|
39
|
+
|
|
40
|
+
const skill = resolvePackageAgentSkill('claude-code', 'buildr');
|
|
41
|
+
const files = [renderSkill(repoRoot, targetRoot, skill)];
|
|
42
|
+
return { targetRoot, files };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function main() {
|
|
46
|
+
const { targetRoot, files } = renderClaudeCode(process.argv.slice(2));
|
|
47
|
+
for (const file of files) {
|
|
48
|
+
console.log(path.relative(targetRoot, file).split(path.sep).join('/'));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
53
|
+
try {
|
|
54
|
+
main();
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error(error.message);
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
function usage(command = 'node tools/runtime/render-claude-code.mjs') {
|
|
2
|
+
console.error(`Usage: ${command} --scope <.|projects/project> --target <dir>`);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function installUsage(command = 'node tools/runtime/render-claude-code.mjs install') {
|
|
6
|
+
console.error(`Usage: ${command} --target <dir>`);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function parseRenderClaudeCodeArgs(argv, command) {
|
|
10
|
+
const args = {};
|
|
11
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
12
|
+
const arg = argv[i];
|
|
13
|
+
if (arg === '--scope' || arg === '--target') {
|
|
14
|
+
const value = argv[i + 1];
|
|
15
|
+
if (!value || value.startsWith('--')) {
|
|
16
|
+
throw new Error(`Missing value for ${arg}`);
|
|
17
|
+
}
|
|
18
|
+
args[arg.slice(2)] = value;
|
|
19
|
+
i += 1;
|
|
20
|
+
} else {
|
|
21
|
+
throw new Error(`Unknown argument: ${arg}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (!args.scope || !args.target) {
|
|
25
|
+
usage(command);
|
|
26
|
+
throw new Error('Missing required arguments');
|
|
27
|
+
}
|
|
28
|
+
return args;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function parseInstallClaudeCodeBuildrSkillArgs(argv, command) {
|
|
32
|
+
const args = {};
|
|
33
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
34
|
+
const arg = argv[i];
|
|
35
|
+
if (arg === '--target') {
|
|
36
|
+
const value = argv[i + 1];
|
|
37
|
+
if (!value || value.startsWith('--')) {
|
|
38
|
+
throw new Error(`Missing value for ${arg}`);
|
|
39
|
+
}
|
|
40
|
+
args[arg.slice(2)] = value;
|
|
41
|
+
i += 1;
|
|
42
|
+
} else {
|
|
43
|
+
throw new Error(`Unknown argument: ${arg}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (!args.target) {
|
|
47
|
+
installUsage(command);
|
|
48
|
+
throw new Error('Missing required arguments');
|
|
49
|
+
}
|
|
50
|
+
return args;
|
|
51
|
+
}
|