Skip to content

Commit 2df79cb

Browse files
committed
+ bin/ruby + shebang support
1 parent 5d93ebb commit 2df79cb

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

src/templates/default/bin/ruby

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env -S node --disable-warning=ExperimentalWarning
2+
3+
import initVM from "../lib/rails.js";
4+
import util from "node:util";
5+
import fs from "node:fs";
6+
7+
const args = process.argv.slice(2);
8+
9+
if (args[0] && fs.existsSync(process.cwd() + '/' + args[0])) {
10+
const firstLine = fs.readFileSync(process.cwd() + '/' + args[0], 'utf8').split('\n')[0];
11+
// Check if called via shebang
12+
if (firstLine.includes('#!/usr/bin/env ruby')) {
13+
const path = args.shift();
14+
const vm = await initVM({ env: { "HOME": "/rails-vm" } });
15+
16+
await vm.evalAsync(`
17+
args = ${util.inspect(args)}
18+
ARGV.replace(args)
19+
20+
begin
21+
load "./${path}"
22+
rescue SystemExit
23+
end
24+
`);
25+
}
26+
} else {
27+
await initVM({ env: { "HOME": "/rails-vm" }, args, skipRails: true });
28+
}

src/templates/default/lib/rails.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import fs from "fs/promises";
44
import { setupDatabase } from "./database.js";
55

66
const rubyWasm = new URL("../node_modules/@rails-tutorial/wasm/dist/rails.wasm", import.meta.url).pathname;
7+
78
const railsRootDir = new URL("../workspace/store", import.meta.url).pathname;
89
const pgDataDir = new URL("../pgdata", import.meta.url).pathname;
910

1011
export default async function initVM(vmopts = {}) {
11-
const { env, args } = vmopts;
12+
const { env, args, skipRails } = vmopts;
1213
const binary = await fs.readFile(rubyWasm);
1314
const module = await WebAssembly.compile(binary);
1415

@@ -19,6 +20,8 @@ export default async function initVM(vmopts = {}) {
1920
`/workspace${process.cwd().slice(workspaceDir.length)}` :
2021
"";
2122

23+
const cliArgs = args?.length ? ['ruby.wasm'].concat(args) : undefined;
24+
2225
const wasi = new WASI(
2326
{
2427
env: {"RUBYOPT": "-EUTF-8 -W0", ...env},
@@ -27,26 +30,31 @@ export default async function initVM(vmopts = {}) {
2730
preopens: {
2831
"/workspace": workspaceDir
2932
},
30-
args: args || [] // FIXME: doesn't work
33+
args: cliArgs
3134
}
3235
);
36+
3337
const { vm } = await RubyVM.instantiateModule({
34-
module, wasip1: wasi
38+
module,
39+
wasip1: wasi,
40+
args: cliArgs
3541
});
3642

37-
try {
38-
await fs.readdir(railsRootDir);
39-
await setupDatabase(pgDataDir);
40-
} catch (error) {
41-
// not database directory — skip it
42-
}
43+
if (!skipRails) {
44+
try {
45+
await fs.readdir(railsRootDir);
46+
await setupDatabase(pgDataDir);
47+
} catch (error) {
48+
// not database directory — skip it
49+
}
4350

44-
vm.eval(`
45-
Dir.chdir("${workdir}") unless "${workdir}".empty?
46-
require "/rails-vm/boot"
51+
vm.eval(`
52+
Dir.chdir("${workdir}") unless "${workdir}".empty?
53+
require "/rails-vm/boot"
4754
48-
require "js"
49-
`)
55+
require "js"
56+
`)
57+
}
5058

5159
return vm;
5260
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env ruby
2+
APP_PATH = File.expand_path("../config/application", __dir__)
3+
require_relative "../config/boot"
4+
require "rails/commands"

0 commit comments

Comments
 (0)