| 
									
										
										
										
											2016-10-08 13:25:38 +02:00
										 |  |  | module Hbc | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |   module CaskLoader | 
					
						
							|  |  |  |     class FromContentLoader | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |       attr_reader :content | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       def initialize(content) | 
					
						
							|  |  |  |         @content = content | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def load | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |         instance_eval(content.force_encoding("UTF-8"), __FILE__, __LINE__) | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-10-08 13:25:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       private | 
					
						
							| 
									
										
										
										
											2016-10-08 13:25:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       def cask(header_token, &block) | 
					
						
							|  |  |  |         Cask.new(header_token, &block) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-10-08 13:25:38 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |     class FromPathLoader < FromContentLoader | 
					
						
							|  |  |  |       def self.can_load?(ref) | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |         path = Pathname(ref) | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |         path.extname == ".rb" && path.expand_path.exist? | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       attr_reader :token, :path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def initialize(path) | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |         path = Pathname(path).expand_path | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         @token = path.basename(".rb").to_s | 
					
						
							|  |  |  |         @path = path | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def load | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |         raise CaskUnavailableError.new(token, "'#{path}' does not exist.")  unless path.exist? | 
					
						
							|  |  |  |         raise CaskUnavailableError.new(token, "'#{path}' is not readable.") unless path.readable? | 
					
						
							|  |  |  |         raise CaskUnavailableError.new(token, "'#{path}' is not a file.")   unless path.file? | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |         @content = IO.read(path) | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         super | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def cask(header_token, &block) | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |         if token != header_token | 
					
						
							|  |  |  |           raise CaskTokenMismatchError.new(token, header_token) | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |         Cask.new(header_token, sourcefile_path: path, &block) | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-10-08 13:25:38 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |     class FromURILoader < FromPathLoader | 
					
						
							|  |  |  |       def self.can_load?(ref) | 
					
						
							| 
									
										
										
										
											2017-05-29 16:35:30 -07:00
										 |  |  |         ref.to_s.match?(::URI.regexp) | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 02:00:59 +02:00
										 |  |  |       attr_reader :url | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       def initialize(url) | 
					
						
							| 
									
										
										
										
											2017-06-11 02:00:59 +02:00
										 |  |  |         @url = URI(url) | 
					
						
							|  |  |  |         super Hbc.cache/File.basename(@url.path) | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def load | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |         path.dirname.mkpath | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         begin | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |           ohai "Downloading #{url}." | 
					
						
							|  |  |  |           curl url, "-o", path | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |         rescue ErrorDuringExecution | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |           raise CaskUnavailableError.new(token, "Failed to download #{Formatter.url(url)}.") | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         super | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-10-08 13:25:38 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |     class FromTapLoader < FromPathLoader | 
					
						
							|  |  |  |       def self.can_load?(ref) | 
					
						
							| 
									
										
										
										
											2017-05-29 16:35:30 -07:00
										 |  |  |         ref.to_s.match?(HOMEBREW_TAP_CASK_REGEX) | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |       attr_reader :tap | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       def initialize(tapped_name) | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |         user, repo, token = tapped_name.split("/", 3) | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |         @tap = Tap.fetch(user, repo) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         super @tap.cask_dir/"#{token}.rb" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-10-08 13:25:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       def load | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |         tap.install unless tap.installed? | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         super | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class NullLoader < FromPathLoader | 
					
						
							|  |  |  |       def self.can_load?(*) | 
					
						
							|  |  |  |         true | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def initialize(ref) | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |         token = File.basename(ref, ".rb") | 
					
						
							|  |  |  |         super CaskLoader.default_path(token) | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def load | 
					
						
							| 
									
										
										
										
											2017-06-28 09:25:14 +02:00
										 |  |  |         raise CaskUnavailableError.new(token, "No Cask with this name exists.") | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.load_from_file(path) | 
					
						
							|  |  |  |       FromPathLoader.new(path).load | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.load_from_string(content) | 
					
						
							|  |  |  |       FromContentLoader.new(content).load | 
					
						
							| 
									
										
										
										
											2016-10-08 13:25:38 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |     def self.path(ref) | 
					
						
							|  |  |  |       self.for(ref).path | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.load(ref) | 
					
						
							|  |  |  |       self.for(ref).load | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.for(ref) | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         FromURILoader, | 
					
						
							|  |  |  |         FromTapLoader, | 
					
						
							|  |  |  |         FromPathLoader, | 
					
						
							|  |  |  |       ].each do |loader_class| | 
					
						
							|  |  |  |         return loader_class.new(ref) if loader_class.can_load?(ref) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if FromPathLoader.can_load?(default_path(ref)) | 
					
						
							|  |  |  |         return FromPathLoader.new(default_path(ref)) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-10-08 13:25:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  |       possible_tap_casks = tap_paths(ref) | 
					
						
							|  |  |  |       if possible_tap_casks.count == 1
 | 
					
						
							|  |  |  |         possible_tap_cask = possible_tap_casks.first | 
					
						
							|  |  |  |         return FromPathLoader.new(possible_tap_cask) | 
					
						
							| 
									
										
										
										
											2016-10-08 13:25:38 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-03-12 19:18:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       possible_installed_cask = Cask.new(ref) | 
					
						
							|  |  |  |       if possible_installed_cask.installed? | 
					
						
							|  |  |  |         return FromPathLoader.new(possible_installed_cask.installed_caskfile) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       NullLoader.new(ref) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.default_path(token) | 
					
						
							|  |  |  |       Hbc.default_tap.cask_dir/"#{token.to_s.downcase}.rb" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.tap_paths(token) | 
					
						
							|  |  |  |       Tap.map { |t| t.cask_dir/"#{token.to_s.downcase}.rb" } | 
					
						
							|  |  |  |          .select(&:exist?) | 
					
						
							| 
									
										
										
										
											2016-10-08 13:25:38 +02:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |