mrubymix 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/mrubymix +1 -1
- data/lib/mrubymix.rb +72 -58
- metadata +1 -1
data/bin/mrubymix
CHANGED
data/lib/mrubymix.rb
CHANGED
@@ -1,79 +1,93 @@
|
|
1
1
|
module MrubyMix
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
23
|
-
|
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
|
-
|
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
|
-
|
37
|
-
skip_lines = []
|
36
|
+
private
|
38
37
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
51
|
-
|
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
|
-
|
56
|
-
|
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
|
-
|
59
|
-
|
60
|
-
|
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
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|