Skip to content

Instantly share code, notes, and snippets.

@natesalisbury
Forked from arthurnn/assets.rake
Last active July 12, 2016 21: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 natesalisbury/4c7ee5c02c0afdacf0fa524a3d9f7a48 to your computer and use it in GitHub Desktop.
Save natesalisbury/4c7ee5c02c0afdacf0fa524a3d9f7a48 to your computer and use it in GitHub Desktop.
require 'non_digest_assets'
namespace :assets do
desc "Copy certain assets to a non-digest version"
task :non_digested do
NonDigestAssets.new.generate
end
# Run non_digested task after precompile
Rake::Task['assets:precompile'].enhance do
Rake::Task['assets:non_digested'].invoke
end
end
class NonDigestAssets
include Rake::DSL
FILES = ["external/app.js"]
def generate
latest_digested_files.map do |file|
file = "public/assets/#{file}"
non_digested = file.gsub /(\-[a-z0-9]{32})/, ""
next unless FILES.any? {|s| non_digested =~ /#{s}/}
copy(file, non_digested)
end
end
def latest_digested_files
manifest_path = Dir.glob(File.join(Rails.root, 'public/assets/manifest-*.json')).first
manifest_data = JSON.load(File.new(manifest_path))
manifest_data['assets'].map(&:second) # digested file names
end
def copy(from, to)
sh "cp #{from} #{to}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment