Class: Rake::Minify

Inherits:
TaskLib
  • Object
show all
Defined in:
lib/rake/minify/group.rb,
lib/rake/minify.rb

Defined Under Namespace

Classes: Group

Instance Method Summary collapse

Constructor Details

#initialize(name = :minify, &block) ⇒ Minify

Returns a new instance of Minify.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rake/minify.rb', line 10

def initialize(name=:minify, &block)
  @sources = {}
  @dir = nil
  @config = block
  @dir_stack = []

  desc "Minifies the specified javascripts" unless Rake.application.last_comment
  task name do
    invoke
  end
end

Instance Method Details

#add(output, *args, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rake/minify.rb', line 22

def add(output, *args, &block)
  source = opts = nil

  if block.nil?
    source = build_path(args.shift)
  else
    source = block
  end

  opts = args.shift
  opts ||= { :minify => true }

  add_source(output, Source.new(source, opts, &block))
end

#build_path(file) ⇒ Object



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

def build_path(file)
  @dir.nil? && file || File.join(@dir, file)
end

#dir(dir, &block) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/rake/minify.rb', line 41

def dir(dir, &block)
  @dir_stack << dir
  @dir = File.join(@dir_stack)
  block.call #FIXME @dir should be a stack
  @dir_stack.pop
  @dir = File.join(@dir_stack)
end

#group(output, &block) ⇒ Object



37
38
39
# File 'lib/rake/minify.rb', line 37

def group(output, &block)
  add_source(output, Group.new(self, &block))
end

#invokeObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rake/minify.rb', line 49

def invoke
  instance_eval(&@config) # to be configured like the pros

  @sources.each do |dest, source|
    FileUtils.mkdir_p(File.dirname(dest))

    Kernel.open(dest, "w") do |output|
      output << source.build
    end
  end
end