diff options
| author | Adam Vandenberg | 2012-03-09 10:26:46 -0800 |
|---|---|---|
| committer | Adam Vandenberg | 2012-03-19 18:50:17 -0700 |
| commit | b8196a41036a7d6284f7f1abefcf0eaaa25f546e (patch) | |
| tree | ebda46f50565d805e9154df9a78dfb2942dc348d /Library | |
| parent | 2c4aa75e1f5326cc189e3ecb29b788db6d4f5d18 (diff) | |
| download | homebrew-b8196a41036a7d6284f7f1abefcf0eaaa25f546e.tar.bz2 | |
Add Patches class
Diffstat (limited to 'Library')
| -rw-r--r-- | Library/Homebrew/patches.rb | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/Library/Homebrew/patches.rb b/Library/Homebrew/patches.rb new file mode 100644 index 000000000..34a59cf2b --- /dev/null +++ b/Library/Homebrew/patches.rb @@ -0,0 +1,103 @@ +class Patches + # The patches defined in a formula and the DATA from that file + def initialize patches + @patches = [] + return if patches.nil? + + normalize_patches(patches).each do |patch_p, urls| + # Wrap the urls list in an array if it isn't already; + # DATA.each does each line, which doesn't work so great + urls = [urls] unless urls.kind_of? Array + urls.each_with_index do |url, n| + @patches << Patch.new(patch_p, '%03d-homebrew.diff' % n, url) + end + end + end + + # Collects the urls and output names of all external patches + def external_curl_args + @patches.select{|p| p.external?}.collect{|p| p.curl_args}.flatten + end + + def each(&blk) + @patches.each(&blk) + end + def empty? + @patches.empty? + end + +private + + def normalize_patches patches + if patches.kind_of? Hash + patches + else + { :p1 => patches } # We assume -p1 + end + end + +end + +class Patch + attr_reader :compression + attr_reader :url + + def initialize patch_p, filename, url + @patch_p = patch_p + @patch_filename = filename + @compression = false + @url = nil + + if url.kind_of? File # true when DATA is passed + write_data url + elsif looks_like_url(url) + @download_filename = @patch_filename + @url = url # Save URL + case @url + when /\.gz$/ + @compression = :gzip + @download_filename += '.gz' + when /\.bz2$/ + @compression = :bzip2 + @download_filename += '.bz2' + end + else + # it's a file on the local filesystem + # use URL as the filename for patch + @patch_filename = url + end + end + + def external? + !!@url + end + + def compressed? + !!@compression + end + + def patch_args + ["-#{@patch_p}", '-i', @patch_filename] + end + + def curl_args + [@url, '-o', @download_filename] + end + +private + + # Write the given file object (DATA) out to a local file for patch + def write_data f + pn = Pathname.new @patch_filename + pn.write(brew_var_substitution f.read.to_s) + end + + # Do any supported substitutions of HOMEBREW vars in a DATA patch + def brew_var_substitution s + s.gsub("HOMEBREW_PREFIX", HOMEBREW_PREFIX) + end + + def looks_like_url str + str =~ %r[^\w+\://] + end +end |
