| 
									
										
										
										
											2011-09-22 20:07:39 -07:00
										 |  |  | require 'ostruct' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require 'formula' | 
					
						
							|  |  |  | require 'vendor/multi_json' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # 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 | 
					
						
							|  |  |  | # `Tab.for_install`. | 
					
						
							|  |  |  | class Tab < OpenStruct | 
					
						
							|  |  |  |   def self.for_install f, args | 
					
						
							|  |  |  |     # Retrieve option flags from command line. | 
					
						
							|  |  |  |     arg_options = args.options_only | 
					
						
							|  |  |  |     # Pick off the option flags from the formula's `options` array by | 
					
						
							|  |  |  |     # discarding the descriptions. | 
					
						
							|  |  |  |     formula_options = f.options.map { |o, _| o } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Tab.new :used_options => formula_options & arg_options, | 
					
						
							|  |  |  |             :unused_options => formula_options - arg_options, | 
					
						
							| 
									
										
										
										
											2012-03-16 12:29:47 +00:00
										 |  |  |             :tabfile => f.prefix + "INSTALL_RECEIPT.json", | 
					
						
							|  |  |  |             :built_bottle => !!args.build_bottle?, | 
					
						
							|  |  |  |             :tapped_from => f.tap | 
					
						
							| 
									
										
										
										
											2011-09-22 20:07:39 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-23 08:36:40 -07:00
										 |  |  |   def self.from_file path | 
					
						
							|  |  |  |     tab = Tab.new MultiJson.decode(open(path).read) | 
					
						
							|  |  |  |     tab.tabfile = path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return tab | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-01 15:22:36 -06:00
										 |  |  |   def self.for_keg keg | 
					
						
							|  |  |  |     path = keg+'INSTALL_RECEIPT.json' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if path.exist? | 
					
						
							|  |  |  |       self.from_file path | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2012-01-05 11:57:24 -06:00
										 |  |  |       begin | 
					
						
							|  |  |  |         self.dummy_tab Formula.factory(keg.parent.basename) | 
					
						
							|  |  |  |       rescue FormulaUnavailableError | 
					
						
							| 
									
										
										
										
											2012-01-16 19:21:04 +00:00
										 |  |  |         Tab.new :used_options => [], | 
					
						
							|  |  |  |                 :unused_options => [], | 
					
						
							| 
									
										
										
										
											2012-03-16 12:29:47 +00:00
										 |  |  |                 :built_bottle => false, | 
					
						
							|  |  |  |                 :tapped_from => "" | 
					
						
							| 
									
										
										
										
											2012-01-05 11:57:24 -06:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2012-01-01 15:22:36 -06:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-23 08:36:40 -07:00
										 |  |  |   def self.for_formula f | 
					
						
							|  |  |  |     f = Formula.factory f unless f.kind_of? Formula | 
					
						
							| 
									
										
										
										
											2012-02-10 17:21:48 -06:00
										 |  |  |     path = f.linked_keg/'INSTALL_RECEIPT.json' | 
					
						
							| 
									
										
										
										
											2011-09-23 08:36:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if path.exist? | 
					
						
							|  |  |  |       self.from_file path | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       # Really should bail out with an error if a formula was not installed | 
					
						
							|  |  |  |       # with a Tab. However, there will be lots of legacy installs that have no | 
					
						
							|  |  |  |       # receipt---so we fabricate one that claims the formula was installed with | 
					
						
							|  |  |  |       # no options. | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # TODO: | 
					
						
							|  |  |  |       # This isn't the best behavior---perhaps a future version of Homebrew can | 
					
						
							|  |  |  |       # treat missing Tabs as errors. | 
					
						
							| 
									
										
										
										
											2012-01-01 15:22:36 -06:00
										 |  |  |       self.dummy_tab f | 
					
						
							| 
									
										
										
										
											2011-09-23 08:36:40 -07:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-01 15:22:36 -06:00
										 |  |  |   def self.dummy_tab f | 
					
						
							|  |  |  |     Tab.new :used_options => [], | 
					
						
							| 
									
										
										
										
											2012-01-16 19:21:04 +00:00
										 |  |  |             :unused_options => f.options.map { |o, _| o}, | 
					
						
							| 
									
										
										
										
											2012-03-16 12:29:47 +00:00
										 |  |  |             :built_bottle => false, | 
					
						
							|  |  |  |             :tapped_from => "" | 
					
						
							| 
									
										
										
										
											2012-01-01 15:22:36 -06:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-23 08:36:40 -07:00
										 |  |  |   def installed_with? opt | 
					
						
							|  |  |  |     used_options.include? opt | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def options | 
					
						
							|  |  |  |     used_options + unused_options | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-22 20:07:39 -07:00
										 |  |  |   def to_json | 
					
						
							|  |  |  |     MultiJson.encode({ | 
					
						
							|  |  |  |       :used_options => used_options, | 
					
						
							| 
									
										
										
										
											2012-01-16 19:21:04 +00:00
										 |  |  |       :unused_options => unused_options, | 
					
						
							| 
									
										
										
										
											2012-03-16 12:29:47 +00:00
										 |  |  |       :built_bottle => built_bottle, | 
					
						
							|  |  |  |       :tapped_from => tapped_from | 
					
						
							| 
									
										
										
										
											2011-09-22 20:07:39 -07:00
										 |  |  |     }) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def write | 
					
						
							|  |  |  |     tabfile.write to_json | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |