| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											2018-04-10 18:37:15 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 11:00:51 +01:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-30 18:25:38 +05:30
										 |  |  |   def mirror_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2018-09-28 21:39:52 +05:30
										 |  |  |       usage_banner <<~EOS | 
					
						
							| 
									
										
										
										
											2019-01-30 21:33:03 +00:00
										 |  |  |         `mirror` <formula> | 
					
						
							| 
									
										
										
										
											2018-10-02 19:54:22 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-02 14:44:38 +05:30
										 |  |  |         Reuploads the stable URL for a formula to Bintray to use it as a mirror. | 
					
						
							| 
									
										
										
										
											2018-09-28 21:39:52 +05:30
										 |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2018-04-01 16:47:30 +05:30
										 |  |  |       switch :verbose | 
					
						
							| 
									
										
										
										
											2018-10-08 22:49:03 -04:00
										 |  |  |       switch :debug | 
					
						
							| 
									
										
										
										
											2019-01-30 21:33:03 +00:00
										 |  |  |       hide_from_man_page! | 
					
						
							| 
									
										
										
										
											2018-04-01 16:47:30 +05:30
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-07-30 18:25:38 +05:30
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def mirror | 
					
						
							|  |  |  |     mirror_args.parse | 
					
						
							| 
									
										
										
										
											2018-04-01 16:47:30 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-08 22:49:03 -04:00
										 |  |  |     odie "This command requires at least one formula argument!" if ARGV.named.empty? | 
					
						
							| 
									
										
										
										
											2016-05-20 11:00:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-22 16:31:19 +01:00
										 |  |  |     bintray_user = ENV["HOMEBREW_BINTRAY_USER"] | 
					
						
							|  |  |  |     bintray_key = ENV["HOMEBREW_BINTRAY_KEY"] | 
					
						
							| 
									
										
										
										
											2019-02-19 13:11:32 +00:00
										 |  |  |     raise "Missing HOMEBREW_BINTRAY_USER or HOMEBREW_BINTRAY_KEY variables!" if !bintray_user || !bintray_key | 
					
						
							| 
									
										
										
										
											2016-05-20 11:00:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ARGV.formulae.each do |f| | 
					
						
							|  |  |  |       bintray_package = Utils::Bottles::Bintray.package f.name | 
					
						
							|  |  |  |       bintray_repo_url = "https://api.bintray.com/packages/homebrew/mirror" | 
					
						
							|  |  |  |       package_url = "#{bintray_repo_url}/#{bintray_package}" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-25 06:33:44 -07:00
										 |  |  |       unless system curl_executable, "--silent", "--fail", "--output", "/dev/null", package_url | 
					
						
							| 
									
										
										
										
											2018-07-11 15:17:40 +02:00
										 |  |  |         package_blob = <<~JSON | 
					
						
							| 
									
										
										
										
											2016-05-20 11:00:51 +01:00
										 |  |  |           {"name": "#{bintray_package}", | 
					
						
							|  |  |  |            "public_download_numbers": true, | 
					
						
							|  |  |  |            "public_stats": true} | 
					
						
							| 
									
										
										
										
											2018-07-11 15:17:40 +02:00
										 |  |  |         JSON | 
					
						
							| 
									
										
										
										
											2017-08-08 18:10:13 +02:00
										 |  |  |         curl "--silent", "--fail", "--user", "#{bintray_user}:#{bintray_key}", | 
					
						
							|  |  |  |              "--header", "Content-Type: application/json", | 
					
						
							|  |  |  |              "--data", package_blob, bintray_repo_url | 
					
						
							| 
									
										
										
										
											2016-05-20 11:00:51 +01:00
										 |  |  |         puts | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-01 10:19:55 +02:00
										 |  |  |       downloader = f.downloader | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       downloader.fetch | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       filename = downloader.basename | 
					
						
							| 
									
										
										
										
											2016-05-20 11:00:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-01 10:19:55 +02:00
										 |  |  |       destination_url = "https://dl.bintray.com/homebrew/mirror/#{filename}" | 
					
						
							| 
									
										
										
										
											2016-05-20 11:00:51 +01:00
										 |  |  |       ohai "Uploading to #{destination_url}" | 
					
						
							| 
									
										
										
										
											2018-10-01 10:19:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       content_url = | 
					
						
							|  |  |  |         "https://api.bintray.com/content/homebrew/mirror/#{bintray_package}/#{f.pkg_version}/#{filename}?publish=1" | 
					
						
							| 
									
										
										
										
											2017-08-08 18:10:13 +02:00
										 |  |  |       curl "--silent", "--fail", "--user", "#{bintray_user}:#{bintray_key}", | 
					
						
							| 
									
										
										
										
											2018-10-01 10:19:55 +02:00
										 |  |  |            "--upload-file", downloader.cached_location, content_url | 
					
						
							| 
									
										
										
										
											2016-05-20 11:00:51 +01:00
										 |  |  |       puts | 
					
						
							|  |  |  |       ohai "Mirrored #{filename}!" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |