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'

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

.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
# 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

  load_paths << 'vendor' if root.join('vendor').directory?
  load_paths << 'build'
  load_paths.uniq
end

.standalone_namespace(project_root, suffix) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/cloudflare_workers/build_support.rb', line 94

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



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cloudflare_workers/build_support.rb', line 103

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