Module: Motion::Require

Defined in:
lib/motion-require.rb,
lib/motion-require/version.rb

Defined Under Namespace

Classes: RequireBuilder

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.require_relative_enabledObject

Returns the value of attribute require_relative_enabled.



8
9
10
# File 'lib/motion-require.rb', line 8

def require_relative_enabled
  @require_relative_enabled
end

Class Method Details

.all(files = nil, options = {}) ⇒ Object

Scan specified files. When nil, fallback to RubyMotion’s default (app/*/.rb).



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/motion-require.rb', line 89

def all(files=nil, options={})
  # if you want the default 'app.files', you can just pass in the options
  if files.is_a?(Hash) && options == {}
    options = files
    files = nil
  end

  check_platform = options.fetch(:platform, nil)
  current_platform = App.respond_to?(:template) ? App.template : :ios
  return unless Motion::Require.check_platform(current_platform, check_platform)

  Motion::Project::App.setup do |app|
    app.exclude_from_detect_dependencies << ext_file

    if files.nil? || files.empty?
      app.files.push ext_file
      app.exclude_from_detect_dependencies += app.files
      app.files_dependencies dependencies_for(app.files.flatten)
    else
    # Place files prior to those in ./app, otherwise at the end.
      preceding_app = app.files.index { |f| f =~ %r(^(?:\./)?app/) } || -1
      required = Array(files).map { |f| explicit_relative(f) }
      app.exclude_from_detect_dependencies += required
      app.files.insert(preceding_app, ext_file, *required)
      app.files.uniq! # Prevent redundancy

      app.files_dependencies dependencies_for(required)
    end
  end
end

.check_platform(current_platform, check_platform) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/motion-require.rb', line 120

def check_platform(current_platform, check_platform)
  case check_platform
  when nil
    true
  when Array
    check_platform.include?(current_platform)
  when Symbol
    current_platform == check_platform
  else
    puts "Unrecognized value for 'check_platform': #{check_platform.inspect}"
    false
  end
end

.dependencies_for(files) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/motion-require.rb', line 52

def dependencies_for(files)
  dependencies = {}
  files.each do |file_path|
    requires = requires_in(file_path)
    if !requires.empty?
      dependencies[file_path] = requires.map { |required|
        required_path = resolve_path(file_path, required)
        if !File.exist?(required_path) && File.extname(required) != ".rb"
          required_path += ".rb"
        end

        if !File.exist?(required_path)
          # TODO: Report line number of failing require
          raise LoadError, "ERROR! In `#{file_path}', could not require `#{required}', file not found."
        end

        required_path
      }
      dependencies[file_path].unshift ext_file
    end
  end
  dependencies
end

.explicit_relative(path) ⇒ Object

RubyMotion prefers relative paths to be explicitly prefixed with ./



135
136
137
138
# File 'lib/motion-require.rb', line 135

def explicit_relative(path)
  # Paths that do not start with "/", "./", or "../" will be prefixed with ./
  path.sub(%r(^(?!\.{0,2}/)), './')
end

.ext_fileObject



140
141
142
# File 'lib/motion-require.rb', line 140

def ext_file
  File.expand_path(File.join(File.dirname(__FILE__), "../motion/ext.rb"))
end

.requires_in(file) ⇒ Object



76
77
78
79
80
# File 'lib/motion-require.rb', line 76

def requires_in(file)
  parser = Motion::Require::RequireBuilder.new(File.read(file))
  parser.parse
  parser.requires
end

.resolve_path(source, required) ⇒ Object

Join ‘required` to directory containing `source`. Preserves relative/absolute nature of source



84
85
86
# File 'lib/motion-require.rb', line 84

def resolve_path(source, required)
  Pathname.new(source).dirname.join(required.to_str).cleanpath.to_path
end