| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 18:22:53 -05:00
										 |  |  | module Utils | 
					
						
							| 
									
										
										
										
											2020-08-19 08:18:57 +02:00
										 |  |  |   # Helper functions for creating symlinks. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api private | 
					
						
							| 
									
										
										
										
											2016-09-30 18:22:53 -05:00
										 |  |  |   module Link | 
					
						
							|  |  |  |     module_function | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def link_src_dst_dirs(src_dir, dst_dir, command, link_dir: false) | 
					
						
							|  |  |  |       return unless src_dir.exist? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 18:22:53 -05:00
										 |  |  |       conflicts = [] | 
					
						
							|  |  |  |       src_paths = link_dir ? [src_dir] : src_dir.find | 
					
						
							|  |  |  |       src_paths.each do |src| | 
					
						
							|  |  |  |         next if src.directory? && !link_dir | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 18:22:53 -05:00
										 |  |  |         dst = dst_dir/src.relative_path_from(src_dir) | 
					
						
							|  |  |  |         if dst.symlink? | 
					
						
							|  |  |  |           next if src == dst.resolved_path | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 18:22:53 -05:00
										 |  |  |           dst.unlink | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         if dst.exist? | 
					
						
							|  |  |  |           conflicts << dst | 
					
						
							|  |  |  |           next | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         dst_dir.parent.mkpath | 
					
						
							|  |  |  |         dst.make_relative_symlink(src) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return if conflicts.empty? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |       onoe <<~EOS | 
					
						
							| 
									
										
										
										
											2016-09-30 18:22:53 -05:00
										 |  |  |         Could not link: | 
					
						
							|  |  |  |         #{conflicts.join("\n")} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-24 21:25:12 -05:00
										 |  |  |         Please delete these paths and run: | 
					
						
							|  |  |  |           #{command} | 
					
						
							| 
									
										
										
										
											2016-09-30 18:22:53 -05:00
										 |  |  |       EOS | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     private_class_method :link_src_dst_dirs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def unlink_src_dst_dirs(src_dir, dst_dir, unlink_dir: false) | 
					
						
							|  |  |  |       return unless src_dir.exist? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 18:22:53 -05:00
										 |  |  |       src_paths = unlink_dir ? [src_dir] : src_dir.find | 
					
						
							|  |  |  |       src_paths.each do |src| | 
					
						
							|  |  |  |         next if src.directory? && !unlink_dir | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 18:22:53 -05:00
										 |  |  |         dst = dst_dir/src.relative_path_from(src_dir) | 
					
						
							|  |  |  |         dst.delete if dst.symlink? && src == dst.resolved_path | 
					
						
							|  |  |  |         dst.parent.rmdir_if_possible | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     private_class_method :unlink_src_dst_dirs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def link_manpages(path, command) | 
					
						
							|  |  |  |       link_src_dst_dirs(path/"manpages", HOMEBREW_PREFIX/"share/man/man1", command) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def unlink_manpages(path) | 
					
						
							|  |  |  |       unlink_src_dst_dirs(path/"manpages", HOMEBREW_PREFIX/"share/man/man1") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def link_completions(path, command) | 
					
						
							|  |  |  |       link_src_dst_dirs(path/"completions/bash", HOMEBREW_PREFIX/"etc/bash_completion.d", command) | 
					
						
							|  |  |  |       link_src_dst_dirs(path/"completions/zsh", HOMEBREW_PREFIX/"share/zsh/site-functions", command) | 
					
						
							|  |  |  |       link_src_dst_dirs(path/"completions/fish", HOMEBREW_PREFIX/"share/fish/vendor_completions.d", command) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def unlink_completions(path) | 
					
						
							|  |  |  |       unlink_src_dst_dirs(path/"completions/bash", HOMEBREW_PREFIX/"etc/bash_completion.d") | 
					
						
							|  |  |  |       unlink_src_dst_dirs(path/"completions/zsh", HOMEBREW_PREFIX/"share/zsh/site-functions") | 
					
						
							|  |  |  |       unlink_src_dst_dirs(path/"completions/fish", HOMEBREW_PREFIX/"share/fish/vendor_completions.d") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def link_docs(path, command) | 
					
						
							|  |  |  |       link_src_dst_dirs(path/"docs", HOMEBREW_PREFIX/"share/doc/homebrew", command, link_dir: true) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |