Module: CloudflareWorkers::BuildSupport

Defined in:
lib/cloudflare_workers/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

Class Method Details

.ensure_standalone_runtime(project_root, current_file: __FILE__, loaded_specs: Gem.loaded_specs) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cloudflare_workers/build_support.rb', line 46

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



31
32
33
# File 'lib/cloudflare_workers/build_support.rb', line 31

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
# File 'lib/cloudflare_workers/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



35
36
37
38
39
40
# File 'lib/cloudflare_workers/build_support.rb', line 35

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/cloudflare_workers/build_support.rb', line 13

def loaded_spec(name, loaded_specs: Gem.loaded_specs)
  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.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/cloudflare_workers/build_support.rb', line 143

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)
    meta = spec.
    next unless meta.is_a?(Hash)
    flag = meta['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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/cloudflare_workers/build_support.rb', line 173

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).expand_path(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



42
43
44
# File 'lib/cloudflare_workers/build_support.rb', line 42

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



24
25
26
27
28
29
# File 'lib/cloudflare_workers/build_support.rb', line 24

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).expand_path.join('../..')
end

.standalone_load_paths(project_root, with_db:, loaded_specs: Gem.loaded_specs) ⇒ Object



63
64
65
66
67
68
69
70
71
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cloudflare_workers/build_support.rb', line 63

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 += ['.', '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),
    gem_lib(SINATRA_GEM_NAME, loaded_specs: loaded_specs),
    gem_vendor(SINATRA_GEM_NAME, loaded_specs: loaded_specs)
  ].compact.each do |path|
    load_paths << path
  end

  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 do |path|
      load_paths << path
    end
  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



116
117
118
119
120
121
122
123
# File 'lib/cloudflare_workers/build_support.rb', line 116

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



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cloudflare_workers/build_support.rb', line 125

def vendor_from_gemfile(project_root)
  gf = Pathname(project_root).join('Gemfile')
  return unless gf.file?

  txt = gf.read
  return unless (m = txt.match(/#{Regexp.escape(RUNTIME_GEM_NAME)}['"]\s*,\s*path:\s*['"]([^'"]+)['"]/))

  runtime_path = Pathname.new(m[1]).expand_path(project_root)
  vend = runtime_path.join('..', '..', 'vendor').expand_path
  vend if vend.directory?
end