Class: JsSourcemap::Api
- Inherits:
-
Object
- Object
- JsSourcemap::Api
- Defined in:
- lib/js-sourcemap/api.rb
Constant Summary collapse
- EXTENSIONS =
%w(.js)
Instance Method Summary collapse
- #clean_unused_files ⇒ Object
- #complete_build ⇒ Object
- #compress?(file) ⇒ Boolean
- #config ⇒ Object
- #config_hash ⇒ Object
- #copy_source(smo) ⇒ Object
- #correct_file?(file) ⇒ Boolean
- #create_min_map_gz(smo, min_content, sourcem_content) ⇒ Object
- #env ⇒ Object
- #generate_mapping ⇒ Object
- #get_mapping_dir(file) ⇒ Object
- #get_relative_path(file) ⇒ Object
- #gzname(file) ⇒ Object
- #mapping_creation_required?(file, smo) ⇒ Boolean
- #mapping_file_path(file) ⇒ Object
- #original_file_path(file) ⇒ Object
- #reload! ⇒ Object
- #remove_files(file) ⇒ Object
- #source_map_options(file) ⇒ Object
- #sync_to_s3 ⇒ Object
- #sync_to_s3? ⇒ Boolean
Instance Method Details
#clean_unused_files ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/js-sourcemap/api.rb', line 143 def clean_unused_files files = Dir[File.join(env.mapping_dir,"**","*.map")] files.each_with_index do |file,i| if env.manifest[get_relative_path(file)].nil? remove_files(file) end end end |
#complete_build ⇒ Object
163 164 165 166 167 |
# File 'lib/js-sourcemap/api.rb', line 163 def complete_build generate_mapping clean_unused_files sync_to_s3 end |
#compress?(file) ⇒ Boolean
75 76 77 78 79 |
# File 'lib/js-sourcemap/api.rb', line 75 def compress?(file) if EXTENSIONS.include?(File.extname(file)) !File.exists?(gzname(file)) || File.mtime(gzname(file)) < File.mtime(file) end end |
#config ⇒ Object
13 14 15 |
# File 'lib/js-sourcemap/api.rb', line 13 def config @config ||= Config.new(env) end |
#config_hash ⇒ Object
17 18 19 |
# File 'lib/js-sourcemap/api.rb', line 17 def config_hash config.to_h end |
#copy_source(smo) ⇒ Object
85 86 87 88 |
# File 'lib/js-sourcemap/api.rb', line 85 def copy_source(smo) puts "=> Copying #{smo["minified_file_path"]} to #{smo["original_file_path"]}" FileUtils.cp(smo["minified_file_path"], smo["original_file_path"]) end |
#correct_file?(file) ⇒ Boolean
55 56 57 |
# File 'lib/js-sourcemap/api.rb', line 55 def correct_file?(file) return (File.extname(file) == ".js" and !(file=~/-original\.js/)) end |
#create_min_map_gz(smo, min_content, sourcem_content) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/js-sourcemap/api.rb', line 59 def create_min_map_gz(smo, min_content,sourcem_content) puts "=> writing minified file : #{smo["minified_file_path"]} ..." f = File.open(smo["minified_file_path"], "w") f.write(min_content) f.close puts "=> writing js-sourcemap file : #{smo["source_map_path"]} ..." f = File.open(smo["source_map_path"], "w") f.write(sourcem_content) f.close gz_file = "#{smo["minified_file_path"]}" puts "=> gzip minified file: #{gzname(gz_file)}" %x(gzip -c -9 "#{gz_file}" > "#{gzname(gz_file)}") if compress?(gz_file) end |
#env ⇒ Object
9 10 11 |
# File 'lib/js-sourcemap/api.rb', line 9 def env @env ||= ::JsSourcemap::Env.new end |
#generate_mapping ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/js-sourcemap/api.rb', line 30 def generate_mapping # empty_dirs beginning_time = Time.now if !File.exists?(env.sources_dir) puts ">>> Directory #{env.sources_dir} doesn't exist" return end count = Find.find(env.sources_dir).count Find.find(env.sources_dir).each_with_index do |file, i| puts "#{i+1} of #{count} - #{file} ..........." if File.file?(file) and correct_file?(file) smo = file if mapping_creation_required?(file,smo) copy_source(smo) #:source_url => "SourceUrl in minified", :source_map_url => "SourceMappingUrl in minified", :source_filename => "original_file_name_in_map", :source_root=> "lol4", :minified_file_path => "lol5", :input_source_map => "lol6" uglified, source_map = Uglifier.new(:source_map_url => smo["source_map_file_absolute_path"], :source_filename => smo["original_file_absolute_path"]).compile_with_map(File.read(file)) create_min_map_gz(smo,uglified,source_map) end end end end_time = Time.now puts ".... Completed ......" puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds" end |
#get_mapping_dir(file) ⇒ Object
106 107 108 109 |
# File 'lib/js-sourcemap/api.rb', line 106 def get_mapping_dir(file) dirpath = File.dirname(file) dirpath.gsub(/.*#{env.sources_dir}/,"#{env.mapping_dir}") # new mapping dir end |
#get_relative_path(file) ⇒ Object
159 160 161 |
# File 'lib/js-sourcemap/api.rb', line 159 def get_relative_path(file) file.gsub(/.*(#{env.mapping_dir}\/)/,'').gsub(/\.map/,'') end |
#gzname(file) ⇒ Object
81 82 83 |
# File 'lib/js-sourcemap/api.rb', line 81 def gzname(file) "#{file}.gz" end |
#mapping_creation_required?(file, smo) ⇒ Boolean
121 122 123 |
# File 'lib/js-sourcemap/api.rb', line 121 def mapping_creation_required?(file,smo) return !(File.exists?(smo["original_file_path"]) and File.exists?(smo["source_map_path"])) end |
#mapping_file_path(file) ⇒ Object
116 117 118 119 |
# File 'lib/js-sourcemap/api.rb', line 116 def mapping_file_path(file) dirpath = get_mapping_dir file File.join(dirpath, File.basename(file)) + ".map" end |
#original_file_path(file) ⇒ Object
111 112 113 114 |
# File 'lib/js-sourcemap/api.rb', line 111 def original_file_path(file) dirpath = get_mapping_dir file File.join(dirpath, File.basename(file,'.js')) + "-original.js" end |
#reload! ⇒ Object
21 22 23 24 |
# File 'lib/js-sourcemap/api.rb', line 21 def reload! @env = nil @config = nil end |
#remove_files(file) ⇒ Object
152 153 154 155 156 157 |
# File 'lib/js-sourcemap/api.rb', line 152 def remove_files(file) original_file = file.gsub(/\.js\.map/,'-original.js') puts "removing #{file} && #{original_file}" File.unlink(file) if File.exists?(file) File.unlink(original_file) if File.exists?(original_file) end |
#source_map_options(file) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/js-sourcemap/api.rb', line 90 def (file) a = Hash.new mapping_dirpath = get_mapping_dir file FileUtils.mkpath(mapping_dirpath) unless File.exists?(mapping_dirpath) # creating new mapping dir a["minified_file_path"] = file # something-digest.js a["original_file_path"] = original_file_path file # something-digest-original.js a["source_map_path"] = mapping_file_path file # something-digest.js.map a["source_map_file_absolute_path"] = File.join(env.build_absolute_path mapping_file_path file) # map absolute url a["original_file_absolute_path"] = File.join(env.build_absolute_path original_file_path file) # original file absolute url a end |
#sync_to_s3 ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/js-sourcemap/api.rb', line 125 def sync_to_s3 if sync_to_s3? if asset_prefix = Rails.application.config.assets.prefix puts "starting sync to s3 bucket" puts "s3cmd sync -r --skip-existing #{env.mapping_dir}/ s3://#{env.sourcemap_config.fetch("privateassets_bucket_name")}#{asset_prefix}/ --acl-private --no-check-md5" if system("s3cmd sync -r --skip-existing #{env.mapping_dir}/ s3://#{env.sourcemap_config.fetch("privateassets_bucket_name")}#{asset_prefix}/ --acl-private --no-check-md5") puts "successfully synced assets to s3" else puts "Failed to sync asets to s3" end else puts "assets host not specified in application configuration" end else puts "Syncing to s3 disabled as per sourcemap configuration" end end |
#sync_to_s3? ⇒ Boolean
26 27 28 |
# File 'lib/js-sourcemap/api.rb', line 26 def sync_to_s3? env.sourcemap_config.fetch("sync_to_s3") == "yes" || false end |