@pollis-inc/cli 0.1.0 → 0.1.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/dist/build.d.ts +6 -3
- package/dist/build.js +51 -21
- package/dist/errors.d.ts +3 -0
- package/dist/errors.js +3 -0
- package/dist/main.d.ts +1 -1
- package/dist/main.js +7 -5
- package/dist/new.d.ts +2 -2
- package/dist/new.js +40 -3
- package/dist/runtime.js +1 -1
- package/dist/rust.d.ts +22 -0
- package/dist/rust.js +150 -0
- package/dist/test.d.ts +5 -3
- package/dist/test.js +12 -2
- package/package.json +8 -2
- package/templates/rust/Cargo.toml.tmpl +25 -0
- package/templates/rust/README.md +18 -0
- package/templates/rust/gitignore +2 -0
- package/templates/rust/rust-toolchain.toml +5 -0
- package/templates/rust/src/lib.rs +26 -0
- package/templates/rust/tests/hello.rs +24 -0
package/dist/build.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `pollis app build`:
|
|
2
|
+
* `pollis app build`: an App's source in, a sandboxed component and its manifest
|
|
3
|
+
* out. Rust Apps compile in `rust.ts`; both languages end at the same check.
|
|
4
|
+
*
|
|
5
|
+
* For TypeScript:
|
|
3
6
|
*
|
|
4
7
|
* 1. Bundle the App with esbuild (refusing Node built-ins) and import it in
|
|
5
8
|
* Node to read `app({ manifest })`; check it and write `pollis-app.json`.
|
|
@@ -13,6 +16,8 @@
|
|
|
13
16
|
*/
|
|
14
17
|
import { type Plugin } from "esbuild";
|
|
15
18
|
import { type ResolvedManifest } from "@pollis-inc/app/manifest";
|
|
19
|
+
import { BuildError } from "./errors.js";
|
|
20
|
+
export { BuildError };
|
|
16
21
|
export interface BuildOptions {
|
|
17
22
|
/** Project directory (holds package.json). */
|
|
18
23
|
dir: string;
|
|
@@ -29,8 +34,6 @@ export interface BuildResult {
|
|
|
29
34
|
appHash: string;
|
|
30
35
|
bytes: number;
|
|
31
36
|
}
|
|
32
|
-
export declare class BuildError extends Error {
|
|
33
|
-
}
|
|
34
37
|
/** Refuses every Node built-in, naming the importer and saying why. */
|
|
35
38
|
export declare function noNodeBuiltins(): Plugin;
|
|
36
39
|
export declare function buildApp(options: BuildOptions): Promise<BuildResult>;
|
package/dist/build.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `pollis app build`:
|
|
2
|
+
* `pollis app build`: an App's source in, a sandboxed component and its manifest
|
|
3
|
+
* out. Rust Apps compile in `rust.ts`; both languages end at the same check.
|
|
4
|
+
*
|
|
5
|
+
* For TypeScript:
|
|
3
6
|
*
|
|
4
7
|
* 1. Bundle the App with esbuild (refusing Node built-ins) and import it in
|
|
5
8
|
* Node to read `app({ manifest })`; check it and write `pollis-app.json`.
|
|
@@ -20,8 +23,9 @@ import { build as esbuild } from "esbuild";
|
|
|
20
23
|
import { isApp } from "@pollis-inc/app";
|
|
21
24
|
import { HANDLER_FOR, ManifestError, resolveManifest } from "@pollis-inc/app/manifest";
|
|
22
25
|
import { devTool, findDevTool } from "./devtool.js";
|
|
23
|
-
|
|
24
|
-
}
|
|
26
|
+
import { BuildError } from "./errors.js";
|
|
27
|
+
import { compileRust, isRustApp } from "./rust.js";
|
|
28
|
+
export { BuildError };
|
|
25
29
|
/**
|
|
26
30
|
* Packages are resolved as for a browser: the sandbox has web basics
|
|
27
31
|
* (`crypto.getRandomValues`, TextEncoder, timers) and no Node built-ins, so a
|
|
@@ -215,11 +219,53 @@ function log(quiet, line) {
|
|
|
215
219
|
console.log(line);
|
|
216
220
|
}
|
|
217
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* Write the build out and load it in the real App host, so an App the runtime
|
|
224
|
+
* would refuse is refused here, then compute its App hash.
|
|
225
|
+
*/
|
|
226
|
+
function finishBuild(bin, out, manifest, component) {
|
|
227
|
+
const manifestPath = join(out, "pollis-app.json");
|
|
228
|
+
writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + "\n");
|
|
229
|
+
const componentPath = join(out, "app.wasm");
|
|
230
|
+
writeFileSync(componentPath, component);
|
|
231
|
+
// A development signature from an earlier `pollis app dev` signs another App hash. Left beside
|
|
232
|
+
// this build it makes a development installation refuse the build as tampered instead of
|
|
233
|
+
// offering it to an admin; `pollis app dev` signs each new build itself.
|
|
234
|
+
rmSync(`${componentPath}.sig.json`, { force: true });
|
|
235
|
+
try {
|
|
236
|
+
devTool(bin, ["check", componentPath, manifestPath]);
|
|
237
|
+
}
|
|
238
|
+
catch (e) {
|
|
239
|
+
rmSync(componentPath, { force: true });
|
|
240
|
+
throw new BuildError(`the App host refuses this build: ${e.message}`);
|
|
241
|
+
}
|
|
242
|
+
const appHash = devTool(bin, ["hash", componentPath, manifestPath]);
|
|
243
|
+
return { component: componentPath, manifestPath, manifest, appHash, bytes: component.length };
|
|
244
|
+
}
|
|
245
|
+
async function buildRustApp(dir, options) {
|
|
246
|
+
const out = resolve(dir, options.out ?? "dist");
|
|
247
|
+
const bin = findDevTool();
|
|
248
|
+
mkdirSync(out, { recursive: true });
|
|
249
|
+
const scratch = join(out, ".build");
|
|
250
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
251
|
+
mkdirSync(scratch, { recursive: true });
|
|
252
|
+
try {
|
|
253
|
+
log(options.quiet, `compiling ${relative(process.cwd(), dir) || "."} to a component...`);
|
|
254
|
+
const { manifest, component } = compileRust(dir, scratch);
|
|
255
|
+
return finishBuild(bin, out, manifest, component);
|
|
256
|
+
}
|
|
257
|
+
finally {
|
|
258
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
259
|
+
}
|
|
260
|
+
}
|
|
218
261
|
export async function buildApp(options) {
|
|
219
262
|
const dir = resolve(options.dir);
|
|
263
|
+
if (isRustApp(dir)) {
|
|
264
|
+
return buildRustApp(dir, options);
|
|
265
|
+
}
|
|
220
266
|
const pkgPath = join(dir, "package.json");
|
|
221
267
|
if (!existsSync(pkgPath)) {
|
|
222
|
-
throw new BuildError(`no
|
|
268
|
+
throw new BuildError(`no App in ${dir} (no package.json, or Cargo.toml that depends on pollis-app); run this in your App's directory`);
|
|
223
269
|
}
|
|
224
270
|
const pkg = readJson(pkgPath);
|
|
225
271
|
const configured = pkg.pollis?.entry;
|
|
@@ -251,8 +297,6 @@ export async function buildApp(options) {
|
|
|
251
297
|
}
|
|
252
298
|
throw e;
|
|
253
299
|
}
|
|
254
|
-
const manifestPath = join(out, "pollis-app.json");
|
|
255
|
-
writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + "\n");
|
|
256
300
|
// 2. The sandbox bundle.
|
|
257
301
|
let bundle;
|
|
258
302
|
try {
|
|
@@ -298,22 +342,8 @@ export async function buildApp(options) {
|
|
|
298
342
|
catch (e) {
|
|
299
343
|
throw new BuildError(`compiling to a component failed:\n${e.message}`);
|
|
300
344
|
}
|
|
301
|
-
const componentPath = join(out, "app.wasm");
|
|
302
|
-
writeFileSync(componentPath, component);
|
|
303
|
-
// A development signature from an earlier `pollis app dev` signs another App hash. Left beside
|
|
304
|
-
// this build it makes a development installation refuse the build as tampered instead of
|
|
305
|
-
// offering it to an admin; `pollis app dev` signs each new build itself.
|
|
306
|
-
rmSync(`${componentPath}.sig.json`, { force: true });
|
|
307
345
|
// 4. The real host's verdict, and the App hash.
|
|
308
|
-
|
|
309
|
-
devTool(bin, ["check", componentPath, manifestPath]);
|
|
310
|
-
}
|
|
311
|
-
catch (e) {
|
|
312
|
-
rmSync(componentPath, { force: true });
|
|
313
|
-
throw new BuildError(`the App host refuses this build: ${e.message}`);
|
|
314
|
-
}
|
|
315
|
-
const appHash = devTool(bin, ["hash", componentPath, manifestPath]);
|
|
316
|
-
return { component: componentPath, manifestPath, manifest, appHash, bytes: component.length };
|
|
346
|
+
return finishBuild(bin, out, manifest, component);
|
|
317
347
|
}
|
|
318
348
|
finally {
|
|
319
349
|
rmSync(scratch, { recursive: true, force: true });
|
package/dist/errors.d.ts
ADDED
package/dist/errors.js
ADDED
package/dist/main.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* `pollis`: the Pollis command line for App developers.
|
|
4
4
|
*
|
|
5
|
-
* pollis app new <name> --lang ts a new App: one handler, a test, a README
|
|
5
|
+
* pollis app new <name> --lang ts|rust a new App: one handler, a test, a README
|
|
6
6
|
* pollis app build compile to dist/app.wasm + dist/pollis-app.json, print the App hash
|
|
7
7
|
* pollis app test build, then run the tests offline in the real sandbox
|
|
8
8
|
* pollis app dev --installation ID development installation: rebuild and sign on save, in pollis-bot
|
package/dist/main.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* `pollis`: the Pollis command line for App developers.
|
|
4
4
|
*
|
|
5
|
-
* pollis app new <name> --lang ts a new App: one handler, a test, a README
|
|
5
|
+
* pollis app new <name> --lang ts|rust a new App: one handler, a test, a README
|
|
6
6
|
* pollis app build compile to dist/app.wasm + dist/pollis-app.json, print the App hash
|
|
7
7
|
* pollis app test build, then run the tests offline in the real sandbox
|
|
8
8
|
* pollis app dev --installation ID development installation: rebuild and sign on save, in pollis-bot
|
|
@@ -16,7 +16,8 @@ import { devApp, runApp } from "./runtime.js";
|
|
|
16
16
|
import { testApp } from "./test.js";
|
|
17
17
|
const HELP = `pollis app <command>
|
|
18
18
|
|
|
19
|
-
new <name> --lang ts
|
|
19
|
+
new <name> --lang ts|rust
|
|
20
|
+
Create an App in TypeScript or Rust: one handler, a test and a README.
|
|
20
21
|
build Compile the App to dist/app.wasm and write dist/pollis-app.json.
|
|
21
22
|
Prints the App hash an admin approves.
|
|
22
23
|
test [files...] Build, then run the App's tests offline in the real sandbox.
|
|
@@ -59,15 +60,16 @@ async function main(argv) {
|
|
|
59
60
|
case "new": {
|
|
60
61
|
const name = rest[0];
|
|
61
62
|
if (!name) {
|
|
62
|
-
console.error("usage: pollis app new <name> --lang ts");
|
|
63
|
+
console.error("usage: pollis app new <name> --lang ts|rust");
|
|
63
64
|
return 2;
|
|
64
65
|
}
|
|
65
66
|
if (!values.lang) {
|
|
66
|
-
console.error("say which language with --lang (TypeScript: --lang ts)");
|
|
67
|
+
console.error("say which language with --lang (TypeScript: --lang ts, Rust: --lang rust)");
|
|
67
68
|
return 2;
|
|
68
69
|
}
|
|
69
70
|
const target = newApp({ name, lang: values.lang, parent: values.dir, sdk: values.sdk });
|
|
70
|
-
|
|
71
|
+
const next = values.lang === "rust" ? "npx pollis app test" : "pnpm install\n pnpm test";
|
|
72
|
+
console.log(`Created ${target}\n\nNext:\n cd ${name}\n ${next}`);
|
|
71
73
|
return 0;
|
|
72
74
|
}
|
|
73
75
|
case "build": {
|
package/dist/new.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `pollis app new <name> --lang ts`: a new App from the template: one
|
|
2
|
+
* `pollis app new <name> --lang ts|rust`: a new App from the template: one
|
|
3
3
|
* handler, a test and a README.
|
|
4
4
|
*/
|
|
5
5
|
export declare class NewError extends Error {
|
|
@@ -11,7 +11,7 @@ export interface NewOptions {
|
|
|
11
11
|
parent?: string;
|
|
12
12
|
/**
|
|
13
13
|
* Use local copies of the SDK packages (a path to the Pollis repo's
|
|
14
|
-
* `sdk/ts`), for working before or beside the npm releases.
|
|
14
|
+
* `sdk/ts`), for working before or beside the npm and crates.io releases.
|
|
15
15
|
*/
|
|
16
16
|
sdk?: string;
|
|
17
17
|
}
|
package/dist/new.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `pollis app new <name> --lang ts`: a new App from the template: one
|
|
2
|
+
* `pollis app new <name> --lang ts|rust`: a new App from the template: one
|
|
3
3
|
* handler, a test and a README.
|
|
4
4
|
*/
|
|
5
5
|
import { cpSync, existsSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
@@ -16,9 +16,42 @@ function sdkVersion() {
|
|
|
16
16
|
const path = join(dirname(dirname(fileURLToPath(import.meta.url))), "package.json");
|
|
17
17
|
return `^${JSON.parse(readFileSync(path, "utf8")).version}`;
|
|
18
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* The `pollis-app` dependency line of a new Rust App. With `--sdk` the crate is
|
|
21
|
+
* copied into the App: Cargo hashes the absolute path of a dependency outside
|
|
22
|
+
* the project into the build, so a link would change the App hash with the
|
|
23
|
+
* location of the checkout.
|
|
24
|
+
*/
|
|
25
|
+
function rustDependency(target, sdk) {
|
|
26
|
+
if (!sdk) {
|
|
27
|
+
// The crate shares the CLI's major.minor; its patch releases are its own,
|
|
28
|
+
// so a CLI patch release never asks for a crate version that does not exist.
|
|
29
|
+
const [major, minor] = sdkVersion().slice(1).split(".");
|
|
30
|
+
return JSON.stringify(`${major}.${minor}`);
|
|
31
|
+
}
|
|
32
|
+
const crate = join(resolve(sdk), "..", "rust", "pollis-app");
|
|
33
|
+
if (!existsSync(join(crate, "Cargo.toml"))) {
|
|
34
|
+
throw new NewError(`--sdk ${sdk}: expected the Pollis repo's sdk/ts directory (with ../rust/pollis-app beside it)`);
|
|
35
|
+
}
|
|
36
|
+
cpSync(crate, join(target, "vendor", "pollis-app"), {
|
|
37
|
+
recursive: true,
|
|
38
|
+
filter: (source) => !["target", "Cargo.lock"].includes(relative(crate, source)),
|
|
39
|
+
});
|
|
40
|
+
return '{ path = "vendor/pollis-app" }';
|
|
41
|
+
}
|
|
42
|
+
function newRustApp(target, options) {
|
|
43
|
+
cpSync(join(templates(), "rust"), target, { recursive: true });
|
|
44
|
+
const dependency = rustDependency(target, options.sdk);
|
|
45
|
+
renameSync(join(target, "gitignore"), join(target, ".gitignore"));
|
|
46
|
+
const fill = (text) => text.replaceAll("{{name}}", options.name).replaceAll("{{pollis_app}}", dependency);
|
|
47
|
+
const tmpl = join(target, "Cargo.toml.tmpl");
|
|
48
|
+
writeFileSync(join(target, "Cargo.toml"), fill(readFileSync(tmpl, "utf8")));
|
|
49
|
+
unlinkSync(tmpl);
|
|
50
|
+
writeFileSync(join(target, "README.md"), fill(readFileSync(join(target, "README.md"), "utf8")));
|
|
51
|
+
}
|
|
19
52
|
export function newApp(options) {
|
|
20
|
-
if (options.lang !== "ts") {
|
|
21
|
-
throw new NewError(`--lang ${options.lang} is not available; TypeScript (--lang ts)
|
|
53
|
+
if (options.lang !== "ts" && options.lang !== "rust") {
|
|
54
|
+
throw new NewError(`--lang ${options.lang} is not available; use TypeScript (--lang ts) or Rust (--lang rust).`);
|
|
22
55
|
}
|
|
23
56
|
if (!NAME.test(options.name)) {
|
|
24
57
|
throw new NewError(`"${options.name}" is not a valid App name: lowercase letters, digits and '-', starting with a letter or digit`);
|
|
@@ -27,6 +60,10 @@ export function newApp(options) {
|
|
|
27
60
|
if (existsSync(target) && readdirSync(target).length > 0) {
|
|
28
61
|
throw new NewError(`${target} already exists and is not empty`);
|
|
29
62
|
}
|
|
63
|
+
if (options.lang === "rust") {
|
|
64
|
+
newRustApp(target, options);
|
|
65
|
+
return target;
|
|
66
|
+
}
|
|
30
67
|
cpSync(join(templates(), "ts"), target, { recursive: true });
|
|
31
68
|
renameSync(join(target, "gitignore"), join(target, ".gitignore"));
|
|
32
69
|
let app = sdkVersion();
|
package/dist/runtime.js
CHANGED
|
@@ -146,7 +146,7 @@ export async function devApp(options) {
|
|
|
146
146
|
} while (again);
|
|
147
147
|
building = false;
|
|
148
148
|
};
|
|
149
|
-
const watched = ["src", "package.json"].map((p) => join(dir, p)).filter((p) => existsSync(p));
|
|
149
|
+
const watched = ["src", "package.json", "Cargo.toml", "Cargo.lock"].map((p) => join(dir, p)).filter((p) => existsSync(p));
|
|
150
150
|
const watchers = watched.map((p) => watch(p, { recursive: true }, () => {
|
|
151
151
|
void rebuild();
|
|
152
152
|
}));
|
package/dist/rust.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `pollis app build` and `test` for an App written in Rust.
|
|
3
|
+
*
|
|
4
|
+
* 1. Run the App's generated `__pollis_app_describe` test natively, which
|
|
5
|
+
* writes what `pollis_app::app!` declares, and resolve the manifest with the
|
|
6
|
+
* same code a TypeScript App uses.
|
|
7
|
+
* 2. `cargo build --target wasm32-wasip2`: rustc links a component directly, so
|
|
8
|
+
* an App developer needs no extra tool. Source paths are remapped out of the
|
|
9
|
+
* binary and the lockfile is enforced, so the same source, lockfile and
|
|
10
|
+
* toolchain give the same bytes wherever they are built.
|
|
11
|
+
*/
|
|
12
|
+
import { type ResolvedManifest } from "@pollis-inc/app/manifest";
|
|
13
|
+
/** Whether `dir` holds a Rust App: a crate that depends on `pollis-app`. */
|
|
14
|
+
export declare function isRustApp(dir: string): boolean;
|
|
15
|
+
export interface RustBuild {
|
|
16
|
+
manifest: ResolvedManifest;
|
|
17
|
+
component: Uint8Array;
|
|
18
|
+
}
|
|
19
|
+
/** Compile the Rust App in `dir` to a component and read its manifest. */
|
|
20
|
+
export declare function compileRust(dir: string, scratch: string): RustBuild;
|
|
21
|
+
/** Run the App's Rust tests against the build in `dist/`. */
|
|
22
|
+
export declare function testRust(dir: string, patterns: string[], env: NodeJS.ProcessEnv): number;
|
package/dist/rust.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `pollis app build` and `test` for an App written in Rust.
|
|
3
|
+
*
|
|
4
|
+
* 1. Run the App's generated `__pollis_app_describe` test natively, which
|
|
5
|
+
* writes what `pollis_app::app!` declares, and resolve the manifest with the
|
|
6
|
+
* same code a TypeScript App uses.
|
|
7
|
+
* 2. `cargo build --target wasm32-wasip2`: rustc links a component directly, so
|
|
8
|
+
* an App developer needs no extra tool. Source paths are remapped out of the
|
|
9
|
+
* binary and the lockfile is enforced, so the same source, lockfile and
|
|
10
|
+
* toolchain give the same bytes wherever they are built.
|
|
11
|
+
*/
|
|
12
|
+
import { spawnSync } from "node:child_process";
|
|
13
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
14
|
+
import { homedir } from "node:os";
|
|
15
|
+
import { dirname, join } from "node:path";
|
|
16
|
+
import { ManifestError, resolveManifest } from "@pollis-inc/app/manifest";
|
|
17
|
+
import { BuildError } from "./errors.js";
|
|
18
|
+
const TARGET = "wasm32-wasip2";
|
|
19
|
+
const NO_CARGO = "`cargo` was not found. Rust Apps need Rust: install it from https://rustup.rs";
|
|
20
|
+
/** Whether `dir` holds a Rust App: a crate that depends on `pollis-app`. */
|
|
21
|
+
export function isRustApp(dir) {
|
|
22
|
+
const path = join(dir, "Cargo.toml");
|
|
23
|
+
return existsSync(path) && /^\s*(pollis-app\s*[=.]|\[dependencies\.pollis-app\])/m.test(readFileSync(path, "utf8"));
|
|
24
|
+
}
|
|
25
|
+
function cargo(dir, args, env = {}) {
|
|
26
|
+
const result = spawnSync("cargo", args, {
|
|
27
|
+
cwd: dir,
|
|
28
|
+
encoding: "utf8",
|
|
29
|
+
maxBuffer: 256 * 1024 * 1024,
|
|
30
|
+
env: { ...process.env, ...env },
|
|
31
|
+
});
|
|
32
|
+
if (result.error?.code === "ENOENT") {
|
|
33
|
+
throw new BuildError(NO_CARGO);
|
|
34
|
+
}
|
|
35
|
+
if (result.error) {
|
|
36
|
+
throw result.error;
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
/** Cargo's error, with the fix for the mistakes a new App developer is likely to hit. */
|
|
41
|
+
function cargoFailure(what, stderr) {
|
|
42
|
+
let advice = "";
|
|
43
|
+
if (/no matching package named `pollis-app`/.test(stderr)) {
|
|
44
|
+
advice =
|
|
45
|
+
"\n\nThe `pollis-app` crate could not be downloaded. To use the copy in a Pollis checkout, create the App " +
|
|
46
|
+
"with `pollis app new <name> --lang rust --sdk <checkout>/sdk/ts`.";
|
|
47
|
+
}
|
|
48
|
+
else if (/can't find crate for `(core|std)`|target may not be installed/.test(stderr)) {
|
|
49
|
+
advice = `\n\nThe WebAssembly target is missing. Install it with: rustup target add ${TARGET}`;
|
|
50
|
+
}
|
|
51
|
+
else if (/lock file .* needs to be updated|--locked was passed/.test(stderr)) {
|
|
52
|
+
advice = "\n\nCargo.lock does not match Cargo.toml. Run `cargo update --workspace`, then commit Cargo.lock.";
|
|
53
|
+
}
|
|
54
|
+
return new BuildError(`${what} failed:\n${stderr.trim()}${advice}`);
|
|
55
|
+
}
|
|
56
|
+
function metadata(dir) {
|
|
57
|
+
const result = cargo(dir, ["metadata", "--format-version", "1", "--locked"]);
|
|
58
|
+
if (result.status !== 0) {
|
|
59
|
+
throw cargoFailure("`cargo metadata`", result.stderr);
|
|
60
|
+
}
|
|
61
|
+
const meta = JSON.parse(result.stdout);
|
|
62
|
+
const manifest = join(realpathSync(dir), "Cargo.toml");
|
|
63
|
+
const root = meta.packages.find((p) => p.manifest_path === manifest);
|
|
64
|
+
if (!root) {
|
|
65
|
+
throw new BuildError(`no package in ${manifest}; a Rust App is a single crate, not a virtual workspace`);
|
|
66
|
+
}
|
|
67
|
+
return { root, packages: meta.packages, targetDir: meta.target_directory };
|
|
68
|
+
}
|
|
69
|
+
/** Handler names as Rust spells them, in a message written for TypeScript. */
|
|
70
|
+
function rustNames(message) {
|
|
71
|
+
return message.replace(/\bon[A-Z][a-z]+\b/g, (name) => name.replace(/[A-Z]/g, (c) => `_${c.toLowerCase()}`));
|
|
72
|
+
}
|
|
73
|
+
/** Read the manifest out of the code, by running the App's describe test natively. */
|
|
74
|
+
function describe(dir, scratch) {
|
|
75
|
+
const out = join(scratch, "describe.json");
|
|
76
|
+
const result = cargo(dir, ["test", "--locked", "--lib", "--quiet", "--", "__pollis_app_describe"], {
|
|
77
|
+
POLLIS_APP_DESCRIBE_OUT: out,
|
|
78
|
+
});
|
|
79
|
+
if (result.status !== 0) {
|
|
80
|
+
throw cargoFailure("compiling the App", result.stderr || result.stdout);
|
|
81
|
+
}
|
|
82
|
+
if (!existsSync(out)) {
|
|
83
|
+
throw new BuildError("src/lib.rs must call `pollis_app::app! { manifest: ..., on_command: ... }` at the root of the crate");
|
|
84
|
+
}
|
|
85
|
+
const described = JSON.parse(readFileSync(out, "utf8"));
|
|
86
|
+
try {
|
|
87
|
+
return resolveManifest(described.manifest, described.handlers, described.package);
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
if (e instanceof ManifestError) {
|
|
91
|
+
throw new BuildError(`manifest: ${rustNames(e.message)}`);
|
|
92
|
+
}
|
|
93
|
+
throw e;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The flags that keep machine-specific paths out of the component. Later
|
|
98
|
+
* remaps win, so the more specific paths come after the App's own directory.
|
|
99
|
+
*/
|
|
100
|
+
function rustFlags(dir, packages, targetDir) {
|
|
101
|
+
const cargoHome = process.env.CARGO_HOME ?? join(homedir(), ".cargo");
|
|
102
|
+
const remaps = [
|
|
103
|
+
[realpathSync(dir), "/app"],
|
|
104
|
+
[cargoHome, "/cargo"],
|
|
105
|
+
...packages
|
|
106
|
+
.filter((p) => p.source === null)
|
|
107
|
+
.map((p) => [dirname(p.manifest_path), `/path/${p.name}`]),
|
|
108
|
+
[targetDir, "/target"],
|
|
109
|
+
];
|
|
110
|
+
const flags = ["-Cdebuginfo=0", "-Cstrip=symbols", ...remaps.map(([from, to]) => `--remap-path-prefix=${from}=${to}`)];
|
|
111
|
+
// CARGO_ENCODED_RUSTFLAGS replaces anything set in RUSTFLAGS or .cargo/config.toml.
|
|
112
|
+
return flags.join("\x1f");
|
|
113
|
+
}
|
|
114
|
+
/** Compile the Rust App in `dir` to a component and read its manifest. */
|
|
115
|
+
export function compileRust(dir, scratch) {
|
|
116
|
+
if (!existsSync(join(dir, "Cargo.lock"))) {
|
|
117
|
+
const locked = cargo(dir, ["generate-lockfile"]);
|
|
118
|
+
if (locked.status !== 0) {
|
|
119
|
+
throw cargoFailure("`cargo generate-lockfile`", locked.stderr);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const { root, packages, targetDir } = metadata(dir);
|
|
123
|
+
const lib = root.targets.find((t) => t.crate_types.includes("cdylib"));
|
|
124
|
+
if (!lib) {
|
|
125
|
+
throw new BuildError('Cargo.toml needs a component library: add `[lib]` with `crate-type = ["cdylib"]`');
|
|
126
|
+
}
|
|
127
|
+
const manifest = describe(dir, scratch);
|
|
128
|
+
const result = cargo(dir, ["build", "--locked", "--release", "--lib", "--target", TARGET], {
|
|
129
|
+
CARGO_ENCODED_RUSTFLAGS: rustFlags(dir, packages, targetDir),
|
|
130
|
+
SOURCE_DATE_EPOCH: "0",
|
|
131
|
+
});
|
|
132
|
+
if (result.status !== 0) {
|
|
133
|
+
throw cargoFailure("compiling to a component", result.stderr);
|
|
134
|
+
}
|
|
135
|
+
const wasm = join(targetDir, TARGET, "release", `${lib.name.replaceAll("-", "_")}.wasm`);
|
|
136
|
+
return { manifest, component: readFileSync(wasm) };
|
|
137
|
+
}
|
|
138
|
+
/** Run the App's Rust tests against the build in `dist/`. */
|
|
139
|
+
export function testRust(dir, patterns, env) {
|
|
140
|
+
const filters = patterns.length > 0 ? ["--", ...patterns] : [];
|
|
141
|
+
const result = spawnSync("cargo", ["test", "--locked", ...filters], {
|
|
142
|
+
cwd: dir,
|
|
143
|
+
stdio: "inherit",
|
|
144
|
+
env: { ...process.env, ...env },
|
|
145
|
+
});
|
|
146
|
+
if (result.error?.code === "ENOENT") {
|
|
147
|
+
throw new BuildError(NO_CARGO);
|
|
148
|
+
}
|
|
149
|
+
return result.status ?? 1;
|
|
150
|
+
}
|
package/dist/test.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `pollis app test`: build the App, then run its
|
|
3
|
-
* test
|
|
2
|
+
* `pollis app test`: build the App, then run its tests against the build. A
|
|
3
|
+
* TypeScript App's `*.test.ts` files run under Node's test runner, wired up
|
|
4
|
+
* through `@pollis-inc/app/testing`; a Rust App's run under `cargo test`, wired
|
|
5
|
+
* up through `pollis_app::testing`.
|
|
4
6
|
*/
|
|
5
7
|
export interface TestOptions {
|
|
6
8
|
dir: string;
|
|
7
9
|
build: boolean;
|
|
8
|
-
/** Test file globs, relative to `dir`. */
|
|
10
|
+
/** Test file globs, relative to `dir`. For a Rust App, `cargo test` name filters. */
|
|
9
11
|
patterns: string[];
|
|
10
12
|
}
|
|
11
13
|
export declare function testApp(options: TestOptions): Promise<number>;
|
package/dist/test.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `pollis app test`: build the App, then run its
|
|
3
|
-
* test
|
|
2
|
+
* `pollis app test`: build the App, then run its tests against the build. A
|
|
3
|
+
* TypeScript App's `*.test.ts` files run under Node's test runner, wired up
|
|
4
|
+
* through `@pollis-inc/app/testing`; a Rust App's run under `cargo test`, wired
|
|
5
|
+
* up through `pollis_app::testing`.
|
|
4
6
|
*/
|
|
5
7
|
import { spawnSync } from "node:child_process";
|
|
6
8
|
import { globSync } from "node:fs";
|
|
7
9
|
import { join, resolve } from "node:path";
|
|
8
10
|
import { buildApp } from "./build.js";
|
|
9
11
|
import { findDevTool } from "./devtool.js";
|
|
12
|
+
import { isRustApp, testRust } from "./rust.js";
|
|
10
13
|
export async function testApp(options) {
|
|
11
14
|
const dir = resolve(options.dir);
|
|
12
15
|
const bin = findDevTool();
|
|
@@ -16,6 +19,13 @@ export async function testApp(options) {
|
|
|
16
19
|
const built = await buildApp({ dir, quiet: true });
|
|
17
20
|
console.log(`built ${built.manifest.name} ${built.manifest.version} (App hash ${built.appHash})`);
|
|
18
21
|
}
|
|
22
|
+
if (isRustApp(dir)) {
|
|
23
|
+
return testRust(dir, options.patterns, {
|
|
24
|
+
POLLIS_APP_COMPONENT: component,
|
|
25
|
+
POLLIS_APP_MANIFEST: manifest,
|
|
26
|
+
POLLIS_APP_DEV_BIN: bin,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
19
29
|
const patterns = options.patterns.length > 0 ? options.patterns : ["src/**/*.test.ts", "test/**/*.test.ts"];
|
|
20
30
|
const files = patterns.flatMap((p) => globSync(p, { cwd: dir })).sort();
|
|
21
31
|
if (files.length === 0) {
|
package/package.json
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pollis-inc/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "The `pollis app` command: create, build and test Pollis Apps.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
+
"homepage": "https://pollis.com/bots-developers.html",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/pollis-app/pollis.git",
|
|
10
|
+
"directory": "sdk/ts/cli"
|
|
11
|
+
},
|
|
6
12
|
"type": "module",
|
|
7
13
|
"bin": {
|
|
8
14
|
"pollis": "./dist/main.js"
|
|
@@ -17,7 +23,7 @@
|
|
|
17
23
|
"dependencies": {
|
|
18
24
|
"@bytecodealliance/componentize-js": "0.23.0",
|
|
19
25
|
"esbuild": "0.28.2",
|
|
20
|
-
"@pollis-inc/app": "0.1.
|
|
26
|
+
"@pollis-inc/app": "0.1.1"
|
|
21
27
|
},
|
|
22
28
|
"devDependencies": {
|
|
23
29
|
"@types/node": "^22.19.0",
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "{{name}}"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
description = "A Pollis App."
|
|
6
|
+
publish = false
|
|
7
|
+
|
|
8
|
+
[lib]
|
|
9
|
+
name = "app"
|
|
10
|
+
crate-type = ["cdylib"]
|
|
11
|
+
|
|
12
|
+
[dependencies]
|
|
13
|
+
pollis-app = {{pollis_app}}
|
|
14
|
+
|
|
15
|
+
# Small and reproducible: the same source and Cargo.lock give the same App hash.
|
|
16
|
+
[profile.release]
|
|
17
|
+
opt-level = "s"
|
|
18
|
+
lto = true
|
|
19
|
+
codegen-units = 1
|
|
20
|
+
panic = "abort"
|
|
21
|
+
strip = true
|
|
22
|
+
|
|
23
|
+
# This App stands alone, even inside another Cargo workspace.
|
|
24
|
+
[workspace]
|
|
25
|
+
exclude = ["vendor"]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# {{name}}
|
|
2
|
+
|
|
3
|
+
A [Pollis](https://pollis.com) App, in Rust. It answers `/hello` with a greeting.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npx pollis app test # build, then run tests/hello.rs against the built App, offline
|
|
7
|
+
npx pollis app build # writes dist/app.wasm and dist/pollis-app.json, prints the App hash
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
- `src/lib.rs`: the App. Its `manifest` says what it needs; its handlers say what it does.
|
|
11
|
+
- `tests/hello.rs`: tests. Each one runs the built App in the same sandbox the bot
|
|
12
|
+
runtime uses, so anything the manifest does not allow fails here too.
|
|
13
|
+
- `Cargo.lock` and `rust-toolchain.toml`: commit both. With them, anyone can rebuild this
|
|
14
|
+
App from source and get the same App hash.
|
|
15
|
+
- `dist/`: build output. The App hash identifies exactly this build; an admin approves it.
|
|
16
|
+
|
|
17
|
+
The developer guide explains every handler, what `ctx` can do, the manifest and the
|
|
18
|
+
sandbox's limits: https://github.com/pollis-app/pollis/blob/main/docs/bots/developer-guide.md
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
use pollis_app::{Action, Command, CommandSpec, Context, Manifest};
|
|
2
|
+
|
|
3
|
+
// Runs when a member sends one of the commands declared below, e.g. "/hello Sam".
|
|
4
|
+
fn on_command(cmd: Command, ctx: &Context) -> pollis_app::Result {
|
|
5
|
+
if cmd.name != "hello" {
|
|
6
|
+
return Ok(());
|
|
7
|
+
}
|
|
8
|
+
let who = match cmd.args.trim() {
|
|
9
|
+
"" => cmd.message.author.display_name.as_str(),
|
|
10
|
+
name => name,
|
|
11
|
+
};
|
|
12
|
+
ctx.reply(&cmd.message, format!("Hello, {who}!"))
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
pollis_app::app! {
|
|
16
|
+
manifest: Manifest::new()
|
|
17
|
+
// Where the App works. "*" means: whichever channels the admin picks
|
|
18
|
+
// when approving it. Use "#name" to ask for one channel by name.
|
|
19
|
+
.channels(["*"])
|
|
20
|
+
// What the App may do there. The admin sees this list before approving.
|
|
21
|
+
.actions([Action::Reply])
|
|
22
|
+
// No `.egress(...)`: this App cannot send anything outside the group.
|
|
23
|
+
// The slash commands members can pick in the composer.
|
|
24
|
+
.command(CommandSpec::new("hello", "Say hello").arg_hint("[name]")),
|
|
25
|
+
on_command: on_command,
|
|
26
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
use pollis_app::testing::{AuthorInput, JobOptions, MessageInput, TestApp};
|
|
2
|
+
|
|
3
|
+
// Each test runs the built App in the same sandbox the bot runtime uses, offline.
|
|
4
|
+
|
|
5
|
+
#[test]
|
|
6
|
+
fn hello_greets_by_name() {
|
|
7
|
+
let result = TestApp::new().command("hello", "Sam");
|
|
8
|
+
assert!(result.ok, "{:?}", result.error);
|
|
9
|
+
assert_eq!(result.replies, ["Hello, Sam!"]);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
#[test]
|
|
13
|
+
fn hello_with_no_name_greets_the_sender() {
|
|
14
|
+
let app = TestApp::new();
|
|
15
|
+
let sender = MessageInput::default().author(AuthorInput::named("Ann"));
|
|
16
|
+
let result = app.with(JobOptions::new()).command_from("hello", "", sender);
|
|
17
|
+
assert_eq!(result.replies, ["Hello, Ann!"]);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
#[test]
|
|
21
|
+
fn a_command_the_app_did_not_declare_never_reaches_it() {
|
|
22
|
+
let result = TestApp::new().command("weather", "");
|
|
23
|
+
assert!(result.actions.is_empty());
|
|
24
|
+
}
|