2009-08-29 22:52:21 +00:00
|
|
|
require 'net/http'
|
|
|
|
require 'fileutils'
|
|
|
|
require 'rubygems'
|
|
|
|
require 'zip/zip'
|
|
|
|
|
|
|
|
|
|
|
|
def fetch(uri_str, limit = 10)
|
|
|
|
raise ArgumentError, 'HTTP redirect too deep' if limit == 0
|
|
|
|
|
|
|
|
url = URI.parse(uri_str)
|
|
|
|
http = Net::HTTP.new(url.host, url.port)
|
2011-09-06 17:06:25 +00:00
|
|
|
http.open_timeout = 10
|
2009-08-29 22:52:21 +00:00
|
|
|
http.read_timeout = 30
|
2011-09-06 17:06:25 +00:00
|
|
|
http.use_ssl = true
|
2009-08-29 22:52:21 +00:00
|
|
|
response = http.start do |http|
|
|
|
|
puts "getting #{url.path}"
|
|
|
|
http.request_get(url.path)
|
|
|
|
end
|
|
|
|
|
|
|
|
case response
|
|
|
|
when Net::HTTPSuccess then response
|
|
|
|
when Net::HTTPRedirection then fetch(response['location'], limit - 1)
|
|
|
|
else
|
|
|
|
response.error!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-27 19:41:58 +00:00
|
|
|
def install_from_github(user, project, ext_name, branch = "master", working_directory = Dir.pwd)
|
2011-09-06 17:06:25 +00:00
|
|
|
download_link = "https://github.com/#{user}/#{project}/zipball/#{branch}"
|
2009-08-29 22:52:21 +00:00
|
|
|
extdir = File.join(working_directory,'extensions')
|
|
|
|
|
2010-04-13 13:58:53 +00:00
|
|
|
if !File.exists?("#{extdir}/#{ext_name}")
|
2009-08-29 22:52:21 +00:00
|
|
|
begin
|
|
|
|
puts "Downloading the #{ext_name} plugin into #{extdir}."
|
2010-04-25 06:31:43 +00:00
|
|
|
FileUtils.mkdir_p("#{extdir}")
|
2009-08-29 22:52:21 +00:00
|
|
|
zipfile = File.join(extdir, "#{ext_name}.zip")
|
|
|
|
open(zipfile, "wb") do |tgz|
|
|
|
|
tgz << fetch(download_link).body
|
|
|
|
end
|
|
|
|
puts "Unzipping the #{ext_name} plugin."
|
|
|
|
Zip::ZipFile::open(zipfile) { |zf|
|
|
|
|
zf.each { |e|
|
|
|
|
fpath = File.join(extdir, e.name)
|
|
|
|
FileUtils.mkdir_p(File.dirname(fpath))
|
|
|
|
zf.extract(e, fpath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
File.unlink(zipfile)
|
|
|
|
funky_directory = Dir.glob(File.join(extdir,"#{user}-#{project}-*"))[0]
|
|
|
|
FileUtils.mv(funky_directory, File.join(extdir, ext_name))
|
|
|
|
puts "#{ext_name} installed."
|
|
|
|
rescue Exception => e
|
|
|
|
FileUtils.rm_rf(extdir)
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|