Skip to content

Commit

Permalink
binding: upgrade ruby implementation, add error on upgrading deps
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Jan 28, 2024
1 parent c93a4cb commit 3a98b1a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1,225 deletions.
52 changes: 36 additions & 16 deletions packages/binding/src/bindings/depend.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
import { tryreturn } from "rcompat/async";
import { to } from "rcompat/object";
import { packager } from "rcompat/meta";
import { packager, manifest } from "rcompat/meta";
import { File } from "rcompat/fs";
import errors from "../errors.js";

const { MissingDependencies } = errors;
const MODULE_NOT_FOUND = "ERR_MODULE_NOT_FOUND";
// this isn't a complete semver comparison, but should work for most cases
const semver = (target, is) => {
if (target.startsWith("^")) {
return semver(Number(target.slice(1), is));
}
if (is.startsWith("^")) {
return semver(target, Number(is.slice(1)));
}

return is >= target;
};

export default async (dependencies, from) => {
const modules = Object.keys(dependencies);
const compare_dependencies = (target, current) =>
to(target).filter(([key, value]) => !semver(value, current[key]));

const results = await Promise.all(modules.map(module =>
tryreturn(_ => import(module))
.orelse(({ code }) => code === MODULE_NOT_FOUND ? module : {})));
const find_dependencies = (target, current) =>
to(target).filter(([key]) => !current[key]);

const { MissingDependencies, UpgradeDependencies } = errors;

export default async (target_dependencies, from) => {
const { dependencies } = await (await File.root()).join(manifest).json();

const versions = find_dependencies(target_dependencies, dependencies);
if (versions.length > 0) {
const keys = versions.map(([key]) => key);
const to_upgrade = versions.map(([key, value]) => `${key}@${value}`);
const install = `${packager} install ${to_upgrade.join(" ")}`;
MissingDependencies.throw(keys.join(", "), from, install);
}

const errored = results.filter(result => typeof result === "string");
const versions = to(dependencies)
.filter(([dependency]) => errored.includes(dependency))
.map(([key, value]) => `${key}@${value}`);
if (errored.length > 0) {
const install = module => `${packager} install ${module.join(" ")}`;
MissingDependencies.throw(errored.join(", "), from, install(versions));
const upgradeable = compare_dependencies(target_dependencies, dependencies);
if (upgradeable.length > 0) {
const keys = upgradeable.map(([key]) => key);
const to_upgrade = upgradeable.map(([key, value]) => `${key}@${value}`);
const install = `${packager} install ${to_upgrade.join(" ")}`;
UpgradeDependencies.throw(keys.join(", "), from, install);
}
return results.filter(result => typeof result !== "string");
};
4 changes: 2 additions & 2 deletions packages/binding/src/bindings/ruby/exports.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { File } from "rcompat/fs";
import { DefaultRubyVM as RubyVM } from "./rubyvm.js";
import { DefaultRubyVM } from "@ruby/wasm-wasi/dist/node";

const ruby_path = (await File.root())
.join("node_modules/@ruby/head-wasm-wasi/dist/ruby+stdlib.wasm");
Expand All @@ -8,4 +8,4 @@ const module = await WebAssembly.compile(ruby_wasm);

export { default as make_response } from "./make_response.js";
export { module };
export { RubyVM as rubyvm };
export { DefaultRubyVM as rubyvm };
Loading

0 comments on commit 3a98b1a

Please sign in to comment.