ruby-anything 0.2.0 → 0.2.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/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +9 -2
- package/README.md +15 -1
- package/adapters/node/harness.mjs +3 -1
- package/adapters/node/package-lock.json +26 -26
- package/adapters/node/package.json +7 -7
- package/adapters/polyglot/harness.py +350 -350
- package/adapters/polyglot/lang_csharp.py +222 -222
- package/adapters/polyglot/lang_java.py +510 -510
- package/adapters/polyglot/lang_rust.py +430 -430
- package/adapters/polyglot/lang_swift.py +337 -337
- package/adapters/ruby/harness.rb +134 -0
- package/package.json +3 -3
- package/src/adapters.js +60 -63
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# PortPilot Ruby adapter.
|
|
2
|
+
#
|
|
3
|
+
# Runs in a separate process (never inside Rails). Reads one JSON request on stdin and
|
|
4
|
+
# writes JSON lines on stdout. The migrated code must define `module Migrated` with
|
|
5
|
+
# singleton methods named like the original functions.
|
|
6
|
+
#
|
|
7
|
+
# Commands: replay (run each case once, capture behaviour), bench (time N iterations per case).
|
|
8
|
+
require "json"
|
|
9
|
+
require "stringio"
|
|
10
|
+
require "tmpdir"
|
|
11
|
+
require "digest"
|
|
12
|
+
require "set"
|
|
13
|
+
|
|
14
|
+
REAL_STDOUT = $stdout.dup
|
|
15
|
+
REAL_STDOUT.sync = true
|
|
16
|
+
|
|
17
|
+
def emit(event, **data)
|
|
18
|
+
REAL_STDOUT.write(JSON.generate(data.merge(event: event)) + "\n")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def encode(value)
|
|
22
|
+
case value
|
|
23
|
+
when nil, true, false, String, Integer then value
|
|
24
|
+
when Float
|
|
25
|
+
return { "$float" => "nan" } if value.nan?
|
|
26
|
+
return { "$float" => value.positive? ? "inf" : "-inf" } if value.infinite?
|
|
27
|
+
return { "$float" => "-0.0" } if value.zero? && (1.0 / value).negative?
|
|
28
|
+
return { "$float" => value.to_s } if value == value.floor && value.abs > 2**53 # inexact integral double
|
|
29
|
+
|
|
30
|
+
value
|
|
31
|
+
when Array then value.map { |v| encode(v) }
|
|
32
|
+
when Hash then value.to_h { |k, v| [ k.to_s, encode(v) ] }
|
|
33
|
+
when Set then { "$set" => value.map { |v| encode(v) }.sort_by { |v| JSON.generate(v) } }
|
|
34
|
+
else { "$repr" => value.inspect[0, 200] }
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def decode(value)
|
|
39
|
+
case value
|
|
40
|
+
when Array then value.map { |v| decode(v) }
|
|
41
|
+
when Hash
|
|
42
|
+
if value.key?("$float")
|
|
43
|
+
{ "nan" => Float::NAN, "inf" => Float::INFINITY, "-inf" => -Float::INFINITY, "-0.0" => -0.0 }.fetch(value["$float"]) { Float(value["$float"]) }
|
|
44
|
+
elsif value.key?("$bytes") then value["$bytes"].unpack1("m0").b
|
|
45
|
+
elsif value.key?("$set") then Set.new(decode(value["$set"]))
|
|
46
|
+
else value.transform_values { |v| decode(v) }
|
|
47
|
+
end
|
|
48
|
+
else value
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def snapshot(root)
|
|
53
|
+
Dir.glob("**/*", File::FNM_DOTMATCH, base: root).filter_map do |rel|
|
|
54
|
+
full = File.join(root, rel)
|
|
55
|
+
next unless File.file?(full)
|
|
56
|
+
data = File.binread(full)
|
|
57
|
+
{ "path" => rel, "size" => data.bytesize, "sha256" => Digest::SHA256.hexdigest(data) }
|
|
58
|
+
end.sort_by { |e| e["path"] }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def call(function, case_data)
|
|
62
|
+
args = decode(case_data.fetch("args", []))
|
|
63
|
+
kwargs = decode(case_data.fetch("kwargs", {})).transform_keys(&:to_sym)
|
|
64
|
+
kwargs.empty? ? Migrated.public_send(function, *args) : Migrated.public_send(function, *args, **kwargs)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def run_case(function, case_data)
|
|
68
|
+
out = StringIO.new
|
|
69
|
+
err = StringIO.new
|
|
70
|
+
Dir.mktmpdir("pp-case-") do |scratch|
|
|
71
|
+
Dir.chdir(scratch) do
|
|
72
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
|
|
73
|
+
$stdout, $stderr = out, err
|
|
74
|
+
outcome =
|
|
75
|
+
begin
|
|
76
|
+
{ "kind" => "return", "value" => encode(call(function, case_data)) }
|
|
77
|
+
rescue StandardError => e
|
|
78
|
+
{ "kind" => "raise", "type" => e.class.name, "message" => e.message }
|
|
79
|
+
ensure
|
|
80
|
+
$stdout, $stderr = STDOUT, STDERR
|
|
81
|
+
end
|
|
82
|
+
duration = (Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) - started) / 1000
|
|
83
|
+
{ "outcome" => outcome, "stdout" => out.string, "stderr" => err.string,
|
|
84
|
+
"effects" => snapshot(scratch), "duration_us" => duration }
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def load_code(path)
|
|
90
|
+
# Silence output produced while loading the file itself.
|
|
91
|
+
$stdout = StringIO.new
|
|
92
|
+
load(path)
|
|
93
|
+
ensure
|
|
94
|
+
$stdout = STDOUT
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
request = JSON.parse($stdin.read)
|
|
98
|
+
load_code(request.fetch("code_file"))
|
|
99
|
+
function = request.fetch("function")
|
|
100
|
+
unless defined?(Migrated) && Migrated.respond_to?(function)
|
|
101
|
+
emit("fatal", message: "Migrated.#{function} is not defined. Define `module Migrated; def self.#{function}(...)`.")
|
|
102
|
+
exit 2
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
emit("plan", total: request["cases"].size, runtime: "ruby #{RUBY_VERSION}")
|
|
106
|
+
case request.fetch("command")
|
|
107
|
+
when "replay"
|
|
108
|
+
request["cases"].each { |c| emit("case", ref: c["ref"], **run_case(function, c).transform_keys(&:to_sym)) }
|
|
109
|
+
when "bench"
|
|
110
|
+
iterations = request.fetch("iterations", 30).to_i.clamp(1, 10_000)
|
|
111
|
+
request["cases"].each do |c|
|
|
112
|
+
samples = Dir.mktmpdir("pp-bench-") do |scratch|
|
|
113
|
+
Dir.chdir(scratch) do
|
|
114
|
+
$stdout = StringIO.new
|
|
115
|
+
Array.new(iterations) do
|
|
116
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
|
|
117
|
+
begin
|
|
118
|
+
call(function, c)
|
|
119
|
+
rescue StandardError
|
|
120
|
+
nil
|
|
121
|
+
end
|
|
122
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) - started
|
|
123
|
+
end
|
|
124
|
+
ensure
|
|
125
|
+
$stdout = STDOUT
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
emit("case", ref: c["ref"], samples_ns: samples, peak_memory_bytes: nil)
|
|
129
|
+
end
|
|
130
|
+
else
|
|
131
|
+
emit("fatal", message: "unknown command")
|
|
132
|
+
exit 2
|
|
133
|
+
end
|
|
134
|
+
emit("done")
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ruby-anything",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Migrate any app (TypeScript, JavaScript, Python, Go, Java/Spring, Kotlin, C#, PHP, Rust, Swift) to Ruby on Rails with Hotwire, Tailwind and daisyUI, one piece at a time, checked against the original's recorded behavior at every step.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ruby",
|
|
@@ -39,10 +39,10 @@
|
|
|
39
39
|
"README.md",
|
|
40
40
|
"LICENSE",
|
|
41
41
|
"docs",
|
|
42
|
-
"action.yml"
|
|
42
|
+
"action.yml",
|
|
43
|
+
"!adapters/node/node_modules"
|
|
43
44
|
],
|
|
44
45
|
"scripts": {
|
|
45
|
-
"prepack": "node scripts/sync-adapters.js",
|
|
46
46
|
"test": "node scripts/test.js"
|
|
47
47
|
},
|
|
48
48
|
"engines": {
|
package/src/adapters.js
CHANGED
|
@@ -1,63 +1,60 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
// Runs
|
|
3
|
-
// Recording EXECUTES the original: work on a copy, with a test database.
|
|
4
|
-
const fs = require('node:fs')
|
|
5
|
-
const path = require('node:path')
|
|
6
|
-
const { PKG, python, which, runHarness, canon } = require('./util')
|
|
7
|
-
|
|
8
|
-
const POLYGLOT = { go: 'go', java: 'javac', kotlin: 'kotlinc', csharp: 'dotnet', php: 'php', rust: 'rustc', swift: 'swiftc', ruby: 'ruby' }
|
|
9
|
-
|
|
10
|
-
function adaptersDir() {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return { argv: [
|
|
26
|
-
}
|
|
27
|
-
if (lang
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
module.exports = { adaptersDir, harnessFor, supported, analyzeUnits, record }
|
|
1
|
+
'use strict'
|
|
2
|
+
// Runs the language adapters (the recorders) on the original code.
|
|
3
|
+
// Recording EXECUTES the original: work on a copy, with a test database.
|
|
4
|
+
const fs = require('node:fs')
|
|
5
|
+
const path = require('node:path')
|
|
6
|
+
const { PKG, python, which, runHarness, canon } = require('./util')
|
|
7
|
+
|
|
8
|
+
const POLYGLOT = { go: 'go', java: 'javac', kotlin: 'kotlinc', csharp: 'dotnet', php: 'php', rust: 'rustc', swift: 'swiftc', ruby: 'ruby' }
|
|
9
|
+
|
|
10
|
+
function adaptersDir() {
|
|
11
|
+
const bundled = path.join(PKG, 'adapters')
|
|
12
|
+
if (fs.existsSync(path.join(bundled, 'polyglot'))) return bundled
|
|
13
|
+
throw new Error('language adapters missing: reinstall ruby-anything')
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// argv + env for a language's harness, or throws with what to install.
|
|
17
|
+
function harnessFor(lang) {
|
|
18
|
+
const dir = adaptersDir()
|
|
19
|
+
const py = python()
|
|
20
|
+
if (lang === 'python') {
|
|
21
|
+
if (!py) throw new Error('python 3 is required to record Python code')
|
|
22
|
+
return { argv: [py, '-X', 'utf8', '-I', '-B', path.join(dir, 'python', 'harness.py')], env: {} }
|
|
23
|
+
}
|
|
24
|
+
if (lang === 'javascript' || lang === 'typescript') {
|
|
25
|
+
return { argv: [process.execPath, '--enable-source-maps', path.join(dir, 'node', 'harness.mjs')], env: {} }
|
|
26
|
+
}
|
|
27
|
+
if (POLYGLOT[lang]) {
|
|
28
|
+
if (!py) throw new Error(`python 3 is required to drive the ${lang} recorder`)
|
|
29
|
+
if (!which(POLYGLOT[lang])) throw new Error(`${POLYGLOT[lang]} is not installed (needed to record ${lang}); run ruby-anything doctor`)
|
|
30
|
+
return { argv: [py, '-X', 'utf8', '-B', path.join(dir, 'polyglot', 'harness.py')], env: { PORTPILOT_LANGUAGE: lang } }
|
|
31
|
+
}
|
|
32
|
+
throw new Error(`no recorder for ${lang}`)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function supported(lang) {
|
|
36
|
+
return lang === 'python' || lang === 'javascript' || lang === 'typescript' || !!POLYGLOT[lang]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function analyzeUnits(root, lang) {
|
|
40
|
+
const h = harnessFor(lang)
|
|
41
|
+
const { events } = await runHarness(h.argv, { command: 'analyze' }, { env: { ...h.env, PORTPILOT_SRC: root }, cwd: root })
|
|
42
|
+
return {
|
|
43
|
+
files: events.filter((e) => e.event === 'file'),
|
|
44
|
+
units: events.filter((e) => e.event === 'unit'),
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function record(root, lang, { moduleFile, fn, cases = [], count = 40, seed = 1, mode = 'edge', pinClock = false, onEvent }) {
|
|
49
|
+
const h = harnessFor(lang)
|
|
50
|
+
const req = { command: 'record', module_file: moduleFile, function: fn, cases: '__CASES__', generate: count ? { count, seed, mode } : null, pin_clock: !!pinClock }
|
|
51
|
+
// cases keep their int/float kinds ({ $n } values) on the way in
|
|
52
|
+
const raw = JSON.stringify(req).replace('"__CASES__"', canon(cases))
|
|
53
|
+
const { events } = await runHarness(h.argv, raw, { env: { ...h.env, PORTPILOT_SRC: root }, cwd: root, onEvent })
|
|
54
|
+
return {
|
|
55
|
+
runtime: events.find((e) => e.event === 'plan')?.runtime,
|
|
56
|
+
cases: events.filter((e) => e.event === 'case'),
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
module.exports = { adaptersDir, harnessFor, supported, analyzeUnits, record }
|