Class: Spade::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/spade/loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ Loader

Returns a new instance of Loader.



42
43
44
# File 'lib/spade/loader.rb', line 42

def initialize(ctx)
  @ctx = ctx
end

Instance Method Details

#discoverRoot(path) ⇒ Object



46
47
48
# File 'lib/spade/loader.rb', line 46

def discoverRoot(path)
  Spade.discover_root path
end

#exists(spade, id, formats) ⇒ Object

exposed to JS. Determines if the named id exists in the system



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/spade/loader.rb', line 130

def exists(spade, id, formats)
  
  # individual files
  return File.exists?(id[5..-1]) if id =~ /^file:\//

  parts = id.split '/'
  package_name = parts.shift
  package_info = packages[package_name]
  
  return false if package_info.nil?
  return true if parts.size==1 && parts[0] == '~package'
  
  dirname = extract_dirname(parts, package_info)
  
  
  filename = parts.pop
  base_path = package_info[:path]
  formats = ['js'] if formats.nil?
  formats.each do |fmt|
    cur_path = File.join(base_path, dirname, parts, filename+'.'+fmt)
    return true if File.exist? cur_path
  end
  
  rb_path = File.join(package_info[:path],dirname,parts, filename+'.rb')
  return File.exists? rb_path
end

#load_module(id, module_path, format, path) ⇒ Object



157
158
159
160
161
# File 'lib/spade/loader.rb', line 157

def load_module(id, module_path, format, path)
  module_contents = File.read(module_path).to_json # encode as string
  @ctx.eval("spade.register('#{id}',#{module_contents}, { format: #{format.to_s.to_json}, filename: #{path.to_s.to_json} });")
  nil
end

#load_ruby(id, rb_path) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/spade/loader.rb', line 163

def load_ruby(id, rb_path)

  klass = Spade.exports_for rb_path
  exports = klass.nil? ? {} : klass.new(@ctx)
  @ctx['$__rb_exports__'] = exports

  @ctx.eval(%[(function() { 
    var exp = $__rb_exports__; 
    spade.register('#{id}', function(r,e,m) { m.exports = exp; }); 
  })();])

  @ctx['$__rb_exports__'] = nil
end

#loadFactory(spade, id, formats, done = nil) ⇒ Object

exposed to JS. Find the JS file on disk and register the module



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
119
120
121
122
123
124
125
126
127
# File 'lib/spade/loader.rb', line 57

def loadFactory(spade, id, formats, done=nil)
  formats = formats.to_a # We may get a V8::Array, we want a normal one

  # load individual files
  if id =~ /^file:\//
    js_path = id[5..-1]
    if File.exists? js_path
      load_module id, js_path, ['js'], js_path
    end
    return nil
  end

  parts = id.split '/'
  package_name = parts.shift
  package_info = packages[package_name]
  skip_module  = false

  return nil if package_info.nil?

  if parts.size==1 && parts[0] == '~package'
    skip_module = true
  elsif parts.size==1 && parts[0] == 'main'
    parts = (package_info[:json]['main'] || 'lib/main').split('/')
    dirname = parts[0...-1]
    parts   = [parts[-1]]
  else
    dirname = extract_dirname(parts, package_info)
  end

  # register the package first - also make sure dependencies are 
  # registered since they are needed for loading plugins
  unless package_info[:registered]
    package_info[:registered] = true
    @ctx.eval "spade.register('#{package_name}', #{package_info[:json].to_json});"
    
    deps = package_info[:json]['dependencies'] || [];
    deps.each do |dep_name, ignored|
      dep_package_info = packages[dep_name]
      next unless dep_package_info && !dep_package_info[:registered]
      dep_package_info[:registered] = true
      @ctx.eval "spade.register('#{dep_name}', #{dep_package_info[:json].to_json});"

      # Add new formats if they are specified in our dependencies
      dep_formats = dep_package_info[:json]['plugin:formats']
      formats.unshift(*dep_formats.keys).uniq! if dep_formats
    end
          
  end
  
  unless skip_module
    filename = parts.pop
    base_path = package_info[:path]
    formats << ['js'] if formats.empty?
    formats.each do |fmt|
      cur_path = File.join(base_path, dirname, parts, filename+'.'+fmt)
      if File.exist? cur_path
        load_module id, cur_path, fmt, cur_path
        return nil
      end
    end
    
    rb_path = File.join(package_info[:path],dirname,parts, filename+'.rb')
    if File.exists? rb_path
      load_ruby id, rb_path
      return nil
    end
    
  end
  
  return nil
end

#packagesObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/spade/loader.rb', line 177

def packages
  @packages unless @packages.nil?
  @packages = {}

  # add global packages in spade project
  # globals = File.expand_path(File.join(__FILE__, '..', '..', '..', 'packages'))
  # package_paths = Dir.glob File.join(globals,'*')
  # package_paths.each { |path| add_package(path) }      

  # Do this to get the Gem.dir right
  env = Spade::Environment.new

  package_paths = Dir.glob(File.join(env.spade_dir, 'gems', '*'))
  package_paths.each{|path| add_package(path) }

  # in reverse order of precedence
  %w[.spade/packages vendor/cache vendor/packages packages].each do |p|
    package_paths = Dir.glob File.join(@ctx.rootdir, p.split('/'), '*')
    package_paths.each { |path| add_package(path) }
  end

  # add self
  add_package @ctx.rootdir

  @packages
end

#root(path = nil) ⇒ Object



50
51
52
53
54
# File 'lib/spade/loader.rb', line 50

def root(path=nil)
  return @ctx.rootdir if path.nil?
  @ctx.rootdir = path 
  @packages = nil
end