dependence 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/bin/dependence CHANGED
@@ -1,46 +1,121 @@
1
1
  #!/usr/bin/env ruby
2
+
3
+ $: << "."
4
+ require 'rubygems'
5
+ require 'coffee-script'
6
+ require 'lib/dependence'
2
7
  require 'optparse'
3
8
  require File.join(File.dirname(__FILE__), "..", "lib/dependence.rb")
4
9
 
5
- options = {:output => "compiled.js"}
10
+ class DependenceCompiler
11
+ def initialize(config)
12
+ @config = config
13
+ @src_glob = "**/*.#{@config[:type]}"
14
+ @concatenator = Dependence::Concatenator.new(
15
+ :load_path => @config[:src_dir],
16
+ :source_type => @src_glob
17
+ )
18
+ end
19
+
20
+ def compile
21
+ @config[:watch] ? compile_with_watcher : run_compile
22
+ end
23
+
24
+ private
25
+
26
+ def compile_with_watcher
27
+ FileWatcher.new(:load_path => @config[:src_dir], :glob_str => @src_glob) do
28
+ run_compile
29
+ end
30
+ end
31
+
32
+ def run_compile
33
+ begin
34
+ @concatenator.concat do |module_name, module_content|
35
+ stdout_compile_msg(module_name)
36
+ output = File.join(@config[:output], "#{module_name}.js")
37
+ compiled = compile_code(module_content)
38
+ compiled = ModuleInjector.modularize(module_name, compiled) unless @config[:bare]
39
+ File.open(output, 'w') { |f| f.syswrite compiled }
40
+
41
+ stdout_compile_complete_msg(module_name, output)
42
+ compress_code(output) if @config[:compress]
43
+ end
44
+
45
+ rescue
46
+ puts Colors.red("exiting")
47
+ exit 1
48
+ end
49
+ end
50
+
51
+
52
+ def stdout_compile_msg(module_name)
53
+ puts ""
54
+ puts "================================"
55
+ puts Colors.green("Detected Module: #{module_name}")
56
+ puts "================================"
57
+ end
58
+
59
+ def stdout_compile_complete_msg(module_name, output)
60
+ puts Colors.green("Compiled #{module_name} to #{output}")
61
+ end
62
+
63
+ def compile_code(raw_code)
64
+ return CoffeeScript.compile(raw_code, :bare => true) if @config[:type] == "coffee"
65
+ raw_code
66
+ end
67
+
68
+ def compress_code(source_file)
69
+ JsCompiler.new(source_file).compile
70
+ end
71
+ end
72
+
73
+
74
+ options = {
75
+ :output => ".",
76
+ :type => "js",
77
+ :watch => false,
78
+ :bare => false,
79
+ :compress => false
80
+ }
81
+
6
82
  OptionParser.new do |opts|
7
- opts.banner = "Usage: compile.rb js_source_dir [options]"
83
+ opts.banner = "Usage: src_dir [options]"
8
84
 
9
- opts.on("-o", "--output FILE", "Output .js file") do |file|
10
- options[:output] = file
85
+ opts.on("-o", "--output DIR", "Output directory, defaults to '.'") do |dir|
86
+ options[:output] = dir
11
87
  end
12
88
 
13
- opts.on("-w", "--watch", "Watch src directory for changes and recompile") do |bool|
89
+ opts.on("-t", "--type TYPE", "Source file extension [js, coffee]") do |type|
90
+ options[:type] = type
91
+ end
92
+
93
+ opts.on("-w", "--watch", "Watch src_dir for changes and recompile") do |bool|
14
94
  options[:watch] = bool
15
95
  end
96
+
97
+ opts.on("-b", "--bare", "Do not wrap modules in closures with export var") do |bool|
98
+ options[:bare] = bool
99
+ end
100
+
101
+ opts.on("-c", "--compress", "Compress output with Googles Closure compiler") do |bool|
102
+ options[:compress] = bool
103
+ end
104
+
16
105
  end.parse(ARGV)
17
106
 
18
107
  if ARGV.length < 1
19
- puts "You need to inlcude your js src directory"
108
+ puts "You need a src directory to compile from!"
20
109
  exit 1
21
- elsif !File.directory? ARGV[0]
22
- puts "Can't find your js source directory: #{ARGV[0]}"
110
+ elsif !File.directory? ARGV[0]
111
+ puts "#{ARGV[0]} is not a directory!"
23
112
  exit 1
24
113
  end
25
114
 
26
- options[:input] = ARGV[0]
27
- output_file = options[:output]
28
- compiler = JsCompiler.new(options[:input])
29
-
30
- def recompile(compiler, output_file)
31
- File.delete(output_file) if File.exists?(output_file)
32
- compiler.compile(output_file)
33
- end
34
-
35
- recompile compiler, output_file
36
-
37
- if options[:watch]
38
- puts "Watching #{options[:input]} for changes. Your javascript files will be compiled automatically"
39
- FileWatcher.new "**/*.js" do
40
- recompile compiler, output_file
41
- end
42
- end
115
+ options[:src_dir] = ARGV[0]
43
116
 
44
117
 
118
+ compiler = DependenceCompiler.new options
119
+ compiler.compile
45
120
 
46
121
 
@@ -0,0 +1,55 @@
1
+ require 'fileutils'
2
+ require File.join(File.dirname(__FILE__), 'colors')
3
+
4
+ module Dependence
5
+ # Take a load path + file glob and concat them into 1 file per directory
6
+ class Concatenator
7
+ @@defaults = {
8
+ :load_path => ".",
9
+ :source_type => "**/*.js"
10
+ }
11
+
12
+ def initialize(opts = {})
13
+ @options = @@defaults.merge(opts)
14
+ end
15
+
16
+ def concat(&block)
17
+ get_dirs.each { |dir| concat_module(dir, &block) }
18
+ end
19
+
20
+ private
21
+
22
+ def concat_module(dir, &block)
23
+ module_name = File.basename(dir)
24
+
25
+ files = Dir.glob File.join(dir, @options[:source_type])
26
+ no_files_error if files.empty?
27
+
28
+ files_list = get_files_in_dependency_order(dir, files)
29
+
30
+ block.call module_name, concat_files(files_list)
31
+ end
32
+
33
+ def no_files_error
34
+ puts Colors.red("No files of the specified type #{File.extname(@options[:source_type])} were found in #{@options[:load_path]}")
35
+ throw :no_files_to_concatenate
36
+ end
37
+
38
+ def get_files_in_dependency_order(dir, files)
39
+ resolver = DependencyResolver.new(files, dir)
40
+ resolver.sorted_files
41
+ end
42
+
43
+ def concat_files(file_list)
44
+ content = ""
45
+ file_list.each { |f| content << File.read(f); content << "\n" }
46
+ content
47
+ end
48
+
49
+ # Top level dirs only
50
+ def get_dirs
51
+ Dir.glob("#{@options[:load_path]}/*/**/")
52
+ end
53
+
54
+ end
55
+ end
@@ -1,6 +1,10 @@
1
1
  class FileWatcher
2
- def initialize(glob_str, &block)
3
- @dir = glob_str
2
+ @@defaults = {
3
+ :load_path => ".",
4
+ :glob_str => "**/*.js"
5
+ }
6
+ def initialize(opts, &block)
7
+ @options = @@defaults.merge(opts)
4
8
  recreate_timetable
5
9
  poll(&block)
6
10
  end
@@ -32,7 +36,7 @@ class FileWatcher
32
36
  end
33
37
 
34
38
  def get_files
35
- Dir.glob @dir
39
+ Dir.glob File.join(@options[:load_path], @options[:glob_str])
36
40
  end
37
41
 
38
42
  def recreate_timetable
@@ -2,36 +2,24 @@ require File.join(File.dirname(__FILE__),'dependency_resolver')
2
2
  require File.join(File.dirname(__FILE__),'colors')
3
3
 
4
4
  class JsCompiler
5
- def initialize(source_path)
6
- @source_path = source_path
5
+ def initialize(source_file, output_file = nil)
6
+ output_file = source_file.gsub(".js", ".min.js") unless output_file
7
+ @source = source_file
8
+ @output = output_file
7
9
  end
8
10
 
9
- def compile(output_file)
11
+ def compile
10
12
  @cmd = cmd_prefix
11
- @source_files = get_source_files
12
- dep_resolver = DependencyResolver.new(@source_files,@source_path)
13
- file_order = dep_resolver.sorted_files
14
- puts "#{Colors.green('Source Files')}: #{file_order.to_s}"
15
- puts ""
16
- puts Colors.red "Compiler Output:"
17
- file_order.each {|source_file| add_file source_file }
18
- execute_compile(output_file)
13
+ puts Colors.green("Compressing: #{@source}")
14
+ puts Colors.red "Compressor Output:"
15
+ execute_compile
19
16
  end
20
17
 
21
18
  private
22
- def add_file(filename)
23
- @cmd += " --js=#{filename}"
24
- end
25
-
26
- def get_source_files
27
- Dir.glob File.join(@source_path,"/**/*.js")
28
- end
29
-
30
- def execute_compile(output_file)
31
- @cmd += " --js_output_file #{output_file}"
19
+ def execute_compile
20
+ @cmd += " --js #{@source} --js_output_file #{@output}"
32
21
  `#{@cmd}`
33
- puts Colors.green("compilted #{@source_files.size} javascript files into #{output_file}")
34
- puts "------------------------------------------------------"
22
+ puts Colors.green("compressed #{@source} to #{@output}")
35
23
  end
36
24
 
37
25
  def cmd_prefix
@@ -0,0 +1,28 @@
1
+ # Wraps code in a closure that provides an exports object to attach properties to the global namespace
2
+ module ModuleInjector
3
+ extend self
4
+
5
+ @@module_code = <<MODULE_FUNC
6
+ var global = (global != undefined)? global : window
7
+
8
+ if (global.module == undefined) {
9
+ global.module = function(name, body) {
10
+ var exports = global[name]
11
+ if (exports == undefined) {
12
+ global[name] = {}
13
+ }
14
+ body(exports)
15
+ }
16
+ }
17
+ MODULE_FUNC
18
+
19
+ def modularize(name, content)
20
+ module_code = <<-JS
21
+ #{@@module_code}
22
+
23
+ module('#{name.capitalize}', function(exports) {
24
+ #{content}
25
+ })
26
+ JS
27
+ end
28
+ end
data/lib/dependence.rb CHANGED
@@ -1,2 +1,8 @@
1
+ require File.join(File.dirname(__FILE__), "dependence", "dependency_resolver.rb")
2
+ require File.join(File.dirname(__FILE__), "dependence", "module_injector.rb")
1
3
  require File.join(File.dirname(__FILE__), 'dependence', 'js_compiler.rb')
2
4
  require File.join(File.dirname(__FILE__), "dependence", "file_watcher.rb")
5
+ require File.join(File.dirname(__FILE__), "dependence", "concatenator.rb")
6
+
7
+ module Dependence
8
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dependence
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease: false
4
+ hash: 19
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Carver
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-11 00:00:00 -07:00
19
- default_executable:
18
+ date: 2011-08-22 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: rgl
@@ -44,11 +43,12 @@ files:
44
43
  - bin/dependence
45
44
  - compiler/compiler.jar
46
45
  - lib/dependence/colors.rb
46
+ - lib/dependence/concatenator.rb
47
47
  - lib/dependence/dependency_resolver.rb
48
48
  - lib/dependence/file_watcher.rb
49
49
  - lib/dependence/js_compiler.rb
50
+ - lib/dependence/module_injector.rb
50
51
  - lib/dependence.rb
51
- has_rdoc: true
52
52
  homepage:
53
53
  licenses: []
54
54
 
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  requirements: []
80
80
 
81
81
  rubyforge_project:
82
- rubygems_version: 1.3.7
82
+ rubygems_version: 1.8.7
83
83
  signing_key:
84
84
  specification_version: 3
85
85
  summary: An easy way to handle your client side javascript dependencies