-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
69 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,75 @@ | ||
Rake::Task["assets:precompile"].enhance do | ||
Rake::Task["assets:symlink_non_digested_assets"].invoke if ReactOnRails.configuration.symlink_non_digested_assets_regex | ||
module ReactOnRails | ||
class << self | ||
def assets_path | ||
dir = File.join(Rails.configuration.paths['public'].first, | ||
Rails.configuration.assets.prefix) | ||
Pathname.new(dir) | ||
end | ||
|
||
def symlink_file(target, symlink) | ||
if not File.exist?(symlink) or File.lstat(symlink).symlink? | ||
if File.exist?(target) | ||
puts "React On Rails: Symlinking #{target} to #{symlink}" | ||
FileUtils.ln_s target, symlink, force: true | ||
end | ||
else | ||
puts "React On Rails: File #{symlink} already exists. Failed to symlink #{target}" | ||
end | ||
end | ||
end | ||
end | ||
|
||
if ReactOnRails.configuration.symlink_non_digested_assets_regex | ||
Rake::Task["assets:precompile"].enhance do | ||
Rake::Task["react_on_rails:assets:symlink_non_digested_assets"].invoke | ||
Rake::Task["react_on_rails:assets:delete_broken_symlinks"].invoke | ||
end | ||
end | ||
|
||
namespace :assets do | ||
task symlink_non_digested_assets: :"assets:environment" do | ||
manifest_path = Dir.glob(File.join(Rails.root, 'public/assets/.sprockets-manifest-*.json')) | ||
.first | ||
manifest_data = JSON.load(File.new(manifest_path)) | ||
|
||
manifest_data["assets"].each do |logical_path, digested_path| | ||
regex = ReactOnRails.configuration.symlink_non_digested_assets_regex | ||
if logical_path =~ regex | ||
full_digested_path = File.join(Rails.root, 'public/assets', digested_path) | ||
full_nondigested_path = File.join(Rails.root, 'public/assets', logical_path) | ||
puts "Symlinking #{full_digested_path} to #{full_nondigested_path}" | ||
FileUtils.ln_s full_digested_path, full_nondigested_path, force: true | ||
namespace :react_on_rails do | ||
namespace :assets do | ||
|
||
desc "Creates non-digested symlinks for the assets in the public asset dir" | ||
task symlink_non_digested_assets: :"assets:environment" do | ||
manifest_path = Dir.glob(ReactOnRails::assets_path.join(".sprockets-manifest-*.json")) | ||
.first | ||
manifest_data = JSON.load(File.new(manifest_path)) | ||
|
||
manifest_data["assets"].each do |logical_path, digested_path| | ||
regex = ReactOnRails.configuration.symlink_non_digested_assets_regex | ||
if logical_path =~ regex | ||
full_digested_path = ReactOnRails::assets_path.join(digested_path) | ||
full_nondigested_path = ReactOnRails::assets_path.join(logical_path) | ||
extension = full_digested_path.extname | ||
full_digested_gz_path = full_digested_path.sub_ext("#{extension}.gz") | ||
full_nondigested_gz_path = full_nondigested_path.sub_ext("#{extension}.gz") | ||
ReactOnRails::symlink_file(full_digested_path, full_nondigested_path) | ||
ReactOnRails::symlink_file(full_digested_gz_path, full_nondigested_gz_path) | ||
end | ||
end | ||
end | ||
|
||
desc "Cleans all broken symlinks for the assets in the public asset dir" | ||
task delete_broken_symlinks: :"assets:environment" do | ||
Dir.glob(ReactOnRails::assets_path.join("*")).each do |filename| | ||
if File.lstat(filename).symlink? | ||
begin | ||
target = File.readlink(filename) | ||
rescue | ||
puts "React on Rails: Warning: your platform doesn't support File::readlink method."/ | ||
"Skipping broken link check." | ||
return | ||
end | ||
path = Pathname.new(File.dirname(filename)) | ||
target_path = path.join(target) | ||
unless File.exist?(target_path) | ||
puts "React on Rails: Deleting broken link: #{filename}" | ||
File.delete(filename) | ||
end | ||
end | ||
end | ||
end | ||
|
||
end | ||
end | ||
|