instant_record 0.1.0 → 0.3.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +58 -0
- data/README.md +267 -61
- data/app/controllers/instant_record/bootstraps_controller.rb +39 -12
- data/app/controllers/instant_record/events_controller.rb +58 -6
- data/app/controllers/instant_record/files_controller.rb +116 -0
- data/app/controllers/instant_record/mutations_controller.rb +84 -5
- data/app/controllers/instant_record/native_screens_controller.rb +60 -0
- data/app/controllers/instant_record/records_controller.rb +86 -8
- data/app/controllers/instant_record/shells_controller.rb +64 -0
- data/app/controllers/instant_record/sync_notices_controller.rb +81 -0
- data/app/helpers/instant_record/sync_notices_helper.rb +41 -0
- data/app/jobs/instant_record/shell_build_job.rb +16 -0
- data/app/models/instant_record/change.rb +36 -3
- data/app/models/instant_record/mutation_applier.rb +633 -50
- data/app/views/instant_record/sync_notices/_notices.html.erb +63 -0
- data/compiled/README.md +272 -0
- data/compiled/pipeline/browser_migrations.rb +18 -0
- data/compiled/pipeline/build.rb +13 -0
- data/compiled/pipeline/build_native.rb +14 -0
- data/compiled/pipeline/native/host.c +78 -0
- data/compiled/pipeline/native/instant_record_native.h +57 -0
- data/compiled/pipeline/native_shim.c +149 -0
- data/compiled/pipeline/reactor_shim.c +66 -0
- data/compiled/pipeline/sqlite/rh_shim.c +5 -0
- data/compiled/pipeline/sqlite_release.rb +43 -0
- data/compiled/toolchain.env.example +22 -0
- data/compiled/web/index.html +20 -0
- data/compiled/web/package-lock.json +884 -0
- data/compiled/web/package.json +17 -0
- data/compiled/web/src/app-host-core.js +556 -0
- data/compiled/web/src/app-host.js +16 -0
- data/compiled/web/src/branch-manager.js +140 -0
- data/compiled/web/src/branch-preview.js +397 -0
- data/compiled/web/src/changes.js +191 -0
- data/compiled/web/src/faults.js +172 -0
- data/compiled/web/src/flight-recorder.js +347 -0
- data/compiled/web/src/history-fill.js +68 -0
- data/compiled/web/src/http-headers.js +100 -0
- data/compiled/web/src/leader-election.js +705 -0
- data/compiled/web/src/main.js +608 -0
- data/compiled/web/src/manifest.js +112 -0
- data/compiled/web/src/opfs-wasi-core.js +109 -0
- data/compiled/web/src/opfs-wasi-file.js +8 -0
- data/compiled/web/src/reactor-runtime.js +1692 -0
- data/compiled/web/src/redactor.js +518 -0
- data/compiled/web/src/release-updates.js +48 -0
- data/compiled/web/src/replay.js +338 -0
- data/compiled/web/src/schema-recovery.js +204 -0
- data/compiled/web/src/server-request.js +68 -0
- data/compiled/web/src/sha256.js +98 -0
- data/compiled/web/src/shell.js +52 -0
- data/compiled/web/src/snapshot-store.js +38 -0
- data/compiled/web/src/sqlite-redactor.js +111 -0
- data/compiled/web/src/storage-backend.js +159 -0
- data/compiled/web/src/stream-leader.js +675 -0
- data/compiled/web/src/sync-context.js +91 -0
- data/compiled/web/src/sync-drain.js +104 -0
- data/compiled/web/src/sync-schedule.js +9 -0
- data/compiled/web/src/wasi-minimal.js +275 -0
- data/compiled/web/src/worker.js +39 -0
- data/compiled/web/vite.config.js +27 -0
- data/config/routes.rb +25 -0
- data/db/migrate/20261119000201_add_scope_to_instant_record_changes.rb +7 -0
- data/db/migrate/20261121000001_create_instant_record_sync_notices.rb +21 -0
- data/db/migrate/20261121000101_add_partition_to_instant_record_changes.rb +8 -0
- data/db/migrate/20261121230300_add_delay_line_to_instant_record_outbox.rb +10 -0
- data/exe/instant_record_mcp +6 -0
- data/frontend/LiveList.js +67 -0
- data/frontend/chord.js +79 -0
- data/frontend/file.js +65 -0
- data/frontend/files.js +738 -0
- data/frontend/focus.js +10 -0
- data/frontend/held.js +192 -0
- data/frontend/http.js +76 -0
- data/frontend/index.js +53 -0
- data/frontend/keyboard.js +56 -0
- data/frontend/live.js +298 -0
- data/frontend/notices.js +118 -0
- data/frontend/package.json +15 -0
- data/frontend/range.js +10 -0
- data/frontend/selection.js +47 -0
- data/frontend/shortcuts.js +25 -0
- data/frontend/sync-notices.js +130 -0
- data/frontend/undo.js +48 -0
- data/frontend/window.js +181 -0
- data/lib/generators/instant_record/install/install_generator.rb +18 -30
- data/lib/generators/instant_record/install/templates/instant_record.rb +34 -0
- data/lib/generators/instant_record/ios/ios_generator.rb +71 -0
- data/lib/generators/instant_record/ios/templates/App.swift.tt +35 -0
- data/lib/generators/instant_record/ios/templates/Info.plist +11 -0
- data/lib/generators/instant_record/ios/templates/LibraryEngine.swift +30 -0
- data/lib/instant_record/branch/sqlite_store.rb +142 -0
- data/lib/instant_record/branch.rb +388 -0
- data/lib/instant_record/client/notifier.rb +8 -12
- data/lib/instant_record/client/rebaser.rb +565 -0
- data/lib/instant_record/client/transport.rb +64 -42
- data/lib/instant_record/client.rb +1600 -169
- data/lib/instant_record/compiled_site.rb +85 -0
- data/lib/instant_record/compiler/app_stage.rb +212 -0
- data/lib/instant_record/compiler/ast_lowerer.rb +659 -0
- data/lib/instant_record/compiler/browser_migrations.rb +161 -0
- data/lib/instant_record/compiler/entry_builder.rb +832 -0
- data/lib/instant_record/compiler/fts5_smoke.rb +205 -0
- data/lib/instant_record/compiler/gem_vendor.rb +194 -0
- data/lib/instant_record/compiler/host_adapters.rb +506 -0
- data/lib/instant_record/compiler/kotlin_codegen.rb +209 -0
- data/lib/instant_record/compiler/manifest.rb +178 -0
- data/lib/instant_record/compiler/native_build.rb +586 -0
- data/lib/instant_record/compiler/native_contracts.rb +338 -0
- data/lib/instant_record/compiler/path_scrubber.rb +31 -0
- data/lib/instant_record/compiler/reachability_tracer.rb +1188 -0
- data/lib/instant_record/compiler/route_analyzer.rb +623 -0
- data/lib/instant_record/compiler/runner.rb +42 -0
- data/lib/instant_record/compiler/schema_lock.rb +606 -0
- data/lib/instant_record/compiler/source_pages.rb +77 -0
- data/lib/instant_record/compiler/static_linter.rb +770 -0
- data/lib/instant_record/compiler/swift_codegen.rb +341 -0
- data/lib/instant_record/compiler/toolchain.rb +219 -0
- data/lib/instant_record/compiler/wasm_build.rb +704 -0
- data/lib/instant_record/compiler/wasm_tree.rb +518 -0
- data/lib/instant_record/compiler.rb +277 -0
- data/lib/instant_record/configuration.rb +104 -0
- data/lib/instant_record/engine.rb +68 -18
- data/lib/instant_record/file_store.rb +162 -0
- data/lib/instant_record/files.rb +92 -0
- data/lib/instant_record/hlc.rb +387 -0
- data/lib/instant_record/host.rb +66 -0
- data/lib/instant_record/inertia/renderer.rb +52 -0
- data/lib/instant_record/inertia.rb +222 -0
- data/lib/instant_record/mcp/cli.rb +319 -0
- data/lib/instant_record/mcp/host_process.rb +229 -0
- data/lib/instant_record/mcp/http_exchange.rb +233 -0
- data/lib/instant_record/mcp/replica.rb +556 -0
- data/lib/instant_record/mcp/runtime.rb +406 -0
- data/lib/instant_record/mcp/server.rb +429 -0
- data/lib/instant_record/mcp.rb +47 -0
- data/lib/instant_record/native/contract.rb +367 -0
- data/lib/instant_record/native/node.rb +125 -0
- data/lib/instant_record/native/screen.rb +366 -0
- data/lib/instant_record/native/xcode_project.rb +344 -0
- data/lib/instant_record/native.rb +41 -0
- data/lib/instant_record/outbox_mutation.rb +81 -9
- data/lib/instant_record/outbox_upcaster.rb +161 -0
- data/lib/instant_record/read_tracker.rb +610 -0
- data/lib/instant_record/routing.rb +33 -0
- data/lib/instant_record/runtime_scoped.rb +4 -0
- data/lib/instant_record/shell.rb +486 -0
- data/lib/instant_record/sync_actions.rb +800 -0
- data/lib/instant_record/sync_metadata.rb +17 -2
- data/lib/instant_record/sync_notice.rb +234 -0
- data/lib/instant_record/sync_partition.rb +63 -0
- data/lib/instant_record/sync_scope.rb +300 -0
- data/lib/instant_record/sync_shape.rb +102 -0
- data/lib/instant_record/sync_window.rb +92 -15
- data/lib/instant_record/syncable.rb +313 -17
- data/lib/instant_record/testing/authoritative_server.rb +547 -0
- data/lib/instant_record/testing/chaos_simulator.rb +429 -0
- data/lib/instant_record/testing/cruby_host_bridge.rb +370 -0
- data/lib/instant_record/testing/fault_network.rb +140 -0
- data/lib/instant_record/testing/parity_suite.rb +435 -0
- data/lib/instant_record/testing/runtimes/base.rb +184 -0
- data/lib/instant_record/testing/runtimes/cruby_runtime.rb +255 -0
- data/lib/instant_record/testing/runtimes/native_runtime.rb +346 -0
- data/lib/instant_record/testing/runtimes/wasm_runtime.rb +371 -0
- data/lib/instant_record/testing/wasm_host_bridge.mjs +240 -0
- data/lib/instant_record/testing.rb +333 -0
- data/lib/instant_record/trace/redactor.rb +278 -0
- data/lib/instant_record/trace/replayer.rb +126 -0
- data/lib/instant_record/trace/schema_extractor.rb +37 -0
- data/lib/instant_record/trace.rb +22 -0
- data/lib/instant_record/version.rb +1 -1
- data/lib/instant_record.rb +107 -37
- data/lib/tasks/instant_record.rake +299 -47
- data/native/ios/InstantRecordNative/Package.swift +12 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/AppHost.swift +706 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Engine.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/FileStore.swift +1048 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/HTTPEngine.swift +36 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Icons.swift +19 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Node.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/ScreenView.swift +481 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Screens.swift +94 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/StreamClient.swift +74 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncNotice.swift +282 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncSchedule.swift +153 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Theme.swift +140 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Transport.swift +108 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/TypedContracts.swift +237 -0
- data/native/ios/README.md +341 -0
- data/sig/instant_record/inertia.rbs +47 -0
- data/sig/instant_record/native.rbs +38 -0
- metadata +194 -18
- data/lib/generators/instant_record/install/templates/database.js +0 -32
- data/lib/generators/instant_record/install/templates/index.html +0 -69
- data/lib/generators/instant_record/install/templates/rails.sw.js +0 -750
- data/lib/instant_record/local_schema.rb +0 -75
- data/lib/instant_record/pglite_compat.rb +0 -40
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
require "digest"
|
|
2
|
+
require "fileutils"
|
|
3
|
+
require "json"
|
|
4
|
+
require "open3"
|
|
5
|
+
require "tmpdir"
|
|
6
|
+
require_relative "native_build"
|
|
7
|
+
require_relative "runner"
|
|
8
|
+
require_relative "toolchain"
|
|
9
|
+
require_relative "wasm_build"
|
|
10
|
+
|
|
11
|
+
module InstantRecord
|
|
12
|
+
class Compiler
|
|
13
|
+
# Compiles and executes a minimal C program against the configured SQLite
|
|
14
|
+
# amalgamation using the exact SQLITE_CFLAGS from NativeBuild and/or
|
|
15
|
+
# WasmBuild to prove FTS5 virtual tables, BM25 ranking, prefix queries,
|
|
16
|
+
# phrase queries, and snippets work offline in compiled SQLite.
|
|
17
|
+
class Fts5Smoke
|
|
18
|
+
TARGETS = %i[native wasm].freeze
|
|
19
|
+
|
|
20
|
+
SMOKE_C = <<~'C'
|
|
21
|
+
#include <stdio.h>
|
|
22
|
+
#include <string.h>
|
|
23
|
+
#include "sqlite3.h"
|
|
24
|
+
|
|
25
|
+
static int fail(sqlite3 *db, const char *step, int rc) {
|
|
26
|
+
fprintf(stderr, "fts5 smoke failed at %s (rc=%d): %s\n",
|
|
27
|
+
step, rc, db ? sqlite3_errmsg(db) : "no db");
|
|
28
|
+
if (db) sqlite3_close(db);
|
|
29
|
+
return 1;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
int main(void) {
|
|
33
|
+
sqlite3 *db = NULL;
|
|
34
|
+
int rc = sqlite3_open(":memory:", &db);
|
|
35
|
+
if (rc != SQLITE_OK) return fail(db, "open", rc);
|
|
36
|
+
|
|
37
|
+
const char *setup =
|
|
38
|
+
"CREATE VIRTUAL TABLE notes_fts USING fts5(title, body, tokenize='unicode61');"
|
|
39
|
+
"INSERT INTO notes_fts(title, body) VALUES"
|
|
40
|
+
" ('Offline Sync', 'Local SQLite full text search works without network connectivity'),"
|
|
41
|
+
" ('File Chunks', 'Chunked uploads stay below reverse proxy request limits'),"
|
|
42
|
+
" ('Native iOS', 'Swift offline cache and full text search parity');";
|
|
43
|
+
rc = sqlite3_exec(db, setup, NULL, NULL, NULL);
|
|
44
|
+
if (rc != SQLITE_OK) return fail(db, "setup", rc);
|
|
45
|
+
|
|
46
|
+
sqlite3_stmt *stmt = NULL;
|
|
47
|
+
const char *rank_sql =
|
|
48
|
+
"SELECT title, bm25(notes_fts), snippet(notes_fts, 1, '[', ']', '...', 8) "
|
|
49
|
+
"FROM notes_fts WHERE notes_fts MATCH 'offline OR search' ORDER BY bm25(notes_fts);";
|
|
50
|
+
rc = sqlite3_prepare_v2(db, rank_sql, -1, &stmt, NULL);
|
|
51
|
+
if (rc != SQLITE_OK) return fail(db, "prepare rank", rc);
|
|
52
|
+
|
|
53
|
+
int matches = 0;
|
|
54
|
+
int snippet_marked = 0;
|
|
55
|
+
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
|
56
|
+
const unsigned char *snippet = sqlite3_column_text(stmt, 2);
|
|
57
|
+
if (snippet && strchr((const char *)snippet, '[') != NULL) snippet_marked = 1;
|
|
58
|
+
matches++;
|
|
59
|
+
}
|
|
60
|
+
sqlite3_finalize(stmt);
|
|
61
|
+
if (rc != SQLITE_DONE || matches != 2 || !snippet_marked) {
|
|
62
|
+
fprintf(stderr, "fts5 rank check failed: rc=%d matches=%d snippet_marked=%d\n",
|
|
63
|
+
rc, matches, snippet_marked);
|
|
64
|
+
sqlite3_close(db);
|
|
65
|
+
return 1;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
rc = sqlite3_prepare_v2(db, "SELECT count(*) FROM notes_fts WHERE notes_fts MATCH 'local*';", -1, &stmt, NULL);
|
|
69
|
+
if (rc != SQLITE_OK) return fail(db, "prepare prefix", rc);
|
|
70
|
+
int prefix_count = -1;
|
|
71
|
+
if (sqlite3_step(stmt) == SQLITE_ROW) prefix_count = sqlite3_column_int(stmt, 0);
|
|
72
|
+
sqlite3_finalize(stmt);
|
|
73
|
+
if (prefix_count != 1) {
|
|
74
|
+
fprintf(stderr, "fts5 prefix check failed: count=%d\n", prefix_count);
|
|
75
|
+
sqlite3_close(db);
|
|
76
|
+
return 1;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
rc = sqlite3_prepare_v2(db, "SELECT title FROM notes_fts WHERE notes_fts MATCH '\"full text\" AND parity';", -1, &stmt, NULL);
|
|
80
|
+
if (rc != SQLITE_OK) return fail(db, "prepare phrase", rc);
|
|
81
|
+
char phrase[64] = {0};
|
|
82
|
+
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
|
83
|
+
const unsigned char *text = sqlite3_column_text(stmt, 0);
|
|
84
|
+
if (text) snprintf(phrase, sizeof(phrase), "%s", (const char *)text);
|
|
85
|
+
}
|
|
86
|
+
sqlite3_finalize(stmt);
|
|
87
|
+
if (strcmp(phrase, "Native iOS") != 0) {
|
|
88
|
+
fprintf(stderr, "fts5 phrase check failed: got '%s'\n", phrase);
|
|
89
|
+
sqlite3_close(db);
|
|
90
|
+
return 1;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
sqlite3_close(db);
|
|
94
|
+
printf("{\"ok\":true,\"matches\":%d,\"prefix\":%d,\"phrase\":\"%s\"}\n", matches, prefix_count, phrase);
|
|
95
|
+
return 0;
|
|
96
|
+
}
|
|
97
|
+
C
|
|
98
|
+
|
|
99
|
+
WASI_RUNNER_JS = <<~'JS'
|
|
100
|
+
import { readFileSync } from "node:fs";
|
|
101
|
+
import { WASI } from "node:wasi";
|
|
102
|
+
|
|
103
|
+
const wasi = new WASI({ version: "preview1" });
|
|
104
|
+
const wasm = await WebAssembly.compile(readFileSync(process.argv[2]));
|
|
105
|
+
const instance = await WebAssembly.instantiate(wasm, wasi.getImportObject());
|
|
106
|
+
wasi.start(instance);
|
|
107
|
+
JS
|
|
108
|
+
|
|
109
|
+
def self.run(toolchain:, target: :native)
|
|
110
|
+
new(toolchain: toolchain, target: target).run
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def initialize(toolchain:, target: :native)
|
|
114
|
+
@toolchain = Toolchain.load(toolchain)
|
|
115
|
+
@target = target.to_sym
|
|
116
|
+
unless TARGETS.include?(@target)
|
|
117
|
+
raise ArgumentError, "Unknown FTS5 smoke target: #{target.inspect} (expected :native or :wasm)"
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def run
|
|
122
|
+
case @target
|
|
123
|
+
when :native then run_native
|
|
124
|
+
when :wasm then run_wasm
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
private
|
|
129
|
+
|
|
130
|
+
def run_native
|
|
131
|
+
sqlite_src = @toolchain.fetch("SQLITE_SRC")
|
|
132
|
+
cc = ENV.fetch("CC", "cc")
|
|
133
|
+
Dir.mktmpdir("instant-record-fts5-native-") do |dir|
|
|
134
|
+
runner = Runner.new(build_dir: dir)
|
|
135
|
+
sqlite_obj = cached_sqlite_object(
|
|
136
|
+
runner: runner,
|
|
137
|
+
key_prefix: "fts5-native",
|
|
138
|
+
cc: cc,
|
|
139
|
+
cflags: NativeBuild::SQLITE_CFLAGS,
|
|
140
|
+
sqlite_src: sqlite_src
|
|
141
|
+
)
|
|
142
|
+
smoke_c = File.join(dir, "smoke.c")
|
|
143
|
+
binary = File.join(dir, "fts5_smoke")
|
|
144
|
+
File.write(smoke_c, SMOKE_C)
|
|
145
|
+
runner.run([cc, "-O2", "-I#{sqlite_src}", smoke_c, sqlite_obj, "-lm", "-o", binary])
|
|
146
|
+
parse_result(runner.run([binary]))
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def run_wasm
|
|
151
|
+
sqlite_src = @toolchain.fetch("SQLITE_SRC")
|
|
152
|
+
clang = @toolchain.join("WASI_SDK", "bin/clang")
|
|
153
|
+
node = Toolchain.which("node")
|
|
154
|
+
raise BuildError, "node is required to execute the WASI FTS5 smoke binary" unless node
|
|
155
|
+
|
|
156
|
+
Dir.mktmpdir("instant-record-fts5-wasm-") do |dir|
|
|
157
|
+
runner = Runner.new(build_dir: dir)
|
|
158
|
+
sqlite_obj = cached_sqlite_object(
|
|
159
|
+
runner: runner,
|
|
160
|
+
key_prefix: "fts5-wasm",
|
|
161
|
+
cc: clang,
|
|
162
|
+
cflags: WasmBuild::WASI_CFLAGS + WasmBuild::SQLITE_CFLAGS,
|
|
163
|
+
sqlite_src: sqlite_src
|
|
164
|
+
)
|
|
165
|
+
smoke_c = File.join(dir, "smoke.c")
|
|
166
|
+
wasm = File.join(dir, "fts5_smoke.wasm")
|
|
167
|
+
js = File.join(dir, "run_wasi.mjs")
|
|
168
|
+
File.write(smoke_c, SMOKE_C)
|
|
169
|
+
File.write(js, WASI_RUNNER_JS)
|
|
170
|
+
runner.run([
|
|
171
|
+
clang, *WasmBuild::WASI_CFLAGS, WasmBuild::WASM_OPT_LEVEL,
|
|
172
|
+
"-I#{sqlite_src}", smoke_c, sqlite_obj,
|
|
173
|
+
"-lm",
|
|
174
|
+
"-lwasi-emulated-signal", "-lwasi-emulated-process-clocks",
|
|
175
|
+
"-lwasi-emulated-getpid", "-lwasi-emulated-mman",
|
|
176
|
+
"-o", wasm
|
|
177
|
+
])
|
|
178
|
+
parse_result(runner.run([node, "--no-warnings", js, wasm]))
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def cached_sqlite_object(runner:, key_prefix:, cc:, cflags:, sqlite_src:)
|
|
183
|
+
sqlite_c = File.join(sqlite_src, "sqlite3.c")
|
|
184
|
+
cache = @toolchain["CACHE"]
|
|
185
|
+
digest = Digest::SHA256.hexdigest([cc, *cflags, sqlite_c, File.size(sqlite_c)].join("\0"))[0, 16]
|
|
186
|
+
out_dir = cache && !cache.empty? ? File.join(cache, "#{key_prefix}-#{digest}") : runner.build_dir
|
|
187
|
+
FileUtils.mkdir_p(out_dir)
|
|
188
|
+
obj = File.join(out_dir, "sqlite3.o")
|
|
189
|
+
unless File.exist?(obj)
|
|
190
|
+
tmp_obj = File.join(out_dir, "sqlite3.#{Process.pid}.o")
|
|
191
|
+
runner.run([cc, *cflags, "-c", sqlite_c, "-o", tmp_obj])
|
|
192
|
+
File.rename(tmp_obj, obj)
|
|
193
|
+
end
|
|
194
|
+
obj
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def parse_result(stdout)
|
|
198
|
+
parsed = JSON.parse(stdout.strip)
|
|
199
|
+
raise BuildError, "FTS5 smoke test returned #{parsed.inspect}" unless parsed["ok"] == true
|
|
200
|
+
|
|
201
|
+
parsed.transform_keys(&:to_sym).merge(target: @target)
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
require "fileutils"
|
|
2
|
+
require "prism"
|
|
3
|
+
require_relative "ast_lowerer"
|
|
4
|
+
require_relative "host_adapters"
|
|
5
|
+
require_relative "runner"
|
|
6
|
+
require_relative "static_linter"
|
|
7
|
+
|
|
8
|
+
module InstantRecord
|
|
9
|
+
class Compiler
|
|
10
|
+
# Vendors the instant_record gem's Ruby into the staged app's lib/, installs
|
|
11
|
+
# the target's swappable host-adapter module, and runs the Prism AST lowering
|
|
12
|
+
# pass (stripping `server_only`, unwrapping `browser_only`/`client_only`,
|
|
13
|
+
# folding `InstantRecord.browser?`, and unnesting `Syncable`).
|
|
14
|
+
class GemVendor
|
|
15
|
+
COPY = %w[
|
|
16
|
+
lib/instant_record.rb
|
|
17
|
+
lib/instant_record/version.rb
|
|
18
|
+
lib/instant_record/host.rb
|
|
19
|
+
lib/instant_record/configuration.rb
|
|
20
|
+
lib/instant_record/runtime_scoped.rb
|
|
21
|
+
lib/instant_record/inertia.rb
|
|
22
|
+
lib/instant_record/read_tracker.rb
|
|
23
|
+
lib/instant_record/sync_actions.rb
|
|
24
|
+
lib/instant_record/syncable.rb
|
|
25
|
+
lib/instant_record/outbox_mutation.rb
|
|
26
|
+
lib/instant_record/sync_metadata.rb
|
|
27
|
+
lib/instant_record/sync_partition.rb
|
|
28
|
+
lib/instant_record/hlc.rb
|
|
29
|
+
lib/instant_record/sync_notice.rb
|
|
30
|
+
lib/instant_record/sync_window.rb
|
|
31
|
+
lib/instant_record/client.rb
|
|
32
|
+
lib/instant_record/client/rebaser.rb
|
|
33
|
+
lib/instant_record/client/transport.rb
|
|
34
|
+
lib/instant_record/client/notifier.rb
|
|
35
|
+
lib/instant_record/branch.rb
|
|
36
|
+
lib/instant_record/native/node.rb
|
|
37
|
+
lib/instant_record/native/screen.rb
|
|
38
|
+
].freeze
|
|
39
|
+
|
|
40
|
+
SQLITE_DATABASE_YML = <<~YAML
|
|
41
|
+
default: &default
|
|
42
|
+
adapter: sqlite3
|
|
43
|
+
database: storage/development.sqlite3
|
|
44
|
+
|
|
45
|
+
development:
|
|
46
|
+
<<: *default
|
|
47
|
+
|
|
48
|
+
production:
|
|
49
|
+
<<: *default
|
|
50
|
+
YAML
|
|
51
|
+
|
|
52
|
+
attr_reader :adapter, :server_only_constants
|
|
53
|
+
|
|
54
|
+
def self.vendor(gem_src:, app_dir:, demo: false, target: :wasm, adapter: nil)
|
|
55
|
+
new(gem_src: gem_src, app_dir: app_dir, demo: demo, target: target, adapter: adapter).run
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def initialize(gem_src:, app_dir:, demo: false, target: :wasm, adapter: nil)
|
|
59
|
+
@gem = gem_src.to_s
|
|
60
|
+
@app = app_dir.to_s
|
|
61
|
+
@demo = demo
|
|
62
|
+
@adapter = HostAdapters.for(adapter || target)
|
|
63
|
+
@server_only_constants = {}
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def run
|
|
67
|
+
copy_gem_files
|
|
68
|
+
@adapter.install(gem_src: @gem, app_dir: @app)
|
|
69
|
+
unnest_syncable_concern
|
|
70
|
+
write_sqlite_database_yml
|
|
71
|
+
HostAdapters::Demo.apply(app_dir: @app) if @demo
|
|
72
|
+
@server_only_constants = StaticLinter.collect_server_only_constants(@app, staged: true)
|
|
73
|
+
lower_staged_ruby
|
|
74
|
+
COPY.length + 1
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
private
|
|
78
|
+
|
|
79
|
+
def copy_gem_files
|
|
80
|
+
FileUtils.rm_rf(File.join(@app, "lib/instant_record"))
|
|
81
|
+
COPY.each do |rel|
|
|
82
|
+
target = File.join(@app, rel)
|
|
83
|
+
FileUtils.mkdir_p(File.dirname(target))
|
|
84
|
+
FileUtils.cp(File.join(@gem, rel), target)
|
|
85
|
+
end
|
|
86
|
+
FileUtils.mkdir_p(File.join(@app, "app/models/instant_record"))
|
|
87
|
+
FileUtils.cp(
|
|
88
|
+
File.join(@gem, "app/models/instant_record/change.rb"),
|
|
89
|
+
File.join(@app, "app/models/instant_record/change.rb")
|
|
90
|
+
)
|
|
91
|
+
copy_optional_gem_file("app/helpers/instant_record/sync_notices_helper.rb")
|
|
92
|
+
copy_optional_gem_file("app/views/instant_record/sync_notices/_notices.html.erb")
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def copy_optional_gem_file(rel)
|
|
96
|
+
source = File.join(@gem, rel)
|
|
97
|
+
return unless File.file?(source)
|
|
98
|
+
|
|
99
|
+
target = File.join(@app, rel)
|
|
100
|
+
FileUtils.mkdir_p(File.dirname(target))
|
|
101
|
+
FileUtils.cp(source, target)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def unnest_syncable_concern
|
|
105
|
+
path = File.join(@app, "lib/instant_record/syncable.rb")
|
|
106
|
+
unnested = AstLowerer.unnest_syncable(File.read(path, encoding: Encoding::UTF_8))
|
|
107
|
+
|
|
108
|
+
concerns_dir = File.join(@app, "app/models/concerns")
|
|
109
|
+
FileUtils.mkdir_p(concerns_dir)
|
|
110
|
+
FileUtils.rm_rf(File.join(concerns_dir, "instant_record"))
|
|
111
|
+
File.write(File.join(concerns_dir, "syncable.rb"), unnested)
|
|
112
|
+
File.delete(path)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def write_sqlite_database_yml
|
|
116
|
+
config_dir = File.join(@app, "config")
|
|
117
|
+
FileUtils.mkdir_p(config_dir)
|
|
118
|
+
File.write(File.join(config_dir, "database.yml"), SQLITE_DATABASE_YML)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def lower_staged_ruby
|
|
122
|
+
lower_directory("app/models", rewrite_syncable_include: true)
|
|
123
|
+
%w[app/controllers app/helpers app/screens lib/instant_record].each do |subdir|
|
|
124
|
+
lower_directory(subdir, rewrite_syncable_include: false)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def lower_directory(subdir, rewrite_syncable_include:)
|
|
129
|
+
root = File.join(@app, subdir)
|
|
130
|
+
return unless File.directory?(root)
|
|
131
|
+
|
|
132
|
+
Dir.glob("**/*.rb", base: root).sort.each do |rel|
|
|
133
|
+
path = File.join(root, rel)
|
|
134
|
+
AstLowerer.lower_file(
|
|
135
|
+
path,
|
|
136
|
+
runtime: :client,
|
|
137
|
+
rewrite_syncable_include: rewrite_syncable_include
|
|
138
|
+
)
|
|
139
|
+
strip_native_contracts_file(path) if subdir == "app/screens"
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Remove class-level `contract ... do ... end` declarations from staged
|
|
144
|
+
# app/screens files before Roundhouse/Spinel compile them, preserving line
|
|
145
|
+
# counts so `#line` diagnostics in generated C remain exact.
|
|
146
|
+
def strip_native_contracts_file(path)
|
|
147
|
+
source = File.read(path, encoding: Encoding::UTF_8)
|
|
148
|
+
stripped = strip_native_contracts(source)
|
|
149
|
+
File.write(path, stripped) if stripped != source
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def strip_native_contracts(source)
|
|
153
|
+
parsed = Prism.parse(source)
|
|
154
|
+
return source unless parsed.success?
|
|
155
|
+
|
|
156
|
+
ranges = []
|
|
157
|
+
collect_contract_ranges(parsed.value, ranges)
|
|
158
|
+
return source if ranges.empty?
|
|
159
|
+
|
|
160
|
+
output = source.b
|
|
161
|
+
ranges.sort_by { |start_off, _| -start_off }.each do |start_off, end_off|
|
|
162
|
+
slice = output.byteslice(start_off...end_off)
|
|
163
|
+
output[start_off...end_off] = "\n" * slice.count("\n")
|
|
164
|
+
end
|
|
165
|
+
output.force_encoding(source.encoding)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def collect_contract_ranges(node, ranges)
|
|
169
|
+
return unless node
|
|
170
|
+
|
|
171
|
+
case node
|
|
172
|
+
when Prism::ProgramNode
|
|
173
|
+
collect_contract_ranges(node.statements, ranges)
|
|
174
|
+
when Prism::StatementsNode
|
|
175
|
+
node.body.each { |stmt| collect_contract_ranges(stmt, ranges) }
|
|
176
|
+
when Prism::ModuleNode
|
|
177
|
+
collect_contract_ranges(node.body, ranges)
|
|
178
|
+
when Prism::ClassNode
|
|
179
|
+
if node.body.is_a?(Prism::StatementsNode)
|
|
180
|
+
node.body.body.each do |stmt|
|
|
181
|
+
if stmt.is_a?(Prism::CallNode) && stmt.receiver.nil? && stmt.name == :contract
|
|
182
|
+
ranges << [stmt.location.start_offset, stmt.location.end_offset]
|
|
183
|
+
else
|
|
184
|
+
collect_contract_ranges(stmt, ranges)
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
else
|
|
188
|
+
collect_contract_ranges(node.body, ranges)
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
end
|