Module: Spade::Bundle

Defined in:
lib/spade/bundle.rb

Class Method Summary collapse

Class Method Details

.gen_spade_boot(rootdir, opts = {}) ⇒ Object



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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/spade/bundle.rb', line 74

def gen_spade_boot(rootdir, opts={})
  verbose = opts[:verbose]
  spade_path = File.join(rootdir, Spade::SPADE_DIR, 'packages', '*')
  known_packages = Dir.glob(spade_path).map do |path|
    package_name = File.basename path
    info = JSON.load File.read(File.join(path, 'package.json'))
    info["sync"] = true
    info["root"] = "#{Spade::SPADE_DIR}/packages/#{package_name}"
    info["files"] = package_file_list(path)
    info
  end

  if File.exists? File.join(rootdir, 'package.json')
    info = JSON.load File.read(File.join(rootdir, 'package.json'))
  else
    info = { 
      "name" => File.basename(rootdir), 
      "directories" => { "lib" => "lib" } 
    }
  end

  info["root"] = '.'
  info["sync"] = true
  info["files"] = package_file_list('.')

  packages = resolve_dependencies(info, known_packages)

%[// GENERATED: #{Time.now.to_s}
// This file is automatically generated by spade.  To update run 
// 'spade update'. To use this file, reference it in your HTML file.  Base 
// sure that your base URL is to the top level directory containing all of 
// your files.

/*globals spade */
//@ begin boot
(function() {
  // load spade itself
  var script = document.createElement('script');
  script.src = "#{Spade::SPADE_DIR}/boot/spade.js";

  function boot() {
    // Register remaining packages with spade
#{packages.map{|p| %[spade.register("#{p["name"]}", #{JSON.pretty_generate(p)});\n] } * "\n"}

    // find the main module to run
    var main = null;
    var scripts = document.scripts || document.getElementsByTagName("script"),
  len = scripts.length;
    for(var idx=0;!main && idx<len;idx++) {
main = scripts[idx].getAttribute('data-require');
    }
    scripts = null; // avoid memory leaks in IE

    if (main) spade.ready(function() { spade.require(main); });
  };

  script.onload = boot;
  // IE doesn't support onload for scripts
  script.onreadystatechange = function(){
    if (script.readyState === 'loaded' || script.readyState === 'complete') {
boot();
script = null; // avoid memory leaks in IE
    }
  };

  var head = document.head || document.body || document;
  head.appendChild(script);

  head = null; // avoid memory leaks in IE
})();
//@ end boot
]

end

.update(rootdir, opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
# File 'lib/spade/bundle.rb', line 13

def update(rootdir, opts ={})

  verbose = opts[:verbose]
  spade_path = File.join(rootdir, Spade::SPADE_DIR)
  spade_package_path = File.join(spade_path, 'packages')
  FileUtils.rm_r(spade_path) if File.exists? spade_path

  FileUtils.mkdir_p File.join(spade_package_path)

  FileUtils.ln_s Spade.boot_path, File.join(spade_path, 'boot')

  installed = []
  package_dirs = []

  # In order of precedence
  package_dirs += %w[packages vendor/packages vendor/cache].map{|p| File.join(rootdir, p.split('/')) }
  package_dirs << File.join(LibGems.dir, 'gems') if defined?(BPM)

  for package_dir in package_dirs
    Dir.glob(File.join(package_dir, '*')).each do |path|
      json_file = File.join(path, 'package.json')
      next unless File.exists?(json_file)

      # How would this happen?
      next if installed.include? path
      installed << path

      json = JSON.load File.read(json_file)
      package_name = json['name']
      package_version = json['version']

      local = path.index(rootdir) == 0

      # Use relative paths if embedded
      old_path = if local
        # Figure out how many levels deep the spade_path is in the project
        levels = spade_package_path.sub(rootdir, '').split(File::SEPARATOR).reject{|p| p.empty? }.count
        # Build relative path
        File.join(['..'] * levels, path.sub(rootdir, ''))
      else
        path
      end

      new_path = File.join(spade_path, 'packages', package_name)

      unless File.exist?(new_path)
        FileUtils.ln_s old_path, new_path, :force => true

        puts "Installing #{local ? "local" : "remote"} package #{package_name}" if verbose
      end
    end
  end

  File.open(File.join(rootdir, 'spade-boot.js'), 'w+') do |fp|
    fp.write gen_spade_boot(rootdir, opts)
  end
  puts "Wrote spade-boot.js" if verbose


end