Permalink
Browse files

It bundles and minifies

  • Loading branch information...
0 parents commit 9000bdfdcdefdf063cecb9819e808e80c53a9264 @ismasan committed Nov 3, 2010
@@ -0,0 +1,6 @@
+pkg/*
+*.gem
+.bundle
+spec/dist/*
+.rvmrc
+.DS_Store
@@ -0,0 +1,4 @@
+source :gemcutter
+
+# Specify your gem's dependencies in jbundle.gemspec
+gemspec
@@ -0,0 +1,30 @@
+PATH
+ remote: .
+ specs:
+ jbundle (0.0.1)
+ closure-compiler
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ closure-compiler (0.3.3)
+ diff-lcs (1.1.2)
+ rspec (2.0.1)
+ rspec-core (~> 2.0.1)
+ rspec-expectations (~> 2.0.1)
+ rspec-mocks (~> 2.0.1)
+ rspec-core (2.0.1)
+ rspec-expectations (2.0.1)
+ diff-lcs (>= 1.1.2)
+ rspec-mocks (2.0.1)
+ rspec-core (~> 2.0.1)
+ rspec-expectations (~> 2.0.1)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bundler (>= 1.0.0)
+ closure-compiler
+ jbundle!
+ rspec
@@ -0,0 +1,25 @@
+
+JBundle.config do
+
+end
+
+
+version 2
+dist 'dist'
+
+bundle :some_file do
+ file 'file1'
+ file 'file2
+end
+
+file 'file3'
+
+filter do |src|
+ src.gsub /<VERSION>/, version
+end
+
+dist/2/some_file.js
+dist/2/some_file.min.js
+dist/2/file3.js
+dist/2/file3.min.js
+
@@ -0,0 +1,10 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'spec/rake/spectask'
+Spec::Rake::SpecTask.new(:spec) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.spec_files = FileList['spec/**/*_spec.rb']
+end
+
+task :default => :spec
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path("../lib/jbundle/version", __FILE__)
+
+Gem::Specification.new do |s|
+ s.name = "jbundle"
+ s.version = JBundle::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = []
+ s.email = []
+ s.homepage = "http://rubygems.org/gems/jbundle"
+ s.summary = "TODO: Write a gem summary"
+ s.description = "TODO: Write a gem description"
+
+ s.required_rubygems_version = ">= 1.3.6"
+ s.rubyforge_project = "jbundle"
+
+ s.add_development_dependency "bundler", ">= 1.0.0"
+ s.add_development_dependency "rspec"
+ s.add_dependency 'closure-compiler'
+
+ s.files = `git ls-files`.split("\n")
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
+ s.require_path = 'lib'
+end
@@ -0,0 +1,36 @@
+require 'fileutils'
+require 'jbundle/config'
+require 'jbundle/file'
+require 'jbundle/bundle'
+require 'jbundle/builder'
+require 'jbundle/writer'
+
+module JBundle
+
+ class << self
+
+ def config(&block)
+ @current_config ||= JBundle::Config.new
+ @current_config.instance_eval(&block) if block_given?
+ @current_config
+ end
+
+ def output
+ @output ||= Builder.new(config).build!
+ end
+
+ def reset!
+ @current_config = nil
+ @output = nil
+ self
+ end
+
+ def write_to(target_dir)
+ output.each do |compiler|
+ Writer.new(compiler, config.version, target_dir).write
+ end
+ end
+
+ end
+
+end
@@ -0,0 +1,51 @@
+require 'closure-compiler'
+
+module JBundle
+
+ class Compiler
+
+ attr_reader :src, :name
+
+ def initialize(name, file_list, src_dir = 'src')
+ @name, @file_list, @src_dir = name.to_s, file_list, src_dir
+ end
+
+ def compile!
+ @src = @file_list.inject('') do |mem, file_name|
+ mem << ::File.read(::File.join(@src_dir, file_name))
+ mem << "\n"
+ mem
+ end
+ self
+ end
+
+ def min
+ @min ||= Closure::Compiler.new.compile(src)
+ end
+
+ def min_name
+ @min_name ||= (
+ ext = ::File.extname(name)
+ name.sub(ext, '') + '.min' + ext
+ )
+ end
+
+ end
+
+
+ class Builder
+
+ def initialize(config)
+ @config = config
+ @sources = []
+ end
+
+ def build!
+ @sources = @config.bundles_and_files.map do |b|
+ Compiler.new(b.name, b, @config.src_dir).compile!
+ end
+ end
+
+ end
+
+end
@@ -0,0 +1,24 @@
+module JBundle
+
+ class Bundle
+
+ include Enumerable
+
+ attr_reader :name
+
+ def initialize(name)
+ @name = name.to_s
+ @files = []
+ end
+
+ def file(f)
+ @files << f
+ end
+
+ def each(&block)
+ @files.each &block
+ end
+
+ end
+
+end
@@ -0,0 +1,73 @@
+module JBundle
+
+ class Version
+
+ def initialize(string)
+ @major, @minor, @patch = string.split('.')
+ raise "require (major.minor.patch) eg: 1.3.1" unless @major && @minor && @patch
+ end
+
+ def full
+ [@major, @minor, @patch].join('.')
+ end
+
+ def major_minor
+ [@major, @minor].join('.')
+ end
+
+ def releaseable
+ prerelease? ? [full] : [full, major_minor]
+ end
+
+ def to_s
+ full
+ end
+
+ protected
+
+ def prerelease?
+ @patch =~ /-pre/
+ end
+
+ end
+
+ class Config
+
+ attr_reader :bundles, :files, :src_dir
+
+ def initialize
+ @bundles = []
+ @files = []
+ end
+
+ def version(v = nil)
+ @version = Version.new(v) if v
+ @version
+ end
+
+ def src_dir(dir = nil)
+ @src_dir = dir if dir
+ @src_dir
+ end
+
+ def bundle(name, &block)
+ name = name.to_sym
+ if !b = @bundles.detect{|a| a.name == name}
+ b = Bundle.new(name)
+ @bundles << b
+ end
+ b.instance_eval &block
+ b
+ end
+
+ def file(f)
+ @files << JBundle::File.new(f)
+ end
+
+
+ def bundles_and_files
+ @bundles + @files
+ end
+
+ end
+end
@@ -0,0 +1,19 @@
+module JBundle
+
+ class File
+
+ include Enumerable
+
+ attr_reader :name
+
+ def initialize(name)
+ @name = name.to_s
+ end
+
+ def each(&block)
+ yield name
+ end
+
+ end
+
+end
@@ -0,0 +1,3 @@
+module JBundle
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,27 @@
+module JBundle
+
+ class Writer
+
+ def initialize(compiler, version, target_dir)
+ @compiler, @version, @target_dir = compiler, version, target_dir
+ end
+
+ def write
+ @version.releaseable.each do |version_dir|
+ write_file @compiler.src, ::File.join(@target_dir, version_dir), @compiler.name
+ write_file @compiler.min, ::File.join(@target_dir, version_dir), @compiler.min_name
+ end
+ end
+
+ protected
+
+ def write_file(content, dir_name, file_name)
+ FileUtils.mkdir_p dir_name
+ ::File.open(::File.join(dir_name, file_name), 'w') do |f|
+ f.write content
+ end
+ end
+
+ end
+
+end
@@ -0,0 +1 @@
+--color
Oops, something went wrong.

0 comments on commit 9000bdf

Please sign in to comment.