motion-require 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -2
- data/lib/motion-require/version.rb +1 -1
- data/lib/motion-require.rb +27 -7
- metadata +1 -1
data/README.md
CHANGED
@@ -17,7 +17,7 @@ end
|
|
17
17
|
require 'motion/project'
|
18
18
|
|
19
19
|
require 'motion-require'
|
20
|
-
Motion::Require.all
|
20
|
+
Motion::Require.all
|
21
21
|
|
22
22
|
Motion::Project::App.setup do |app|
|
23
23
|
# ...
|
@@ -26,6 +26,12 @@ end
|
|
26
26
|
|
27
27
|
![Whoa.](http://i.imgur.com/JLpjqkk.jpg)
|
28
28
|
|
29
|
+
To enable `motion_require` for only select files:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
Motion::Require.all(Dir.glob('app/models/**/*.rb'))
|
33
|
+
```
|
34
|
+
|
29
35
|
It's used in:
|
30
36
|
- [Formotion](https://github.com/clayallsopp/formotion)
|
31
37
|
|
@@ -39,7 +45,15 @@ Can also add to your `Gemfile` etc
|
|
39
45
|
|
40
46
|
motion-require uses static analysis (via [`ripper`](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/ripper/rdoc/Ripper.html)) to find the files using `motion_require` and automatically add the declared dependencies to `Motion::Project::Config#dependencies`. Then the `Kernel#motion_require` method is overriden at compile-time to be a noop.
|
41
47
|
|
42
|
-
The paths attached to `motion_require` are treated like those with `require_relative`.
|
48
|
+
The paths attached to `motion_require` are treated like those with `require_relative`. If you want to use `require_relative` instead of `motion_require`, you can enable this:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
require 'motion-require'
|
52
|
+
Motion::Require.require_relative_enabled = true
|
53
|
+
|
54
|
+
# now Motion::Require will detect require_relative
|
55
|
+
Motion::Require.all(Dir.glob("app/**/*.rb"))
|
56
|
+
```
|
43
57
|
|
44
58
|
## Contact
|
45
59
|
|
data/lib/motion-require.rb
CHANGED
@@ -4,6 +4,11 @@ require_relative 'motion-require/ext'
|
|
4
4
|
|
5
5
|
module Motion
|
6
6
|
module Require
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_accessor :require_relative_enabled
|
10
|
+
end
|
11
|
+
|
7
12
|
class RequireBuilder < Ripper::SexpBuilder
|
8
13
|
REQUIREMENT_TOKENS = %w[motion_require require_relative]
|
9
14
|
|
@@ -15,12 +20,19 @@ module Motion
|
|
15
20
|
|
16
21
|
def on_command(command, args) # scanner event
|
17
22
|
type, name, position = command
|
18
|
-
if
|
23
|
+
if valid_require_command(name)
|
19
24
|
file = parse_args(args)
|
20
25
|
requires << file
|
21
26
|
end
|
22
27
|
end
|
23
28
|
|
29
|
+
def valid_require_command(name)
|
30
|
+
if name == 'require_relative' && !Motion::Require.require_relative_enabled
|
31
|
+
raise NoMethodError, 'require_relative is not enabled, see Motion::Require.require_relative_enabled'
|
32
|
+
end
|
33
|
+
REQUIREMENT_TOKENS.include?(name)
|
34
|
+
end
|
35
|
+
|
24
36
|
def parse_args(args)
|
25
37
|
value = nil
|
26
38
|
args.each do |arg|
|
@@ -44,10 +56,17 @@ module Motion
|
|
44
56
|
requires = requires_in(file_path)
|
45
57
|
if !requires.empty?
|
46
58
|
dependencies[file_path] = requires.map { |required|
|
47
|
-
|
48
|
-
|
59
|
+
required_path = resolve_path(file_path, required)
|
60
|
+
if !File.exist?(required_path) && File.extname(required) != ".rb"
|
61
|
+
required_path += ".rb"
|
62
|
+
end
|
63
|
+
|
64
|
+
if !File.exist?(required_path)
|
65
|
+
# TODO: Report line number of failing require
|
66
|
+
raise LoadError, "ERROR! In `#{file_path}', could not require `#{required}', file not found."
|
49
67
|
end
|
50
|
-
|
68
|
+
|
69
|
+
required_path
|
51
70
|
}
|
52
71
|
dependencies[file_path].unshift ext_file
|
53
72
|
end
|
@@ -67,11 +86,12 @@ module Motion
|
|
67
86
|
Pathname.new(source).dirname.join(required.to_str).cleanpath.to_path
|
68
87
|
end
|
69
88
|
|
70
|
-
|
89
|
+
# Scan specified files. When nil, fallback to RubyMotion's default (app/**/*.rb).
|
90
|
+
def all(files=nil)
|
71
91
|
Motion::Project::App.setup do |app|
|
72
92
|
app.files << ext_file
|
73
|
-
app.files |= files.map { |f| explicit_relative(f) }
|
74
|
-
dependencies = dependencies_for(files)
|
93
|
+
app.files |= Array(files).map { |f| explicit_relative(f) }
|
94
|
+
dependencies = dependencies_for(files || app.files)
|
75
95
|
app.files_dependencies dependencies
|
76
96
|
end
|
77
97
|
end
|