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

.path_gemfile_entries(project_root) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/cloudflare_workers/build_support.rb', line 147

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
# 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 `path:`-resolved gems declared in the consumer's
  # Gemfile (e.g. `sinatra-inertia`). This keeps the build pipeline
  # extensible: users can drop a pure-Ruby gem under `gems/foo`, list
  # it as `gem 'foo', path: '../../gems/foo'`, and Opal will find its
  # `lib/` automatically — no homura-runtime change required.
  #
  # We prefer the auto-await-transformed copy under
  # `build/auto_await/gem_<basename>/lib` if present (homura-build
  # writes one for every path: gem before invoking Opal), so that
  # async chains inside the gem get `__await__` inserted just like
  # consumer app code.
  path_gemfile_entries(root).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



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

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



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

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