Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pgruener/4f3e383599a68386fb03dd730a2d1a3a to your computer and use it in GitHub Desktop.
Save pgruener/4f3e383599a68386fb03dd730a2d1a3a to your computer and use it in GitHub Desktop.
namespace :hulk do
desc 'Updates the host applications\' packs (new: entry_points) published from engines'
task :publish_engine_packs do
detected_missing_entry_points = 0
puts "\n"
Dir[Rails.root.join('engines/*/app/javascript/host_entrypoints/*.js')].each do |engine_entry_point|
relative_entrypoint_path = engine_entry_point.sub("#{Rails.root}/", '')
matcher = relative_entrypoint_path.match(/^engines\/([^\/]+).*host_entrypoints\/([^\/]+\.js)$/)
engine_name, entry_point = matcher[1], matcher[2]
host_dest_path = Rails.root.join('app/javascript/packs', engine_name, entry_point)
next if File.exist?(host_dest_path) || File.exist?("#{host_dest_path}.skip")
detected_missing_entry_points += 1
File.open(host_dest_path, 'w') do |f|
f << "// publish engine's pack: #{entry_point}\n"
f << "// (rename it to #{entry_point}.skip to prevent this pack to be published\n\n"
f << "require('#{relative_entrypoint_path.sub(/^engines\//, '')}')\n"
end
puts "Created new host entry_point for engine: #{engine_name} entry_point: #{entry_point}"
end
if detected_missing_entry_points.zero?
puts 'No missing entry_points detected in loaded engines.'
else
puts "\nDetected (and added) #{detected_missing_entry_points} missing entry_points in engines."
end
puts "\n\nchecking for obsolete ones..."
Rake::Task['hulk:remove_unpublished_engine_packs'].invoke
end
desc 'Removes host applications\' packs from engine entry_points, which aren\'t available anymore'
task :remove_unpublished_engine_packs do
if (real_run = ENV['REAL_RUN'] == '1')
puts 'Real run'
end
obsolete_detections = false
Dir[Rails.root.join('app/javascript/packs/*/*')].each do |host_entry_point|
published_entry_point_file = host_entry_point
host_entry_point = host_entry_point.sub(/\.skip$/, '') if host_entry_point.match(/\.skip$/)
matcher = host_entry_point.match(/\/packs\/([^\/]+)\/([^\/]+\.js)$/)
engine_name, entry_point = matcher[1], matcher[2]
next if File.exist?(Rails.root.join("engines/#{engine_name}/app/javascript/host_entrypoints/#{entry_point}"))
obsolete_detections = true
puts "Detected obsolete entry_point publication in engine: #{engine_name}, host_entrypoint: #{entry_point}"
if real_run
FileUtils.rm_rf(published_entry_point_file)
puts "\tdeleted: #{published_entry_point_file}"
end
end
if obsolete_detections
unless real_run
puts "\nThe obsolete host entry_points were detected in a dry run."
puts 'To delete them call:'
puts "$ rails hulk:remove_unpublished_engine_packs REAL_RUN=1\n\n"
end
else
puts 'No obsolete publications found for active engines.'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment