@smoothbricks/nx-plugin 0.2.5 → 0.3.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.
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +136 -1
- package/package.json +1 -1
- package/src/index.test.ts +144 -4
- package/src/index.ts +153 -1
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAuB,aAAa,EAAuB,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAuB,aAAa,EAAuB,MAAM,0BAA0B,CAAC;AAgDxG,eAAO,MAAM,aAAa,EAAE,aAsB3B,CAAC;;;;AAEF,wBAAiC"}
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,42 @@ const RESERVED_ZIG_STEPS = new Set(['all', 'clean', 'install', 'test']);
|
|
|
7
7
|
const ZIG_STEP_PATTERN = /\bb\.step\(\s*["']([^"']+)["']\s*,/g;
|
|
8
8
|
const VALID_ZIG_STEP_NAME = /^[A-Za-z0-9_-]+$/;
|
|
9
9
|
const BUILD_OUTPUT_TARGET_PATTERN = /-(?:js|web|html|css|ios|android|native|napi|bun|wasm)$/;
|
|
10
|
+
// Cargo workspace inference: a package.json sitting next to a Cargo.toml that
|
|
11
|
+
// declares [workspace] gets direct cargo-test/test targets, cargo-lint feeding
|
|
12
|
+
// the lint aggregate, mutation (cargo-mutants), bench, and — per cdylib member
|
|
13
|
+
// crate whose package name ends in `-wasm` — a cargo-wasm build producing
|
|
14
|
+
// dist/<crate>.wasm. Explicit nx.targets entries in the package.json always win
|
|
15
|
+
// over inference.
|
|
16
|
+
const CARGO_WORKSPACE_PATTERN = /^\s*\[workspace\]/m;
|
|
17
|
+
const CARGO_MEMBERS_PATTERN = /^\s*members\s*=\s*\[([^\]]*)\]/m;
|
|
18
|
+
const CARGO_MEMBER_ENTRY_PATTERN = /["']([^"']+)["']/g;
|
|
19
|
+
const CARGO_PACKAGE_NAME_PATTERN = /^\s*name\s*=\s*["']([^"']+)["']/m;
|
|
20
|
+
const CARGO_WASM_CRATE_NAME_PATTERN = /-wasm$/;
|
|
21
|
+
const CARGO_CDYLIB_CRATE_TYPE_PATTERN = /^\s*crate-type\s*=\s*\[[^\]]*["']cdylib["']/m;
|
|
22
|
+
const CARGO_WASM_RELEASE_PROFILE_PATTERN = /^\s*\[profile\.wasm-release\]/m;
|
|
23
|
+
const CARGO_INPUTS = [
|
|
24
|
+
'{projectRoot}/**/*.rs',
|
|
25
|
+
'{projectRoot}/**/Cargo.toml',
|
|
26
|
+
'{projectRoot}/Cargo.lock',
|
|
27
|
+
'{projectRoot}/.cargo/config.toml',
|
|
28
|
+
'!{projectRoot}/target/**',
|
|
29
|
+
];
|
|
30
|
+
function createCargoTestTarget(projectRoot) {
|
|
31
|
+
return {
|
|
32
|
+
executor: '@smoothbricks/nx-plugin:bounded-exec',
|
|
33
|
+
cache: true,
|
|
34
|
+
inputs: CARGO_INPUTS,
|
|
35
|
+
options: {
|
|
36
|
+
command: 'cargo test --workspace',
|
|
37
|
+
cwd: projectRoot,
|
|
38
|
+
timeoutMs: 600000,
|
|
39
|
+
killAfterMs: 10000,
|
|
40
|
+
},
|
|
41
|
+
configurations: {
|
|
42
|
+
production: { command: 'cargo test --workspace --release' },
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
10
46
|
export const createNodesV2 = [
|
|
11
47
|
'**/package.json',
|
|
12
48
|
async (projectConfigurationFiles, _options, context) => {
|
|
@@ -70,6 +106,72 @@ async function createProjectTargets(packageJsonPath, workspaceRoot) {
|
|
|
70
106
|
else if (hasLibTsconfig) {
|
|
71
107
|
validationTargets.push('typecheck');
|
|
72
108
|
}
|
|
109
|
+
const cargo = await readCargoWorkspace(absoluteProjectRoot);
|
|
110
|
+
if (cargo) {
|
|
111
|
+
const declared = packageJson.nx?.targets ?? {};
|
|
112
|
+
if (!('cargo-test' in declared)) {
|
|
113
|
+
targets['cargo-test'] = createCargoTestTarget(projectRoot);
|
|
114
|
+
}
|
|
115
|
+
if (!('cargo-lint' in declared)) {
|
|
116
|
+
targets['cargo-lint'] = {
|
|
117
|
+
executor: 'nx:run-commands',
|
|
118
|
+
cache: true,
|
|
119
|
+
inputs: CARGO_INPUTS,
|
|
120
|
+
options: {
|
|
121
|
+
commands: ['cargo fmt --all --check', 'cargo clippy --workspace --all-targets -- -D warnings'],
|
|
122
|
+
cwd: projectRoot,
|
|
123
|
+
parallel: false,
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
validationTargets.push('cargo-lint');
|
|
128
|
+
if (!('test' in declared) && typeof packageJson.scripts?.test !== 'string') {
|
|
129
|
+
// Execute Cargo directly: workspace targetDefaults may replace test.dependsOn.
|
|
130
|
+
targets.test = createCargoTestTarget(projectRoot);
|
|
131
|
+
}
|
|
132
|
+
if (!('mutation' in declared)) {
|
|
133
|
+
// Mutation runs are minutes-to-hours: never cached, never part of build/lint.
|
|
134
|
+
// CI runs these per-PR via `cargo mutants --in-diff` (see mutants.toml docs).
|
|
135
|
+
targets.mutation = {
|
|
136
|
+
executor: 'nx:run-commands',
|
|
137
|
+
cache: false,
|
|
138
|
+
options: { command: 'cargo mutants --workspace', cwd: projectRoot },
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
if (!('bench' in declared)) {
|
|
142
|
+
targets.bench = {
|
|
143
|
+
executor: 'nx:run-commands',
|
|
144
|
+
cache: false,
|
|
145
|
+
options: { command: 'cargo bench --workspace', cwd: projectRoot },
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
if (cargo.wasmCrates.length > 0 && !('cargo-wasm' in declared)) {
|
|
149
|
+
const profile = cargo.hasWasmReleaseProfile ? 'wasm-release' : 'release';
|
|
150
|
+
const buildAndCopy = (crate, profileName) => {
|
|
151
|
+
const artifact = crate.replace(/-/g, '_');
|
|
152
|
+
const profileFlag = profileName === null ? '' : ` --profile ${profileName}`;
|
|
153
|
+
const outputDirectory = profileName ?? 'debug';
|
|
154
|
+
return `cargo build${profileFlag} --target wasm32-unknown-unknown -p ${crate} && mkdir -p dist && cp target/wasm32-unknown-unknown/${outputDirectory}/${artifact}.wasm dist/${artifact}.wasm`;
|
|
155
|
+
};
|
|
156
|
+
targets['cargo-wasm'] = {
|
|
157
|
+
executor: 'nx:run-commands',
|
|
158
|
+
cache: true,
|
|
159
|
+
inputs: CARGO_INPUTS,
|
|
160
|
+
outputs: ['{projectRoot}/dist/**/*.wasm'],
|
|
161
|
+
options: {
|
|
162
|
+
commands: cargo.wasmCrates.map((crate) => buildAndCopy(crate, profile)),
|
|
163
|
+
cwd: projectRoot,
|
|
164
|
+
parallel: false,
|
|
165
|
+
},
|
|
166
|
+
configurations: {
|
|
167
|
+
development: {
|
|
168
|
+
commands: cargo.wasmCrates.map((crate) => buildAndCopy(crate, null)),
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
hasBuildOutputTarget = true;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
73
175
|
const zigSteps = await readZigSteps(absoluteProjectRoot, projectRoot);
|
|
74
176
|
for (const step of zigSteps) {
|
|
75
177
|
const targetName = `zig-${step}`;
|
|
@@ -118,9 +220,13 @@ async function createProjectTargets(packageJsonPath, workspaceRoot) {
|
|
|
118
220
|
dependsOn: validationTargets,
|
|
119
221
|
};
|
|
120
222
|
}
|
|
223
|
+
const projectName = packageJson.nx?.name ?? packageJson.name;
|
|
224
|
+
if (typeof projectName !== 'string' || projectName.length === 0) {
|
|
225
|
+
throw new Error(`${packageJsonPath} must declare a non-empty package or nx project name`);
|
|
226
|
+
}
|
|
121
227
|
return {
|
|
122
228
|
projects: {
|
|
123
|
-
[projectRoot]: { targets },
|
|
229
|
+
[projectRoot]: { name: projectName, targets },
|
|
124
230
|
},
|
|
125
231
|
};
|
|
126
232
|
}
|
|
@@ -175,6 +281,35 @@ function hasPackageLocalBuildOutputTarget(packageJson) {
|
|
|
175
281
|
}
|
|
176
282
|
return Object.keys(targets).some((targetName) => BUILD_OUTPUT_TARGET_PATTERN.test(targetName));
|
|
177
283
|
}
|
|
284
|
+
async function readCargoWorkspace(absoluteProjectRoot) {
|
|
285
|
+
const cargoTomlPath = join(absoluteProjectRoot, 'Cargo.toml');
|
|
286
|
+
if (!existsSync(cargoTomlPath)) {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
const cargoToml = await readFile(cargoTomlPath, 'utf-8');
|
|
290
|
+
if (!CARGO_WORKSPACE_PATTERN.test(cargoToml)) {
|
|
291
|
+
// Member crates get their targets from the workspace-root package.json,
|
|
292
|
+
// never per-crate — one Nx project per Cargo workspace.
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
const wasmCrates = [];
|
|
296
|
+
const membersList = CARGO_MEMBERS_PATTERN.exec(cargoToml)?.[1] ?? '';
|
|
297
|
+
for (const match of membersList.matchAll(CARGO_MEMBER_ENTRY_PATTERN)) {
|
|
298
|
+
const memberTomlPath = join(absoluteProjectRoot, match[1], 'Cargo.toml');
|
|
299
|
+
if (!existsSync(memberTomlPath)) {
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
const memberToml = await readFile(memberTomlPath, 'utf-8');
|
|
303
|
+
const name = CARGO_PACKAGE_NAME_PATTERN.exec(memberToml)?.[1];
|
|
304
|
+
if (name &&
|
|
305
|
+
CARGO_WASM_CRATE_NAME_PATTERN.test(name) &&
|
|
306
|
+
CARGO_CDYLIB_CRATE_TYPE_PATTERN.test(memberToml) &&
|
|
307
|
+
!wasmCrates.includes(name)) {
|
|
308
|
+
wasmCrates.push(name);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return { wasmCrates, hasWasmReleaseProfile: CARGO_WASM_RELEASE_PROFILE_PATTERN.test(cargoToml) };
|
|
312
|
+
}
|
|
178
313
|
async function readZigSteps(absoluteProjectRoot, projectRoot) {
|
|
179
314
|
const buildZigPath = join(absoluteProjectRoot, 'build.zig');
|
|
180
315
|
if (!existsSync(buildZigPath)) {
|
package/package.json
CHANGED
package/src/index.test.ts
CHANGED
|
@@ -22,6 +22,27 @@ const buildOutputDependencies = [
|
|
|
22
22
|
];
|
|
23
23
|
|
|
24
24
|
describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
25
|
+
it('names standalone package projects from package metadata', async () => {
|
|
26
|
+
const workspace = await createWorkspace();
|
|
27
|
+
try {
|
|
28
|
+
await workspace.write(
|
|
29
|
+
'specs/prototype/package.json',
|
|
30
|
+
'{"name":"standalone-package","nx":{"name":"standalone-project"}}\n',
|
|
31
|
+
);
|
|
32
|
+
await workspace.write('specs/package-fallback/package.json', '{"name":"package-fallback"}\n');
|
|
33
|
+
|
|
34
|
+
const explicit = await inferProject(workspace, 'specs/prototype/package.json');
|
|
35
|
+
const fallback = await inferProject(workspace, 'specs/package-fallback/package.json');
|
|
36
|
+
|
|
37
|
+
expect(explicit?.name).toBe('standalone-project');
|
|
38
|
+
expect(explicit?.targets).toEqual({});
|
|
39
|
+
expect(fallback?.name).toBe('package-fallback');
|
|
40
|
+
expect(fallback?.targets).toEqual({});
|
|
41
|
+
} finally {
|
|
42
|
+
await workspace.cleanup();
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
25
46
|
it('infers validation and aggregate build targets without owning TypeScript lib build', async () => {
|
|
26
47
|
const workspace = await createWorkspace();
|
|
27
48
|
try {
|
|
@@ -152,6 +173,123 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
152
173
|
}
|
|
153
174
|
});
|
|
154
175
|
|
|
176
|
+
it('infers cargo workspace targets including explicitly named wasm crate builds', async () => {
|
|
177
|
+
const workspace = await createWorkspace();
|
|
178
|
+
try {
|
|
179
|
+
await workspace.write('packages/ferris/package.json', '{"name":"ferris"}\n');
|
|
180
|
+
await workspace.write(
|
|
181
|
+
'packages/ferris/Cargo.toml',
|
|
182
|
+
'[workspace]\nmembers = ["crates/ferris-core", "crates/ferris-wasm"]\n\n[profile.wasm-release]\ninherits = "release"\n',
|
|
183
|
+
);
|
|
184
|
+
await workspace.write('packages/ferris/crates/ferris-core/Cargo.toml', '[package]\nname = "ferris-core"\n');
|
|
185
|
+
await workspace.write(
|
|
186
|
+
'packages/ferris/crates/ferris-wasm/Cargo.toml',
|
|
187
|
+
'[package]\nname = "ferris-wasm"\n\n[lib]\ncrate-type = ["cdylib", "rlib"]\n',
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
const targets = await inferProjectTargets(workspace, 'packages/ferris/package.json');
|
|
191
|
+
|
|
192
|
+
expect(Object.keys(targets).sort()).toEqual([
|
|
193
|
+
'bench',
|
|
194
|
+
'build',
|
|
195
|
+
'cargo-lint',
|
|
196
|
+
'cargo-test',
|
|
197
|
+
'cargo-wasm',
|
|
198
|
+
'clean',
|
|
199
|
+
'lint',
|
|
200
|
+
'mutation',
|
|
201
|
+
'test',
|
|
202
|
+
]);
|
|
203
|
+
expect(targets['cargo-test']?.executor).toBe('@smoothbricks/nx-plugin:bounded-exec');
|
|
204
|
+
expect(targets['cargo-test']?.options).toMatchObject({
|
|
205
|
+
command: 'cargo test --workspace',
|
|
206
|
+
cwd: 'packages/ferris',
|
|
207
|
+
});
|
|
208
|
+
expect(targets['cargo-test']?.inputs).toEqual([
|
|
209
|
+
'{projectRoot}/**/*.rs',
|
|
210
|
+
'{projectRoot}/**/Cargo.toml',
|
|
211
|
+
'{projectRoot}/Cargo.lock',
|
|
212
|
+
'{projectRoot}/.cargo/config.toml',
|
|
213
|
+
'!{projectRoot}/target/**',
|
|
214
|
+
]);
|
|
215
|
+
expect(targets['cargo-lint']?.options).toMatchObject({
|
|
216
|
+
commands: ['cargo fmt --all --check', 'cargo clippy --workspace --all-targets -- -D warnings'],
|
|
217
|
+
});
|
|
218
|
+
expect(targets.lint?.dependsOn).toEqual(['cargo-lint']);
|
|
219
|
+
expect(targets.test?.executor).toBe('@smoothbricks/nx-plugin:bounded-exec');
|
|
220
|
+
expect(targets.test?.options).toMatchObject({
|
|
221
|
+
command: 'cargo test --workspace',
|
|
222
|
+
cwd: 'packages/ferris',
|
|
223
|
+
});
|
|
224
|
+
expect(targets.mutation?.cache).toBe(false);
|
|
225
|
+
expect(targets.mutation?.options).toMatchObject({ command: 'cargo mutants --workspace' });
|
|
226
|
+
expect(targets['cargo-wasm']?.outputs).toEqual(['{projectRoot}/dist/**/*.wasm']);
|
|
227
|
+
expect(targets['cargo-wasm']?.options).toMatchObject({
|
|
228
|
+
commands: [
|
|
229
|
+
'cargo build --profile wasm-release --target wasm32-unknown-unknown -p ferris-wasm && mkdir -p dist && cp target/wasm32-unknown-unknown/wasm-release/ferris_wasm.wasm dist/ferris_wasm.wasm',
|
|
230
|
+
],
|
|
231
|
+
});
|
|
232
|
+
expect(targets['cargo-wasm']?.configurations?.development).toEqual({
|
|
233
|
+
commands: [
|
|
234
|
+
'cargo build --target wasm32-unknown-unknown -p ferris-wasm && mkdir -p dist && cp target/wasm32-unknown-unknown/debug/ferris_wasm.wasm dist/ferris_wasm.wasm',
|
|
235
|
+
],
|
|
236
|
+
});
|
|
237
|
+
// cargo-wasm matches the *-wasm build-output pattern → aggregate build/clean exist
|
|
238
|
+
expect(targets.build?.executor).toBe('nx:noop');
|
|
239
|
+
} finally {
|
|
240
|
+
await workspace.cleanup();
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('does not infer wasm targets for native N-API cdylib crates', async () => {
|
|
245
|
+
const workspace = await createWorkspace();
|
|
246
|
+
try {
|
|
247
|
+
await workspace.write('packages/cowshed/package.json', '{"name":"cowshed"}\n');
|
|
248
|
+
await workspace.write('packages/cowshed/Cargo.toml', '[workspace]\nmembers = ["crates/cowshed-napi"]\n');
|
|
249
|
+
await workspace.write(
|
|
250
|
+
'packages/cowshed/crates/cowshed-napi/Cargo.toml',
|
|
251
|
+
'[package]\nname = "cowshed-napi"\n\n[lib]\ncrate-type = ["cdylib"]\n',
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
const targets = await inferProjectTargets(workspace, 'packages/cowshed/package.json');
|
|
255
|
+
|
|
256
|
+
expect(targets['cargo-wasm']).toBeUndefined();
|
|
257
|
+
expect(targets.build).toBeUndefined();
|
|
258
|
+
expect(targets.clean).toBeUndefined();
|
|
259
|
+
expect(targets['cargo-test']).toBeDefined();
|
|
260
|
+
} finally {
|
|
261
|
+
await workspace.cleanup();
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('lets explicit nx.targets override wasm inference and skips non-workspace Cargo.toml', async () => {
|
|
266
|
+
const workspace = await createWorkspace();
|
|
267
|
+
try {
|
|
268
|
+
await workspace.write(
|
|
269
|
+
'packages/custom/package.json',
|
|
270
|
+
'{"name":"custom","nx":{"targets":{"cargo-wasm":{"options":{"command":"custom"}},"test":{}}}}\n',
|
|
271
|
+
);
|
|
272
|
+
await workspace.write('packages/custom/Cargo.toml', '[workspace]\nmembers = ["crates/x-wasm"]\n');
|
|
273
|
+
await workspace.write(
|
|
274
|
+
'packages/custom/crates/x-wasm/Cargo.toml',
|
|
275
|
+
'[package]\nname = "x-wasm"\n\n[lib]\ncrate-type = ["cdylib"]\n',
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
const targets = await inferProjectTargets(workspace, 'packages/custom/package.json');
|
|
279
|
+
expect(targets['cargo-wasm']).toBeUndefined();
|
|
280
|
+
expect(targets.test).toBeUndefined();
|
|
281
|
+
expect(targets['cargo-test']).toBeDefined();
|
|
282
|
+
|
|
283
|
+
// A member crate's own Cargo.toml (no [workspace]) infers nothing.
|
|
284
|
+
await workspace.write('packages/member/package.json', '{"name":"member"}\n');
|
|
285
|
+
await workspace.write('packages/member/Cargo.toml', '[package]\nname = "member"\n');
|
|
286
|
+
const memberTargets = await inferProjectTargets(workspace, 'packages/member/package.json');
|
|
287
|
+
expect(memberTargets).toEqual({});
|
|
288
|
+
} finally {
|
|
289
|
+
await workspace.cleanup();
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
|
|
155
293
|
it('leaves build.zig without b.step declarations to smoo validation', async () => {
|
|
156
294
|
const workspace = await createWorkspace();
|
|
157
295
|
try {
|
|
@@ -192,12 +330,14 @@ interface WorkspaceFixture {
|
|
|
192
330
|
cleanup(): Promise<void>;
|
|
193
331
|
}
|
|
194
332
|
|
|
333
|
+
async function inferProject(workspace: WorkspaceFixture, packageJsonPath: string) {
|
|
334
|
+
const result = await inferTargets([packageJsonPath], undefined, workspace.context);
|
|
335
|
+
return result[0]?.[1].projects?.[dirname(packageJsonPath)];
|
|
336
|
+
}
|
|
337
|
+
|
|
195
338
|
async function inferProjectTargets(
|
|
196
339
|
workspace: WorkspaceFixture,
|
|
197
340
|
packageJsonPath: string,
|
|
198
341
|
): Promise<Record<string, TargetConfiguration>> {
|
|
199
|
-
|
|
200
|
-
const projectRoot = dirname(packageJsonPath);
|
|
201
|
-
|
|
202
|
-
return result[0]?.[1].projects?.[projectRoot]?.targets ?? {};
|
|
342
|
+
return (await inferProject(workspace, packageJsonPath))?.targets ?? {};
|
|
203
343
|
}
|
package/src/index.ts
CHANGED
|
@@ -12,6 +12,44 @@ const ZIG_STEP_PATTERN = /\bb\.step\(\s*["']([^"']+)["']\s*,/g;
|
|
|
12
12
|
const VALID_ZIG_STEP_NAME = /^[A-Za-z0-9_-]+$/;
|
|
13
13
|
const BUILD_OUTPUT_TARGET_PATTERN = /-(?:js|web|html|css|ios|android|native|napi|bun|wasm)$/;
|
|
14
14
|
|
|
15
|
+
// Cargo workspace inference: a package.json sitting next to a Cargo.toml that
|
|
16
|
+
// declares [workspace] gets direct cargo-test/test targets, cargo-lint feeding
|
|
17
|
+
// the lint aggregate, mutation (cargo-mutants), bench, and — per cdylib member
|
|
18
|
+
// crate whose package name ends in `-wasm` — a cargo-wasm build producing
|
|
19
|
+
// dist/<crate>.wasm. Explicit nx.targets entries in the package.json always win
|
|
20
|
+
// over inference.
|
|
21
|
+
const CARGO_WORKSPACE_PATTERN = /^\s*\[workspace\]/m;
|
|
22
|
+
const CARGO_MEMBERS_PATTERN = /^\s*members\s*=\s*\[([^\]]*)\]/m;
|
|
23
|
+
const CARGO_MEMBER_ENTRY_PATTERN = /["']([^"']+)["']/g;
|
|
24
|
+
const CARGO_PACKAGE_NAME_PATTERN = /^\s*name\s*=\s*["']([^"']+)["']/m;
|
|
25
|
+
const CARGO_WASM_CRATE_NAME_PATTERN = /-wasm$/;
|
|
26
|
+
const CARGO_CDYLIB_CRATE_TYPE_PATTERN = /^\s*crate-type\s*=\s*\[[^\]]*["']cdylib["']/m;
|
|
27
|
+
const CARGO_WASM_RELEASE_PROFILE_PATTERN = /^\s*\[profile\.wasm-release\]/m;
|
|
28
|
+
const CARGO_INPUTS = [
|
|
29
|
+
'{projectRoot}/**/*.rs',
|
|
30
|
+
'{projectRoot}/**/Cargo.toml',
|
|
31
|
+
'{projectRoot}/Cargo.lock',
|
|
32
|
+
'{projectRoot}/.cargo/config.toml',
|
|
33
|
+
'!{projectRoot}/target/**',
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
function createCargoTestTarget(projectRoot: string): TargetConfiguration {
|
|
37
|
+
return {
|
|
38
|
+
executor: '@smoothbricks/nx-plugin:bounded-exec',
|
|
39
|
+
cache: true,
|
|
40
|
+
inputs: CARGO_INPUTS,
|
|
41
|
+
options: {
|
|
42
|
+
command: 'cargo test --workspace',
|
|
43
|
+
cwd: projectRoot,
|
|
44
|
+
timeoutMs: 600000,
|
|
45
|
+
killAfterMs: 10000,
|
|
46
|
+
},
|
|
47
|
+
configurations: {
|
|
48
|
+
production: { command: 'cargo test --workspace --release' },
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
15
53
|
export const createNodesV2: CreateNodesV2 = [
|
|
16
54
|
'**/package.json',
|
|
17
55
|
async (projectConfigurationFiles, _options, context) => {
|
|
@@ -39,8 +77,10 @@ export const createNodesV2: CreateNodesV2 = [
|
|
|
39
77
|
export default { createNodesV2 };
|
|
40
78
|
|
|
41
79
|
interface PackageJson {
|
|
80
|
+
name?: string;
|
|
42
81
|
scripts?: Record<string, unknown>;
|
|
43
82
|
nx?: {
|
|
83
|
+
name?: string;
|
|
44
84
|
targets?: Record<string, unknown>;
|
|
45
85
|
};
|
|
46
86
|
}
|
|
@@ -89,6 +129,73 @@ async function createProjectTargets(packageJsonPath: string, workspaceRoot: stri
|
|
|
89
129
|
validationTargets.push('typecheck');
|
|
90
130
|
}
|
|
91
131
|
|
|
132
|
+
const cargo = await readCargoWorkspace(absoluteProjectRoot);
|
|
133
|
+
if (cargo) {
|
|
134
|
+
const declared = packageJson.nx?.targets ?? {};
|
|
135
|
+
if (!('cargo-test' in declared)) {
|
|
136
|
+
targets['cargo-test'] = createCargoTestTarget(projectRoot);
|
|
137
|
+
}
|
|
138
|
+
if (!('cargo-lint' in declared)) {
|
|
139
|
+
targets['cargo-lint'] = {
|
|
140
|
+
executor: 'nx:run-commands',
|
|
141
|
+
cache: true,
|
|
142
|
+
inputs: CARGO_INPUTS,
|
|
143
|
+
options: {
|
|
144
|
+
commands: ['cargo fmt --all --check', 'cargo clippy --workspace --all-targets -- -D warnings'],
|
|
145
|
+
cwd: projectRoot,
|
|
146
|
+
parallel: false,
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
validationTargets.push('cargo-lint');
|
|
151
|
+
if (!('test' in declared) && typeof packageJson.scripts?.test !== 'string') {
|
|
152
|
+
// Execute Cargo directly: workspace targetDefaults may replace test.dependsOn.
|
|
153
|
+
targets.test = createCargoTestTarget(projectRoot);
|
|
154
|
+
}
|
|
155
|
+
if (!('mutation' in declared)) {
|
|
156
|
+
// Mutation runs are minutes-to-hours: never cached, never part of build/lint.
|
|
157
|
+
// CI runs these per-PR via `cargo mutants --in-diff` (see mutants.toml docs).
|
|
158
|
+
targets.mutation = {
|
|
159
|
+
executor: 'nx:run-commands',
|
|
160
|
+
cache: false,
|
|
161
|
+
options: { command: 'cargo mutants --workspace', cwd: projectRoot },
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
if (!('bench' in declared)) {
|
|
165
|
+
targets.bench = {
|
|
166
|
+
executor: 'nx:run-commands',
|
|
167
|
+
cache: false,
|
|
168
|
+
options: { command: 'cargo bench --workspace', cwd: projectRoot },
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
if (cargo.wasmCrates.length > 0 && !('cargo-wasm' in declared)) {
|
|
172
|
+
const profile = cargo.hasWasmReleaseProfile ? 'wasm-release' : 'release';
|
|
173
|
+
const buildAndCopy = (crate: string, profileName: string | null) => {
|
|
174
|
+
const artifact = crate.replace(/-/g, '_');
|
|
175
|
+
const profileFlag = profileName === null ? '' : ` --profile ${profileName}`;
|
|
176
|
+
const outputDirectory = profileName ?? 'debug';
|
|
177
|
+
return `cargo build${profileFlag} --target wasm32-unknown-unknown -p ${crate} && mkdir -p dist && cp target/wasm32-unknown-unknown/${outputDirectory}/${artifact}.wasm dist/${artifact}.wasm`;
|
|
178
|
+
};
|
|
179
|
+
targets['cargo-wasm'] = {
|
|
180
|
+
executor: 'nx:run-commands',
|
|
181
|
+
cache: true,
|
|
182
|
+
inputs: CARGO_INPUTS,
|
|
183
|
+
outputs: ['{projectRoot}/dist/**/*.wasm'],
|
|
184
|
+
options: {
|
|
185
|
+
commands: cargo.wasmCrates.map((crate) => buildAndCopy(crate, profile)),
|
|
186
|
+
cwd: projectRoot,
|
|
187
|
+
parallel: false,
|
|
188
|
+
},
|
|
189
|
+
configurations: {
|
|
190
|
+
development: {
|
|
191
|
+
commands: cargo.wasmCrates.map((crate) => buildAndCopy(crate, null)),
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
hasBuildOutputTarget = true;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
92
199
|
const zigSteps = await readZigSteps(absoluteProjectRoot, projectRoot);
|
|
93
200
|
for (const step of zigSteps) {
|
|
94
201
|
const targetName = `zig-${step}`;
|
|
@@ -140,9 +247,14 @@ async function createProjectTargets(packageJsonPath: string, workspaceRoot: stri
|
|
|
140
247
|
};
|
|
141
248
|
}
|
|
142
249
|
|
|
250
|
+
const projectName = packageJson.nx?.name ?? packageJson.name;
|
|
251
|
+
if (typeof projectName !== 'string' || projectName.length === 0) {
|
|
252
|
+
throw new Error(`${packageJsonPath} must declare a non-empty package or nx project name`);
|
|
253
|
+
}
|
|
254
|
+
|
|
143
255
|
return {
|
|
144
256
|
projects: {
|
|
145
|
-
[projectRoot]: { targets },
|
|
257
|
+
[projectRoot]: { name: projectName, targets },
|
|
146
258
|
},
|
|
147
259
|
};
|
|
148
260
|
}
|
|
@@ -211,6 +323,46 @@ function hasPackageLocalBuildOutputTarget(packageJson: PackageJson): boolean {
|
|
|
211
323
|
return Object.keys(targets).some((targetName) => BUILD_OUTPUT_TARGET_PATTERN.test(targetName));
|
|
212
324
|
}
|
|
213
325
|
|
|
326
|
+
interface CargoWorkspace {
|
|
327
|
+
wasmCrates: string[];
|
|
328
|
+
hasWasmReleaseProfile: boolean;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async function readCargoWorkspace(absoluteProjectRoot: string): Promise<CargoWorkspace | null> {
|
|
332
|
+
const cargoTomlPath = join(absoluteProjectRoot, 'Cargo.toml');
|
|
333
|
+
if (!existsSync(cargoTomlPath)) {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const cargoToml = await readFile(cargoTomlPath, 'utf-8');
|
|
338
|
+
if (!CARGO_WORKSPACE_PATTERN.test(cargoToml)) {
|
|
339
|
+
// Member crates get their targets from the workspace-root package.json,
|
|
340
|
+
// never per-crate — one Nx project per Cargo workspace.
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const wasmCrates: string[] = [];
|
|
345
|
+
const membersList = CARGO_MEMBERS_PATTERN.exec(cargoToml)?.[1] ?? '';
|
|
346
|
+
for (const match of membersList.matchAll(CARGO_MEMBER_ENTRY_PATTERN)) {
|
|
347
|
+
const memberTomlPath = join(absoluteProjectRoot, match[1], 'Cargo.toml');
|
|
348
|
+
if (!existsSync(memberTomlPath)) {
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
const memberToml = await readFile(memberTomlPath, 'utf-8');
|
|
352
|
+
const name = CARGO_PACKAGE_NAME_PATTERN.exec(memberToml)?.[1];
|
|
353
|
+
if (
|
|
354
|
+
name &&
|
|
355
|
+
CARGO_WASM_CRATE_NAME_PATTERN.test(name) &&
|
|
356
|
+
CARGO_CDYLIB_CRATE_TYPE_PATTERN.test(memberToml) &&
|
|
357
|
+
!wasmCrates.includes(name)
|
|
358
|
+
) {
|
|
359
|
+
wasmCrates.push(name);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return { wasmCrates, hasWasmReleaseProfile: CARGO_WASM_RELEASE_PROFILE_PATTERN.test(cargoToml) };
|
|
364
|
+
}
|
|
365
|
+
|
|
214
366
|
async function readZigSteps(absoluteProjectRoot: string, projectRoot: string): Promise<string[]> {
|
|
215
367
|
const buildZigPath = join(absoluteProjectRoot, 'build.zig');
|
|
216
368
|
|