Skip to content

Instantly share code, notes, and snippets.

@Samsinite
Created October 13, 2015 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Samsinite/632e99134b43e96d7162 to your computer and use it in GitHub Desktop.
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
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
@skylar
Copy link

skylar commented Oct 13, 2015

@Samsinite can you post the broccoli generated manifest as well as the merged manifest? (rails 4)

@Samsinite
Copy link
Author

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.

@Samsinite
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment