Skip to content

Instantly share code, notes, and snippets.

@astashov
Created October 5, 2009 04:26
Show Gist options
  • Save astashov/201871 to your computer and use it in GitHub Desktop.
Save astashov/201871 to your computer and use it in GitHub Desktop.
# == css_js_generator
#
# It generates one big file from all CSS and from all JS small files. It
# scans public/stylesheets and public/javascripts, gets all files from there
# and creates application.css and application.js). Order can be specified
# in css_js_generator.rb. There are also 'notifier' script, it uses
# inotifywatch program for tracking changes in public directory and running
# css_js_generator.rb after every change in public directory. So, big files
# will be generated automatically and you don't need to run it
# css_js_generator.rb after every change of files in public dir.
require 'rubygems'
#require 'packr'
class CssAccumulator
def initialize
@input_dir = File.expand_path(File.dirname(__FILE__) + '/public/stylesheets')
end
def get_filelist
global_files = [
@input_dir + '/framework/global/global.reset.css',
@input_dir + '/framework/global/global.css',
@input_dir + '/framework/global/global.reset.ie.css',
@input_dir + '/framework/global/global.ie.css'
]
dirs = [ '/framework/layout', '/framework/mixin', '/framework/block' ]
dirs_with_unexpanded_symlinks = global_files + dirs.inject([]) do |files, dir|
files_of_dir = Dir[@input_dir + dir + '/**/*']
files_of_dir.each do |file|
if File.exists?(file)
if File.symlink?(file)
real_dir = File.readlink(file)
files += Dir[File.expand_path(File.dirname(file) + '/' + real_dir) + '/**/*']
else
files << file
end
end
end
files
end
end
def accumulated_text
usual_text = ""
ie_text = ""
get_filelist.each do |files|
files.each do |file|
if File.exists?(file) && File.file?(file)
text = File.read(file)
if file =~ /\.ie\.css/
ie_text += text
else
usual_text += text
end
end
end
end
[ usual_text, ie_text ]
end
def accumulate!
usual_text, ie_text = accumulated_text
File.open(@input_dir + '/application.css', 'w') { |file| file.write(usual_text) }
File.open(@input_dir + '/application.ie.css', 'w') { |file| file.write(ie_text) }
end
end
class JsAccumulator
def initialize
@input_dir = File.expand_path(File.dirname(__FILE__) + '/public/javascripts')
end
def get_filelist
dirs = [ '/framework/global', '/framework/layout', '/framework/mixin', '/framework/block' ]
dirs.inject([]) do |files, dir|
files_of_dir = Dir[@input_dir + dir + '/**/*']
files_of_dir.each do |file|
if File.exists?(file)
if File.symlink?(file)
real_dir = File.readlink(file)
files += Dir[File.expand_path(File.dirname(file) + '/' + real_dir) + '/**/*']
else
files << file
end
end
end
files
end
end
def accumulated_text
text = ""
get_filelist.each do |files|
files.each do |file|
text += File.read(file) if File.exists?(file) && File.file?(file)
end
end
text
end
def accumulate!
text = accumulated_text
File.open(@input_dir + '/application.js', 'w') { |file| file.write(text) }
end
end
CssAccumulator.new.accumulate!
JsAccumulator.new.accumulate!
#js_code = File.read(File.expand_path(File.dirname(__FILE__)) + '/public/javascripts/jquery.js')
#js_code += File.read(File.expand_path(File.dirname(__FILE__)) + '/public/javascripts/jquery.form.js')
#js_code += File.read(File.expand_path(File.dirname(__FILE__)) + '/public/javascripts/app.js')
#compressed = Packr.pack(js_code, :shrink_vars => true)
#File.open(File.expand_path(File.dirname(__FILE__)) + '/public/javascripts/app.min.js', 'wb') { |f| f.write(compressed) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment