Class: AssetPocket::Generator
- Inherits:
-
Object
- Object
- AssetPocket::Generator
- Defined in:
- lib/asset_pocket/generator.rb
Instance Attribute Summary collapse
-
#root_path ⇒ Object
Returns the value of attribute root_path.
Instance Method Summary collapse
-
#initialize ⇒ Generator
constructor
A new instance of Generator.
- #parse_string(content) ⇒ Object
- #process_copy_files(definition) ⇒ Object
- #process_create_file(definition) ⇒ Object
- #read_file(filename) ⇒ Object
- #run! ⇒ Object
Constructor Details
#initialize ⇒ Generator
Returns a new instance of Generator.
11 12 13 14 |
# File 'lib/asset_pocket/generator.rb', line 11 def initialize self.root_path = "." @pocket = nil end |
Instance Attribute Details
#root_path ⇒ Object
Returns the value of attribute root_path.
9 10 11 |
# File 'lib/asset_pocket/generator.rb', line 9 def root_path @root_path end |
Instance Method Details
#parse_string(content) ⇒ Object
20 21 22 23 |
# File 'lib/asset_pocket/generator.rb', line 20 def parse_string(content) @pocket = Pocket.new(self) @pocket.parse_string content end |
#process_copy_files(definition) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/asset_pocket/generator.rb', line 68 def process_copy_files(definition) base = Pathname.new(File.(definition.base, root_path)) dest_dir = root_path.join(definition.filename) dest_dir.mkpath definition.content.each do |content| case content[0] when :pattern Dir[root_path.join(content[1])].each do |source_file| source_file = Pathname.new(source_file) # Only work with regular file next unless source_file.file? dest_file = dest_dir.join(source_file.relative_path_from(base)) if dest_file.exist? # Compare it using timestamps and size if source_file.size == dest_file.size and source_file.mtime == dest_file.mtime next end # Remove it, since we have to recreate dest_file.delete end # Try to create a hard link (Unix on same mount points). # If it fails, copy it dest_file.dirname.mkpath begin dest_file.make_link source_file rescue Exception => e dest_file.open("w") {|f| f.write source_file.read } end end end end end |
#process_create_file(definition) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/asset_pocket/generator.rb', line 36 def process_create_file(definition) generated_filename = root_path.join(definition.filename) FileUtils.mkpath File.dirname(generated_filename) File.open(generated_filename, "w") do |generated_file| generated_content = [] definition.content.each do |content| case content[0] when :pattern generated_content.concat( Dir[root_path.join(content[1])]. sort!. map! {|found_file| SourceFilter.filter found_file }) when :string generated_content << content[1] else raise ArgumentError, "Unknown content type: #{content[0]}" end end generated_content = generated_content.join(definition.separator) if definition.use_compressor? generated_content = definition.compressor.compress(generated_content) end generated_content = definition.post_process(generated_content) generated_file.write(generated_content) end end |
#read_file(filename) ⇒ Object
25 26 27 |
# File 'lib/asset_pocket/generator.rb', line 25 def read_file(filename) root_path.join(filename).read end |
#run! ⇒ Object
29 30 31 32 33 34 |
# File 'lib/asset_pocket/generator.rb', line 29 def run! @pocket.definitions.each do |definition| send "process_#{definition.process}", definition end true end |