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.



14
15
16
# File 'lib/spade/loader.rb', line 14

def initialize(ctx)
  @ctx = ctx
end

Instance Method Details

#discoverRoot(path) ⇒ Object



18
19
20
# File 'lib/spade/loader.rb', line 18

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/spade/loader.rb', line 104

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'
  
  dirs = extract_dirs(parts, package_info)
  
  
  filename = parts.pop
  base_path = package_info[:path]
  formats = ['js'] if formats.nil?
  dirs.each do |dirname|
    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
end

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



133
134
135
136
137
# File 'lib/spade/loader.rb', line 133

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



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/spade/loader.rb', line 139

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
# File 'lib/spade/loader.rb', line 29

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('/')
    dirs  = [parts[0...-1]]
    parts = [parts[-1]]
  else
    dirs = extract_dirs(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?

    dirs.each do |dirname|
      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
  end
  
  return nil
end

#packagesObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/spade/loader.rb', line 153

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

  if defined?(BPM)
    package_paths = Dir.glob(File.join(LibGems.dir, 'gems', '*'))
    package_paths.each{|path| add_package(path) }
  end

  # 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



22
23
24
25
26
# File 'lib/spade/loader.rb', line 22

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