dependence 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/dependence ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require 'rb-inotify'
4
+ require File.join(File.dirname(__FILE__), "..", "lib/dependence.rb")
5
+
6
+ options = {:output => "compiled.js"}
7
+ OptionParser.new do |opts|
8
+ opts.banner = "Usage: compile.rb js_source_dir [options]"
9
+
10
+ opts.on("-o", "--output FILE", "Output .js file") do |file|
11
+ options[:output] = file
12
+ end
13
+
14
+ opts.on("-w", "--watch", "Watch src directory for changes and recompile") do |bool|
15
+ options[:watch] = bool
16
+ end
17
+ end.parse(ARGV)
18
+
19
+ if ARGV.length < 1
20
+ puts "You need to inlcude your js src directory"
21
+ exit 1
22
+ elsif !File.directory? ARGV[0]
23
+ puts "Can't find your js source directory: #{ARGV[0]}"
24
+ exit 1
25
+ end
26
+
27
+ options[:input] = ARGV[0]
28
+ output_file = options[:output]
29
+ compiler = JsCompiler.new(options[:input])
30
+
31
+ def recompile(compiler, output_file)
32
+ File.delete(output_file) if File.exists?(output_file)
33
+ compiler.compile(output_file)
34
+ end
35
+
36
+ recompile compiler, output_file
37
+
38
+ if options[:watch]
39
+ puts "Watching #{options[:input]} for changes. Your javascript files will be compiled automatically"
40
+ FileWatcher.new "**/*.js" do
41
+ recompile compiler, output_file
42
+ end
43
+ end
44
+
45
+
46
+
47
+
Binary file
@@ -0,0 +1,11 @@
1
+ module Colors
2
+ extend self
3
+
4
+ def colorize(text, color_code)
5
+ "\e[#{color_code}m#{text}\e[0m"
6
+ end
7
+
8
+ def red(text); colorize(text, 31); end
9
+ def green(text); colorize(text, 32); end
10
+
11
+ end
@@ -0,0 +1,33 @@
1
+ require 'rgl/adjacency'
2
+ require 'rgl/topsort'
3
+
4
+ class DependencyResolver
5
+ def initialize(file_list, file_path)
6
+ @files = file_list
7
+ @file_path = file_path
8
+ @graph = RGL::DirectedAdjacencyGraph.new
9
+ end
10
+
11
+ def sorted_files
12
+ @files.each do |file|
13
+ @graph.add_vertex(file)
14
+ dependencies = get_dependencies_in(file)
15
+ dependencies.each {|dependency| @graph.add_edge(dependency,file) }
16
+ end
17
+ @graph.topsort_iterator.to_a
18
+ end
19
+
20
+ private
21
+ def get_dependencies_in(file)
22
+ dependencies = []
23
+
24
+ File.foreach(file) do |s|
25
+ if s.include?("@import")
26
+ file_name = s.match(/@import (.*)/)[1].strip
27
+ dependencies << File.join(@file_path, file_name)
28
+ end
29
+ end
30
+ return dependencies
31
+ end
32
+ end
33
+
@@ -0,0 +1,49 @@
1
+ class FileWatcher
2
+ def initialize(glob_str, &block)
3
+ @dir = glob_str
4
+ recreate_timetable
5
+ poll(&block)
6
+ end
7
+
8
+ private
9
+ def poll(&block)
10
+ while true
11
+ if fs_modified?
12
+ block.call
13
+ recreate_timetable
14
+ end
15
+ sleep 3
16
+ end
17
+ end
18
+
19
+ def fs_modified?
20
+ new_files = get_files
21
+ return true if new_files.length != @files.length
22
+ new_timetable = create_file_modified_timetable(new_files)
23
+
24
+ modified = false
25
+ new_timetable.each do |filename,time|
26
+ if time != @timetable[filename]
27
+ modified = true
28
+ break
29
+ end
30
+ end
31
+ modified
32
+ end
33
+
34
+ def get_files
35
+ Dir.glob @dir
36
+ end
37
+
38
+ def recreate_timetable
39
+ @files = get_files
40
+ @timetable = create_file_modified_timetable(@files)
41
+ end
42
+
43
+ def create_file_modified_timetable(filenames)
44
+ filenames.inject({}) do |table, filename|
45
+ table[filename] = File.mtime(filename) if File.exists? filename
46
+ table
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,42 @@
1
+ require File.join(File.dirname(__FILE__),'dependency_resolver')
2
+ require File.join(File.dirname(__FILE__),'colors')
3
+
4
+ class JsCompiler
5
+ def initialize(source_path)
6
+ @source_path = source_path
7
+ end
8
+
9
+ def compile(output_file)
10
+ @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)
19
+ end
20
+
21
+ 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}"
32
+ `#{@cmd}`
33
+ puts Colors.green("compilted #{@source_files.size} javascript files into #{output_file}")
34
+ puts "------------------------------------------------------"
35
+ end
36
+
37
+ def cmd_prefix
38
+ "java -jar compiler/compiler.jar"
39
+ # --create_source_map js_map
40
+ end
41
+ end
42
+
data/lib/dependence.rb ADDED
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__), 'dependence', 'js_compiler.rb')
2
+ require File.join(File.dirname(__FILE__), "dependence", "file_watcher.rb")
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dependence
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Joshua Carver
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-07 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: jcarver989@gmail.com
24
+ executables:
25
+ - dependence
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - bin/dependence
32
+ - compiler/compiler.jar
33
+ - lib/dependence/colors.rb
34
+ - lib/dependence/dependency_resolver.rb
35
+ - lib/dependence/file_watcher.rb
36
+ - lib/dependence/js_compiler.rb
37
+ - lib/dependence.rb
38
+ has_rdoc: true
39
+ homepage:
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ - compiler
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: An easy way to handle your client side javascript dependencies
73
+ test_files: []
74
+