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"- 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
- .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_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
Class Method Details
.ensure_standalone_runtime(project_root, current_file: __FILE__, loaded_specs: Gem.loaded_specs) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/homura/runtime/build_support.rb', line 72 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 |
.gem_lib(name, loaded_specs: Gem.loaded_specs) ⇒ Object
33 34 35 |
# File 'lib/homura/runtime/build_support.rb', line 33 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
17 18 19 20 21 22 23 24 |
# File 'lib/homura/runtime/build_support.rb', line 17 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
37 38 39 40 41 42 |
# File 'lib/homura/runtime/build_support.rb', line 37 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
13 14 15 |
# File 'lib/homura/runtime/build_support.rb', line 13 def loaded_spec(name, loaded_specs: Gem.loaded_specs) loaded_specs[name] end |
.maybe_gem_lib(name, loaded_specs: Gem.loaded_specs) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/homura/runtime/build_support.rb', line 44 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
51 52 53 54 55 56 57 58 59 |
# File 'lib/homura/runtime/build_support.rb', line 51 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_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.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/homura/runtime/build_support.rb', line 188 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)) 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
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/homura/runtime/build_support.rb', line 225 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
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/homura/runtime/build_support.rb', line 61 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
26 27 28 29 30 31 |
# File 'lib/homura/runtime/build_support.rb', line 26 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
100 101 102 103 104 105 106 107 108 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 |
# File 'lib/homura/runtime/build_support.rb', line 100 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
154 155 156 157 158 159 160 161 |
# File 'lib/homura/runtime/build_support.rb', line 154 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
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/homura/runtime/build_support.rb', line 163 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 |