Move download strategies into their own file

This commit is contained in:
Max Howell 2009-08-21 20:30:13 +01:00
parent e7594de797
commit 72bde8c583
4 changed files with 114 additions and 99 deletions

View File

@ -17,6 +17,7 @@
#
require 'osx/cocoa' # to get number of cores
require 'formula'
require 'download_strategy'
require 'hw.model'
ENV['MACOSX_DEPLOYMENT_TARGET']='10.5'

View File

@ -0,0 +1,109 @@
# Copyright 2009 Max Howell <max@methylblue.com>
#
# This file is part of Homebrew.
#
# Homebrew is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Homebrew is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Homebrew. If not, see <http://www.gnu.org/licenses/>.
#
class AbstractDownloadStrategy
def initialize url, name, version
@url=url
@unique_token="#{name}-#{version}"
end
end
class HttpDownloadStrategy <AbstractDownloadStrategy
def fetch
ohai "Downloading #{@url}"
@dl=HOMEBREW_CACHE+(@unique_token+ext)
unless @dl.exist?
curl @url, '-o', @dl
else
puts "File already downloaded and cached"
end
return @dl # thus performs checksum verification
end
def stage
case `file -b #{@dl}`
when /^Zip archive data/
safe_system 'unzip', '-qq', @dl
chdir
when /^(gzip|bzip2) compressed data/
# TODO do file -z now to see if it is in fact a tar
safe_system 'tar', 'xf', @dl
chdir
else
# we are assuming it is not an archive, use original filename
# this behaviour is due to ScriptFileFormula expectations
@dl.mv File.basename(@url)
end
end
private
def chdir
entries=Dir['*']
case entries.length
when 0 then raise "Empty archive"
when 1 then Dir.chdir entries.first rescue nil
end
end
def ext
# GitHub uses odd URLs for zip files, so check for those
rx=%r[http://(www\.)?github\.com/.*/(zip|tar)ball/]
if rx.match @url
if $2 == 'zip'
'.zip'
else
'.tgz'
end
else
Pathname.new(@url).extname
end
end
end
class SubversionDownloadStrategy <AbstractDownloadStrategy
def fetch
ohai "Checking out #{@url}"
@co=HOMEBREW_CACHE+@unique_token
unless @co.exist?
safe_system 'svn', 'checkout', @url, @co
else
# TODO svn up?
puts "Repository already checked out"
end
end
def stage
# Force the export, since the target directory will already exist
safe_system 'svn', 'export', '--force', @co, Dir.pwd
end
end
class GitDownloadStrategy <AbstractDownloadStrategy
def fetch
ohai "Cloning #{@url}"
@clone=HOMEBREW_CACHE+@unique_token
unless @clone.exist?
safe_system 'git', 'clone', @url, @clone
else
# TODO git pull?
puts "Repository already cloned"
end
end
def stage
dst=Dir.getwd
Dir.chdir @clone do
# http://stackoverflow.com/questions/160608/how-to-do-a-git-export-like-svn-export
safe_system 'git', 'checkout-index', '-af', "--prefix=#{dst}"
end
end
end

View File

@ -14,110 +14,14 @@
#
# You should have received a copy of the GNU General Public License
# along with Homebrew. If not, see <http://www.gnu.org/licenses/>.
class AbstractDownloadStrategy
def initialize url, name, version
@url=url
@unique_token="#{name}-#{version}"
end
end
class HttpDownloadStrategy <AbstractDownloadStrategy
def fetch
ohai "Downloading #{@url}"
@dl=HOMEBREW_CACHE+(@unique_token+ext)
unless @dl.exist?
curl @url, '-o', @dl
else
puts "File already downloaded and cached"
end
return @dl # thus performs checksum verification
end
def stage
case `file -b #{@dl}`
when /^Zip archive data/
safe_system 'unzip', '-qq', @dl
chdir
when /^(gzip|bzip2) compressed data/
# TODO do file -z now to see if it is in fact a tar
safe_system 'tar', 'xf', @dl
chdir
else
# we are assuming it is not an archive, use original filename
# this behaviour is due to ScriptFileFormula expectations
@dl.mv File.basename(@url)
end
end
private
def chdir
entries=Dir['*']
case entries.length
when 0 then raise "Empty archive"
when 1 then Dir.chdir entries.first rescue nil
end
end
def ext
# GitHub uses odd URLs for zip files, so check for those
rx=%r[http://(www\.)?github\.com/.*/(zip|tar)ball/]
if rx.match @url
if $2 == 'zip'
'.zip'
else
'.tgz'
end
else
Pathname.new(@url).extname
end
end
end
class SubversionDownloadStrategy <AbstractDownloadStrategy
def fetch
ohai "Checking out #{@url}"
@co=HOMEBREW_CACHE+@unique_token
unless @co.exist?
safe_system 'svn', 'checkout', @url, @co
else
# TODO svn up?
puts "Repository already checked out"
end
end
def stage
# Force the export, since the target directory will already exist
safe_system 'svn', 'export', '--force', @co, Dir.pwd
end
end
class GitDownloadStrategy <AbstractDownloadStrategy
def fetch
ohai "Cloning #{@url}"
@clone=HOMEBREW_CACHE+@unique_token
unless @clone.exist?
safe_system 'git', 'clone', @url, @clone
else
# TODO git pull?
puts "Repository already cloned"
end
end
def stage
dst=Dir.getwd
Dir.chdir @clone do
# http://stackoverflow.com/questions/160608/how-to-do-a-git-export-like-svn-export
safe_system 'git', 'checkout-index', '-af', "--prefix=#{dst}"
end
end
end
#
class ExecutionError <RuntimeError
def initialize cmd, args=[]
super "#{cmd} #{args*' '}"
end
end
class BuildError <ExecutionError; end
class BuildError <ExecutionError
end
class FormulaUnavailableError <RuntimeError
def initialize name
super "No available formula for #{name}"

View File

@ -2,6 +2,7 @@
$:.unshift File.dirname(__FILE__)
require 'pathname+yeast'
require 'formula'
require 'download_strategy'
require 'keg'
require 'utils'