Module: Kettle::Jem::Tasks::PrepareTask
- Defined in:
- lib/kettle/jem/tasks/prepare_task.rb
Constant Summary collapse
- PREPARE_ONLY_PATHS =
[ Kettle::Jem::KETTLE_CONFIG_PATH, "Gemfile", "*.gemspec", # Gemfile evaluates these fragments immediately. Preparing only the # templating pair leaves a newly migrated project with references to # files that do not exist yet, so Bundler cannot perform the bootstrap. "gemfiles/modular/**", "mise.toml" ].freeze
- CRITICAL_TEMPLATING_GEMS =
%w[nomono].freeze
- LOCKED_TEMPLATING_GEMS =
%w[tree_sitter_language_pack].freeze
Class Method Summary collapse
- .bundle_install_after_bootstrap_step(project_root:, setup_env:, quiet:, command_runner:, events:, bootstrap_command:) ⇒ Object
- .bundle_update_templating_bootstrap_command(project_root = Dir.pwd) ⇒ Object
- .changed_files_since(project_root, snapshot) ⇒ Object
- .declared_managed_dependency_names(project_root) ⇒ Object
- .local_path_development_env?(env) ⇒ Boolean
- .locked_gem_names(project_root) ⇒ Object
- .locked_templating_gems(project_root) ⇒ Object
- .managed_bootstrap_gems(project_root) ⇒ Object
- .merge_supplemental_prepare_report(report, supplemental_report) ⇒ Object
- .normalize_existing_local_gemfile_bootstraps_step(project_root, events:) ⇒ Object
- .reconcile_template_managed_dependencies_step(project_root, events:) ⇒ Object
- .reset_release_lockfiles_step(project_root:, setup_env:, quiet:, command_runner:, events:) ⇒ Object
- .run(project_root: Dir.pwd, env: ENV, run_options: {}, command_runner: Kettle::Jem::Tasks::InstallTask.method(:run_system_command)) ⇒ Object
- .snapshot_files(paths) ⇒ Object
- .synchronize_monorepo_subgem_profile_step(project_root, report) ⇒ Object
- .templating_bootstrap_command(project_root = Dir.pwd) ⇒ Object
- .templating_bootstrap_lockfile_ready?(project_root) ⇒ Boolean
- .templating_bootstrap_step_name(project_root = Dir.pwd) ⇒ Object
Class Method Details
.bundle_install_after_bootstrap_step(project_root:, setup_env:, quiet:, command_runner:, events:, bootstrap_command:) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 268 def bundle_install_after_bootstrap_step(project_root:, setup_env:, quiet:, command_runner:, events:, bootstrap_command:) if bootstrap_command == %w[bundle install] return { name: "bundle_install", command: %w[bundle install], status: "skipped", reason: "already_ran_as_templating_bootstrap" } end Kettle::Jem.emit_step_event( events, "command_step", {name: "bundle_install", status: "started", command: %w[bundle install]}, phase: "prepare" ) Kettle::Jem::Tasks::InstallTask.run_command_step( "bundle_install", %w[bundle install], project_root: project_root, env: setup_env, quiet: quiet, command_runner: command_runner ) end |
.bundle_update_templating_bootstrap_command(project_root = Dir.pwd) ⇒ Object
220 221 222 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 220 def bundle_update_templating_bootstrap_command(project_root = Dir.pwd) %w[bundle update] + managed_bootstrap_gems(project_root) + locked_templating_gems(project_root) end |
.changed_files_since(project_root, snapshot) ⇒ Object
311 312 313 314 315 316 317 318 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 311 def changed_files_since(project_root, snapshot) snapshot.filter_map do |path, before| after = File.file?(path) ? File.binread(path) : nil next if before == after Pathname.new(path).relative_path_from(Pathname.new(project_root)).to_s end.sort end |
.declared_managed_dependency_names(project_root) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 254 def declared_managed_dependency_names(project_root) paths = [ File.join(project_root.to_s, "Gemfile"), *Dir.glob(File.join(project_root.to_s, "*.gemfile")), *Dir.glob(File.join(project_root.to_s, "*.gemspec")), *Dir.glob(File.join(project_root.to_s, "gemfiles/modular/**/*.gemfile")) ].select { |path| File.file?(path) } paths.flat_map do |path| Kettle::Jem.ruby_call_records(File.read(path), nil).filter_map do |call| Kettle::Jem.ruby_string_argument(call) if Kettle::Jem.template_managed_dependency_call?(call) end end.uniq end |
.local_path_development_env?(env) ⇒ Boolean
211 212 213 214 215 216 217 218 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 211 def local_path_development_env?(env) (env || {}).any? do |key, raw_value| next false unless key.to_s.end_with?("_DEV", "_LOCAL") value = raw_value.to_s.strip !value.empty? && !Kettle::Jem::DecisionPolicy.falsey?(value) end end |
.locked_gem_names(project_root) ⇒ Object
298 299 300 301 302 303 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 298 def locked_gem_names(project_root) lock_path = File.join(project_root.to_s, "Gemfile.lock") return [] unless File.file?(lock_path) Bundler::LockfileParser.new(Bundler.read_file(lock_path)).specs.map(&:name) end |
.locked_templating_gems(project_root) ⇒ Object
294 295 296 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 294 def locked_templating_gems(project_root) LOCKED_TEMPLATING_GEMS & locked_gem_names(project_root) end |
.managed_bootstrap_gems(project_root) ⇒ Object
247 248 249 250 251 252 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 247 def managed_bootstrap_gems(project_root) declared = declared_managed_dependency_names(project_root) (CRITICAL_TEMPLATING_GEMS + Kettle::Jem.template_bootstrap_dependency_names.select do |name| declared.include?(name) end).uniq end |
.merge_supplemental_prepare_report(report, supplemental_report) ⇒ Object
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 112 def merge_supplemental_prepare_report(report, supplemental_report) return report unless supplemental_report report.merge( changed_files: ( report.fetch(:changed_files, []) + supplemental_report.fetch(:changed_files, []) ).uniq.sort, diagnostics: report.fetch(:diagnostics, []) + supplemental_report.fetch(:diagnostics, []) ) end |
.normalize_existing_local_gemfile_bootstraps_step(project_root, events:) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 123 def normalize_existing_local_gemfile_bootstraps_step(project_root, events:) paths = Dir.glob(File.join(project_root.to_s, "gemfiles/modular/**/*_local.gemfile")).sort changed_files = paths.filter_map do |path| before = File.read(path) after = Kettle::Jem.normalize_local_gemfile_nomono_bootstrap(before) next if after == before File.write(path, after) Pathname.new(path).relative_path_from(Pathname.new(project_root.to_s)).to_s end step = { name: "normalize_local_gemfile_bootstraps", status: changed_files.empty? ? "already_current" : "applied", changed_files: changed_files } Kettle::Jem.emit_step_event(events, "command_step", step, phase: "prepare") step end |
.reconcile_template_managed_dependencies_step(project_root, events:) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 142 def reconcile_template_managed_dependencies_step(project_root, events:) paths = [ File.join(project_root.to_s, "Gemfile"), *Dir.glob(File.join(project_root.to_s, "*.gemfile")), *Dir.glob(File.join(project_root.to_s, "*.gemspec")), *Dir.glob(File.join(project_root.to_s, "gemfiles/modular/**/*.gemfile")) ].select { |path| File.file?(path) }.sort changed_files = paths.filter_map do |path| before = File.read(path) after = Kettle::Jem.reconcile_template_managed_dependencies(before) next if after == before File.write(path, after) Pathname.new(path).relative_path_from(Pathname.new(project_root.to_s)).to_s end step = { name: "reconcile_template_managed_dependencies", status: changed_files.empty? ? "already_current" : "applied", changed_files: changed_files } Kettle::Jem.emit_step_event(events, "command_step", step, phase: "prepare") step end |
.reset_release_lockfiles_step(project_root:, setup_env:, quiet:, command_runner:, events:) ⇒ Object
166 167 168 169 170 171 172 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 208 209 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 166 def reset_release_lockfiles_step(project_root:, setup_env:, quiet:, command_runner:, events:) if local_path_development_env?(setup_env) return { name: "reset_release_lockfiles", command: %w[kettle-reset release-lockfiles], status: "skipped", reason: "local_path_development_env" } end reset_command_runner = lambda do |command| command_runner.call(Shellwords.split(command), chdir: project_root, env: setup_env, quiet: quiet) end resetter = Kettle::Dev::LockfileReset.new( root: project_root, command_runner: reset_command_runner ) paths = resetter.lockfile_paths if paths.empty? return { name: "reset_release_lockfiles", command: %w[kettle-reset release-lockfiles], status: "skipped", reason: "no_release_lockfiles" } end Kettle::Jem.emit_step_event( events, "command_step", {name: "reset_release_lockfiles", status: "started", command: %w[kettle-reset release-lockfiles]}, phase: "prepare" ) before = snapshot_files(paths) resetter.reset(Kettle::Dev::LockfileReset::RELEASE_LOCKFILES_TARGET) changed_files = changed_files_since(project_root, before) { name: "reset_release_lockfiles", command: %w[kettle-reset release-lockfiles], status: "succeeded", changed_files: changed_files, quiet: quiet } end |
.run(project_root: Dir.pwd, env: ENV, run_options: {}, command_runner: Kettle::Jem::Tasks::InstallTask.method(:run_system_command)) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 27 def run(project_root: Dir.pwd, env: ENV, run_options: {}, command_runner: Kettle::Jem::Tasks::InstallTask.method(:run_system_command)) = Kettle::Jem::Tasks::TemplateTask.(env || {}).merge( || {}) = .merge( only: PREPARE_ONLY_PATHS, skip_lock_normalization: true ) events = Kettle::Jem.() report = Kettle::Jem.apply_project(project_root, env: env, run_options: ) profile_sync_step = synchronize_monorepo_subgem_profile_step(project_root, report) supplemental_report = if profile_sync_step && profile_sync_step.fetch(:status) == "applied" Kettle::Jem.apply_project(project_root, env: env, run_options: ) end report = merge_supplemental_prepare_report(report, supplemental_report) transition_step = reconcile_template_managed_dependencies_step(project_root, events: events) nomono_bootstrap_step = normalize_existing_local_gemfile_bootstraps_step(project_root, events: events) setup_env = Kettle::Jem::Tasks::InstallTask.setup_command_env(project_root, env) setup_env["BUNDLE_DISABLE_CHECKSUM_VALIDATION"] = "true" setup_env["K_JEM_TEMPLATING"] = "true" if local_path_development_env?(env) reset_step = reset_release_lockfiles_step( project_root: project_root, setup_env: setup_env, quiet: Kettle::Jem::DecisionPolicy.value_to_boolean([:quiet]), command_runner: command_runner, events: events ) Kettle::Jem.emit_step_event(events, "command_step", reset_step, phase: "prepare") bootstrap_name = templating_bootstrap_step_name(project_root) bootstrap_command = templating_bootstrap_command(project_root) Kettle::Jem.emit_step_event( events, "command_step", {name: bootstrap_name, status: "started", command: bootstrap_command}, phase: "prepare" ) bootstrap_step = Kettle::Jem::Tasks::InstallTask.run_command_step( bootstrap_name, bootstrap_command, project_root: project_root, env: setup_env, quiet: Kettle::Jem::DecisionPolicy.value_to_boolean([:quiet]), command_runner: command_runner ) Kettle::Jem.emit_step_event(events, "command_step", bootstrap_step, phase: "prepare") bundle_step = bundle_install_after_bootstrap_step( project_root: project_root, setup_env: setup_env, quiet: Kettle::Jem::DecisionPolicy.value_to_boolean([:quiet]), command_runner: command_runner, events: events, bootstrap_command: bootstrap_command ) Kettle::Jem.emit_step_event(events, "command_step", bundle_step, phase: "prepare") final_report = report.merge( mode: "prepare", prepared: bootstrap_step.fetch(:status) == "succeeded" && %w[skipped succeeded].include?(reset_step.fetch(:status)) && %w[skipped succeeded].include?(bundle_step.fetch(:status)), prepare_only: PREPARE_ONLY_PATHS, prepare_steps: [profile_sync_step, transition_step, nomono_bootstrap_step, reset_step, bootstrap_step, bundle_step].compact, changed_files: ( report.fetch(:changed_files, []) + transition_step.fetch(:changed_files, []) + nomono_bootstrap_step.fetch(:changed_files, []) + reset_step.fetch(:changed_files, []) + bootstrap_step.fetch(:changed_files, []) + bundle_step.fetch(:changed_files, []) ).uniq.sort, diagnostics: report.fetch(:diagnostics, []) + [{ severity: "advisory", message: "kettle:jem:prepare applied the templating dependency bootstrap payload, " \ "updated critical templating gems, and ran bundle install." }] ) Kettle::Jem.emit_summary_event(events, final_report) final_report end |
.snapshot_files(paths) ⇒ Object
305 306 307 308 309 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 305 def snapshot_files(paths) paths.each_with_object({}) do |path, snapshot| snapshot[path] = File.file?(path) ? File.binread(path) : nil end end |
.synchronize_monorepo_subgem_profile_step(project_root, report) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 105 def synchronize_monorepo_subgem_profile_step(project_root, report) facts = report.fetch(:facts, {}) return unless Kettle::Jem.send(:monorepo_subgem_template_profile?, facts) Kettle::Jem.send(:monorepo_subgem_kettle_config_profile_sync_step, project_root, report) end |
.templating_bootstrap_command(project_root = Dir.pwd) ⇒ Object
224 225 226 227 228 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 224 def templating_bootstrap_command(project_root = Dir.pwd) return %w[bundle install] unless templating_bootstrap_lockfile_ready?(project_root) bundle_update_templating_bootstrap_command(project_root) end |
.templating_bootstrap_lockfile_ready?(project_root) ⇒ Boolean
236 237 238 239 240 241 242 243 244 245 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 236 def templating_bootstrap_lockfile_ready?(project_root) lock_path = File.join(project_root.to_s, "Gemfile.lock") return false unless File.file?(lock_path) locked_gem_names(project_root).then do |names| managed_bootstrap_gems(project_root).all? { |gem_name| names.include?(gem_name) } end rescue Bundler::LockfileError false end |
.templating_bootstrap_step_name(project_root = Dir.pwd) ⇒ Object
230 231 232 233 234 |
# File 'lib/kettle/jem/tasks/prepare_task.rb', line 230 def templating_bootstrap_step_name(project_root = Dir.pwd) return "bundle_install_templating_bootstrap" unless templating_bootstrap_lockfile_ready?(project_root) "bundle_update_templating_bootstrap" end |