Class: Klenod::Build::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/klenod/build/context.rb

Defined Under Namespace

Classes: DefaultPlugins

Constant Summary collapse

DEFAULT_PLUGINS =
DefaultPlugins.new.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_dir:, plugins: DEFAULT_PLUGINS, mode: :development, asset_generation_concurrency: AssetGenerationQueue::DEFAULT_CONCURRENCY, asset_download_concurrency: AssetGenerationQueue::DEFAULT_DOWNLOAD_CONCURRENCY, profiler: nil) ⇒ Context

Returns a new instance of Context.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/klenod/build/context.rb', line 107

def initialize(
  source_dir:,
  plugins: DEFAULT_PLUGINS,
  mode: :development,
  asset_generation_concurrency: AssetGenerationQueue::DEFAULT_CONCURRENCY,
  asset_download_concurrency: AssetGenerationQueue::DEFAULT_DOWNLOAD_CONCURRENCY,
  profiler: nil
)
  @source_dir = source_dir
  plugins = plugins.to_a if plugins.equal?(DEFAULT_PLUGINS)
  @plugins = plugins
  @mode = mode
  @update_handlers = []
  @graph =
    Graph.new(
      source_dir: source_dir,
      plugins: plugins,
      mode: mode,
      asset_generation_concurrency: asset_generation_concurrency,
      asset_download_concurrency: asset_download_concurrency,
      profiler: profiler
    )
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



131
132
133
# File 'lib/klenod/build/context.rb', line 131

def graph
  @graph
end

#modeObject (readonly)

Returns the value of attribute mode.



131
132
133
# File 'lib/klenod/build/context.rb', line 131

def mode
  @mode
end

Class Method Details

.default_pluginsObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/klenod/build/context.rb', line 91

def self.default_plugins
  [
    Plugins::RubyPlugin.new,
    Plugins::IntlPlugin.new,
    Plugins::HamlPlugin.new,
    Plugins::MarkdownPlugin.new,
    Plugins::GemImportPlugin.new,
    Plugins::SvgPlugin.new,
    Plugins::ImagePlugin.new,
    Plugins::JsonPlugin.new,
    Plugins::YamlPlugin.new,
    Plugins::TomlPlugin.new,
    Plugins::TextPlugin.new
  ]
end

Instance Method Details

#apply_update(event, entry:, assets_dir: nil) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/klenod/build/context.rb', line 254

def apply_update(event, entry:, assets_dir: nil)
  return AppliedUpdate.new(event, nil, nil, nil, event.result.errors.freeze) if event.result.errors.any?

  entry = loaded(entry)
  entry.record
  asset_write_result = assets_dir && write_asset_updates(event.asset_updates, assets_dir: assets_dir)

  AppliedUpdate.new(
    event,
    entry,
    evaluated?(entry) ? entry.exports : nil,
    asset_write_result,
    [].freeze
  )
end

#asset(output_path) ⇒ Object



176
177
178
# File 'lib/klenod/build/context.rb', line 176

def asset(output_path)
  @graph.asset(output_path)
end

#asset_bytes(output_path, assets_dir: nil) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/klenod/build/context.rb', line 197

def asset_bytes(output_path, assets_dir: nil)
  asset = asset(output_path)
  return asset.bytes unless assets_dir

  path = asset_disk_path(asset.output_path, Pathname.new(assets_dir))
  write_asset(asset, Pathname.new(assets_dir)) unless File.file?(path)
  File.binread(path)
end

#asset_generation_queueObject



226
227
228
# File 'lib/klenod/build/context.rb', line 226

def asset_generation_queue
  @graph.asset_generation_queue
end

#asset_references_for_module(record_or_module_id, type: nil, content_type: nil, recursive: true) ⇒ Object



214
215
216
# File 'lib/klenod/build/context.rb', line 214

def asset_references_for_module(record_or_module_id, type: nil, content_type: nil, recursive: true)
  @graph.asset_references_for_module(module_refs_for_assets(record_or_module_id), type: type, content_type: content_type, recursive: recursive)
end

#assetsObject



172
173
174
# File 'lib/klenod/build/context.rb', line 172

def assets
  @graph.assets
end

#assets_for(logical_name) ⇒ Object



206
207
208
# File 'lib/klenod/build/context.rb', line 206

def assets_for(logical_name)
  @graph.assets_for(logical_name)
end

#assets_for_module(record_or_module_id, type: nil, content_type: nil, recursive: true) ⇒ Object



210
211
212
# File 'lib/klenod/build/context.rb', line 210

def assets_for_module(record_or_module_id, type: nil, content_type: nil, recursive: true)
  @graph.assets_for_module(module_refs_for_assets(record_or_module_id), type: type, content_type: content_type, recursive: recursive)
end

#build(entrypoints:, output:, assets_dir: nil) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/klenod/build/context.rb', line 151

def build(entrypoints:, output:, assets_dir: nil)
  bundle = @graph.bundle(entrypoints: entrypoints)
  assets_dir ? write_assets(assets_dir) : wait_for_assets
  FileUtils.mkdir_p(File.dirname(output))
  File.binwrite(output, Runtime::BundleFormat.dump(bundle))
  bundle
end

#build_executable(entrypoints:, output:, assets_dir: nil) ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/klenod/build/context.rb', line 159

def build_executable(entrypoints:, output:, assets_dir: nil)
  bundle = @graph.bundle(entrypoints: entrypoints)
  assets_dir ? write_assets(assets_dir) : wait_for_assets
  FileUtils.mkdir_p(File.dirname(output))
  File.binwrite(output, executable_bundle_source + Runtime::BundleFormat.dump(bundle))
  FileUtils.chmod("+x", output)
  bundle
end

#collect(specifier) ⇒ Object



137
138
139
# File 'lib/klenod/build/context.rb', line 137

def collect(specifier)
  loaded(@graph.collect(specifier))
end

#each_asset(&block) ⇒ Object



218
219
220
# File 'lib/klenod/build/context.rb', line 218

def each_asset(&block)
  @graph.each_asset(&block)
end

#emit_update(event) ⇒ Object



274
275
276
# File 'lib/klenod/build/context.rb', line 274

def emit_update(event)
  @update_handlers.each { |handler| handler.call(event) }
end

#entry(specifier) ⇒ Object



141
142
143
# File 'lib/klenod/build/context.rb', line 141

def entry(specifier)
  collect(specifier)
end

#evaluate(specifier) ⇒ Object



133
134
135
# File 'lib/klenod/build/context.rb', line 133

def evaluate(specifier)
  @graph.load(specifier)
end

#evaluated?(record_or_module_id) ⇒ Boolean

Returns:

  • (Boolean)


186
187
188
189
# File 'lib/klenod/build/context.rb', line 186

def evaluated?(record_or_module_id)
  record_or_module_id = record_or_module_id.id if record_or_module_id.is_a?(LoadedModule)
  @graph.evaluated?(record_or_module_id)
end

#exports(record_or_module_id) ⇒ Object



180
181
182
183
184
# File 'lib/klenod/build/context.rb', line 180

def exports(record_or_module_id)
  return record_or_module_id.exports if record_or_module_id.is_a?(LoadedModule)

  @graph.exports(record_or_module_id)
end

#invalidate_paths(changed_paths, removed_paths: []) ⇒ Object



168
169
170
# File 'lib/klenod/build/context.rb', line 168

def invalidate_paths(changed_paths, removed_paths: [])
  @graph.invalidate_paths(changed_paths, removed_paths: removed_paths)
end

#loaded(record_or_module_id) ⇒ Object



145
146
147
148
149
# File 'lib/klenod/build/context.rb', line 145

def loaded(record_or_module_id)
  return record_or_module_id if record_or_module_id.is_a?(LoadedModule)

  LoadedModule.new(self, record_or_module_id.respond_to?(:id) ? record_or_module_id.id : record_or_module_id)
end

#module_id_for(record_or_module_id) ⇒ Object



191
192
193
194
195
# File 'lib/klenod/build/context.rb', line 191

def module_id_for(record_or_module_id)
  return record_or_module_id.id if record_or_module_id.is_a?(LoadedModule)

  @graph.module_id_for(record_or_module_id)
end

#on_update(&block) ⇒ Object



270
271
272
# File 'lib/klenod/build/context.rb', line 270

def on_update(&block)
  @update_handlers << block
end

#wait_for_assetsObject



222
223
224
# File 'lib/klenod/build/context.rb', line 222

def wait_for_assets
  @graph.wait_for_assets
end

#write_asset_updates(asset_updates, assets_dir:, &block) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/klenod/build/context.rb', line 240

def write_asset_updates(asset_updates, assets_dir:, &block)
  assets_root = Pathname.new(assets_dir)
  removed_paths =
    asset_updates.filter_map do |update|
      remove_asset(update.output_path, assets_root) if update.removed?
    end
  written_paths =
    asset_updates.filter_map do |update|
      write_asset(update.current_asset, assets_root, &block) if update.current_asset
    end

  asset_write_result(written_paths, removed_paths: removed_paths)
end

#write_assets(assets_dir, &block) ⇒ Object



230
231
232
233
234
235
236
237
238
# File 'lib/klenod/build/context.rb', line 230

def write_assets(assets_dir, &block)
  assets_root = Pathname.new(assets_dir)
  results =
    with_async_task do |task|
      assets.each_value.map { |asset| task.async { write_asset(asset, assets_root, &block) } }.map(&:wait)
    end

  asset_write_result(results, removed_paths: [])
end