Class: ZotPlus::RakeHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/zotplus-rakehelper.rb,
lib/zotplus-rakehelper/version.rb

Constant Summary collapse

VERSION =
"0.0.138"

Class Method Summary collapse

Class Method Details

.download(url, file) ⇒ Object



285
286
287
288
289
290
291
# File 'lib/zotplus-rakehelper.rb', line 285

def self.download(url, file)
  STDERR.puts "Downloading #{url} to #{file}"
  FileUtils.mkdir_p File.dirname(file)

  data = open(url)
  File.open(file, 'wb'){|f| f.write(data.read) }
end

.geturl(url) ⇒ Object



280
281
282
283
# File 'lib/zotplus-rakehelper.rb', line 280

def self.geturl(url)
  return open(url).read
  #OpenURI::HTTPError
end

.getxpis(sources, dir) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/zotplus-rakehelper.rb', line 335

def self.getxpis(sources, dir)
  return if ENV['OFFLINE'].to_s.downcase == 'true'

  dir = File.expand_path(dir)
  FileUtils.mkdir_p(dir)
  installed = Dir["#{dir}/*.xpi"].collect{|f| File.basename(f) }

  sources = sources.collect{|s| self.resolvexpi(s) }

  (installed - sources.collect{|s| s.xpi}).each{|xpi|
    STDERR.puts "Removing #{xpi}"
    File.unlink("#{dir}/#{xpi}")
  }
  sources.reject{|s| installed.include?(s.xpi) && s.url !~ /^file:/ }.each{|s|
    if s.xpi =~ /(.*)-master-.*.xpi$/
      src = $1
      tgt = "#{dir}/#{s.xpi}"
      STDERR.puts "Zipping #{s.xpi} to #{tgt}"
      Dir.chdir(src){|path|
        Zip::File.open(tgt, 'w') do |zipfile|
          Dir["**/*"].sort.each{|file|
            zipfile.add(file, file)
          }
        end
      }
    else
      STDERR.puts "Downloading #{s.xpi}"
      download(s.url, "#{dir}/#{s.xpi}")
    end
  }
end

.resolvexpi(source) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/zotplus-rakehelper.rb', line 293

def self.resolvexpi(source)
  STDERR.puts "Resolving #{source}"

  xpi = nil

  if source =~ /update\.rdf$/
    update_rdf = Nokogiri::XML(self.geturl(source))
    update_rdf.remove_namespaces!
    url = update_rdf.at('//updateLink').inner_text
  elsif source =~ /^https:\/\/addons\.mozilla\.org\//
    page = self.geturl(source)
    page = Nokogiri::HTML(page)
    url = page.at_css('p.install-button').at_css('a')['href']

    url = URI.join(source, url ).to_s if url !~ /^http/

    return self.resolvexpi(url) if url =~ /\/contribute\/roadblock\//

    # AMO uses redirects, so I can't write the file to the final name just yet
    final_uri = nil
    open(url) do |h|
      final_uri = h.base_uri
    end
    url = final_uri.to_s
  elsif source =~ /^https:\/\/github\.com\/zotero\/([^\/]+)\/zipball\/master$/
    url = source
    src = $1
    Dir.chdir(src) {
      rev = `git log -n 1 --pretty=format:"%h"`
      xpi = "#{src}-master-#{rev}.xpi"
    }
  elsif source =~ /^file:/ || source =~ /\.xpi(\?|$)/
    url = source
  else
    throw "Unsupported XPI source #{source}"
  end

  xpi ||= url.sub(/.*\//, '').sub(/\?.*$/, '')
  STDERR.puts "Resolved to #{url}"
  return OpenStruct.new(url: url, xpi: xpi)
end