mrubymix 0.0.1

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 +29 -0
  2. data/lib/mrubymix.rb +79 -0
  3. metadata +49 -0
data/bin/mrubymix ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # %% -*- ruby -*-
3
+
4
+ require "#{File.dirname(__FILE__)}/../lib/mrubymix"
5
+
6
+ if ARGV.length == 2
7
+ MrubyMix.mix(ARGV[0], ARGV[1])
8
+ else
9
+ puts <<END
10
+ mrubymix: a mruby source code preprocessor using Rails asset pipeline-like syntax.
11
+
12
+ Usage:
13
+ mrubymix [source file name] [destination file name]
14
+
15
+ Syntax:
16
+ You can add following lines in your source code(treated as comments by Ruby):
17
+
18
+ #= require aaa
19
+ Replace this line with the content of aaa.rb in the same directory as this file.
20
+
21
+ #= require ./foo/bar
22
+ Replace this line with the content of ./foo/bar relative to current file's path.
23
+
24
+ Note for the same file will only appear in the destination file once(at the first
25
+ occurrence location).
26
+ END
27
+ exit 0
28
+ end
29
+
data/lib/mrubymix.rb ADDED
@@ -0,0 +1,79 @@
1
+ module MrubyMix
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
18
+ end
19
+ results
20
+ end
21
+
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)
33
+
34
+ results = []
35
+
36
+ required_files = parse_single_file(file_name)
37
+ skip_lines = []
38
+
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
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ root_item = [file_name, skip_lines]
51
+ if !results.index(root_item)
52
+ results << root_item
53
+ end
54
+
55
+ results
56
+ end
57
+
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)
61
+
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"
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mrubymix
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Xuejie Xiao
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-26 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: mrubymix preprocesses mruby source code using Rails asset pipeline-like
15
+ syntax
16
+ email: xxuejie@gmail.com
17
+ executables:
18
+ - mrubymix
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - bin/mrubymix
23
+ - lib/mrubymix.rb
24
+ homepage: https://github.com/xxuejie/mrubymix
25
+ licenses:
26
+ - MIT
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.25
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: mruby source code preprocessor
49
+ test_files: []