@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,661 @@
|
|
|
1
|
+
import { fs, path, process, spawnSync } from '../shared/platform.mjs';
|
|
2
|
+
import { PUBLIC_JSON_SCHEMAS, withJsonSchema } from '../shared/json-contracts.mjs';
|
|
3
|
+
|
|
4
|
+
export function registerDomainsCommands(runtime) {
|
|
5
|
+
const doctor = (...args) => runtime.doctor(...args);
|
|
6
|
+
const workspaceSymlinkSegment = (...args) => runtime.workspaceSymlinkSegment(...args);
|
|
7
|
+
const componentOwnerForMember = (...args) => runtime.componentOwnerForMember(...args);
|
|
8
|
+
const isValidAssetId = (...args) => runtime.isValidAssetId(...args);
|
|
9
|
+
const assertName = (...args) => runtime.assertName(...args);
|
|
10
|
+
const normalizeRelativePathForBuildr = (...args) => runtime.normalizeRelativePathForBuildr(...args);
|
|
11
|
+
const quoteYaml = (...args) => runtime.quoteYaml(...args);
|
|
12
|
+
const parseYamlValue = (...args) => runtime.parseYamlValue(...args);
|
|
13
|
+
const optionValue = (...args) => runtime.optionValue(...args);
|
|
14
|
+
const optionValueRaw = (...args) => runtime.optionValueRaw(...args);
|
|
15
|
+
const atomicWriteFile = (...args) => runtime.atomicWriteFile(...args);
|
|
16
|
+
const parseYamlDocument = (...args) => runtime.parseYamlDocument(...args);
|
|
17
|
+
const withWorkspaceMutation = (...args) => runtime.withWorkspaceMutation(...args);
|
|
18
|
+
const hasFlag = (...args) => runtime.hasFlag(...args);
|
|
19
|
+
const toPosixRelative = (...args) => runtime.toPosixRelative(...args);
|
|
20
|
+
const existsDirectory = (...args) => runtime.existsDirectory(...args);
|
|
21
|
+
const existsFile = (...args) => runtime.existsFile(...args);
|
|
22
|
+
const assertInitializedBuildrWorkspace = (...args) => runtime.assertInitializedBuildrWorkspace(...args);
|
|
23
|
+
|
|
24
|
+
function normalizeCommandCollection(collection) {
|
|
25
|
+
if (!collection) return null;
|
|
26
|
+
const normalized = normalizeRelativePathForBuildr(collection, `Command collection must stay inside commands/: ${collection}`)
|
|
27
|
+
.replace(/^commands\//, '')
|
|
28
|
+
.replace(/\/manifest\.yml$/, '');
|
|
29
|
+
if (!normalized || normalized === 'manifest.yml') throw new Error(`Command collection must name a nested collection: ${collection}`);
|
|
30
|
+
return normalized;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function commandsManifestPath(targetRoot, collection = null) {
|
|
34
|
+
const normalized = normalizeCommandCollection(collection);
|
|
35
|
+
return normalized
|
|
36
|
+
? path.join(targetRoot, 'commands', normalized, 'manifest.yml')
|
|
37
|
+
: path.join(targetRoot, 'commands', 'manifest.yml');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function assertSafeCommandCollectionTarget(targetRoot, manifestPath) {
|
|
41
|
+
const relative = toPosixRelative(targetRoot, manifestPath);
|
|
42
|
+
const symlink = workspaceSymlinkSegment(targetRoot, relative);
|
|
43
|
+
if (symlink) throw new Error(`Command collection path crosses a symbolic link: ${symlink}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function listCommandsManifestPaths(targetRoot) {
|
|
47
|
+
const root = path.join(targetRoot, 'commands');
|
|
48
|
+
if (!existsDirectory(root)) return [];
|
|
49
|
+
const files = [];
|
|
50
|
+
function visit(dir) {
|
|
51
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
52
|
+
const absolute = path.join(dir, entry.name);
|
|
53
|
+
if (entry.isSymbolicLink()) continue;
|
|
54
|
+
if (entry.isDirectory()) visit(absolute);
|
|
55
|
+
else if (entry.isFile() && entry.name === 'manifest.yml') files.push(absolute);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
visit(root);
|
|
59
|
+
return files.sort((left, right) => {
|
|
60
|
+
const rootManifest = commandsManifestPath(targetRoot);
|
|
61
|
+
if (left === rootManifest) return -1;
|
|
62
|
+
if (right === rootManifest) return 1;
|
|
63
|
+
return left.localeCompare(right);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function parseCommandsManifestYaml(content) {
|
|
68
|
+
const manifest = parseYamlDocument(content, 'commands manifest');
|
|
69
|
+
return manifest;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isPlainObject(value) {
|
|
73
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function validateCommandsManifest(manifest) {
|
|
77
|
+
const errors = [];
|
|
78
|
+
if (manifest.schemaVersion !== 'buildr.commands/v1') {
|
|
79
|
+
errors.push('commands manifest schemaVersion must be buildr.commands/v1.');
|
|
80
|
+
}
|
|
81
|
+
if (!Array.isArray(manifest.commands)) {
|
|
82
|
+
errors.push('commands manifest must declare commands as an array.');
|
|
83
|
+
return errors;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const ids = new Set();
|
|
87
|
+
const allowedCommandKeys = new Set(['id', 'source', 'enabled', 'required', 'state', 'name', 'executable', 'purpose', 'description', 'version', 'installHint', 'reason']);
|
|
88
|
+
const allowedVersionKeys = new Set(['constraint', 'args']);
|
|
89
|
+
manifest.commands.forEach((command, index) => {
|
|
90
|
+
const label = `commands[${index}]`;
|
|
91
|
+
if (!isPlainObject(command)) {
|
|
92
|
+
errors.push(`${label} must be an object.`);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
for (const key of Object.keys(command)) {
|
|
96
|
+
if (!allowedCommandKeys.has(key)) {
|
|
97
|
+
errors.push(`${label}.${key} is not a supported commands manifest field.`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (!isValidAssetId(command.id)) {
|
|
101
|
+
errors.push(`${label}.id must contain only letters, digits, dots, underscores, or dashes.`);
|
|
102
|
+
} else if (ids.has(command.id)) {
|
|
103
|
+
errors.push(`Duplicate command id: ${command.id}.`);
|
|
104
|
+
} else {
|
|
105
|
+
ids.add(command.id);
|
|
106
|
+
}
|
|
107
|
+
if (!command.executable || typeof command.executable !== 'string' || /\s/.test(command.executable) || command.executable.includes('/') || command.executable.includes('\\')) {
|
|
108
|
+
if (command.enabled !== false && command.state !== 'uninstalled') {
|
|
109
|
+
errors.push(`${label}.executable must be a command name without whitespace or path separators.`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (!command.purpose || typeof command.purpose !== 'string') {
|
|
113
|
+
errors.push(`${label}.purpose is required.`);
|
|
114
|
+
}
|
|
115
|
+
if (command.source !== undefined && !['buildr', 'workspace', 'project', 'service'].includes(command.source)) {
|
|
116
|
+
errors.push(`${label}.source must be buildr, workspace, project, or service when provided.`);
|
|
117
|
+
}
|
|
118
|
+
if (command.enabled !== undefined && typeof command.enabled !== 'boolean') {
|
|
119
|
+
errors.push(`${label}.enabled must be a boolean.`);
|
|
120
|
+
}
|
|
121
|
+
if (command.required !== undefined && typeof command.required !== 'boolean') {
|
|
122
|
+
errors.push(`${label}.required must be a boolean.`);
|
|
123
|
+
}
|
|
124
|
+
if (command.state !== undefined && !['installed', 'modified', 'uninstalled', 'missing'].includes(command.state)) {
|
|
125
|
+
errors.push(`${label}.state must be installed, modified, uninstalled, or missing.`);
|
|
126
|
+
}
|
|
127
|
+
if (command.name !== undefined && typeof command.name !== 'string') {
|
|
128
|
+
errors.push(`${label}.name must be a string when provided.`);
|
|
129
|
+
}
|
|
130
|
+
if (command.description !== undefined && typeof command.description !== 'string') {
|
|
131
|
+
errors.push(`${label}.description must be a string when provided.`);
|
|
132
|
+
}
|
|
133
|
+
if (command.installHint !== undefined && typeof command.installHint !== 'string') {
|
|
134
|
+
errors.push(`${label}.installHint must be a string when provided.`);
|
|
135
|
+
}
|
|
136
|
+
if (command.install !== undefined) {
|
|
137
|
+
errors.push(`${label}.install is not supported. Use installHint instead.`);
|
|
138
|
+
}
|
|
139
|
+
if (command.version !== undefined) {
|
|
140
|
+
if (!isPlainObject(command.version)) {
|
|
141
|
+
errors.push(`${label}.version must be an object.`);
|
|
142
|
+
} else {
|
|
143
|
+
for (const key of Object.keys(command.version)) {
|
|
144
|
+
if (!allowedVersionKeys.has(key)) {
|
|
145
|
+
errors.push(`${label}.version.${key} is not a supported version field.`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const { constraint, args } = command.version;
|
|
149
|
+
if (!constraint || typeof constraint !== 'string') {
|
|
150
|
+
errors.push(`${label}.version.constraint is required when version is declared.`);
|
|
151
|
+
} else if (!parseVersionConstraint(constraint)) {
|
|
152
|
+
errors.push(`${label}.version.constraint is invalid: ${constraint}.`);
|
|
153
|
+
}
|
|
154
|
+
if (!Array.isArray(args) || !args.every((arg) => typeof arg === 'string')) {
|
|
155
|
+
errors.push(`${label}.version.args must be an array of strings.`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
return errors;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function renderCommandsManifestYaml(manifest) {
|
|
164
|
+
const lines = ['schemaVersion: buildr.commands/v1'];
|
|
165
|
+
if (!manifest.commands || manifest.commands.length === 0) {
|
|
166
|
+
lines.push('commands: []');
|
|
167
|
+
return `${lines.join('\n')}\n`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
lines.push('commands:');
|
|
171
|
+
for (const command of manifest.commands) {
|
|
172
|
+
lines.push(` - id: ${quoteYaml(command.id)}`);
|
|
173
|
+
for (const key of ['source']) {
|
|
174
|
+
if (command[key] !== undefined) lines.push(` ${key}: ${quoteYaml(command[key])}`);
|
|
175
|
+
}
|
|
176
|
+
if (command.enabled !== undefined) lines.push(` enabled: ${quoteYaml(Boolean(command.enabled))}`);
|
|
177
|
+
if (command.required !== undefined) lines.push(` required: ${quoteYaml(Boolean(command.required))}`);
|
|
178
|
+
if (command.state !== undefined) lines.push(` state: ${quoteYaml(command.state)}`);
|
|
179
|
+
for (const key of ['name', 'executable', 'purpose', 'description']) {
|
|
180
|
+
if (command[key] !== undefined) lines.push(` ${key}: ${quoteYaml(command[key])}`);
|
|
181
|
+
}
|
|
182
|
+
if (command.version) {
|
|
183
|
+
lines.push(' version:');
|
|
184
|
+
lines.push(` constraint: ${quoteYaml(command.version.constraint)}`);
|
|
185
|
+
lines.push(` args: ${quoteYaml(command.version.args)}`);
|
|
186
|
+
}
|
|
187
|
+
if (command.installHint !== undefined) {
|
|
188
|
+
lines.push(` installHint: ${quoteYaml(command.installHint)}`);
|
|
189
|
+
}
|
|
190
|
+
if (command.reason !== undefined) {
|
|
191
|
+
lines.push(` reason: ${quoteYaml(command.reason)}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return `${lines.join('\n')}\n`;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function readCommandsManifestForWrite(targetRoot, collection = null) {
|
|
198
|
+
const manifestPath = commandsManifestPath(targetRoot, collection);
|
|
199
|
+
assertSafeCommandCollectionTarget(targetRoot, manifestPath);
|
|
200
|
+
if (!existsFile(manifestPath)) {
|
|
201
|
+
return { schemaVersion: 'buildr.commands/v1', commands: [] };
|
|
202
|
+
}
|
|
203
|
+
const manifest = parseCommandsManifestYaml(fs.readFileSync(manifestPath, 'utf8'));
|
|
204
|
+
const validationErrors = validateCommandsManifest(manifest);
|
|
205
|
+
if (validationErrors.length > 0) {
|
|
206
|
+
throw new Error(`${toPosixRelative(targetRoot, manifestPath)} is invalid:\n- ${validationErrors.join('\n- ')}`);
|
|
207
|
+
}
|
|
208
|
+
return manifest;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function writeCommandsManifest(targetRoot, manifest, collection = null) {
|
|
212
|
+
const manifestPath = commandsManifestPath(targetRoot, collection);
|
|
213
|
+
assertSafeCommandCollectionTarget(targetRoot, manifestPath);
|
|
214
|
+
atomicWriteFile(manifestPath, renderCommandsManifestYaml(manifest));
|
|
215
|
+
return manifestPath;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function parseVersionArgs(value) {
|
|
219
|
+
if (value === null || value === undefined) return null;
|
|
220
|
+
const trimmed = String(value).trim();
|
|
221
|
+
if (!trimmed) return [];
|
|
222
|
+
if (trimmed.startsWith('[')) {
|
|
223
|
+
const parsed = parseYamlValue(trimmed);
|
|
224
|
+
if (!Array.isArray(parsed) || !parsed.every((item) => typeof item === 'string')) {
|
|
225
|
+
throw new Error('--version-args must be a JSON array of strings or a comma-separated string.');
|
|
226
|
+
}
|
|
227
|
+
return parsed;
|
|
228
|
+
}
|
|
229
|
+
return trimmed.split(',').map((item) => item.trim()).filter(Boolean);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function assertNoUnknownOptions(args, allowedFlags, booleanFlags = new Set()) {
|
|
233
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
234
|
+
const arg = args[index];
|
|
235
|
+
if (!arg.startsWith('--')) continue;
|
|
236
|
+
if (!allowedFlags.has(arg)) {
|
|
237
|
+
throw new Error(`Unknown argument: ${arg}`);
|
|
238
|
+
}
|
|
239
|
+
if (!booleanFlags.has(arg)) index += 1;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function positionalArgs(args) {
|
|
244
|
+
const booleanFlags = new Set(['--replace', '--ignore-unsupported', '--keep-file', '--json', '--include-info', '--verbose']);
|
|
245
|
+
const result = [];
|
|
246
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
247
|
+
const arg = args[index];
|
|
248
|
+
if (!arg.startsWith('--')) {
|
|
249
|
+
result.push(arg);
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (!booleanFlags.has(arg)) index += 1;
|
|
253
|
+
}
|
|
254
|
+
return result;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function buildCommandEntry(id, args) {
|
|
258
|
+
assertName(id, 'Command id');
|
|
259
|
+
const purpose = optionValue(args, '--purpose', null);
|
|
260
|
+
if (!purpose) throw new Error('Missing required option: --purpose');
|
|
261
|
+
const entry = {
|
|
262
|
+
id,
|
|
263
|
+
executable: optionValue(args, '--executable', id),
|
|
264
|
+
purpose,
|
|
265
|
+
};
|
|
266
|
+
const name = optionValue(args, '--name', null);
|
|
267
|
+
const description = optionValue(args, '--description', null);
|
|
268
|
+
const installHint = optionValue(args, '--install-hint', optionValue(args, '--installHint', null));
|
|
269
|
+
const versionConstraint = optionValue(args, '--version-constraint', null);
|
|
270
|
+
const versionArgs = parseVersionArgs(optionValueRaw(args, '--version-args', null));
|
|
271
|
+
|
|
272
|
+
if (name) entry.name = name;
|
|
273
|
+
if (description) entry.description = description;
|
|
274
|
+
if (versionConstraint || versionArgs) {
|
|
275
|
+
if (!versionConstraint || !versionArgs) {
|
|
276
|
+
throw new Error('Version declaration requires both --version-constraint and --version-args.');
|
|
277
|
+
}
|
|
278
|
+
entry.version = { constraint: versionConstraint, args: versionArgs };
|
|
279
|
+
}
|
|
280
|
+
if (installHint) entry.installHint = installHint;
|
|
281
|
+
|
|
282
|
+
const errors = validateCommandsManifest({ schemaVersion: 'buildr.commands/v1', commands: [entry] });
|
|
283
|
+
if (errors.length > 0) throw new Error(errors.join('\n'));
|
|
284
|
+
return entry;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function printCommandsMutationReceipt(action, targetRoot, id, updatedPaths, nextAction) {
|
|
288
|
+
console.log(`已${action}命令行工具清单条目:${id}`);
|
|
289
|
+
console.log('已更新 Buildr 源资产:');
|
|
290
|
+
for (const file of updatedPaths) console.log(` ${toPosixRelative(targetRoot, file)}`);
|
|
291
|
+
console.log('下一步:');
|
|
292
|
+
console.log(` ${nextAction}`);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function commandsAddUnsafe(args) {
|
|
296
|
+
const allowedFlags = new Set(['--target', '--collection', '--purpose', '--executable', '--name', '--description', '--version-constraint', '--version-args', '--install-hint', '--installHint', '--replace']);
|
|
297
|
+
assertNoUnknownOptions(args, allowedFlags, new Set(['--replace']));
|
|
298
|
+
const [id] = positionalArgs(args);
|
|
299
|
+
if (!id) throw new Error('Missing command id');
|
|
300
|
+
const targetRoot = path.resolve(optionValue(args, '--target', process.cwd()));
|
|
301
|
+
assertInitializedBuildrWorkspace(targetRoot);
|
|
302
|
+
const collection = optionValue(args, '--collection', null);
|
|
303
|
+
const manifestPath = commandsManifestPath(targetRoot, collection);
|
|
304
|
+
const relativeManifest = toPosixRelative(targetRoot, manifestPath);
|
|
305
|
+
const owner = componentOwnerForMember(targetRoot, relativeManifest);
|
|
306
|
+
if (owner) throw new Error(`Command collection is managed by Component ${owner}: ${relativeManifest}. Use buildr component lifecycle commands.`);
|
|
307
|
+
|
|
308
|
+
const manifest = readCommandsManifestForWrite(targetRoot, collection);
|
|
309
|
+
const entry = buildCommandEntry(id, args);
|
|
310
|
+
const replace = hasFlag(args, '--replace');
|
|
311
|
+
const existingIndex = manifest.commands.findIndex((command) => command.id === id);
|
|
312
|
+
if (existingIndex !== -1 && !replace) {
|
|
313
|
+
throw new Error(`Command already exists in ${relativeManifest}: ${id}. Use --replace to replace the whole entry.`);
|
|
314
|
+
}
|
|
315
|
+
if (existingIndex === -1) {
|
|
316
|
+
manifest.commands.push(entry);
|
|
317
|
+
} else {
|
|
318
|
+
manifest.commands[existingIndex] = entry;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
writeCommandsManifest(targetRoot, manifest, collection);
|
|
322
|
+
printCommandsMutationReceipt(
|
|
323
|
+
existingIndex === -1 ? '添加' : '替换',
|
|
324
|
+
targetRoot,
|
|
325
|
+
id,
|
|
326
|
+
[manifestPath],
|
|
327
|
+
'运行命令行工具清单检查或 doctor 查看当前本机与组织清单的差异。'
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function commandsAdd(args) {
|
|
332
|
+
const targetRoot = path.resolve(optionValue(args, '--target', process.cwd()));
|
|
333
|
+
return withWorkspaceMutation(targetRoot, 'commands.add', [path.join(targetRoot, 'commands')], () => commandsAddUnsafe(args));
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function commandsRemoveUnsafe(args) {
|
|
337
|
+
const allowedFlags = new Set(['--target', '--collection']);
|
|
338
|
+
assertNoUnknownOptions(args, allowedFlags);
|
|
339
|
+
const [id] = positionalArgs(args);
|
|
340
|
+
if (!id) throw new Error('Missing command id');
|
|
341
|
+
assertName(id, 'Command id');
|
|
342
|
+
const targetRoot = path.resolve(optionValue(args, '--target', process.cwd()));
|
|
343
|
+
assertInitializedBuildrWorkspace(targetRoot);
|
|
344
|
+
const collection = optionValue(args, '--collection', null);
|
|
345
|
+
const manifestPath = commandsManifestPath(targetRoot, collection);
|
|
346
|
+
const relativeManifest = toPosixRelative(targetRoot, manifestPath);
|
|
347
|
+
const owner = componentOwnerForMember(targetRoot, relativeManifest);
|
|
348
|
+
if (owner) throw new Error(`Command collection is managed by Component ${owner}: ${relativeManifest}. Use buildr component lifecycle commands.`);
|
|
349
|
+
|
|
350
|
+
const manifest = readCommandsManifestForWrite(targetRoot, collection);
|
|
351
|
+
const existingIndex = manifest.commands.findIndex((command) => command.id === id);
|
|
352
|
+
if (existingIndex === -1) {
|
|
353
|
+
throw new Error(`Command not found in ${relativeManifest}: ${id}`);
|
|
354
|
+
}
|
|
355
|
+
manifest.commands.splice(existingIndex, 1);
|
|
356
|
+
writeCommandsManifest(targetRoot, manifest, collection);
|
|
357
|
+
printCommandsMutationReceipt(
|
|
358
|
+
'删除',
|
|
359
|
+
targetRoot,
|
|
360
|
+
id,
|
|
361
|
+
[manifestPath],
|
|
362
|
+
'运行命令行工具清单检查或 doctor 确认当前 workspace 状态。'
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function commandsRemove(args) {
|
|
367
|
+
const targetRoot = path.resolve(optionValue(args, '--target', process.cwd()));
|
|
368
|
+
return withWorkspaceMutation(targetRoot, 'commands.remove', [path.join(targetRoot, 'commands')], () => commandsRemoveUnsafe(args));
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function parseVersionConstraint(constraint) {
|
|
372
|
+
const match = String(constraint).trim().match(/^(>=|>|<=|<|=)?\s*(\d+)\.(\d+)\.(\d+)$/);
|
|
373
|
+
if (!match) return null;
|
|
374
|
+
return {
|
|
375
|
+
operator: match[1] || '=',
|
|
376
|
+
version: [Number(match[2]), Number(match[3]), Number(match[4])],
|
|
377
|
+
rawVersion: `${match[2]}.${match[3]}.${match[4]}`,
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function parseVersion(version) {
|
|
382
|
+
const match = String(version).match(/(\d+)\.(\d+)\.(\d+)(?:[-+][0-9A-Za-z.-]+)?/);
|
|
383
|
+
return match ? [Number(match[1]), Number(match[2]), Number(match[3])] : null;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function compareVersions(left, right) {
|
|
387
|
+
for (let index = 0; index < 3; index += 1) {
|
|
388
|
+
if (left[index] > right[index]) return 1;
|
|
389
|
+
if (left[index] < right[index]) return -1;
|
|
390
|
+
}
|
|
391
|
+
return 0;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function versionSatisfies(version, constraint) {
|
|
395
|
+
const comparison = compareVersions(version, constraint.version);
|
|
396
|
+
switch (constraint.operator) {
|
|
397
|
+
case '>=': return comparison >= 0;
|
|
398
|
+
case '>': return comparison > 0;
|
|
399
|
+
case '<=': return comparison <= 0;
|
|
400
|
+
case '<': return comparison < 0;
|
|
401
|
+
case '=': return comparison === 0;
|
|
402
|
+
default: return false;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function findExecutableOnPath(executable) {
|
|
407
|
+
const pathValue = process.env.PATH || '';
|
|
408
|
+
const pathExt = process.platform === 'win32'
|
|
409
|
+
? (process.env.PATHEXT || '.EXE;.CMD;.BAT;.COM').split(';')
|
|
410
|
+
: [''];
|
|
411
|
+
|
|
412
|
+
for (const dir of pathValue.split(path.delimiter).filter(Boolean)) {
|
|
413
|
+
for (const ext of pathExt) {
|
|
414
|
+
const candidate = path.join(dir, process.platform === 'win32' ? `${executable}${ext}` : executable);
|
|
415
|
+
try {
|
|
416
|
+
fs.accessSync(candidate, fs.constants.X_OK);
|
|
417
|
+
return candidate;
|
|
418
|
+
} catch {
|
|
419
|
+
// Try the next PATH entry.
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function createCommandsCheckResult(targetRoot) {
|
|
427
|
+
return {
|
|
428
|
+
targetRoot,
|
|
429
|
+
manifest: {
|
|
430
|
+
path: 'commands/manifest.yml',
|
|
431
|
+
exists: false,
|
|
432
|
+
valid: false,
|
|
433
|
+
schemaVersion: null,
|
|
434
|
+
},
|
|
435
|
+
manifests: [],
|
|
436
|
+
ok: true,
|
|
437
|
+
summary: { ok: 0, warning: 0, error: 0 },
|
|
438
|
+
commands: [],
|
|
439
|
+
findings: [],
|
|
440
|
+
nextSteps: [],
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function addCommandsFinding(result, status, code, message, extra = {}) {
|
|
445
|
+
result.findings.push({ status, code, message, ...extra });
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function stableValue(value) {
|
|
449
|
+
if (Array.isArray(value)) return value.map(stableValue);
|
|
450
|
+
if (!isPlainObject(value)) return value;
|
|
451
|
+
return Object.fromEntries(Object.keys(value).sort().map((key) => [key, stableValue(value[key])]));
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function normalizedCommandSignature(command) {
|
|
455
|
+
return JSON.stringify(stableValue(command));
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function finalizeCommandsCheckResult(result) {
|
|
459
|
+
const counts = { ok: 0, info: 0, warning: 0, error: 0 };
|
|
460
|
+
if (result.manifest.exists && result.manifest.valid) counts.ok += 1;
|
|
461
|
+
for (const command of result.commands) counts[command.status] += 1;
|
|
462
|
+
for (const finding of result.findings) {
|
|
463
|
+
if (finding.status === 'error' || finding.status === 'warning') counts[finding.status] += 1;
|
|
464
|
+
}
|
|
465
|
+
result.summary = counts;
|
|
466
|
+
result.ok = counts.error === 0;
|
|
467
|
+
result.nextSteps = result.findings
|
|
468
|
+
.filter((finding) => finding.suggestion || finding.installHint)
|
|
469
|
+
.map((finding) => ({
|
|
470
|
+
code: finding.code,
|
|
471
|
+
suggestion: finding.suggestion,
|
|
472
|
+
installHint: finding.installHint,
|
|
473
|
+
commandId: finding.commandId,
|
|
474
|
+
}))
|
|
475
|
+
.slice(0, 10);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function runCommandsCheck(targetRoot) {
|
|
479
|
+
const result = createCommandsCheckResult(targetRoot);
|
|
480
|
+
const rootManifestPath = commandsManifestPath(targetRoot);
|
|
481
|
+
const manifestPaths = listCommandsManifestPaths(targetRoot);
|
|
482
|
+
result.manifest.exists = existsFile(rootManifestPath);
|
|
483
|
+
if (!result.manifest.exists) {
|
|
484
|
+
addCommandsFinding(result, 'warning', 'commands.manifest_missing', '命令行工具清单不存在。', {
|
|
485
|
+
path: result.manifest.path,
|
|
486
|
+
suggestion: '创建 commands/manifest.yml;新 workspace 可通过 buildr init 初始化默认空清单。',
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
const commandsById = new Map();
|
|
490
|
+
for (const manifestPath of manifestPaths) {
|
|
491
|
+
const relative = toPosixRelative(targetRoot, manifestPath);
|
|
492
|
+
const record = { path: relative, exists: true, valid: false, schemaVersion: null, commandCount: 0 };
|
|
493
|
+
result.manifests.push(record);
|
|
494
|
+
try {
|
|
495
|
+
const manifest = parseCommandsManifestYaml(fs.readFileSync(manifestPath, 'utf8'));
|
|
496
|
+
record.schemaVersion = manifest.schemaVersion;
|
|
497
|
+
const validationErrors = validateCommandsManifest(manifest);
|
|
498
|
+
if (validationErrors.length) {
|
|
499
|
+
for (const message of validationErrors) addCommandsFinding(result, 'error', 'commands.manifest_invalid', message, { path: relative });
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
record.valid = true;
|
|
503
|
+
record.commandCount = manifest.commands.length;
|
|
504
|
+
if (manifestPath === rootManifestPath) {
|
|
505
|
+
result.manifest.schemaVersion = manifest.schemaVersion;
|
|
506
|
+
result.manifest.valid = true;
|
|
507
|
+
}
|
|
508
|
+
for (const command of manifest.commands) {
|
|
509
|
+
const signature = normalizedCommandSignature(command);
|
|
510
|
+
const existing = commandsById.get(command.id);
|
|
511
|
+
if (!existing) {
|
|
512
|
+
commandsById.set(command.id, { command, signature, sources: [relative] });
|
|
513
|
+
} else if (existing.signature === signature) {
|
|
514
|
+
existing.sources.push(relative);
|
|
515
|
+
} else {
|
|
516
|
+
addCommandsFinding(result, 'error', 'commands.collection_conflict', `Command ${command.id} 在多个 collection 中声明冲突。`, {
|
|
517
|
+
commandId: command.id,
|
|
518
|
+
sources: [...existing.sources, relative],
|
|
519
|
+
suggestion: '统一重复 Command 的有效字段,或删除其中一个声明。',
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
} catch (error) {
|
|
524
|
+
addCommandsFinding(result, 'error', 'commands.manifest_invalid', `命令行工具清单不可解析:${error.message}`, { path: relative });
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
for (const { command, sources } of commandsById.values()) {
|
|
529
|
+
if (command.enabled === false || command.state === 'uninstalled') {
|
|
530
|
+
addCommandsFinding(result, 'info', 'commands.uninstalled', `命令行工具声明已卸载:${command.id}`, {
|
|
531
|
+
path: sources[0],
|
|
532
|
+
sources,
|
|
533
|
+
commandId: command.id,
|
|
534
|
+
suggestion: '如需恢复,运行 buildr builtin restore。',
|
|
535
|
+
});
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
const item = {
|
|
539
|
+
id: command.id,
|
|
540
|
+
name: command.name || command.id,
|
|
541
|
+
executable: command.executable,
|
|
542
|
+
purpose: command.purpose,
|
|
543
|
+
description: command.description || null,
|
|
544
|
+
installHint: command.installHint || null,
|
|
545
|
+
status: 'ok',
|
|
546
|
+
executablePath: null,
|
|
547
|
+
version: command.version ? {
|
|
548
|
+
constraint: command.version.constraint,
|
|
549
|
+
args: command.version.args,
|
|
550
|
+
current: null,
|
|
551
|
+
} : null,
|
|
552
|
+
message: '命令行工具可用。',
|
|
553
|
+
difference: null,
|
|
554
|
+
sources,
|
|
555
|
+
};
|
|
556
|
+
result.commands.push(item);
|
|
557
|
+
|
|
558
|
+
const executablePath = findExecutableOnPath(command.executable);
|
|
559
|
+
item.executablePath = executablePath;
|
|
560
|
+
if (!executablePath) {
|
|
561
|
+
item.status = 'warning';
|
|
562
|
+
item.message = `当前本机找不到 executable:${command.executable}`;
|
|
563
|
+
item.difference = { expected: command.executable, actual: null };
|
|
564
|
+
addCommandsFinding(result, 'warning', 'commands.executable_missing', item.message, {
|
|
565
|
+
path: sources[0],
|
|
566
|
+
sources,
|
|
567
|
+
commandId: command.id,
|
|
568
|
+
executable: command.executable,
|
|
569
|
+
installHint: command.installHint || null,
|
|
570
|
+
suggestion: command.installHint || '根据组织约定安装该命令行工具。',
|
|
571
|
+
});
|
|
572
|
+
continue;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (!command.version) continue;
|
|
576
|
+
|
|
577
|
+
const versionCheck = spawnSync(command.executable, command.version.args, {
|
|
578
|
+
encoding: 'utf8',
|
|
579
|
+
timeout: 5000,
|
|
580
|
+
windowsHide: true,
|
|
581
|
+
});
|
|
582
|
+
const output = `${versionCheck.stdout || ''}\n${versionCheck.stderr || ''}`.trim();
|
|
583
|
+
const currentVersion = parseVersion(output);
|
|
584
|
+
if (!currentVersion) {
|
|
585
|
+
item.status = 'warning';
|
|
586
|
+
item.message = `无法从版本输出判断 ${command.id} 的版本。`;
|
|
587
|
+
item.difference = { expected: command.version.constraint, actual: output || null };
|
|
588
|
+
addCommandsFinding(result, 'warning', 'commands.version_unknown', item.message, {
|
|
589
|
+
path: sources[0],
|
|
590
|
+
sources,
|
|
591
|
+
commandId: command.id,
|
|
592
|
+
executable: command.executable,
|
|
593
|
+
versionArgs: command.version.args,
|
|
594
|
+
installHint: command.installHint || null,
|
|
595
|
+
suggestion: command.installHint || '根据组织约定确认或升级该命令行工具。',
|
|
596
|
+
});
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
item.version.current = currentVersion.join('.');
|
|
601
|
+
const constraint = parseVersionConstraint(command.version.constraint);
|
|
602
|
+
if (!versionSatisfies(currentVersion, constraint)) {
|
|
603
|
+
item.status = 'warning';
|
|
604
|
+
item.message = `${command.id} 当前版本 ${item.version.current} 不满足 ${command.version.constraint}。`;
|
|
605
|
+
item.difference = { expected: command.version.constraint, actual: item.version.current };
|
|
606
|
+
addCommandsFinding(result, 'warning', 'commands.version_unsatisfied', item.message, {
|
|
607
|
+
path: sources[0],
|
|
608
|
+
sources,
|
|
609
|
+
commandId: command.id,
|
|
610
|
+
executable: command.executable,
|
|
611
|
+
expected: command.version.constraint,
|
|
612
|
+
actual: item.version.current,
|
|
613
|
+
installHint: command.installHint || null,
|
|
614
|
+
suggestion: command.installHint || '根据组织约定升级该命令行工具。',
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
finalizeCommandsCheckResult(result);
|
|
620
|
+
return result;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function printCommandsCheckReport(result) {
|
|
624
|
+
console.log(`Buildr commands check for ${result.targetRoot}`);
|
|
625
|
+
console.log(`Status: ok=${result.summary.ok} warning=${result.summary.warning} error=${result.summary.error}`);
|
|
626
|
+
console.log('');
|
|
627
|
+
for (const manifest of result.manifests) console.log(`Manifest: ${manifest.path} exists=${manifest.exists} valid=${manifest.valid}`);
|
|
628
|
+
if (result.manifests.length === 0) console.log(`Manifest: ${result.manifest.path} exists=${result.manifest.exists} valid=${result.manifest.valid}`);
|
|
629
|
+
|
|
630
|
+
if (result.commands.length > 0) {
|
|
631
|
+
console.log('');
|
|
632
|
+
for (const command of result.commands) {
|
|
633
|
+
console.log(`[${command.status}] ${command.id} (${command.executable}) [${command.sources.join(', ')}] - ${command.message}`);
|
|
634
|
+
if (command.installHint && command.status !== 'ok') console.log(` 安装提示:${command.installHint}`);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
if (result.findings.length > 0) {
|
|
639
|
+
console.log('');
|
|
640
|
+
for (const finding of result.findings) {
|
|
641
|
+
console.log(`[${finding.status}] ${finding.code} - ${finding.message}`);
|
|
642
|
+
if (finding.suggestion) console.log(` 建议:${finding.suggestion}`);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
function commandsCheck(args) {
|
|
648
|
+
const targetRoot = path.resolve(optionValue(args, '--target', process.cwd()));
|
|
649
|
+
const json = hasFlag(args, '--json');
|
|
650
|
+
const result = runCommandsCheck(targetRoot);
|
|
651
|
+
if (json) {
|
|
652
|
+
process.stdout.write(`${JSON.stringify(withJsonSchema(PUBLIC_JSON_SCHEMAS.commandsCheck, result), null, 2)}\n`);
|
|
653
|
+
} else {
|
|
654
|
+
printCommandsCheckReport(result);
|
|
655
|
+
}
|
|
656
|
+
process.exitCode = result.ok ? 0 : 1;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
Object.assign(runtime, { normalizeCommandCollection, commandsManifestPath, assertSafeCommandCollectionTarget, listCommandsManifestPaths, parseCommandsManifestYaml, isPlainObject, validateCommandsManifest, renderCommandsManifestYaml, readCommandsManifestForWrite, writeCommandsManifest, parseVersionArgs, assertNoUnknownOptions, positionalArgs, buildCommandEntry, printCommandsMutationReceipt, commandsAddUnsafe, commandsAdd, commandsRemoveUnsafe, commandsRemove, parseVersionConstraint, parseVersion, compareVersions, versionSatisfies, findExecutableOnPath, createCommandsCheckResult, addCommandsFinding, stableValue, normalizedCommandSignature, finalizeCommandsCheckResult, runCommandsCheck, printCommandsCheckReport, commandsCheck });
|
|
660
|
+
return runtime;
|
|
661
|
+
}
|