Class: UnifiedAssets::Tasks

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/unified_assets/tasks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Tasks

Returns a new instance of Tasks.

Yields:

  • (_self)

Yield Parameters:



11
12
13
14
15
16
17
# File 'lib/unified_assets/tasks.rb', line 11

def initialize(options = {})
  @assets = options[:assets]
  @minify = options[:minify]
  @server = options[:server] || {}
  yield(self) if block_given?
  define
end

Instance Attribute Details

#assetsObject

Returns the value of attribute assets.



7
8
9
# File 'lib/unified_assets/tasks.rb', line 7

def assets
  @assets
end

#minifyObject

Returns the value of attribute minify.



8
9
10
# File 'lib/unified_assets/tasks.rb', line 8

def minify
  @minify
end

#serverObject

Returns the value of attribute server.



9
10
11
# File 'lib/unified_assets/tasks.rb', line 9

def server
  @server
end

Instance Method Details

#defineObject



19
20
21
22
23
24
25
# File 'lib/unified_assets/tasks.rb', line 19

def define
  namespace :assets do
    define_clear_task
    define_create_task
    define_server_task
  end
end

#define_clear_taskObject



27
28
29
30
31
32
33
34
# File 'lib/unified_assets/tasks.rb', line 27

def define_clear_task
  desc "clear unified asset files"
  task :clear do
    assets.keys.each do |asset|
      Dir[asset, Minifier.minified_name(asset)].each {|f| File.delete(f) if File.exists?(f) }
    end
  end
end

#define_create_taskObject



36
37
38
39
40
41
42
43
44
# File 'lib/unified_assets/tasks.rb', line 36

def define_create_task
  desc "create unified asset files"
  task :create do
    Rake::Task["assets:clear"].invoke
    assets.keys.each do |asset|
      UnifiedAssets.build(assets[asset], asset, :minify => minify)
    end
  end
end

#define_server_taskObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/unified_assets/tasks.rb', line 46

def define_server_task
  desc "simple webserver that auto-regenerates assets if they are out of date"
  task :server do |t, args|
    require 'rack'
    require 'unified_assets/server'
    options = {
      :root   => ENV['ROOT']  || server[:root]  || '.',
      :port   => ENV['PORT']  || server[:port]  || 3000,
      :debug  => ENV['DEBUG'] || server[:debug] || true,
      :assets => assets
    }
    app = Rack::Builder.new do
      use Rack::CommonLogger
      use Rack::ShowExceptions
      use Rack::Lint
      use UnifiedAssets::Server, options
      run Rack::File.new(options[:root])
    end.to_app
    handler   = Rack::Handler.get(ENV['HANDLER'] || server[:handler])
    handler ||= begin; Rack::Handler::Thin;    rescue LoadError; end
    handler ||= begin; Rack::Handler::Mongrel; rescue LoadError; end
    handler ||= Rack::Handler::WEBrick
    handler.run(app, :Port => options[:port])
  end
end