Skip to content

Commit

Permalink
v0.0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
zfben committed Aug 31, 2011
1 parent 89494a4 commit a978940
Show file tree
Hide file tree
Showing 25 changed files with 151 additions and 105 deletions.
111 changes: 27 additions & 84 deletions lib/zfben_libjs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Compile
end
end

['lib.rb', 'source.rb', 'initialize.rb', 'railtie.rb'].each { |f| require File.join(File.dirname(__FILE__), 'zfben_libjs', f) }
['lib.rb', 'source.rb', 'initialize.rb', 'collection.rb', 'railtie.rb'].each { |f| require File.join(File.dirname(__FILE__), 'zfben_libjs', f) }

def err msg
STDERR.print "#{msg}\n".color(:red)
Expand Down Expand Up @@ -53,94 +53,36 @@ def build!
if @libs.has_key?(url) && name != url
lib.push(url)
else
p path = File.join(@opts[:config]['src/source'], name, File.basename(url))
dir = File.dirname(path)
system('mkdir ' + dir) unless File.exists?(dir)
download url, path
File.open(path, 'w'){ |f| f.write(Zfben_libjs.get_source(path).compile) }
=begin
case get_filetype(path)
when 'css'
css = "/* @import #{url} */\n" << css_import(url, dir)
File.open(path, 'w'){ |f| f.write(css) }
images = download_images(name, url, path)
if images.length > 0
lib.push images
end
when 'js'
js = "/* @import #{url} */\n" << File.read(path)
File.open(path, 'w'){ |f| f.write(js) }
when 'rb'
script = eval(File.read(path))
css = ''
js = ''
script.each do | type, content |
case type
when :css
css << content
when :js
js << content
end
end
if css != ''
path = File.join(dir, File.basename(path, '.rb') << '.css')
File.open(path, 'w'){ |f| f.write("/* @import #{url} */\n" + css) }
elsif js != ''
path = File.join(dir, File.basename(path, '.rb') << '.js')
File.open(path, 'w'){ |f| f.write("/* @import #{url} */\n" + js) }
end
when 'sass'
options = { :syntax => :sass, :cache => false }.merge(Compass.sass_engine_options)
options[:load_paths].push File.dirname(path), File.dirname(url)
css = "/* @import #{url} */\n" + Sass::Engine.new(File.read(path), options).render
path = File.join(dir, File.basename(path) << '.css')
File.open(path, 'w'){ |f| f.write(css) }
when 'scss'
options = { :syntax => :scss, :cache => false }.merge(Compass.sass_engine_options)
options[:load_paths].push File.dirname(path), File.dirname(url)
css = "/* @import #{url} */\n" + Sass::Engine.new(File.read(path), options).render
path = File.join(dir, File.basename(path) << '.css')
File.open(path, 'w'){ |f| f.write(css) }
when 'coffee'
js = "/* @import #{url} */\n" + CoffeeScript.compile(File.read(path))
path = File.join(dir, File.basename(path) << '.js')
File.open(path, 'w'){ |f| f.write(js) }
else
lib.push url
end
=end
lib.push(path)
source = Zfben_libjs.get_source(url, @opts[:config])
lib.push(source)
end
end
lib = lib.flatten.uniq

css = ''
js = ''
lib = lib.map{ |file|
if File.exists?(file)
content = "/* @import #{file} */\n" + File.read(file)
case File.extname(file)
when '.css'
css << content
file = nil
when '.js'
js << content << ';'
file = nil
end
end
file
}.compact
if css != ''
file = File.join(@opts[:config]['src/source'], name + '.css')
File.open(file, 'w'){ |f| f.write(css) }
lib.push(file)
collection = Zfben_libjs::Collection.new(lib, @libs)
collection.merge!

lib = []

if collection.css.length > 0
path = File.join(@opts[:config]['src/stylesheets'], name + '.css')
File.open(path, 'w'){ |f| f.write(collection.merge_css) }
lib.push path
end
if js != ''
file = File.join(@opts[:config]['src/source'], name + '.js')
File.open(file, 'w'){ |f| f.write(js) }
lib.push(file)

if collection.js.length > 0
path = File.join(@opts[:config]['src/javascripts'], name + '.js')
File.open(path, 'w'){ |f| f.write(collection.merge_js) }
lib.push path
end

if collection.image.length > 0
lib.push collection.image
end

@libs[name] = lib.flatten.compact.uniq
end
=begin
@libs[name] = lib.map{ |file|
if File.exists?(file)
case File.extname(file)
Expand Down Expand Up @@ -197,7 +139,7 @@ def build!
}.compact.flatten.uniq
@libs[name] = @libs[name][0] if @libs[name].length == 1
end
=end
tip '== [2/2] Generate lib.js =='

libjs = File.read(@libs['lazyload']) << ';'
Expand All @@ -209,7 +151,8 @@ def build!
libjs << libjs_core << ';'

@urls = {}


p @libs
@libs.each do |lib, path|
path = [path] unless path.class == Array
path = path.map{ |url|
Expand Down
48 changes: 48 additions & 0 deletions lib/zfben_libjs/collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Zfben_libjs::Collection
attr_accessor :css, :js, :image, :merge_css, :merge_js

def initialize sources, libs
sources = [sources] unless sources.class.to_s != 'Array'

@css = []
@js = []
@image = []

process_sources sources
end

def merge!
merge_css = ''
@css.each do |css|
merge_css << css.to_css
end
@merge_css = merge_css

merge_js = ''
@js.each do |js|
merge_js << js.to_js
end
@merge_js = merge_js

return {
:merge_css => @merge_css,
:merge_js => @merge_js
}
end

private

def process_sources sources, libs
sources.each do |source|
if source.class.to_s == 'String'
process_sources libs[source]
elsif source.respond_to(:to_css)
@css.push source
elsif source.respond_to(:to_js)
@js.push source
else
@image.push source
end
end
end
end
17 changes: 9 additions & 8 deletions lib/zfben_libjs/initialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class Zfben_libjs::Libjs
'src/stylesheets' => 'src/stylesheets',

'url' => '',
'url/javascripts' => 'url/javascripts',
'url/images' => 'url/images',
'url/stylesheets' => 'url/stylesheets',
'url/javascripts' => '/javascripts',
'url/images' => '/images',
'url/stylesheets' => '/stylesheets',

'download' => false,
'minify' => true,
Expand Down Expand Up @@ -43,7 +43,7 @@ def initialize *opts
opts = {}
end

@opts = merge_and_convert_options opts
@opts = merge_and_convert_options(opts)

@path_gem = File.realpath(File.dirname(__FILE__))

Expand Down Expand Up @@ -91,11 +91,12 @@ def merge_and_convert_options opts

[:config, :libs, :bundle, :routes, :preload, :support_source].each do |name|
if opts.has_key?(name)
case opts[name].class
when Hash
case opts[name].class.to_s
when 'Hash'
p opts[name]
options[name] = options[name].merge(opts[name])
when Array
options[name] = options[name] + opts[name]
when 'Array'
options[name] = (options[name] + opts[name]).uniq
end
end
end
Expand Down
25 changes: 22 additions & 3 deletions lib/zfben_libjs/source.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Zfben_libjs

def self.get_source filepath, *options
source = File.read(filepath)
type = File.extname(filepath).delete('.')

class_name = type.capitalize
Expand All @@ -11,7 +10,7 @@ def self.get_source filepath, *options
raise Exception.new(class_name + ' isn\'t exists!')
end

return source_class.new :filepath => filepath, :source => source, :options => options[0]
return source_class.new :filepath => filepath, :options => options[0]
end

class Source
Expand All @@ -24,11 +23,31 @@ def initialize *opts

@options = {} if @options.nil?

if remote?
download!
end

@source = @source || File.read(@filepath)

after_initialize if self.respond_to?(:after_initialize)
end

def remote?
return /^https?:\/\// =~ @filepath
end

def download!
@remote_path = @filepath
@filepath = File.join(@options['src/source'], '.download', File.basename(@remote_path))
FileUtils.mkdir(File.dirname(@filepath)) unless File.exists?(File.dirname(@filepath))
unless system 'wget ' + @remote_path + ' -O ' + @filepath
FileUtils.rm @filepath
raise Exception.new(@remote_path + ' download failed!')
end
end

def name
self.class.to_s.downcase
self.class.to_s.downcase.gsub(/Zfben_libjs::/, '')
end

def compile
Expand Down
4 changes: 4 additions & 0 deletions lib/zfben_libjs/support_source/coffee.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Zfben_libjs::Coffee < Zfben_libjs::Source
def to_js
compile
end

def before_compile
@compiled = CoffeeScript.compile(@source)
end
Expand Down
6 changes: 5 additions & 1 deletion lib/zfben_libjs/support_source/css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ def after_initialize
@options = @options.merge({ :syntax => :sass, :style => :compressed, :cache => false })
end

def to_css
@source
end

def to_sass
Sass::CSS.new(@source, @options).render(:sass)
end
Expand All @@ -12,7 +16,7 @@ def to_scss
end

def download_images
@source.
@source
end

def before_minify
Expand Down
4 changes: 4 additions & 0 deletions lib/zfben_libjs/support_source/js.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class Zfben_libjs::Js < Zfben_libjs::Source

def to_js
compile
end

def before_minify
Uglifier.compile(@source, :copyright => false)
end
Expand Down
1 change: 1 addition & 0 deletions lib/zfben_libjs/support_source/rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Zfben_libjs::Rb < Zfben_libjs::Source
def self.new *options
filepath = options[0][:filepath]
if !filepath.nil?
p filepath
script = eval(File.read(filepath))
class_name = script.keys[0].capitalize
source = script.values[0]
Expand Down
4 changes: 4 additions & 0 deletions lib/zfben_libjs/support_source/sass.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ def after_initialize
@options[:load_paths].push File.dirname(@filepath)
end

def to_css
compile
end

def before_compile
@compiled = Sass::Engine.new(@source, @options).render
end
Expand Down
4 changes: 4 additions & 0 deletions lib/zfben_libjs/support_source/scss.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ def after_initialize
@options[:load_paths].push File.dirname(@filepath)
end

def to_css
compile
end

def before_compile
@compiled = Sass::Engine.new(@source, @options).render
end
Expand Down
9 changes: 5 additions & 4 deletions test/ruby/compile_test.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
class TestSource < Test::Unit::TestCase
def test
['js', 'css', 'sass', 'scss', 'coffee'].each do |type|
p type
source = Zfben_libjs.get_source('test/support_filetype/' + type + '.' + type)
assert_equal source.class.to_s.downcase, 'zfben_libjs::' + type
p source.compile
p source.minify
source.compile
source.minify

ruby = Zfben_libjs.get_source('test/support_filetype/rb_' + type + '.rb')
assert_equal ruby.class.to_s.downcase, 'zfben_libjs::' + type
p ruby.compile
p ruby.minify
ruby.compile
ruby.minify
end
end
end
1 change: 1 addition & 0 deletions test/support_filetype (copy)/coffee.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this.coffee = true
1 change: 1 addition & 0 deletions test/support_filetype (copy)/css.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.css{ color: rgb(0,0,1) !important }
1 change: 1 addition & 0 deletions test/support_filetype (copy)/js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
js = true;
1 change: 1 addition & 0 deletions test/support_filetype (copy)/rb_coffee.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ :coffee => 'this.rb_coffee = true' }
1 change: 1 addition & 0 deletions test/support_filetype (copy)/rb_css.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ :css => '.rb_css{ color: rgb(0,0,4) !important; }' }
1 change: 1 addition & 0 deletions test/support_filetype (copy)/rb_js.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ :js => 'rb_js = true;' }
1 change: 1 addition & 0 deletions test/support_filetype (copy)/rb_sass.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ :sass => ".rb_sass\n color: rgb(0,0,4) !important" }
1 change: 1 addition & 0 deletions test/support_filetype (copy)/rb_scss.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ :scss => '.rb_scss{ color: rgb(0,0,4) !important }' }
2 changes: 2 additions & 0 deletions test/support_filetype (copy)/sass.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.sass
color: rgb(0,0,4) !important
3 changes: 3 additions & 0 deletions test/support_filetype (copy)/scss.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.scss {
color: rgb(0,0,4) !important
}
2 changes: 1 addition & 1 deletion test/support_filetype/rb_coffee.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{:coffee => 'this.rb_coffee = true'}
{ :coffee => 'this.rb_coffee = true' }
Loading

0 comments on commit a978940

Please sign in to comment.