mrubymix 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/mrubymix +1 -1
  2. data/lib/mrubymix.rb +72 -58
  3. metadata +1 -1
@@ -4,7 +4,7 @@
4
4
  require "#{File.dirname(__FILE__)}/../lib/mrubymix"
5
5
 
6
6
  if ARGV.length == 2
7
- MrubyMix.mix(ARGV[0], ARGV[1])
7
+ MrubyMix::Processor.new(ARGV[0], ARGV[1]).mix
8
8
  else
9
9
  puts <<END
10
10
  mrubymix: a mruby source code preprocessor using Rails asset pipeline-like syntax.
@@ -1,79 +1,93 @@
1
1
  module MrubyMix
2
2
 
3
- # Processes a single file and returned all required items with line numbers
4
- # and item name in the following format:
5
- #
6
- # [[2, "require", "foo"], [3, "require", "bar"]]
7
- #
8
- def self.parse_single_file(file_name)
9
- results = []
10
- File.open(file_name, "r") do |f|
11
- f.each do |l|
12
- words = l.split(' ').select {|w| !w.empty? }
13
- if (words.length == 3) &&
14
- (words[0] == '#=')
15
- results <<= [f.lineno, words[1], words[2]]
16
- end
17
- end
3
+ class Processor
4
+
5
+ attr_reader :root_file_name, :dest_file_name
6
+ attr_accessor :results
7
+
8
+ def initialize(root_file_name, dest_file_name)
9
+ @root_file_name = root_file_name
10
+ @dest_file_name = dest_file_name
11
+ @results = []
18
12
  end
19
- results
20
- end
21
13
 
22
- # Starts recursive processing from a base file, and return an array of files
23
- # needed to add(including it self). The result format is:
24
- #
25
- # [["~/foo.rb", [1, 2, 3]], ["~/tmp/bar.rb", [4, 5]], ["~/app.rb", [1]]]
26
- #
27
- # The file name returned are in full path. For each file, the line needed to
28
- # skip is also returned
29
- def self.process(file_name)
30
- file_name <<= '.rb' unless file_name.end_with?('.rb')
31
- file_name = File.expand_path(file_name)
32
- file_dir = File.dirname(file_name)
14
+ # mix all source files trackable from the root file
15
+ def mix
16
+ process_files(File.expand_path(root_file_name))
33
17
 
34
- results = []
18
+ File.open(dest_file_name, "w") do |o_f|
19
+ results.each do |name, skip_lines|
20
+ File.open(name, "r") do |i_f|
21
+ o_f.write "\# File: #{name}\n"
22
+ i_f.each do |l|
23
+ # skip_lines is sorted
24
+ if i_f.lineno == skip_lines.first
25
+ skip_lines.shift
26
+ else
27
+ o_f.write l
28
+ end
29
+ end
30
+ o_f.write "\n"
31
+ end
32
+ end
33
+ end
34
+ end
35
35
 
36
- required_files = parse_single_file(file_name)
37
- skip_lines = []
36
+ private
38
37
 
39
- required_files.each do |lineno, cmd, arg|
40
- skip_lines <<= lineno
41
- if cmd == 'require'
42
- process(File.join(file_dir, arg)).each do |r|
43
- if !results.index(r)
44
- results << r
38
+ # Processes a single file and returned all required items with line numbers
39
+ # and item name in the following format:
40
+ #
41
+ # [[2, "require", "foo"], [3, "require", "bar"]]
42
+ #
43
+ def parse_single_file(file_name)
44
+ r = []
45
+ File.open(file_name, "r") do |f|
46
+ f.each do |l|
47
+ words = l.split(' ').select {|w| !w.empty? }
48
+ if (words.length == 3) &&
49
+ (words[0] == '#=')
50
+ r <<= [f.lineno, words[1], words[2]]
45
51
  end
46
52
  end
47
53
  end
54
+ r
48
55
  end
49
56
 
50
- root_item = [file_name, skip_lines]
51
- if !results.index(root_item)
52
- results << root_item
57
+ def file_not_processed(full_file_name)
58
+ results.none? {|r| r[0] == full_file_name}
53
59
  end
54
60
 
55
- results
56
- end
61
+ def normalize_file_name(file_name, base_dir)
62
+ file_name = File.expand_path(file_name, base_dir)
63
+ file_name <<= '.rb' unless file_name.end_with?('.rb')
64
+ file_name
65
+ end
57
66
 
58
- # mix all source files trackable from the root file
59
- def self.mix(root_file_name, dst_file_name)
60
- results = process(root_file_name)
67
+ # Starts recursive processing from a base file, and adds the required files
68
+ # in results field. The result format is:
69
+ #
70
+ # [["~/foo.rb", [1, 2, 3]], ["~/tmp/bar.rb", [4, 5]], ["~/app.rb", [1]]]
71
+ #
72
+ # The file name returned are in full path. For each file, the line needed to
73
+ # skip is also returned
74
+ #
75
+ # +file_name+ must be a full path
76
+ def process_files(root_file_name)
77
+ file_dir = File.dirname(root_file_name)
61
78
 
62
- File.open(dst_file_name, "w") do |o_f|
63
- results.each do |name, skip_lines|
64
- File.open(name, "r") do |i_f|
65
- o_f.write "\# File: #{name}\n"
66
- i_f.each do |l|
67
- # skip_lines is sorted
68
- if i_f.lineno == skip_lines.first
69
- skip_lines.shift
70
- else
71
- o_f.write l
72
- end
73
- end
74
- o_f.write "\n"
79
+ required_files = parse_single_file(root_file_name)
80
+ skip_lines = []
81
+
82
+ required_files.each do |lineno, cmd, arg|
83
+ skip_lines <<= lineno
84
+ if cmd == 'require'
85
+ required_file_path = normalize_file_name(arg, file_dir)
86
+ process_files required_file_path if file_not_processed required_file_path
75
87
  end
76
88
  end
89
+
90
+ results << [root_file_name, skip_lines] if file_not_processed root_file_name
77
91
  end
78
92
  end
79
93
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mrubymix
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Xuejie Xiao