| 
									
										
										
										
											2013-07-27 00:11:45 -07:00
										 |  |  | require 'cxxstdlib' | 
					
						
							| 
									
										
										
										
											2011-09-22 20:07:39 -07:00
										 |  |  | require 'ostruct' | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:23 -06:00
										 |  |  | require 'options' | 
					
						
							| 
									
										
										
										
											2013-06-22 16:51:08 -05:00
										 |  |  | require 'utils/json' | 
					
						
							| 
									
										
										
										
											2011-09-22 20:07:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Inherit from OpenStruct to gain a generic initialization method that takes a | 
					
						
							|  |  |  | # hash and creates an attribute for each key and value. `Tab.new` probably | 
					
						
							|  |  |  | # should not be called directly, instead use one of the class methods like | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:20 -06:00
										 |  |  | # `Tab.create`. | 
					
						
							| 
									
										
										
										
											2011-09-22 20:07:39 -07:00
										 |  |  | class Tab < OpenStruct | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:20 -06:00
										 |  |  |   FILENAME = 'INSTALL_RECEIPT.json' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-22 22:21:59 -07:00
										 |  |  |   def self.create f, compiler, stdlib, args | 
					
						
							| 
									
										
										
										
											2014-03-02 20:35:41 -06:00
										 |  |  |     build = f.build.dup | 
					
						
							|  |  |  |     build.args = args | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:20 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:20 -06:00
										 |  |  |     sha = HOMEBREW_REPOSITORY.cd do | 
					
						
							|  |  |  |       `git rev-parse --verify -q HEAD 2>/dev/null`.chuzzle | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-02 20:35:41 -06:00
										 |  |  |     Tab.new :used_options => build.used_options, | 
					
						
							|  |  |  |             :unused_options => build.unused_options, | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:20 -06:00
										 |  |  |             :tabfile => f.prefix.join(FILENAME), | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:20 -06:00
										 |  |  |             :built_as_bottle => !!ARGV.build_bottle?, | 
					
						
							| 
									
										
										
										
											2013-03-15 00:28:18 +00:00
										 |  |  |             :poured_from_bottle => false, | 
					
						
							| 
									
										
										
										
											2012-08-10 16:06:51 -04:00
										 |  |  |             :tapped_from => f.tap, | 
					
						
							| 
									
										
										
										
											2014-03-22 13:16:16 -05:00
										 |  |  |             :time => Time.now.to_i, | 
					
						
							| 
									
										
										
										
											2013-07-27 00:11:45 -07:00
										 |  |  |             :HEAD => sha, | 
					
						
							| 
									
										
										
										
											2013-10-22 22:21:59 -07:00
										 |  |  |             :compiler => compiler, | 
					
						
							|  |  |  |             :stdlib => stdlib | 
					
						
							| 
									
										
										
										
											2011-09-22 20:07:39 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-23 08:36:40 -07:00
										 |  |  |   def self.from_file path | 
					
						
							| 
									
										
										
										
											2013-10-23 23:22:28 -05:00
										 |  |  |     tab = Tab.new Utils::JSON.load(File.read(path)) | 
					
						
							| 
									
										
										
										
											2014-03-22 11:47:48 -05:00
										 |  |  |     tab.tabfile = path.realpath | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:20 -06:00
										 |  |  |     tab | 
					
						
							| 
									
										
										
										
											2011-09-23 08:36:40 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-01 15:22:36 -06:00
										 |  |  |   def self.for_keg keg | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:20 -06:00
										 |  |  |     path = keg.join(FILENAME) | 
					
						
							| 
									
										
										
										
											2012-01-01 15:22:36 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if path.exist? | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:20 -06:00
										 |  |  |       self.from_file(path) | 
					
						
							| 
									
										
										
										
											2012-01-01 15:22:36 -06:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:20 -06:00
										 |  |  |       self.dummy_tab | 
					
						
							| 
									
										
										
										
											2012-01-01 15:22:36 -06:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-26 10:37:09 -07:00
										 |  |  |   def self.for_name name | 
					
						
							|  |  |  |     for_formula(Formula.factory(name)) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-23 08:36:40 -07:00
										 |  |  |   def self.for_formula f | 
					
						
							| 
									
										
										
										
											2014-03-22 11:34:26 -05:00
										 |  |  |     paths = [f.opt_prefix, f.linked_keg] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if f.rack.directory? && (dirs = f.rack.subdirs).length == 1
 | 
					
						
							|  |  |  |       paths << dirs.first | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     paths << f.prefix | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     path = paths.map { |pn| pn.join(FILENAME) }.find(&:file?) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if path | 
					
						
							|  |  |  |       from_file(path) | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       dummy_tab(f) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-23 08:36:40 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:20 -06:00
										 |  |  |   def self.dummy_tab f=nil | 
					
						
							| 
									
										
										
										
											2012-01-01 15:22:36 -06:00
										 |  |  |     Tab.new :used_options => [], | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:20 -06:00
										 |  |  |             :unused_options => (f.build.as_flags rescue []), | 
					
						
							| 
									
										
										
										
											2012-08-25 11:31:57 -07:00
										 |  |  |             :built_as_bottle => false, | 
					
						
							| 
									
										
										
										
											2013-03-15 00:28:18 +00:00
										 |  |  |             :poured_from_bottle => false, | 
					
						
							| 
									
										
										
										
											2012-08-10 16:06:51 -04:00
										 |  |  |             :tapped_from => "", | 
					
						
							|  |  |  |             :time => nil, | 
					
						
							| 
									
										
										
										
											2013-07-27 00:11:45 -07:00
										 |  |  |             :HEAD => nil, | 
					
						
							|  |  |  |             :compiler => :clang | 
					
						
							| 
									
										
										
										
											2012-01-01 15:22:36 -06:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:24 -06:00
										 |  |  |   def with? name | 
					
						
							|  |  |  |     if options.include? "with-#{name}" | 
					
						
							|  |  |  |       used_options.include? "with-#{name}" | 
					
						
							|  |  |  |     elsif options.include? "without-#{name}" | 
					
						
							|  |  |  |       not used_options.include? "without-#{name}" | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       false | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def include? opt | 
					
						
							| 
									
										
										
										
											2011-09-23 08:36:40 -07:00
										 |  |  |     used_options.include? opt | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:24 -06:00
										 |  |  |   def universal? | 
					
						
							|  |  |  |     used_options.include? "universal" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:23 -06:00
										 |  |  |   def used_options | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:28 -06:00
										 |  |  |     Options.coerce(super) | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:23 -06:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def unused_options | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:28 -06:00
										 |  |  |     Options.coerce(super) | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:23 -06:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-23 08:36:40 -07:00
										 |  |  |   def options | 
					
						
							|  |  |  |     used_options + unused_options | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-27 00:11:45 -07:00
										 |  |  |   def cxxstdlib | 
					
						
							|  |  |  |     # Older tabs won't have these values, so provide sensible defaults | 
					
						
							| 
									
										
										
										
											2013-10-07 00:25:26 -07:00
										 |  |  |     lib = stdlib.to_sym if stdlib | 
					
						
							| 
									
										
										
										
											2013-07-27 00:11:45 -07:00
										 |  |  |     cc = compiler || MacOS.default_compiler | 
					
						
							| 
									
										
										
										
											2013-10-07 00:25:26 -07:00
										 |  |  |     CxxStdlib.new(lib, cc.to_sym) | 
					
						
							| 
									
										
										
										
											2013-07-27 00:11:45 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-22 20:07:39 -07:00
										 |  |  |   def to_json | 
					
						
							| 
									
										
										
										
											2013-06-22 16:51:08 -05:00
										 |  |  |     Utils::JSON.dump({ | 
					
						
							| 
									
										
										
										
											2013-06-22 16:51:09 -05:00
										 |  |  |       :used_options => used_options.map(&:to_s), | 
					
						
							|  |  |  |       :unused_options => unused_options.map(&:to_s), | 
					
						
							| 
									
										
										
										
											2012-08-25 11:31:57 -07:00
										 |  |  |       :built_as_bottle => built_as_bottle, | 
					
						
							| 
									
										
										
										
											2013-03-15 00:28:18 +00:00
										 |  |  |       :poured_from_bottle => poured_from_bottle, | 
					
						
							| 
									
										
										
										
											2012-08-10 16:06:51 -04:00
										 |  |  |       :tapped_from => tapped_from, | 
					
						
							|  |  |  |       :time => time, | 
					
						
							| 
									
										
										
										
											2014-03-22 13:16:16 -05:00
										 |  |  |       :HEAD => self.HEAD, | 
					
						
							| 
									
										
										
										
											2013-07-27 00:11:45 -07:00
										 |  |  |       :stdlib => (stdlib.to_s if stdlib), | 
					
						
							|  |  |  |       :compiler => (compiler.to_s if compiler)}) | 
					
						
							| 
									
										
										
										
											2011-09-22 20:07:39 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def write | 
					
						
							| 
									
										
										
										
											2014-03-22 11:13:33 -05:00
										 |  |  |     tabfile.atomic_write(to_json) | 
					
						
							| 
									
										
										
										
											2011-09-22 20:07:39 -07:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2013-03-27 23:07:25 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def to_s | 
					
						
							|  |  |  |     s = [] | 
					
						
							|  |  |  |     case poured_from_bottle | 
					
						
							|  |  |  |     when true  then s << "Poured from bottle" | 
					
						
							|  |  |  |     when false then s << "Built from source" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     unless used_options.empty? | 
					
						
							|  |  |  |       s << "Installed" if s.empty? | 
					
						
							|  |  |  |       s << "with:" | 
					
						
							|  |  |  |       s << used_options.to_a.join(", ") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     s.join(" ") | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-09-22 20:07:39 -07:00
										 |  |  | end |