| 
									
										
										
										
											2013-03-11 16:41:08 +01:00
										 |  |  | require 'fileutils' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FormulaPin | 
					
						
							| 
									
										
										
										
											2013-04-16 01:43:26 -05:00
										 |  |  |   PINDIR = Pathname.new("#{HOMEBREW_LIBRARY}/PinnedKegs") | 
					
						
							| 
									
										
										
										
											2013-03-11 16:41:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-16 01:43:26 -05:00
										 |  |  |   def initialize(f) | 
					
						
							|  |  |  |     @f = f | 
					
						
							| 
									
										
										
										
											2013-04-14 21:32:30 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def path | 
					
						
							| 
									
										
										
										
											2013-04-16 01:43:26 -05:00
										 |  |  |     Pathname.new("#{PINDIR}/#{@f.name}") | 
					
						
							| 
									
										
										
										
											2013-03-11 16:41:08 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def pin_at(version) | 
					
						
							| 
									
										
										
										
											2013-04-16 01:43:26 -05:00
										 |  |  |     PINDIR.mkpath unless PINDIR.exist? | 
					
						
							|  |  |  |     version_path = @f.rack.join(version) | 
					
						
							|  |  |  |     FileUtils.ln_s(version_path, path) unless pinned? or not version_path.exist? | 
					
						
							| 
									
										
										
										
											2013-03-11 16:41:08 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def pin | 
					
						
							| 
									
										
										
										
											2013-04-16 01:43:26 -05:00
										 |  |  |     versions = @f.rack.children.map { |item| item.basename.to_s } | 
					
						
							| 
									
										
										
										
											2013-03-11 16:41:08 +01:00
										 |  |  |     version = versions.map { |item| Version.new(item) }.sort[0].to_s | 
					
						
							|  |  |  |     pin_at(version) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def unpin | 
					
						
							| 
									
										
										
										
											2013-04-16 01:43:26 -05:00
										 |  |  |     FileUtils.rm(path) if pinned? | 
					
						
							| 
									
										
										
										
											2013-03-11 16:41:08 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def pinned? | 
					
						
							| 
									
										
										
										
											2013-04-14 21:32:30 -05:00
										 |  |  |     path.symlink? | 
					
						
							| 
									
										
										
										
											2013-03-11 16:41:08 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-16 01:43:26 -05:00
										 |  |  |   def pinnable? | 
					
						
							|  |  |  |     @f.rack.exist? && @f.rack.children.length > 0
 | 
					
						
							| 
									
										
										
										
											2013-03-11 16:41:08 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | end |