diff options
| author | Max Howell | 2009-08-21 20:30:13 +0100 |
|---|---|---|
| committer | Max Howell | 2009-08-24 01:04:53 +0100 |
| commit | a0d029cfbc9ad68dcb18aeacb8383d72c4219fcb (patch) | |
| tree | cfc538e213d2b5becf8cfc7e90cbac1a56afa1d9 /Library | |
| parent | 50453eec097d20cd7f69c95b3733e2085ffac4a0 (diff) | |
| download | homebrew-a0d029cfbc9ad68dcb18aeacb8383d72c4219fcb.tar.bz2 | |
Move download strategies into their own file
Diffstat (limited to 'Library')
| -rw-r--r-- | Library/Homebrew/brewkit.rb | 1 | ||||
| -rw-r--r-- | Library/Homebrew/download_strategy.rb | 109 | ||||
| -rw-r--r-- | Library/Homebrew/formula.rb | 102 | ||||
| -rwxr-xr-x | Library/Homebrew/unittest.rb | 1 |
4 files changed, 114 insertions, 99 deletions
diff --git a/Library/Homebrew/brewkit.rb b/Library/Homebrew/brewkit.rb index c8702c4b1..8af00b5af 100644 --- a/Library/Homebrew/brewkit.rb +++ b/Library/Homebrew/brewkit.rb @@ -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' diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb new file mode 100644 index 000000000..14c22468c --- /dev/null +++ b/Library/Homebrew/download_strategy.rb @@ -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 diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 0fea6a607..ff4516777 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -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}" diff --git a/Library/Homebrew/unittest.rb b/Library/Homebrew/unittest.rb index 65ac81d87..3bc0b6f61 100755 --- a/Library/Homebrew/unittest.rb +++ b/Library/Homebrew/unittest.rb @@ -2,6 +2,7 @@ $:.unshift File.dirname(__FILE__) require 'pathname+yeast' require 'formula' +require 'download_strategy' require 'keg' require 'utils' |
