Class: Collage::Packager

Inherits:
Object
  • Object
show all
Defined in:
lib/collage.rb

Direct Known Subclasses

Sass

Defined Under Namespace

Classes: Sass

Constant Summary collapse

COMPRESSOR =
File.expand_path("../vendor/yuicompressor-2.4.2.jar", File.dirname(__FILE__))

Instance Method Summary collapse

Constructor Details

#initialize(path, patterns = nil) ⇒ Packager

Returns a new instance of Packager.



39
40
41
42
# File 'lib/collage.rb', line 39

def initialize(path, patterns = nil)
  @path = path
  @patterns = Array(patterns || "**/*.js")
end

Instance Method Details

#each(&block) ⇒ Object



77
78
79
# File 'lib/collage.rb', line 77

def each(&block)
  result.each_line(&block)
end

#filesObject



55
56
57
58
59
# File 'lib/collage.rb', line 55

def files
  @files ||= @patterns.map do |pattern|
    File.exist?(pattern) ? pattern : Dir[File.join(@path, pattern)]
  end.flatten.uniq
end

#ignore(file) ⇒ Object



89
90
91
92
93
# File 'lib/collage.rb', line 89

def ignore(file)
  if files.delete(file)
    @result = nil
  end
end

#inspectObject



85
86
87
# File 'lib/collage.rb', line 85

def inspect
  "#<Collage::Packager (#{size} files)>"
end

#minifyObject



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/collage.rb', line 107

def minify
  minified = IO.popen("java -jar #{COMPRESSOR} --type #{type}", "r+") do |io|
    io.write(result.to_s)
    io.close_write
    io.read
  end

  $?.success? or raise RuntimeError.new("Error minifying #{files.inspect}.")

  minified
end

#mtimeObject



65
66
67
# File 'lib/collage.rb', line 65

def mtime
  @mtime ||= files.map {|f| File.mtime(f) }.max
end

#packageObject



44
45
46
47
48
49
# File 'lib/collage.rb', line 44

def package
  files.inject("") do |contents,file|
    contents += package_file(file) + "\n\n"
    contents
  end
end

#package_file(file) ⇒ Object



51
52
53
# File 'lib/collage.rb', line 51

def package_file(file)
  File.read(file)
end

#resultObject



73
74
75
# File 'lib/collage.rb', line 73

def result
  @result ||= package
end

#sizeObject



69
70
71
# File 'lib/collage.rb', line 69

def size
  result.size
end

#timestampObject



61
62
63
# File 'lib/collage.rb', line 61

def timestamp
  mtime.to_i.to_s
end

#to_sObject



81
82
83
# File 'lib/collage.rb', line 81

def to_s
  result.to_s
end

#typeObject



119
120
121
# File 'lib/collage.rb', line 119

def type
  :js
end

#write(path, minify = false) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/collage.rb', line 95

def write(path, minify = false)
  contents = minify ? self.minify : result

  File.open(path, "w") do |f|
    f.write(contents.to_s)
  end

  FileUtils.touch(path, :mtime => mtime)
end