ruby-anything 0.0.0-stage → 0.1.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.
Files changed (45) hide show
  1. package/.claude-plugin/marketplace.json +12 -0
  2. package/.claude-plugin/plugin.json +7 -0
  3. package/.mcp.json +8 -0
  4. package/LICENSE +21 -0
  5. package/README.md +82 -2
  6. package/adapters/node/harness.mjs +324 -0
  7. package/adapters/node/package-lock.json +26 -0
  8. package/adapters/node/package.json +7 -0
  9. package/adapters/polyglot/harness.py +350 -0
  10. package/adapters/polyglot/lang_csharp.py +222 -0
  11. package/adapters/polyglot/lang_go.py +283 -0
  12. package/adapters/polyglot/lang_java.py +510 -0
  13. package/adapters/polyglot/lang_kotlin.py +133 -0
  14. package/adapters/polyglot/lang_php.py +156 -0
  15. package/adapters/polyglot/lang_ruby.py +148 -0
  16. package/adapters/polyglot/lang_rust.py +430 -0
  17. package/adapters/polyglot/lang_swift.py +337 -0
  18. package/adapters/python/harness.py +417 -0
  19. package/bin/ruby-anything.js +10 -0
  20. package/docs/assets/how-it-works-dark.svg +51 -0
  21. package/docs/assets/how-it-works-light.svg +51 -0
  22. package/harness/ruby_replay.rb +144 -0
  23. package/package.json +51 -4
  24. package/skills/ruby-anything/SKILL.md +130 -0
  25. package/skills/ruby-anything/frontend.md +64 -0
  26. package/skills/ruby-anything/languages/csharp.md +43 -0
  27. package/skills/ruby-anything/languages/go.md +36 -0
  28. package/skills/ruby-anything/languages/java.md +54 -0
  29. package/skills/ruby-anything/languages/javascript.md +50 -0
  30. package/skills/ruby-anything/languages/kotlin.md +27 -0
  31. package/skills/ruby-anything/languages/php.md +47 -0
  32. package/skills/ruby-anything/languages/python.md +49 -0
  33. package/skills/ruby-anything/languages/rust.md +31 -0
  34. package/skills/ruby-anything/languages/swift.md +27 -0
  35. package/skills/ruby-anything/whole-app.md +168 -0
  36. package/src/adapters.js +60 -0
  37. package/src/analyze.js +183 -0
  38. package/src/app.js +697 -0
  39. package/src/cli.js +75 -0
  40. package/src/detect.js +266 -0
  41. package/src/go.js +139 -0
  42. package/src/mcp.js +89 -0
  43. package/src/setup.js +106 -0
  44. package/src/units.js +444 -0
  45. package/src/util.js +312 -0
package/src/util.js ADDED
@@ -0,0 +1,312 @@
1
+ 'use strict'
2
+ const fs = require('node:fs')
3
+ const path = require('node:path')
4
+ const crypto = require('node:crypto')
5
+ const { spawn, spawnSync } = require('node:child_process')
6
+
7
+ const PKG = path.resolve(__dirname, '..')
8
+ const IS_WIN = process.platform === 'win32'
9
+
10
+ // ------------------------------------------------------------------ args
11
+ // parseArgs(['repo', '--json', '-o', 'x', '--', 'cmd']) -> { _: ['repo'], json: true, o: 'x', rest: ['cmd'] }
12
+ function parseArgs(argv, booleans = []) {
13
+ const out = { _: [], rest: [] }
14
+ for (let i = 0; i < argv.length; i++) {
15
+ const a = argv[i]
16
+ if (a === '--') {
17
+ out.rest = argv.slice(i + 1)
18
+ break
19
+ }
20
+ const m = a.match(/^--?([^=]+)(?:=(.*))?$/)
21
+ if (!m || a === '-') {
22
+ out._.push(a)
23
+ continue
24
+ }
25
+ const key = m[1].replace(/-([a-z])/g, (_, c) => c.toUpperCase())
26
+ if (m[2] !== undefined) out[key] = m[2]
27
+ else if (booleans.includes(key) || i + 1 >= argv.length || argv[i + 1].startsWith('-')) out[key] = true
28
+ else out[key] = argv[++i]
29
+ }
30
+ return out
31
+ }
32
+
33
+ // ------------------------------------------------------------------ files
34
+ const readJSON = (p, fallback) => {
35
+ try {
36
+ return JSON.parse(fs.readFileSync(p, 'utf8'))
37
+ } catch {
38
+ return fallback
39
+ }
40
+ }
41
+ const writeJSON = (p, v) => {
42
+ fs.mkdirSync(path.dirname(p), { recursive: true })
43
+ fs.writeFileSync(p, JSON.stringify(v, null, 2) + '\n')
44
+ }
45
+ const writeFile = (p, text) => {
46
+ fs.mkdirSync(path.dirname(p), { recursive: true })
47
+ fs.writeFileSync(p, text)
48
+ }
49
+ const writeIfMissing = (p, text) => {
50
+ if (fs.existsSync(p)) return false
51
+ writeFile(p, text)
52
+ return true
53
+ }
54
+ const readLines = (p) =>
55
+ fs.existsSync(p)
56
+ ? fs
57
+ .readFileSync(p, 'utf8')
58
+ .split('\n')
59
+ .filter((l) => l.trim())
60
+ .map((l) => JSON.parse(l))
61
+ : []
62
+ // Like readLines, but numbers keep their int/float kind ({ $n } values).
63
+ const readCases = (p) =>
64
+ fs.existsSync(p)
65
+ ? fs.readFileSync(p, 'utf8').split('\n').filter((l) => l.trim()).map((l) => parseJSON(l))
66
+ : []
67
+ const sha256 = (data) => crypto.createHash('sha256').update(data).digest('hex')
68
+ const slug = (s) =>
69
+ String(s)
70
+ .replace(/[^A-Za-z0-9]+/g, '_')
71
+ .replace(/^_+|_+$/g, '')
72
+ .toLowerCase()
73
+ const snake = (s) =>
74
+ String(s)
75
+ .replace(/([a-z0-9])([A-Z])/g, '$1_$2')
76
+ .replace(/[^A-Za-z0-9]+/g, '_')
77
+ .toLowerCase()
78
+ const camel = (s) =>
79
+ snake(s)
80
+ .split('_')
81
+ .filter(Boolean)
82
+ .map((w) => w[0].toUpperCase() + w.slice(1))
83
+ .join('')
84
+
85
+ // ------------------------------------------------------------------ project layout
86
+ // Everything ruby-anything writes lives in <repo>/rails: the Rails app itself, and port/ for the
87
+ // migration's own files (plan, decisions, cases, rules, map).
88
+ function layout(repo) {
89
+ const root = path.resolve(repo || '.')
90
+ const rails = path.join(root, 'rails')
91
+ const port = path.join(rails, 'port')
92
+ return {
93
+ root,
94
+ rails,
95
+ port,
96
+ state: path.join(port, 'state.json'),
97
+ plan: path.join(port, 'PLAN.md'),
98
+ planJson: path.join(port, 'plan.json'),
99
+ task: path.join(port, 'TASK.md'),
100
+ map: path.join(port, 'MAP.md'),
101
+ decisions: path.join(port, 'DECISIONS.md'),
102
+ glossary: path.join(port, 'GLOSSARY.md'),
103
+ handoff: path.join(port, 'HANDOFF.md'),
104
+ rules: path.join(port, 'RULES.md'),
105
+ cases: path.join(port, 'cases'),
106
+ appCases: path.join(port, 'app-cases'),
107
+ settings: path.join(port, 'app-cases', 'ruby-anything.json'),
108
+ reports: path.join(port, 'reports'),
109
+ }
110
+ }
111
+
112
+ const loadState = (L) => readJSON(L.state, { units: {}, gates: {} })
113
+ const saveState = (L, s) => writeJSON(L.state, s)
114
+
115
+ // ------------------------------------------------------------------ processes
116
+ function which(cmd) {
117
+ const r = spawnSync(IS_WIN ? 'where' : 'sh', IS_WIN ? [cmd] : ['-c', `command -v ${cmd}`], { encoding: 'utf8' })
118
+ return r.status === 0 ? r.stdout.split(/\r?\n/)[0].trim() : null
119
+ }
120
+
121
+ let pythonCmd
122
+ function python() {
123
+ if (pythonCmd !== undefined) return pythonCmd
124
+ for (const c of IS_WIN ? ['python', 'python3', 'py'] : ['python3', 'python']) {
125
+ const r = spawnSync(c, ['-c', 'import sys; print(sys.version_info[0])'], { encoding: 'utf8' })
126
+ if (r.status === 0 && r.stdout.trim() === '3') return (pythonCmd = c)
127
+ }
128
+ return (pythonCmd = null)
129
+ }
130
+
131
+ function runSync(cmd, args, opts = {}) {
132
+ const r = spawnSync(cmd, args, { encoding: 'utf8', shell: IS_WIN && !opts.noShell, maxBuffer: 64 << 20, ...opts })
133
+ return { code: r.status, stdout: r.stdout || '', stderr: (r.stderr || '') + (r.error ? r.error.message : '') }
134
+ }
135
+
136
+ // Runs a harness: one JSON request on stdin, JSON-lines events on stdout.
137
+ function runHarness(argv, request, { env = {}, cwd, timeoutMs = 600000, onEvent } = {}) {
138
+ return new Promise((resolve, reject) => {
139
+ const child = spawn(argv[0], argv.slice(1), {
140
+ cwd,
141
+ env: { ...process.env, ...env },
142
+ stdio: ['pipe', 'pipe', 'pipe'],
143
+ })
144
+ const events = []
145
+ let buf = ''
146
+ let err = ''
147
+ const timer = setTimeout(() => child.kill('SIGKILL'), timeoutMs)
148
+ child.stdout.on('data', (d) => {
149
+ buf += d
150
+ let i
151
+ while ((i = buf.indexOf('\n')) >= 0) {
152
+ const line = buf.slice(0, i).trim()
153
+ buf = buf.slice(i + 1)
154
+ if (!line) continue
155
+ let ev
156
+ try {
157
+ ev = parseJSON(line)
158
+ } catch {
159
+ ev = { event: 'log', message: line }
160
+ }
161
+ events.push(ev)
162
+ if (onEvent) onEvent(ev)
163
+ }
164
+ })
165
+ child.stderr.on('data', (d) => (err += d))
166
+ child.on('error', reject)
167
+ child.on('close', (code) => {
168
+ clearTimeout(timer)
169
+ const fatal = events.find((e) => e.event === 'fatal')
170
+ if (fatal) return reject(new Error(fatal.message))
171
+ if (code !== 0 && !events.some((e) => e.event === 'done'))
172
+ return reject(new Error(`harness exited with ${code}: ${err.trim().slice(-1500)}`))
173
+ resolve({ events, stderr: err })
174
+ })
175
+ child.stdin.end(typeof request === 'string' ? request : JSON.stringify(request))
176
+ })
177
+ }
178
+
179
+ // ------------------------------------------------------------------ JSON that keeps 1 and 1.0 apart
180
+ // Numbers parse to { $n: "<lexeme>" } so int/float kinds survive comparison.
181
+ function parseJSON(text) {
182
+ let i = 0
183
+ const ws = () => {
184
+ while (i < text.length && ' \t\r\n'.includes(text[i])) i++
185
+ }
186
+ const val = () => {
187
+ ws()
188
+ const c = text[i]
189
+ if (c === '{') {
190
+ i++
191
+ const o = {}
192
+ ws()
193
+ if (text[i] === '}') return i++, o
194
+ for (;;) {
195
+ ws()
196
+ const k = val()
197
+ ws()
198
+ i++ // :
199
+ o[k] = val()
200
+ ws()
201
+ if (text[i++] === '}') return o
202
+ }
203
+ }
204
+ if (c === '[') {
205
+ i++
206
+ const a = []
207
+ ws()
208
+ if (text[i] === ']') return i++, a
209
+ for (;;) {
210
+ a.push(val())
211
+ ws()
212
+ if (text[i++] === ']') return a
213
+ }
214
+ }
215
+ if (c === '"') {
216
+ let j = i + 1
217
+ while (text[j] !== '"') j += text[j] === '\\' ? 2 : 1
218
+ const s = JSON.parse(text.slice(i, j + 1))
219
+ i = j + 1
220
+ return s
221
+ }
222
+ const m = /^(true|false|null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)/.exec(text.slice(i, i + 400))
223
+ if (!m) throw new Error(`bad JSON at ${i}`)
224
+ i += m[0].length
225
+ if (m[0] === 'true') return true
226
+ if (m[0] === 'false') return false
227
+ if (m[0] === 'null') return null
228
+ return { $n: m[0] }
229
+ }
230
+ const v = val()
231
+ return v
232
+ }
233
+
234
+ // Back to plain JSON values (numbers become numbers).
235
+ function plain(v) {
236
+ if (Array.isArray(v)) return v.map(plain)
237
+ if (v && typeof v === 'object') {
238
+ if ('$n' in v && Object.keys(v).length === 1) return Number(v.$n)
239
+ return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, plain(x)]))
240
+ }
241
+ return v
242
+ }
243
+
244
+ // Canonical JSON text with numbers written by kind: ints as digits, floats always with a dot.
245
+ function canon(v) {
246
+ if (Array.isArray(v)) return '[' + v.map(canon).join(',') + ']'
247
+ if (v && typeof v === 'object') {
248
+ if ('$n' in v && Object.keys(v).length === 1) {
249
+ const isFloat = /[.eE]/.test(v.$n)
250
+ const n = Number(v.$n)
251
+ if (!isFloat) return BigInt(v.$n).toString()
252
+ return Number.isInteger(n) ? n.toFixed(1) : String(n)
253
+ }
254
+ // {"$float": "4.6e+18"} (exact doubles from some harnesses) is the same value as a float literal
255
+ if ('$float' in v && Object.keys(v).length === 1 && Number.isFinite(Number(v.$float)) && !Object.is(Number(v.$float), -0)) {
256
+ return canon({ $n: /[.eE]/.test(v.$float) ? String(v.$float) : `${v.$float}.0` })
257
+ }
258
+ return (
259
+ '{' +
260
+ Object.keys(v)
261
+ .sort()
262
+ .map((k) => JSON.stringify(k) + ':' + canon(v[k]))
263
+ .join(',') +
264
+ '}'
265
+ )
266
+ }
267
+ return JSON.stringify(v)
268
+ }
269
+
270
+ // ------------------------------------------------------------------ output
271
+ const color = (code) => (s) => (process.stdout.isTTY && !process.env.NO_COLOR ? `\x1b[${code}m${s}\x1b[0m` : s)
272
+ const green = color('32')
273
+ const red = color('31')
274
+ const yellow = color('33')
275
+ const bold = color('1')
276
+ const dim = color('2')
277
+
278
+ function relTo(root, p) {
279
+ return path.relative(root, p).split(path.sep).join('/')
280
+ }
281
+
282
+ module.exports = {
283
+ PKG,
284
+ IS_WIN,
285
+ parseArgs,
286
+ readJSON,
287
+ writeJSON,
288
+ writeFile,
289
+ writeIfMissing,
290
+ readLines,
291
+ readCases,
292
+ sha256,
293
+ slug,
294
+ snake,
295
+ camel,
296
+ layout,
297
+ loadState,
298
+ saveState,
299
+ which,
300
+ python,
301
+ runSync,
302
+ runHarness,
303
+ parseJSON,
304
+ plain,
305
+ canon,
306
+ green,
307
+ red,
308
+ yellow,
309
+ bold,
310
+ dim,
311
+ relTo,
312
+ }