Module: HomuraRuntime::BuildSupport
- Defined in:
- lib/homura/runtime/build_support.rb
Constant Summary collapse
- RUNTIME_GEM_NAME =
"homura-runtime"- SINATRA_GEM_NAME =
"sinatra-homura"- SEQUEL_D1_GEM_NAME =
"sequel-d1"- SUPPORTED_OPAL_ENTRY_GEMS =
%w[phlex literal].freeze
- OPAL_ENTRY_COMPAT_REQUIRES =
{ "phlex" => "phlex/opal_compat", "literal" => "literal/opal_compat" }.freeze
- OPAL_ENTRY_PRELOAD_REQUIRES =
{ "phlex" => %w[monitor json phlex/fifo] }.freeze
- EXCLUDED_GROUPS =
Returns absolute Pathnames for every ‘path:`-declared gem in the project’s Gemfile that should ship in the Workers bundle.
Excludes:
-
gems we already wire in explicitly (homura-runtime / sinatra-homura / sequel-d1)
-
‘require: false` gems (dev tooling like `gem ’rspec’, path: …, require: false`)
-
gems declared inside ‘group :development do … end` / `group :test do … end` blocks (they don’t ship to production)
-
%i[ development test dev_test development_test ci tools ].freeze
Class Method Summary collapse
- .ensure_standalone_runtime(project_root, current_file: __FILE__, loaded_specs: Gem.loaded_specs) ⇒ Object
- .env_opal_gem_names(env = ENV) ⇒ Object
- .gem_lib(name, loaded_specs: Gem.loaded_specs) ⇒ Object
- .gem_root(name, loaded_specs: Gem.loaded_specs) ⇒ Object
- .gem_vendor(name, loaded_specs: Gem.loaded_specs) ⇒ Object
- .loaded_spec(name, loaded_specs: Gem.loaded_specs) ⇒ Object
- .maybe_gem_lib(name, loaded_specs: Gem.loaded_specs) ⇒ Object
- .maybe_gem_vendor(name, loaded_specs: Gem.loaded_specs) ⇒ Object
- .opal_dependency_paths(specs, loaded_specs: Gem.loaded_specs) ⇒ Object
- .opal_entry_gem_specs(loaded_specs: Gem.loaded_specs) ⇒ Object
-
.opal_gem_paths(project_root, loaded_specs: Gem.loaded_specs) ⇒ Object
Returns the union of ‘path_gemfile_entries(project_root)` and any bundled gems that opt in to the Opal pipeline via `spec.metadata`.
- .path_gemfile_entries(project_root) ⇒ Object
- .runtime_file(*names, current_file: __FILE__, loaded_specs: Gem.loaded_specs) ⇒ Object
- .runtime_root(current_file:, loaded_specs: Gem.loaded_specs) ⇒ Object
- .standalone_load_paths(project_root, with_db:, loaded_specs: Gem.loaded_specs) ⇒ Object
- .standalone_namespace(project_root, suffix) ⇒ Object
- .vendor_from_gemfile(project_root) ⇒ Object
- .write_opal_gems_prelude(project_root, loaded_specs: Gem.loaded_specs) ⇒ Object
Class Method Details
.ensure_standalone_runtime(project_root, current_file: __FILE__, loaded_specs: Gem.loaded_specs) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/homura/runtime/build_support.rb', line 81 def ensure_standalone_runtime( project_root, current_file: __FILE__, loaded_specs: Gem.loaded_specs ) # The homura runtime needs two .mjs glue files alongside the # generated `worker.entrypoint.mjs`. Until 0.2.22 we wrote them # to `cf-runtime/` at the project root, which made every Ruby # repo carry two opaque JS files in source control. Hide them # under `build/cf-runtime/` so the build artifact tree owns # them — `build/` is already in the example .gitignore template. target_dir = Pathname(project_root).join("build", "cf-runtime") FileUtils.mkdir_p(target_dir) %w[setup-node-crypto.mjs worker_module.mjs].each do |name| FileUtils.cp( runtime_file( name, current_file: current_file, loaded_specs: loaded_specs ), target_dir.join(name) ) end target_dir end |
.env_opal_gem_names(env = ENV) ⇒ Object
285 286 287 288 289 290 291 |
# File 'lib/homura/runtime/build_support.rb', line 285 def env_opal_gem_names(env = ENV) env .fetch("HOMURA_OPAL_GEMS", "") .split(/[\s,]+/) .map(&:strip) .reject(&:empty?) end |
.gem_lib(name, loaded_specs: Gem.loaded_specs) ⇒ Object
41 42 43 |
# File 'lib/homura/runtime/build_support.rb', line 41 def gem_lib(name, loaded_specs: Gem.loaded_specs) File.join(gem_root(name, loaded_specs: loaded_specs), "lib") end |
.gem_root(name, loaded_specs: Gem.loaded_specs) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/homura/runtime/build_support.rb', line 25 def gem_root(name, loaded_specs: Gem.loaded_specs) spec = loaded_spec(name, loaded_specs: loaded_specs) return spec.full_gem_path if spec raise( "homura build: gem #{name} not loaded; use bundle exec from app root" ) end |
.gem_vendor(name, loaded_specs: Gem.loaded_specs) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/homura/runtime/build_support.rb', line 45 def gem_vendor(name, loaded_specs: Gem.loaded_specs) vendor = File.join(gem_root(name, loaded_specs: loaded_specs), "vendor") return vendor if Dir.exist?(vendor) nil end |
.loaded_spec(name, loaded_specs: Gem.loaded_specs) ⇒ Object
21 22 23 |
# File 'lib/homura/runtime/build_support.rb', line 21 def loaded_spec(name, loaded_specs: Gem.loaded_specs) loaded_specs[name] end |
.maybe_gem_lib(name, loaded_specs: Gem.loaded_specs) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/homura/runtime/build_support.rb', line 52 def maybe_gem_lib(name, loaded_specs: Gem.loaded_specs) spec = loaded_spec(name, loaded_specs: loaded_specs) return nil unless spec File.join(spec.full_gem_path, "lib") end |
.maybe_gem_vendor(name, loaded_specs: Gem.loaded_specs) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/homura/runtime/build_support.rb', line 59 def maybe_gem_vendor(name, loaded_specs: Gem.loaded_specs) spec = loaded_spec(name, loaded_specs: loaded_specs) return nil unless spec vendor = File.join(spec.full_gem_path, "vendor") return vendor if Dir.exist?(vendor) nil end |
.opal_dependency_paths(specs, loaded_specs: Gem.loaded_specs) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/homura/runtime/build_support.rb', line 221 def opal_dependency_paths(specs, loaded_specs: Gem.loaded_specs) seen = {} out = [] queue = specs.flat_map { |spec| spec.runtime_dependencies.map(&:name) } until queue.empty? name = queue.shift next if seen[name] seen[name] = true spec = loaded_specs[name] next unless spec&.full_gem_path path = Pathname(spec.full_gem_path) out << path if path.directory? queue.concat(spec.runtime_dependencies.map(&:name)) end out end |
.opal_entry_gem_specs(loaded_specs: Gem.loaded_specs) ⇒ Object
216 217 218 219 |
# File 'lib/homura/runtime/build_support.rb', line 216 def opal_entry_gem_specs(loaded_specs: Gem.loaded_specs) names = SUPPORTED_OPAL_ENTRY_GEMS + env_opal_gem_names names.uniq.filter_map { |name| loaded_specs[name] } end |
.opal_gem_paths(project_root, loaded_specs: Gem.loaded_specs) ⇒ Object
Returns the union of ‘path_gemfile_entries(project_root)` and any bundled gems that opt in to the Opal pipeline via `spec.metadata`. This is the single source of truth for both `standalone_load_paths` and the auto-await pass that `homura-build` runs. Returns `Pathname` objects pointing at each gem’s root directory.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/homura/runtime/build_support.rb', line 193 def opal_gem_paths(project_root, loaded_specs: Gem.loaded_specs) wired = [RUNTIME_GEM_NAME, SINATRA_GEM_NAME, SEQUEL_D1_GEM_NAME] out = [] out.concat(path_gemfile_entries(project_root)) entry_specs = opal_entry_gem_specs(loaded_specs: loaded_specs) out.concat(entry_specs.map { |spec| Pathname(spec.full_gem_path) if spec.full_gem_path }.compact) out.concat(opal_dependency_paths(entry_specs, loaded_specs: loaded_specs)) loaded_specs.each_value do |spec| next if wired.include?(spec.name) = spec. next unless .is_a?(Hash) flag = ["homura.auto_await"] next unless flag == "true" || flag == true next if spec.full_gem_path.nil? gem_path = Pathname(spec.full_gem_path) out << gem_path if gem_path.directory? end out.uniq end |
.path_gemfile_entries(project_root) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/homura/runtime/build_support.rb', line 311 def path_gemfile_entries(project_root) gf = Pathname(project_root).join("Gemfile") return [] unless gf.file? wired = [RUNTIME_GEM_NAME, SINATRA_GEM_NAME, SEQUEL_D1_GEM_NAME] out = [] group_stack = [] gf.read.each_line do |line| stripped = line.strip next if stripped.empty? || stripped.start_with?("#") if (m = stripped.match(/\Agroup\s+(.+?)\s+do\b/)) groups = m[1].scan(/[:'"]([A-Za-z0-9_]+)['"]?/).flatten.map(&:to_sym) group_stack.push(groups) next end if stripped == "end" group_stack.pop unless group_stack.empty? next end next if group_stack.flatten.any? { |g| EXCLUDED_GROUPS.include?(g) } m = line.match(/gem\s+['"]([^'"]+)['"][^#]*?path:\s*['"]([^'"]+)['"]/) next unless m name, rel = m[1], m[2] next if wired.include?(name) next if line.match?(/require:\s*false/) gem_path = Pathname.new(rel).(project_root) out << gem_path if gem_path.directory? end out.uniq end |
.runtime_file(*names, current_file: __FILE__, loaded_specs: Gem.loaded_specs) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/homura/runtime/build_support.rb', line 69 def runtime_file( *names, current_file: __FILE__, loaded_specs: Gem.loaded_specs ) runtime_root( current_file: current_file, loaded_specs: loaded_specs ) .join("runtime", *names) end |
.runtime_root(current_file:, loaded_specs: Gem.loaded_specs) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/homura/runtime/build_support.rb', line 34 def runtime_root(current_file:, loaded_specs: Gem.loaded_specs) spec = loaded_spec(RUNTIME_GEM_NAME, loaded_specs: loaded_specs) return Pathname(spec.full_gem_path) if spec Pathname(current_file)..join("../..") end |
.standalone_load_paths(project_root, with_db:, loaded_specs: Gem.loaded_specs) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/homura/runtime/build_support.rb', line 109 def standalone_load_paths( project_root, with_db:, loaded_specs: Gem.loaded_specs ) root = Pathname(project_root) load_paths = [] hv = vendor_from_gemfile(root) load_paths << hv.to_s if hv load_paths += %w[. build/auto_await build/auto_await/app app] [ gem_lib(RUNTIME_GEM_NAME, loaded_specs: loaded_specs), gem_vendor(RUNTIME_GEM_NAME, loaded_specs: loaded_specs), maybe_gem_lib(SINATRA_GEM_NAME, loaded_specs: loaded_specs), maybe_gem_vendor(SINATRA_GEM_NAME, loaded_specs: loaded_specs) ].compact.each { |path| load_paths << path } if with_db [ gem_vendor(SEQUEL_D1_GEM_NAME, loaded_specs: loaded_specs), gem_lib(SEQUEL_D1_GEM_NAME, loaded_specs: loaded_specs) ].compact.each { |path| load_paths << path } end # Pick up any other gems that should ship in the Workers bundle: # # * `path:`-resolved gems in the consumer's Gemfile (monorepo # dev mode), and # * RubyGems-installed gems that opt in via # `spec.metadata['homura.auto_await'] = 'true'`. # # Both go through the same auto-await pass during `homura-build`, # and we prefer the rewritten copy under # `build/auto_await/gem_<basename>/lib` if present so async chains # inside gem code get `__await__` inserted just like consumer # app code. opal_gem_paths(root, loaded_specs: loaded_specs).each do |gem_path| basename = gem_path.basename.to_s rewritten_lib = root.join("build", "auto_await", "gem_#{basename}", "lib") load_paths << rewritten_lib.to_s if rewritten_lib.directory? %w[lib vendor].each do |sub| dir = gem_path.join(sub) load_paths << dir.to_s if dir.directory? end end load_paths << "vendor" if root.join("vendor").directory? load_paths << "build" load_paths.uniq end |
.standalone_namespace(project_root, suffix) ⇒ Object
162 163 164 165 166 167 168 169 |
# File 'lib/homura/runtime/build_support.rb', line 162 def standalone_namespace(project_root, suffix) base = Pathname(project_root).basename.to_s parts = base.split(/[^A-Za-z0-9]+/).reject(&:empty?) module_name = parts.map { |part| part[0].upcase + part[1..].to_s }.join module_name = "App" if module_name.empty? module_name = "App#{module_name}" if module_name.match?(/\A\d/) "#{module_name}#{suffix}" end |
.vendor_from_gemfile(project_root) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/homura/runtime/build_support.rb', line 171 def vendor_from_gemfile(project_root) gf = Pathname(project_root).join("Gemfile") return unless gf.file? txt = gf.read unless (m = txt.match( /#{Regexp.escape(RUNTIME_GEM_NAME)}['"]\s*,\s*path:\s*['"]([^'"]+)['"]/ )) return end runtime_path = Pathname.new(m[1]).(project_root) vend = runtime_path.join("..", "..", "vendor"). vend if vend.directory? end |
.write_opal_gems_prelude(project_root, loaded_specs: Gem.loaded_specs) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/homura/runtime/build_support.rb', line 242 def write_opal_gems_prelude(project_root, loaded_specs: Gem.loaded_specs) entry_specs = opal_entry_gem_specs(loaded_specs: loaded_specs) return nil if entry_specs.empty? root = Pathname(project_root) out = root.join("build", "homura_opal_gems.rb") lines = ["# frozen_string_literal: true", ""] entry_specs.each do |spec| next unless spec.full_gem_path lib = Pathname(spec.full_gem_path).join("lib") next unless lib.directory? lines << "require_tree #{lib.to_s.inspect}, autoload: true" end lines << "" lines << "require \"erb/opal_compat\"" lines << "require \"zeitwerk/opal_compat\"" entry_specs.each do |spec| compat = OPAL_ENTRY_COMPAT_REQUIRES[spec.name] next unless compat && spec.full_gem_path lib = Pathname(spec.full_gem_path).join("lib") root_file = lib.join("#{spec.name}.rb") next unless root_file.file? lines << "module Phlex; end unless defined?(Phlex)" if spec.name == "phlex" OPAL_ENTRY_PRELOAD_REQUIRES.fetch(spec.name, []).each do |require_path| lines << "require #{require_path.inspect}" end lines << "Zeitwerk.__homura_next_gem_root = #{root_file.to_s.inspect}" lines << "require #{("#{spec.name}.rb").inspect}" lines << "`Opal.loaded([#{spec.name.inspect}])`" lines << "require #{compat.inspect}" end FileUtils.mkdir_p(out.dirname) File.write(out, lines.join("\n") << "\n") out end |