public
Description: Rake tasks for merge and compress your stylesheets and javascripts
Homepage: http://lucaguidi.com/pages/assets_packager
Clone URL: git://github.com/jodosha/assets_packager.git
assets_packager / assets_packager_tasks.rake
100644 115 lines (96 sloc) 3.337 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
67
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
106
107
108
109
110
111
112
113
114
115
RAILS_ROOT = File.expand_path(Dir.pwd) unless defined? RAILS_ROOT
CONFIGURATION = "#{RAILS_ROOT}/config/assets.yml"
JS_PATH = RAILS_ROOT + '/public/javascripts'
CSS_PATH = RAILS_ROOT + '/public/stylesheets'
JAVASCRIPTS = JS_PATH + '/*.js'
STYLESHEETS = CSS_PATH + '/*.css'
JSMIN = "#{RAILS_ROOT}/script/jsmin"
 
namespace :assets do
  desc "Package all the assets"
  task :package => 'package:all'
  
  desc "Clear all the assets"
  task :clear => 'clear:all'
  
  desc "Write the configuration file"
  task :config do
    require 'yaml'
    javascripts = file_list JAVASCRIPTS, true
    stylesheets = file_list STYLESHEETS, true
    configuration = {'js' => javascripts.reverse, 'css' => stylesheets}
    File.open(CONFIGURATION, "w+") { |file| file.write(configuration.to_yaml) }
  end
 
  namespace :package do
    desc "Package all the assets"
    task :all => [ 'clear:all', :js, :css ]
    
    desc "Package all the javascripts"
    task :js => [ 'merge:js', 'compress:js' ]
    
    desc "Package all the stylesheets"
    task :css => [ 'merge:css', 'compress:css' ]
  end
 
  namespace :merge do
    desc "Merge all the assets"
    task :all => [ :js, :css ]
    
    desc "Merge all the javascripts"
    task :js do
      merge JS_PATH, :js
    end
    
    desc "Merge all the stylesheets"
    task :css do
      merge CSS_PATH, :css
    end
  end
  
  namespace :compress do
    desc "Compress all the assets"
    task :all => [ :js, :css ]
    
    desc "Compress all the javascripts"
    task :js do
      path = JS_PATH + '/all.js'
      `#{JSMIN} <#{path} >#{JS_PATH}/all_compressed.js \n`
      `mv #{JS_PATH}/all_compressed.js #{path}`
      # Remove the blank line on the top of the file
      lines = File.readlines(path)[1..-1]
      File.open(path, 'w+') { |file| file.write lines }
    end
    
    desc "Compress all the stylesheets"
    task :css do
      path = "#{CSS_PATH}/all.css"
      lines = compress_css(File.readlines(path).join)
      File.open(path, 'w+') { |file| file.write lines }
    end
  end
  
  namespace :clear do
    desc "Clear all the assets"
    task :all => [ :js, :css ]
    
    desc "Package all the javascripts"
    task :js do
      FileUtils.rm(JS_PATH + '/all.js') if File.exist?(JS_PATH + '/all.js')
    end
    
    desc "Package all the stylesheets"
    task :css do
      FileUtils.rm(CSS_PATH + '/all.css') if File.exist?(CSS_PATH + '/all.css')
    end
  end
end
 
def file_list(path, trail_extension = false)
  Dir[path].map do |file|
    file = file.split('/').last
    file.split('.').first if trail_extension
  end
end
 
def merge(path, type)
  files = YAML.load_file(CONFIGURATION).to_hash[type.to_s].map {|file| "#{path}/#{file}.#{type}"}
  lines = files.inject([]) do |result, file|
    result << File.readlines(file)
    result
  end
  File.open("#{path}/all.#{type}", "w+") { |file| file.write lines }
end
 
# Thanks to Scott Becker
def compress_css(source)
  source.gsub!(/\s+/, " ") # collapse space
  source.gsub!(/\/\*(.*?)\*\/ /, "") # remove comments - caution, might want to remove this if using css hacks
  source.gsub!(/\} /, "}\n") # add line breaks
  source.gsub!(/\n$/, "") # remove last break
  source.gsub!(/ \{ /, " {") # trim inside brackets
  source.gsub!(/; \}/, "}") # trim inside brackets
  source
end