Created
October 13, 2015 22:37
-
-
Save Samsinite/632e99134b43e96d7162 to your computer and use it in GitHub Desktop.
Rake pre_deploy task for pre-compiling rails and ember assets, then updating the rails manifest file to point to ember-cli fingerprinted assets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fileutils' | |
namespace :wildland do | |
namespace :pre_deploy do | |
namespace :precompile_assets do | |
task :rails do | |
system('RAILS_ENV=production bundle exec rake assets:precompile') | |
end | |
task :ember do | |
Dir.chdir('ember') do | |
sh 'DISABLE_FINGERPRINTING=false ./node_modules/.bin/ember build --environment=production' | |
end | |
end | |
task :combine do | |
FileUtils.rm_r('public/assets/admin_panel', force: true) | |
FileUtils.cp_r('ember/dist/assets', 'public/assets/admin_panel') | |
rails_manifest = Manifest.new(rails_assets_manifest_path) | |
ember_manifest = Manifest.new(ember_assets_manifest_path) | |
combine_manifests(rails_manifest, ember_manifest) | |
FileUtils.rm(ember_assets_manifest_path) | |
File.open(rails_assets_manifest_path, "w") do |f| | |
f.write(rails_manifest.manifest.to_json) | |
end | |
end | |
task all: [:rails, :ember, :combine] | |
end | |
task precompile_assets: ['precompile_assets:all'] | |
end | |
task pre_deploy: ['pre_deploy:precompile_assets'] do | |
Rake::Task['wildland:pre_deploy:precompile_assets'].invoke | |
end | |
end | |
class Manifest | |
attr_reader :manifest | |
def initialize(path) | |
@manifest = JSON.parse(File.read(path)) | |
end | |
def assets | |
manifest['assets'] | |
end | |
def files | |
manifest['files'] | |
end | |
end | |
def ember_assets_manifest_path | |
Dir["public/assets/admin_panel/manifest.json"].first || Dir["public/assets/admin_panel/manifest*.json"].first | |
end | |
def rails_assets_manifest_path | |
Dir["public/assets/.sprockets-manifest.json"].first || Dir["public/assets/.sprockets-manifest*.json"].first | |
end | |
def combine_manifests(rails_manifest, ember_manifest) | |
ember_manifest.assets.each_pair do |key, value| | |
prefix_path = "admin_panel\/" | |
non_fingerprinted_filepath = "#{prefix_path}#{key}" | |
fingerprinted_filepath = "#{prefix_path}#{value}" | |
if rails_manifest.assets.has_key?(non_fingerprinted_filepath) | |
files_fingerprinted_key = rails_manifest.assets[non_fingerprinted_filepath] | |
rails_manifest.files.delete(files_fingerprinted_key) | |
end | |
rails_manifest.assets[non_fingerprinted_filepath] = fingerprinted_filepath | |
rails_manifest.files[fingerprinted_filepath] = ember_manifest.files[value].clone | |
rails_manifest.files[fingerprinted_filepath]['logical_path'] = non_fingerprinted_filepath | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have the merged one (which looks just like the original rails one just with the fingerprints from the ember manifest merged in) and the ember one, does that work? If not, I can generate some new sets.