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.
Files changed (198) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +58 -0
  3. data/README.md +267 -61
  4. data/app/controllers/instant_record/bootstraps_controller.rb +39 -12
  5. data/app/controllers/instant_record/events_controller.rb +58 -6
  6. data/app/controllers/instant_record/files_controller.rb +116 -0
  7. data/app/controllers/instant_record/mutations_controller.rb +84 -5
  8. data/app/controllers/instant_record/native_screens_controller.rb +60 -0
  9. data/app/controllers/instant_record/records_controller.rb +86 -8
  10. data/app/controllers/instant_record/shells_controller.rb +64 -0
  11. data/app/controllers/instant_record/sync_notices_controller.rb +81 -0
  12. data/app/helpers/instant_record/sync_notices_helper.rb +41 -0
  13. data/app/jobs/instant_record/shell_build_job.rb +16 -0
  14. data/app/models/instant_record/change.rb +36 -3
  15. data/app/models/instant_record/mutation_applier.rb +633 -50
  16. data/app/views/instant_record/sync_notices/_notices.html.erb +63 -0
  17. data/compiled/README.md +272 -0
  18. data/compiled/pipeline/browser_migrations.rb +18 -0
  19. data/compiled/pipeline/build.rb +13 -0
  20. data/compiled/pipeline/build_native.rb +14 -0
  21. data/compiled/pipeline/native/host.c +78 -0
  22. data/compiled/pipeline/native/instant_record_native.h +57 -0
  23. data/compiled/pipeline/native_shim.c +149 -0
  24. data/compiled/pipeline/reactor_shim.c +66 -0
  25. data/compiled/pipeline/sqlite/rh_shim.c +5 -0
  26. data/compiled/pipeline/sqlite_release.rb +43 -0
  27. data/compiled/toolchain.env.example +22 -0
  28. data/compiled/web/index.html +20 -0
  29. data/compiled/web/package-lock.json +884 -0
  30. data/compiled/web/package.json +17 -0
  31. data/compiled/web/src/app-host-core.js +556 -0
  32. data/compiled/web/src/app-host.js +16 -0
  33. data/compiled/web/src/branch-manager.js +140 -0
  34. data/compiled/web/src/branch-preview.js +397 -0
  35. data/compiled/web/src/changes.js +191 -0
  36. data/compiled/web/src/faults.js +172 -0
  37. data/compiled/web/src/flight-recorder.js +347 -0
  38. data/compiled/web/src/history-fill.js +68 -0
  39. data/compiled/web/src/http-headers.js +100 -0
  40. data/compiled/web/src/leader-election.js +705 -0
  41. data/compiled/web/src/main.js +608 -0
  42. data/compiled/web/src/manifest.js +112 -0
  43. data/compiled/web/src/opfs-wasi-core.js +109 -0
  44. data/compiled/web/src/opfs-wasi-file.js +8 -0
  45. data/compiled/web/src/reactor-runtime.js +1692 -0
  46. data/compiled/web/src/redactor.js +518 -0
  47. data/compiled/web/src/release-updates.js +48 -0
  48. data/compiled/web/src/replay.js +338 -0
  49. data/compiled/web/src/schema-recovery.js +204 -0
  50. data/compiled/web/src/server-request.js +68 -0
  51. data/compiled/web/src/sha256.js +98 -0
  52. data/compiled/web/src/shell.js +52 -0
  53. data/compiled/web/src/snapshot-store.js +38 -0
  54. data/compiled/web/src/sqlite-redactor.js +111 -0
  55. data/compiled/web/src/storage-backend.js +159 -0
  56. data/compiled/web/src/stream-leader.js +675 -0
  57. data/compiled/web/src/sync-context.js +91 -0
  58. data/compiled/web/src/sync-drain.js +104 -0
  59. data/compiled/web/src/sync-schedule.js +9 -0
  60. data/compiled/web/src/wasi-minimal.js +275 -0
  61. data/compiled/web/src/worker.js +39 -0
  62. data/compiled/web/vite.config.js +27 -0
  63. data/config/routes.rb +25 -0
  64. data/db/migrate/20261119000201_add_scope_to_instant_record_changes.rb +7 -0
  65. data/db/migrate/20261121000001_create_instant_record_sync_notices.rb +21 -0
  66. data/db/migrate/20261121000101_add_partition_to_instant_record_changes.rb +8 -0
  67. data/db/migrate/20261121230300_add_delay_line_to_instant_record_outbox.rb +10 -0
  68. data/exe/instant_record_mcp +6 -0
  69. data/frontend/LiveList.js +67 -0
  70. data/frontend/chord.js +79 -0
  71. data/frontend/file.js +65 -0
  72. data/frontend/files.js +738 -0
  73. data/frontend/focus.js +10 -0
  74. data/frontend/held.js +192 -0
  75. data/frontend/http.js +76 -0
  76. data/frontend/index.js +53 -0
  77. data/frontend/keyboard.js +56 -0
  78. data/frontend/live.js +298 -0
  79. data/frontend/notices.js +118 -0
  80. data/frontend/package.json +15 -0
  81. data/frontend/range.js +10 -0
  82. data/frontend/selection.js +47 -0
  83. data/frontend/shortcuts.js +25 -0
  84. data/frontend/sync-notices.js +130 -0
  85. data/frontend/undo.js +48 -0
  86. data/frontend/window.js +181 -0
  87. data/lib/generators/instant_record/install/install_generator.rb +18 -30
  88. data/lib/generators/instant_record/install/templates/instant_record.rb +34 -0
  89. data/lib/generators/instant_record/ios/ios_generator.rb +71 -0
  90. data/lib/generators/instant_record/ios/templates/App.swift.tt +35 -0
  91. data/lib/generators/instant_record/ios/templates/Info.plist +11 -0
  92. data/lib/generators/instant_record/ios/templates/LibraryEngine.swift +30 -0
  93. data/lib/instant_record/branch/sqlite_store.rb +142 -0
  94. data/lib/instant_record/branch.rb +388 -0
  95. data/lib/instant_record/client/notifier.rb +8 -12
  96. data/lib/instant_record/client/rebaser.rb +565 -0
  97. data/lib/instant_record/client/transport.rb +64 -42
  98. data/lib/instant_record/client.rb +1600 -169
  99. data/lib/instant_record/compiled_site.rb +85 -0
  100. data/lib/instant_record/compiler/app_stage.rb +212 -0
  101. data/lib/instant_record/compiler/ast_lowerer.rb +659 -0
  102. data/lib/instant_record/compiler/browser_migrations.rb +161 -0
  103. data/lib/instant_record/compiler/entry_builder.rb +832 -0
  104. data/lib/instant_record/compiler/fts5_smoke.rb +205 -0
  105. data/lib/instant_record/compiler/gem_vendor.rb +194 -0
  106. data/lib/instant_record/compiler/host_adapters.rb +506 -0
  107. data/lib/instant_record/compiler/kotlin_codegen.rb +209 -0
  108. data/lib/instant_record/compiler/manifest.rb +178 -0
  109. data/lib/instant_record/compiler/native_build.rb +586 -0
  110. data/lib/instant_record/compiler/native_contracts.rb +338 -0
  111. data/lib/instant_record/compiler/path_scrubber.rb +31 -0
  112. data/lib/instant_record/compiler/reachability_tracer.rb +1188 -0
  113. data/lib/instant_record/compiler/route_analyzer.rb +623 -0
  114. data/lib/instant_record/compiler/runner.rb +42 -0
  115. data/lib/instant_record/compiler/schema_lock.rb +606 -0
  116. data/lib/instant_record/compiler/source_pages.rb +77 -0
  117. data/lib/instant_record/compiler/static_linter.rb +770 -0
  118. data/lib/instant_record/compiler/swift_codegen.rb +341 -0
  119. data/lib/instant_record/compiler/toolchain.rb +219 -0
  120. data/lib/instant_record/compiler/wasm_build.rb +704 -0
  121. data/lib/instant_record/compiler/wasm_tree.rb +518 -0
  122. data/lib/instant_record/compiler.rb +277 -0
  123. data/lib/instant_record/configuration.rb +104 -0
  124. data/lib/instant_record/engine.rb +68 -18
  125. data/lib/instant_record/file_store.rb +162 -0
  126. data/lib/instant_record/files.rb +92 -0
  127. data/lib/instant_record/hlc.rb +387 -0
  128. data/lib/instant_record/host.rb +66 -0
  129. data/lib/instant_record/inertia/renderer.rb +52 -0
  130. data/lib/instant_record/inertia.rb +222 -0
  131. data/lib/instant_record/mcp/cli.rb +319 -0
  132. data/lib/instant_record/mcp/host_process.rb +229 -0
  133. data/lib/instant_record/mcp/http_exchange.rb +233 -0
  134. data/lib/instant_record/mcp/replica.rb +556 -0
  135. data/lib/instant_record/mcp/runtime.rb +406 -0
  136. data/lib/instant_record/mcp/server.rb +429 -0
  137. data/lib/instant_record/mcp.rb +47 -0
  138. data/lib/instant_record/native/contract.rb +367 -0
  139. data/lib/instant_record/native/node.rb +125 -0
  140. data/lib/instant_record/native/screen.rb +366 -0
  141. data/lib/instant_record/native/xcode_project.rb +344 -0
  142. data/lib/instant_record/native.rb +41 -0
  143. data/lib/instant_record/outbox_mutation.rb +81 -9
  144. data/lib/instant_record/outbox_upcaster.rb +161 -0
  145. data/lib/instant_record/read_tracker.rb +610 -0
  146. data/lib/instant_record/routing.rb +33 -0
  147. data/lib/instant_record/runtime_scoped.rb +4 -0
  148. data/lib/instant_record/shell.rb +486 -0
  149. data/lib/instant_record/sync_actions.rb +800 -0
  150. data/lib/instant_record/sync_metadata.rb +17 -2
  151. data/lib/instant_record/sync_notice.rb +234 -0
  152. data/lib/instant_record/sync_partition.rb +63 -0
  153. data/lib/instant_record/sync_scope.rb +300 -0
  154. data/lib/instant_record/sync_shape.rb +102 -0
  155. data/lib/instant_record/sync_window.rb +92 -15
  156. data/lib/instant_record/syncable.rb +313 -17
  157. data/lib/instant_record/testing/authoritative_server.rb +547 -0
  158. data/lib/instant_record/testing/chaos_simulator.rb +429 -0
  159. data/lib/instant_record/testing/cruby_host_bridge.rb +370 -0
  160. data/lib/instant_record/testing/fault_network.rb +140 -0
  161. data/lib/instant_record/testing/parity_suite.rb +435 -0
  162. data/lib/instant_record/testing/runtimes/base.rb +184 -0
  163. data/lib/instant_record/testing/runtimes/cruby_runtime.rb +255 -0
  164. data/lib/instant_record/testing/runtimes/native_runtime.rb +346 -0
  165. data/lib/instant_record/testing/runtimes/wasm_runtime.rb +371 -0
  166. data/lib/instant_record/testing/wasm_host_bridge.mjs +240 -0
  167. data/lib/instant_record/testing.rb +333 -0
  168. data/lib/instant_record/trace/redactor.rb +278 -0
  169. data/lib/instant_record/trace/replayer.rb +126 -0
  170. data/lib/instant_record/trace/schema_extractor.rb +37 -0
  171. data/lib/instant_record/trace.rb +22 -0
  172. data/lib/instant_record/version.rb +1 -1
  173. data/lib/instant_record.rb +107 -37
  174. data/lib/tasks/instant_record.rake +299 -47
  175. data/native/ios/InstantRecordNative/Package.swift +12 -0
  176. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/AppHost.swift +706 -0
  177. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Engine.swift +64 -0
  178. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/FileStore.swift +1048 -0
  179. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/HTTPEngine.swift +36 -0
  180. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Icons.swift +19 -0
  181. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Node.swift +64 -0
  182. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/ScreenView.swift +481 -0
  183. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Screens.swift +94 -0
  184. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/StreamClient.swift +74 -0
  185. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncNotice.swift +282 -0
  186. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncSchedule.swift +153 -0
  187. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Theme.swift +140 -0
  188. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Transport.swift +108 -0
  189. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/TypedContracts.swift +237 -0
  190. data/native/ios/README.md +341 -0
  191. data/sig/instant_record/inertia.rbs +47 -0
  192. data/sig/instant_record/native.rbs +38 -0
  193. metadata +194 -18
  194. data/lib/generators/instant_record/install/templates/database.js +0 -32
  195. data/lib/generators/instant_record/install/templates/index.html +0 -69
  196. data/lib/generators/instant_record/install/templates/rails.sw.js +0 -750
  197. data/lib/instant_record/local_schema.rb +0 -75
  198. data/lib/instant_record/pglite_compat.rb +0 -40
@@ -0,0 +1,341 @@
1
+ require_relative "../native/contract"
2
+
3
+ module InstantRecord
4
+ class Compiler
5
+ # Emits strongly typed Swift 5.9+ / iOS 17+ `Codable` prop structs,
6
+ # `NativeAction` enums, and `@Observable` state stores from extracted
7
+ # `InstantRecord::Native::Contract` definitions.
8
+ module SwiftCodegen
9
+ KEYWORDS = %w[
10
+ associatedtype class deinit enum extension fileprivate func import init inout internal
11
+ let open operator private precedencegroup protocol public rethrows static struct
12
+ subscript typealias var break case catch continue default defer do else fallthrough
13
+ for guard if in repeat return switch throw where while as false is nil self Self
14
+ super throws true try Type any
15
+ ].freeze
16
+
17
+ SCALAR_TYPES = {
18
+ "string" => "String",
19
+ "int" => "Int",
20
+ "float" => "Double",
21
+ "bool" => "Bool",
22
+ "date" => "Date"
23
+ }.freeze
24
+
25
+ module_function
26
+
27
+ def generate(contracts)
28
+ lines = [
29
+ "// Generated by InstantRecord::Compiler::SwiftCodegen. Do not edit by hand.",
30
+ "import Foundation",
31
+ "import InstantRecordNative",
32
+ "import Observation",
33
+ ""
34
+ ]
35
+
36
+ contracts.each_with_index do |contract, index|
37
+ lines << "" if index > 0
38
+ lines.concat(render_contract(contract))
39
+ end
40
+
41
+ lines.join("\n") + "\n"
42
+ end
43
+
44
+ def render_contract(contract)
45
+ lines = []
46
+ props_name = "#{contract.component}Props"
47
+ action_name = contract.actions.empty? ? nil : "#{contract.component}Action"
48
+ state_name = "#{contract.component}State"
49
+
50
+ lines.concat(render_props_struct(
51
+ name: props_name,
52
+ component_name: contract.component,
53
+ action_type: action_name,
54
+ fields: contract.fields,
55
+ indent: "",
56
+ top_level: true
57
+ ))
58
+
59
+ if action_name
60
+ lines << ""
61
+ lines.concat(render_action_enum(action_name, contract.actions))
62
+ end
63
+
64
+ lines << ""
65
+ lines.concat(render_observable_state(state_name, props_name, action_name, contract.fields))
66
+ lines
67
+ end
68
+
69
+ def render_props_struct(name:, fields:, indent:, top_level:, component_name: nil, action_type: nil)
70
+ protocols = top_level ? "NativeProps, Codable, Equatable, Sendable" : "Codable, Equatable, Sendable"
71
+ lines = ["#{indent}public struct #{name}: #{protocols} {"]
72
+ inner = "#{indent} "
73
+
74
+ if top_level
75
+ lines << "#{inner}public typealias Action = #{action_type}" if action_type
76
+ lines << %(#{inner}public static let componentName = #{swift_string(component_name)})
77
+ end
78
+
79
+ nested_fields = fields.select { |f| f.one? || f.many? }
80
+ nested_fields.each do |field|
81
+ lines << "" if lines.last != "#{indent}public struct #{name}: #{protocols} {"
82
+ lines.concat(render_props_struct(
83
+ name: nested_type_name(field),
84
+ fields: field.fields,
85
+ indent: inner,
86
+ top_level: false
87
+ ))
88
+ end
89
+
90
+ if fields.any?
91
+ lines << "" if lines.last != "#{indent}public struct #{name}: #{protocols} {"
92
+ fields.each do |field|
93
+ lines << "#{inner}public let #{swift_ident(camel_case(field.name))}: #{swift_field_type(field)}"
94
+ end
95
+ end
96
+
97
+ lines << "" if lines.last != "#{indent}public struct #{name}: #{protocols} {"
98
+ if fields.empty?
99
+ lines << "#{inner}public init() {}"
100
+ else
101
+ params = fields.map do |field|
102
+ param = "#{swift_ident(camel_case(field.name))}: #{swift_field_type(field)}"
103
+ field.optional? ? "#{param} = nil" : param
104
+ end
105
+ lines << "#{inner}public init(#{params.join(', ')}) {"
106
+ fields.each do |field|
107
+ ident = swift_ident(camel_case(field.name))
108
+ lines << "#{inner} self.#{ident} = #{ident}"
109
+ end
110
+ lines << "#{inner}}"
111
+ end
112
+
113
+ if needs_coding_keys?(fields)
114
+ lines << ""
115
+ lines << "#{inner}private enum CodingKeys: String, CodingKey {"
116
+ fields.each do |field|
117
+ camel = camel_case(field.name)
118
+ ident = swift_ident(camel)
119
+ if camel == field.name
120
+ lines << "#{inner} case #{ident}"
121
+ else
122
+ lines << "#{inner} case #{ident} = #{swift_string(field.name)}"
123
+ end
124
+ end
125
+ lines << "#{inner}}"
126
+ end
127
+
128
+ lines << "#{indent}}"
129
+ lines
130
+ end
131
+
132
+ def render_action_enum(enum_name, actions)
133
+ lines = ["public enum #{enum_name}: NativeAction, Equatable, Sendable {"]
134
+
135
+ actions.each do |action|
136
+ case_name = swift_ident(camel_case(action.name))
137
+ if action.params.empty?
138
+ lines << " case #{case_name}"
139
+ else
140
+ params = action.params.map do |param|
141
+ base = SCALAR_TYPES.fetch(param.type)
142
+ type = param.optional? ? "#{base}?" : base
143
+ "#{swift_ident(camel_case(param.name))}: #{type}"
144
+ end
145
+ lines << " case #{case_name}(#{params.join(', ')})"
146
+ end
147
+ end
148
+
149
+ lines << ""
150
+ lines << " public var actionName: String {"
151
+ lines << " switch self {"
152
+ actions.each do |action|
153
+ case_name = swift_ident(camel_case(action.name))
154
+ lines << " case .#{case_name}: #{swift_string(action.name)}"
155
+ end
156
+ lines << " }"
157
+ lines << " }"
158
+
159
+ lines << ""
160
+ lines.concat(render_action_dictionary("actionArgs", actions, :args))
161
+ lines << ""
162
+ lines.concat(render_action_dictionary("actionValues", actions, :values))
163
+
164
+ lines << "}"
165
+ lines
166
+ end
167
+
168
+ def render_action_dictionary(property_name, actions, slot_method)
169
+ lines = [
170
+ " public var #{property_name}: [String: String] {",
171
+ " switch self {"
172
+ ]
173
+
174
+ actions.each do |action|
175
+ case_name = swift_ident(camel_case(action.name))
176
+ selected = action.public_send(slot_method)
177
+ if selected.empty?
178
+ lines << " case .#{case_name}: return [:]"
179
+ else
180
+ bindings = action.params.map do |param|
181
+ selected.include?(param) ? "let #{swift_ident(camel_case(param.name))}" : "_"
182
+ end
183
+ if selected.any?(&:optional?)
184
+ lines << " case .#{case_name}(#{bindings.join(', ')}):"
185
+ lines << " var out: [String: String] = [:]"
186
+ selected.each do |param|
187
+ ident = swift_ident(camel_case(param.name))
188
+ if param.optional?
189
+ lines << " if let #{ident} { out[#{swift_string(param.name)}] = #{encode_expr(ident, param.type)} }"
190
+ else
191
+ lines << " out[#{swift_string(param.name)}] = #{encode_expr(ident, param.type)}"
192
+ end
193
+ end
194
+ lines << " return out"
195
+ else
196
+ pairs = selected.map do |param|
197
+ ident = swift_ident(camel_case(param.name))
198
+ "#{swift_string(param.name)}: #{encode_expr(ident, param.type)}"
199
+ end
200
+ lines << " case .#{case_name}(#{bindings.join(', ')}): return [#{pairs.join(', ')}]"
201
+ end
202
+ end
203
+ end
204
+
205
+ lines << " }"
206
+ lines << " }"
207
+ lines
208
+ end
209
+
210
+ def render_observable_state(state_name, props_name, action_name, fields)
211
+ field_names = fields.map { |f| camel_case(f.name) }
212
+ snapshot_prop = field_names.include?("snapshot") ? "_snapshot" : "snapshot"
213
+ lines = [
214
+ "@Observable",
215
+ "public final class #{state_name}: NativePropsState {",
216
+ " public typealias Props = #{props_name}",
217
+ ""
218
+ ]
219
+
220
+ fields.each do |field|
221
+ camel = camel_case(field.name)
222
+ ident = swift_ident(camel)
223
+ type = swift_field_type(field, qualifier: props_name)
224
+ if KEYWORDS.include?(camel)
225
+ storage = state_storage_name(camel)
226
+ lines << " private var #{storage}: #{type}"
227
+ lines << " public var #{ident}: #{type} { #{storage} }"
228
+ else
229
+ lines << " public private(set) var #{ident}: #{type}"
230
+ end
231
+ end
232
+ lines << " public private(set) var #{snapshot_prop}: #{props_name}"
233
+ lines << " @ObservationIgnored private let _actions: TypedActions<#{props_name}.Action>"
234
+ lines << ""
235
+ lines << " public init(props: #{props_name}, actions: TypedActions<#{props_name}.Action>) {"
236
+ fields.each do |field|
237
+ camel = camel_case(field.name)
238
+ ident = swift_ident(camel)
239
+ target = KEYWORDS.include?(camel) ? state_storage_name(camel) : ident
240
+ lines << " self.#{target} = props.#{ident}"
241
+ end
242
+ lines << " self.#{snapshot_prop} = props"
243
+ lines << " self._actions = actions"
244
+ lines << " }"
245
+ lines << ""
246
+ lines << " public func apply(_ props: #{props_name}) {"
247
+ lines << " guard self.#{snapshot_prop} != props else { return }"
248
+ fields.each do |field|
249
+ camel = camel_case(field.name)
250
+ ident = swift_ident(camel)
251
+ target = KEYWORDS.include?(camel) ? state_storage_name(camel) : ident
252
+ lines << " if self.#{target} != props.#{ident} { self.#{target} = props.#{ident} }"
253
+ end
254
+ lines << " self.#{snapshot_prop} = props"
255
+ lines << " }"
256
+
257
+ if action_name
258
+ lines << ""
259
+ lines << " public func send(_ action: #{action_name}) {"
260
+ lines << " _actions.send(action)"
261
+ lines << " }"
262
+ end
263
+
264
+ lines << "}"
265
+ lines
266
+ end
267
+
268
+ def state_storage_name(camel)
269
+ "_#{camel}Value"
270
+ end
271
+
272
+ def swift_field_type(field, qualifier: nil)
273
+ base =
274
+ case field.kind
275
+ when :scalar
276
+ SCALAR_TYPES.fetch(field.type)
277
+ when :one
278
+ nested = nested_type_name(field)
279
+ qualifier ? "#{qualifier}.#{nested}" : nested
280
+ when :many
281
+ nested = nested_type_name(field)
282
+ "[#{qualifier ? "#{qualifier}.#{nested}" : nested}]"
283
+ when :many_scalar
284
+ "[#{SCALAR_TYPES.fetch(field.type)}]"
285
+ end
286
+ field.optional? ? "#{base}?" : base
287
+ end
288
+
289
+ def encode_expr(ident, type)
290
+ type == "string" ? ident : "NativeWire.encode(#{ident})"
291
+ end
292
+
293
+ def nested_type_name(field)
294
+ return field.type_name if field.type_name
295
+
296
+ base = field.many? ? singularize(field.name) : field.name
297
+ pascal_case(base)
298
+ end
299
+
300
+ def needs_coding_keys?(fields)
301
+ fields.any? { |field| camel_case(field.name) != field.name }
302
+ end
303
+
304
+ def camel_case(name)
305
+ parts = name.to_s.split("_")
306
+ return name.to_s if parts.length <= 1
307
+
308
+ parts.first + parts[1..].map { |part| part.empty? ? "" : part[0].upcase + part[1..] }.join
309
+ end
310
+
311
+ def pascal_case(name)
312
+ name.to_s.split("_").map { |part| part.empty? ? "" : part[0].upcase + part[1..] }.join
313
+ end
314
+
315
+ def singularize(name)
316
+ word = name.to_s
317
+ return "#{word[0..-4]}y" if word.end_with?("ies") && word.length > 3
318
+ return word[0..-3] if word.match?(/(?:ch|sh|x|z|ss)es\z/)
319
+ return "person" if word == "people"
320
+ return "child" if word == "children"
321
+ return word[0..-2] if word.end_with?("s") && !word.end_with?("ss") && word.length > 1
322
+
323
+ "#{word}_item"
324
+ end
325
+
326
+ def swift_ident(name)
327
+ KEYWORDS.include?(name) ? "`#{name}`" : name
328
+ end
329
+
330
+ def swift_string(value)
331
+ escaped = value.to_s
332
+ .gsub("\\", "\\\\")
333
+ .gsub("\"", "\\\"")
334
+ .gsub("\n", "\\n")
335
+ .gsub("\r", "\\r")
336
+ .gsub("\t", "\\t")
337
+ %("#{escaped}")
338
+ end
339
+ end
340
+ end
341
+ end
@@ -0,0 +1,219 @@
1
+ require "open3"
2
+ require "pathname"
3
+ require_relative "runner"
4
+
5
+ module InstantRecord
6
+ class Compiler
7
+ # Reads a toolchain.env file (KEY=VALUE lines with $VAR expansion) and
8
+ # validates that the required external tools are present before a build.
9
+ class Toolchain
10
+ Check = Struct.new(:name, :status, :detail, keyword_init: true) do
11
+ def ok? = status == :ok || status == :optional
12
+ end
13
+
14
+ Report = Struct.new(:target, :path, :checks, :lint, keyword_init: true) do
15
+ def ok? = checks.all?(&:ok?) && (lint.nil? || lint.ok?)
16
+
17
+ def with_lint(lint_result)
18
+ lint_check =
19
+ if lint_result.ok?
20
+ Check.new(name: "static lint", status: :ok, detail: "0 issues across #{lint_result.files_checked} files")
21
+ else
22
+ Check.new(name: "static lint", status: :fail, detail: "#{lint_result.diagnostics.size} issue(s) across #{lint_result.files_checked} files")
23
+ end
24
+ Report.new(target: target, path: path, checks: checks + [lint_check], lint: lint_result)
25
+ end
26
+
27
+ def to_s
28
+ lines = ["InstantRecord toolchain check (#{target}) — #{path}"]
29
+ checks.each do |check|
30
+ badge = case check.status
31
+ when :ok then "OK"
32
+ when :optional then "OPTIONAL"
33
+ when :error then "ERROR"
34
+ when :fail then "FAIL"
35
+ else "MISSING"
36
+ end
37
+ lines << format(" [%-8s] %-18s %s", badge, check.name, check.detail)
38
+ end
39
+ if lint && !lint.ok?
40
+ lines << ""
41
+ lines << lint.to_s
42
+ end
43
+ lines.join("\n")
44
+ end
45
+ end
46
+
47
+ attr_reader :path, :values
48
+
49
+ def self.load(path)
50
+ return path if path.is_a?(self)
51
+
52
+ expanded = Pathname(path).expand_path
53
+ raise ArgumentError, "Missing compiler toolchain file: #{expanded}" unless expanded.file?
54
+
55
+ values = {}
56
+ File.foreach(expanded) do |raw|
57
+ line = raw.strip
58
+ next if line.empty? || line.start_with?("#") || !line.include?("=")
59
+
60
+ key, value = line.split("=", 2)
61
+ value = value.gsub(/\$([A-Za-z_]\w*)/) { |match| values.fetch(Regexp.last_match(1), match) }
62
+ values[key] = value
63
+ end
64
+ new(path: expanded.to_s, values: values)
65
+ end
66
+
67
+ def self.which(command, extra_paths: [])
68
+ if command.include?(File::SEPARATOR)
69
+ return (File.file?(command) && File.executable?(command)) ? command : nil
70
+ end
71
+
72
+ search = Array(extra_paths) + ENV.fetch("PATH", "").split(File::PATH_SEPARATOR)
73
+ search.each do |dir|
74
+ next if dir.nil? || dir.empty?
75
+
76
+ candidate = File.join(dir, command)
77
+ return candidate if File.file?(candidate) && File.executable?(candidate)
78
+ end
79
+ nil
80
+ end
81
+
82
+ def initialize(path:, values:)
83
+ @path = path
84
+ @values = values
85
+ end
86
+
87
+ def [](key)
88
+ @values[key.to_s]
89
+ end
90
+
91
+ def fetch(key)
92
+ value = @values[key.to_s]
93
+ raise BuildError, "Missing #{key} in #{@path}" if value.nil? || value.empty?
94
+
95
+ value
96
+ end
97
+
98
+ def join(key, *parts)
99
+ File.join(fetch(key), *parts)
100
+ end
101
+
102
+ def doctor(target: :wasm, web_dir: nil)
103
+ mode = target.nil? || target.to_s.empty? ? :wasm : target.to_sym
104
+ checks = [
105
+ check_exec("spinel", optional_path("SPINEL_HOME", "bin/spinel"), "SPINEL_HOME/bin/spinel"),
106
+ check_exec("spin", optional_path("SPINEL_HOME", "bin/spin"), "SPINEL_HOME/bin/spin"),
107
+ check_exec("roundhouse", optional_path("ROUNDHOUSE_BIN"), "ROUNDHOUSE_BIN"),
108
+ check_file("sqlite3.c", optional_path("SQLITE_SRC", "sqlite3.c"), "SQLITE_SRC/sqlite3.c"),
109
+ check_writable_dir("CACHE", self["CACHE"])
110
+ ]
111
+
112
+ case mode
113
+ when :wasm
114
+ checks << check_file("spinel wasm-rt", optional_path("SPINEL_HOME", "lib/wasm32-wasi/libspinel_rt.a"), "SPINEL_HOME/lib/wasm32-wasi/libspinel_rt.a (run `make wasm-rt`)")
115
+ checks << check_exec("wasi clang", optional_path("WASI_SDK", "bin/clang"), "WASI_SDK/bin/clang")
116
+ checks << check_exec("wasi llvm-ar", optional_path("WASI_SDK", "bin/llvm-ar"), "WASI_SDK/bin/llvm-ar")
117
+ checks << check_exec("wasi llvm-strip", optional_path("WASI_SDK", "bin/llvm-strip"), "WASI_SDK/bin/llvm-strip")
118
+ checks << check_command("npx", "Node/npm on PATH")
119
+ checks << check_web_modules(web_dir)
120
+ checks << check_rails_gems
121
+ wasm_opt = self.class.which("wasm-opt")
122
+ checks << Check.new(name: "wasm-opt", status: wasm_opt ? :ok : :optional, detail: wasm_opt || "not on PATH (optional binaryen pass)")
123
+ when :native
124
+ checks << check_file("spinel rt", optional_path("SPINEL_HOME", "lib/libspinel_rt.a"), "SPINEL_HOME/lib/libspinel_rt.a (run `make`)")
125
+ cc = ENV.fetch("CC", "cc")
126
+ checks << check_command(cc, "C compiler (#{cc})")
127
+ checks << check_command("ar", "archiver (ar)")
128
+ when :iphonesimulator, :iphoneos
129
+ checks << check_file("spinel Makefile", optional_path("SPINEL_HOME", "Makefile"), "SPINEL_HOME/Makefile")
130
+ checks << check_xcode_sdk(mode.to_s)
131
+ checks << check_command("ar", "archiver (ar)")
132
+ else
133
+ raise ArgumentError, "Unknown doctor target: #{target}"
134
+ end
135
+
136
+ Report.new(target: mode, path: @path, checks: checks)
137
+ end
138
+
139
+ private
140
+
141
+ def optional_path(key, *parts)
142
+ base = self[key]
143
+ return nil if base.nil? || base.empty?
144
+
145
+ parts.empty? ? base : File.join(base, *parts)
146
+ end
147
+
148
+ def check_exec(name, path, hint)
149
+ if path && File.file?(path) && File.executable?(path)
150
+ Check.new(name: name, status: :ok, detail: path)
151
+ else
152
+ Check.new(name: name, status: :missing, detail: "not executable: #{path || hint}")
153
+ end
154
+ end
155
+
156
+ def check_file(name, path, hint)
157
+ if path && File.file?(path)
158
+ Check.new(name: name, status: :ok, detail: path)
159
+ else
160
+ Check.new(name: name, status: :missing, detail: "not found: #{path || hint}")
161
+ end
162
+ end
163
+
164
+ def check_command(command, label)
165
+ found = self.class.which(command)
166
+ if found
167
+ Check.new(name: command, status: :ok, detail: found)
168
+ else
169
+ Check.new(name: command, status: :missing, detail: "#{label} not found on PATH")
170
+ end
171
+ end
172
+
173
+ def check_writable_dir(name, dir)
174
+ if dir && !dir.empty?
175
+ parent = File.directory?(dir) ? dir : File.dirname(dir)
176
+ if File.directory?(parent) && File.writable?(parent)
177
+ return Check.new(name: name, status: :ok, detail: dir)
178
+ end
179
+ end
180
+ Check.new(name: name, status: :missing, detail: "not writable: #{dir || name}")
181
+ end
182
+
183
+ def check_web_modules(web_dir)
184
+ configured = optional_path("WEB_NODE_MODULES")
185
+ local = web_dir && File.join(web_dir, "node_modules")
186
+ if configured && File.directory?(configured)
187
+ Check.new(name: "WEB_NODE_MODULES", status: :ok, detail: configured)
188
+ elsif local && File.directory?(local)
189
+ Check.new(name: "WEB_NODE_MODULES", status: :ok, detail: "#{local} (local)")
190
+ else
191
+ Check.new(name: "WEB_NODE_MODULES", status: :missing, detail: "missing directory: #{configured || 'WEB_NODE_MODULES'}")
192
+ end
193
+ end
194
+
195
+ def check_rails_gems
196
+ configured = optional_path("RAILS_GEMS")
197
+ if configured && File.directory?(configured)
198
+ Check.new(name: "RAILS_GEMS", status: :ok, detail: configured)
199
+ elsif %w[turbo-rails stimulus-rails].all? { |gem_name| Gem.loaded_specs.key?(gem_name) }
200
+ Check.new(name: "RAILS_GEMS", status: :ok, detail: "resolved from Gem.loaded_specs")
201
+ else
202
+ Check.new(name: "RAILS_GEMS", status: :missing, detail: "missing directory: #{configured || 'RAILS_GEMS'}")
203
+ end
204
+ end
205
+
206
+ def check_xcode_sdk(sdk)
207
+ out, status = Open3.capture2e("xcrun", "--sdk", sdk, "--show-sdk-path")
208
+ detail = out.strip
209
+ if status.success? && !detail.empty?
210
+ Check.new(name: "#{sdk} SDK", status: :ok, detail: detail)
211
+ else
212
+ Check.new(name: "#{sdk} SDK", status: :missing, detail: detail.empty? ? "xcrun failed" : detail)
213
+ end
214
+ rescue Errno::ENOENT
215
+ Check.new(name: "#{sdk} SDK", status: :missing, detail: "xcrun not found on PATH")
216
+ end
217
+ end
218
+ end
219
+ end