Fixed some spelling errors in Specs and Docs for Array.item. -- thanks fabiomcosta
[mootools.git] / build.rb
blob8937f2a8a9a72493aa6eaca1bf3f99287d05af33
1 #!/usr/bin/env ruby
3 # USAGE:
4
5 # Full:
6 # ./build.rb
7
8 # Fx.Tween, DomReady including all dependencies:
9 # ./build.rb Fx.Tween DomReady
11 require 'rubygems'
12 require 'json'
14 module MooTools
15   class Build
16     
17     attr_reader :included
18     attr_accessor :build_path
19     attr_accessor :dependency_path
20     
21     def initialize(path = File.dirname(__FILE__))
22       @path = path
23       @scripts = []
24       @included = []
25       @data = {}
26       
27       @build_path      ||= @path + '/mootools.js'
28       @dependency_path ||= @path + '/Source/scripts.json'
29       
30       json = JSON.load(File.read( dependency_path ))
31       json.each_pair do |folder, group|
32         group.each_pair do |script, properties|
33           @data[script] = {:folder => folder, :deps => properties["deps"]}
34         end
35       end
36     end
37     
38     def full_build
39       @data.each_key { |name| load_script name }
40       @string
41     end
42     
43     def load_script(name)
44       return if @included.index(name) || name == 'None';
45       unless @data.key? name
46         puts "Script '#{name}' not found!"
47         throw :script_not_found
48       end
49       @included.push name
50       @data[name][:deps].each { |dep| load_script dep }
51       @string ||= ""
52       @string << File.read(@path + "/Source/#{@data[name][:folder]}/#{name}.js") << "\n"
53     end
54     
55     def build
56       @string ||= full_build
57       @string.sub!('%build%', build_number)
58     end
59     alias :to_s :build
60     
61     def build_number
62       ref =  File.read(File.dirname(__FILE__) + '/.git/HEAD').chomp.match(/ref: (.*)/)[1]
63       return File.read(File.dirname(__FILE__) + "/.git/#{ref}").chomp
64     end
65     
66     def save(filename)
67       File.open(filename, 'w') { |fh| fh.write to_s }
68     end
69     
70     def save!
71       save build_path
72     end
73     
74     def self.build!(argv)
75       mootools = MooTools::Build.new
76       
77       catch :script_not_found do
78         if argv.length > 0
79           argv.each { |script| mootools.load_script(script) }
80         else
81           mootools.full_build
82         end
83       end
84       
85       puts "MooTools Built '#{mootools.build_path}'"
86       print "  Included Scripts:","\n    "
87       puts mootools.included.join(" ")
88       mootools.save!
89       
90     end
91     
92   end
93 end
94 if __FILE__ == $0
95   
96   MooTools::Build.build! ARGV
97   
98 end