| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-23 10:14:18 +01:00
										 |  |  | require "context" | 
					
						
							| 
									
										
										
										
											2016-08-09 19:18:43 +01:00
										 |  |  | require "erb" | 
					
						
							| 
									
										
										
										
											2021-01-12 16:27:25 -05:00
										 |  |  | require "settings" | 
					
						
							| 
									
										
										
										
											2016-08-09 19:18:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  | module Utils | 
					
						
							| 
									
										
										
										
											2020-08-19 07:43:40 +02:00
										 |  |  |   # Helper module for fetching and reporting analytics data. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api private | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  |   module Analytics | 
					
						
							|  |  |  |     class << self | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       extend T::Sig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |       include Context | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  |       def report(type, metadata = {}) | 
					
						
							| 
									
										
										
										
											2020-02-22 17:13:51 +00:00
										 |  |  |         return if not_this_run? | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |         return if disabled? | 
					
						
							| 
									
										
										
										
											2016-03-28 09:16:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 18:50:03 +08:00
										 |  |  |         args = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # do not load .curlrc unless requested (must be the first argument) | 
					
						
							| 
									
										
										
										
											2020-04-05 15:44:50 +01:00
										 |  |  |         args << "--disable" unless Homebrew::EnvConfig.curlrc? | 
					
						
							| 
									
										
										
										
											2018-09-14 18:50:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         args += %W[
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  |           --max-time 3
 | 
					
						
							|  |  |  |           --user-agent #{HOMEBREW_USER_AGENT_CURL} | 
					
						
							| 
									
										
										
										
											2016-08-09 19:17:56 +01:00
										 |  |  |           --data v=1
 | 
					
						
							|  |  |  |           --data aip=1
 | 
					
						
							|  |  |  |           --data t=#{type} | 
					
						
							|  |  |  |           --data tid=#{ENV["HOMEBREW_ANALYTICS_ID"]} | 
					
						
							|  |  |  |           --data cid=#{ENV["HOMEBREW_ANALYTICS_USER_UUID"]} | 
					
						
							|  |  |  |           --data an=#{HOMEBREW_PRODUCT} | 
					
						
							|  |  |  |           --data av=#{HOMEBREW_VERSION} | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  |         ] | 
					
						
							| 
									
										
										
										
											2016-08-09 19:18:43 +01:00
										 |  |  |         metadata.each do |key, value| | 
					
						
							|  |  |  |           next unless key | 
					
						
							|  |  |  |           next unless value | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-09 19:18:43 +01:00
										 |  |  |           key = ERB::Util.url_encode key | 
					
						
							|  |  |  |           value = ERB::Util.url_encode value | 
					
						
							|  |  |  |           args << "--data" << "#{key}=#{value}" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-03-28 09:16:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  |         # Send analytics. Don't send or store any personally identifiable information. | 
					
						
							| 
									
										
										
										
											2018-02-22 19:46:58 +00:00
										 |  |  |         # https://docs.brew.sh/Analytics | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  |         # https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide | 
					
						
							|  |  |  |         # https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters | 
					
						
							|  |  |  |         if ENV["HOMEBREW_ANALYTICS_DEBUG"] | 
					
						
							| 
									
										
										
										
											2016-08-08 09:55:23 +01:00
										 |  |  |           url = "https://www.google-analytics.com/debug/collect" | 
					
						
							| 
									
										
										
										
											2018-09-14 18:50:03 +08:00
										 |  |  |           puts "#{ENV["HOMEBREW_CURL"]} #{args.join(" ")} #{url}" | 
					
						
							|  |  |  |           puts Utils.popen_read ENV["HOMEBREW_CURL"], *args, url | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  |         else | 
					
						
							|  |  |  |           pid = fork do | 
					
						
							|  |  |  |             exec ENV["HOMEBREW_CURL"], | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |                  *args, | 
					
						
							|  |  |  |                  "--silent", "--output", "/dev/null", | 
					
						
							|  |  |  |                  "https://www.google-analytics.com/collect" | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2020-11-23 18:15:48 +01:00
										 |  |  |           Process.detach T.must(pid) | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-03-28 09:16:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 09:12:11 +00:00
										 |  |  |       def report_event(category, action, label = os_arch_prefix_ci, value = nil) | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  |         report(:event, | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |                ec: category, | 
					
						
							|  |  |  |                ea: action, | 
					
						
							|  |  |  |                el: label, | 
					
						
							|  |  |  |                ev: value) | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-03-28 09:16:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-07 16:34:54 +01:00
										 |  |  |       def report_build_error(exception) | 
					
						
							|  |  |  |         return unless exception.formula.tap | 
					
						
							|  |  |  |         return unless exception.formula.tap.installed? | 
					
						
							|  |  |  |         return if exception.formula.tap.private? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 14:53:01 +03:00
										 |  |  |         action = exception.formula.full_name | 
					
						
							| 
									
										
										
										
											2020-05-18 12:46:23 +01:00
										 |  |  |         if (options = exception.options.to_a.map(&:to_s).join(" ").presence) | 
					
						
							|  |  |  |           action = "#{action} #{options}".strip | 
					
						
							| 
									
										
										
										
											2017-06-09 14:53:01 +03:00
										 |  |  |         end | 
					
						
							|  |  |  |         report_event("BuildError", action) | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       def messages_displayed? | 
					
						
							|  |  |  |         config_true?(:analyticsmessage) && config_true?(:caskanalyticsmessage) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def disabled? | 
					
						
							| 
									
										
										
										
											2020-04-05 15:44:50 +01:00
										 |  |  |         return true if Homebrew::EnvConfig.no_analytics? | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         config_true?(:analyticsdisabled) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-22 17:13:51 +00:00
										 |  |  |       def not_this_run? | 
					
						
							|  |  |  |         ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"].present? | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |       def no_message_output? | 
					
						
							|  |  |  |         # Used by Homebrew/install | 
					
						
							|  |  |  |         ENV["HOMEBREW_NO_ANALYTICS_MESSAGE_OUTPUT"].present? | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def uuid | 
					
						
							| 
									
										
										
										
											2021-01-13 11:16:09 -05:00
										 |  |  |         Homebrew::Settings.read :analyticsuuid | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def messages_displayed! | 
					
						
							| 
									
										
										
										
											2021-01-13 11:16:09 -05:00
										 |  |  |         Homebrew::Settings.write :analyticsmessage, true | 
					
						
							|  |  |  |         Homebrew::Settings.write :caskanalyticsmessage, true | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def enable! | 
					
						
							| 
									
										
										
										
											2021-01-13 11:16:09 -05:00
										 |  |  |         Homebrew::Settings.write :analyticsdisabled, false | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |         messages_displayed! | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def disable! | 
					
						
							| 
									
										
										
										
											2021-01-13 11:16:09 -05:00
										 |  |  |         Homebrew::Settings.write :analyticsdisabled, true | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |         regenerate_uuid! | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def regenerate_uuid! | 
					
						
							|  |  |  |         # it will be regenerated in next run unless disabled. | 
					
						
							| 
									
										
										
										
											2021-01-13 11:16:09 -05:00
										 |  |  |         Homebrew::Settings.delete :analyticsuuid | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 11:21:45 +01:00
										 |  |  |       def output(args:, filter: nil) | 
					
						
							| 
									
										
										
										
											2020-07-24 23:16:30 +02:00
										 |  |  |         days = args.days || "30" | 
					
						
							|  |  |  |         category = args.category || "install" | 
					
						
							| 
									
										
										
										
											2020-05-19 14:59:43 +05:30
										 |  |  |         json = formulae_brew_sh_json("analytics/#{category}/#{days}d.json") | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |         return if json.blank? || json["items"].blank? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         os_version = category == "os-version" | 
					
						
							|  |  |  |         cask_install = category == "cask-install" | 
					
						
							|  |  |  |         results = {} | 
					
						
							|  |  |  |         json["items"].each do |item| | 
					
						
							|  |  |  |           key = if os_version | 
					
						
							|  |  |  |             item["os_version"] | 
					
						
							|  |  |  |           elsif cask_install | 
					
						
							|  |  |  |             item["cask"] | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             item["formula"] | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2020-09-01 14:05:52 +01:00
										 |  |  |           next if filter.present? && key != filter && !key.start_with?("#{filter} ") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |           results[key] = item["count"].tr(",", "").to_i | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if filter.present? && results.blank? | 
					
						
							|  |  |  |           onoe "No results matching `#{filter}` found!" | 
					
						
							|  |  |  |           return | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         table_output(category, days, results, os_version: os_version, cask_install: cask_install) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 23:16:30 +02:00
										 |  |  |       def get_analytics(json, args:) | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |         full_analytics = args.analytics? || verbose? | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ohai "Analytics" | 
					
						
							|  |  |  |         json["analytics"].each do |category, value| | 
					
						
							|  |  |  |           category = category.tr("_", "-") | 
					
						
							|  |  |  |           analytics = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           value.each do |days, results| | 
					
						
							|  |  |  |             days = days.to_i | 
					
						
							|  |  |  |             if full_analytics | 
					
						
							| 
									
										
										
										
											2020-09-01 14:05:52 +01:00
										 |  |  |               next if args.days.present? && args.days&.to_i != days | 
					
						
							|  |  |  |               next if args.category.present? && args.category != category | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-22 10:39:16 +00:00
										 |  |  |               table_output(category, days, results) | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |             else | 
					
						
							|  |  |  |               total_count = results.values.inject("+") | 
					
						
							|  |  |  |               analytics << "#{number_readable(total_count)} (#{days} days)" | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           puts "#{category}: #{analytics.join(", ")}" unless full_analytics | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 23:16:30 +02:00
										 |  |  |       def formula_output(f, args:) | 
					
						
							| 
									
										
										
										
											2020-05-19 14:59:43 +05:30
										 |  |  |         json = formulae_brew_sh_json("#{formula_path}/#{f}.json") | 
					
						
							| 
									
										
										
										
											2020-05-17 01:38:11 +05:30
										 |  |  |         return if json.blank? || json["analytics"].blank? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 23:16:30 +02:00
										 |  |  |         get_analytics(json, args: args) | 
					
						
							| 
									
										
										
										
											2020-05-17 01:38:11 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 23:16:30 +02:00
										 |  |  |       def cask_output(cask, args:) | 
					
						
							| 
									
										
										
										
											2020-05-19 14:59:43 +05:30
										 |  |  |         json = formulae_brew_sh_json("#{cask_path}/#{cask}.json") | 
					
						
							| 
									
										
										
										
											2020-05-17 01:38:11 +05:30
										 |  |  |         return if json.blank? || json["analytics"].blank? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 23:16:30 +02:00
										 |  |  |         get_analytics(json, args: args) | 
					
						
							| 
									
										
										
										
											2020-05-17 01:38:11 +05:30
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |       def custom_prefix_label | 
					
						
							|  |  |  |         "custom-prefix" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2021-01-07 09:12:11 +00:00
										 |  |  |       alias generic_custom_prefix_label custom_prefix_label | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 09:12:11 +00:00
										 |  |  |       sig { returns(String) } | 
					
						
							|  |  |  |       def arch_label | 
					
						
							|  |  |  |         if Hardware::CPU.arm? | 
					
						
							|  |  |  |           "ARM" | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           "" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def clear_os_arch_prefix_ci | 
					
						
							|  |  |  |         return unless instance_variable_defined?(:@os_arch_prefix_ci) | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 09:12:11 +00:00
										 |  |  |         remove_instance_variable(:@os_arch_prefix_ci) | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 09:12:11 +00:00
										 |  |  |       def os_arch_prefix_ci | 
					
						
							|  |  |  |         @os_arch_prefix_ci ||= begin | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |           os = OS_VERSION | 
					
						
							| 
									
										
										
										
											2021-01-07 09:12:11 +00:00
										 |  |  |           arch = ", #{arch_label}" if arch_label.present? | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |           prefix = ", #{custom_prefix_label}" unless Homebrew.default_prefix? | 
					
						
							|  |  |  |           ci = ", CI" if ENV["CI"] | 
					
						
							| 
									
										
										
										
											2021-01-07 09:12:11 +00:00
										 |  |  |           "#{os}#{arch}#{prefix}#{ci}" | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def table_output(category, days, results, os_version: false, cask_install: false) | 
					
						
							|  |  |  |         oh1 "#{category} (#{days} days)" | 
					
						
							|  |  |  |         total_count = results.values.inject("+") | 
					
						
							|  |  |  |         formatted_total_count = format_count(total_count) | 
					
						
							|  |  |  |         formatted_total_percent = format_percent(100) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         index_header = "Index" | 
					
						
							|  |  |  |         count_header = "Count" | 
					
						
							|  |  |  |         percent_header = "Percent" | 
					
						
							|  |  |  |         name_with_options_header = if os_version | 
					
						
							|  |  |  |           "macOS Version" | 
					
						
							|  |  |  |         elsif cask_install | 
					
						
							|  |  |  |           "Token" | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           "Name (with options)" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         total_index_footer = "Total" | 
					
						
							|  |  |  |         max_index_width = results.length.to_s.length | 
					
						
							|  |  |  |         index_width = [ | 
					
						
							|  |  |  |           index_header.length, | 
					
						
							|  |  |  |           total_index_footer.length, | 
					
						
							|  |  |  |           max_index_width, | 
					
						
							|  |  |  |         ].max | 
					
						
							|  |  |  |         count_width = [ | 
					
						
							|  |  |  |           count_header.length, | 
					
						
							|  |  |  |           formatted_total_count.length, | 
					
						
							|  |  |  |         ].max | 
					
						
							|  |  |  |         percent_width = [ | 
					
						
							|  |  |  |           percent_header.length, | 
					
						
							|  |  |  |           formatted_total_percent.length, | 
					
						
							|  |  |  |         ].max | 
					
						
							|  |  |  |         name_with_options_width = Tty.width - | 
					
						
							|  |  |  |                                   index_width - | 
					
						
							|  |  |  |                                   count_width - | 
					
						
							|  |  |  |                                   percent_width - | 
					
						
							|  |  |  |                                   10 # spacing and lines | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         formatted_index_header = | 
					
						
							|  |  |  |           format "%#{index_width}s", index_header | 
					
						
							|  |  |  |         formatted_name_with_options_header = | 
					
						
							|  |  |  |           format "%-#{name_with_options_width}s", | 
					
						
							|  |  |  |                  name_with_options_header[0..name_with_options_width-1] | 
					
						
							|  |  |  |         formatted_count_header = | 
					
						
							|  |  |  |           format "%#{count_width}s", count_header | 
					
						
							|  |  |  |         formatted_percent_header = | 
					
						
							|  |  |  |           format "%#{percent_width}s", percent_header | 
					
						
							|  |  |  |         puts "#{formatted_index_header} | #{formatted_name_with_options_header} | "\ | 
					
						
							|  |  |  |             "#{formatted_count_header} |  #{formatted_percent_header}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         columns_line = "#{"-"*index_width}:|-#{"-"*name_with_options_width}-|-"\ | 
					
						
							|  |  |  |                       "#{"-"*count_width}:|-#{"-"*percent_width}:" | 
					
						
							|  |  |  |         puts columns_line | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         index = 0
 | 
					
						
							|  |  |  |         results.each do |name_with_options, count| | 
					
						
							|  |  |  |           index += 1
 | 
					
						
							|  |  |  |           formatted_index = format "%0#{max_index_width}d", index | 
					
						
							|  |  |  |           formatted_index = format "%-#{index_width}s", formatted_index | 
					
						
							|  |  |  |           formatted_name_with_options = | 
					
						
							|  |  |  |             format "%-#{name_with_options_width}s", | 
					
						
							|  |  |  |                    name_with_options[0..name_with_options_width-1] | 
					
						
							|  |  |  |           formatted_count = format "%#{count_width}s", format_count(count) | 
					
						
							|  |  |  |           formatted_percent = if total_count.zero? | 
					
						
							|  |  |  |             format "%#{percent_width}s", format_percent(0) | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             format "%#{percent_width}s", | 
					
						
							|  |  |  |                    format_percent((count.to_i * 100) / total_count.to_f) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           puts "#{formatted_index} | #{formatted_name_with_options} | " \ | 
					
						
							|  |  |  |               "#{formatted_count} | #{formatted_percent}%" | 
					
						
							|  |  |  |           next if index > 10
 | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         return unless results.length > 1
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         formatted_total_footer = | 
					
						
							|  |  |  |           format "%-#{index_width}s", total_index_footer | 
					
						
							|  |  |  |         formatted_blank_footer = | 
					
						
							|  |  |  |           format "%-#{name_with_options_width}s", "" | 
					
						
							|  |  |  |         formatted_total_count_footer = | 
					
						
							|  |  |  |           format "%#{count_width}s", formatted_total_count | 
					
						
							|  |  |  |         formatted_total_percent_footer = | 
					
						
							|  |  |  |           format "%#{percent_width}s", formatted_total_percent | 
					
						
							|  |  |  |         puts "#{formatted_total_footer} | #{formatted_blank_footer} | "\ | 
					
						
							|  |  |  |             "#{formatted_total_count_footer} | #{formatted_total_percent_footer}%" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def config_true?(key) | 
					
						
							| 
									
										
										
										
											2021-01-13 11:16:09 -05:00
										 |  |  |         Homebrew::Settings.read(key) == "true" | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-19 14:59:43 +05:30
										 |  |  |       def formulae_brew_sh_json(endpoint) | 
					
						
							| 
									
										
										
										
											2020-04-05 15:44:50 +01:00
										 |  |  |         return if Homebrew::EnvConfig.no_analytics? || Homebrew::EnvConfig.no_github_api? | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         output, = curl_output("--max-time", "5", | 
					
						
							|  |  |  |                               "https://formulae.brew.sh/api/#{endpoint}") | 
					
						
							|  |  |  |         return if output.blank? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         JSON.parse(output) | 
					
						
							|  |  |  |       rescue JSON::ParserError | 
					
						
							|  |  |  |         nil | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def format_count(count) | 
					
						
							|  |  |  |         count.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def format_percent(percent) | 
					
						
							|  |  |  |         format("%<percent>.2f", percent: percent) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |       def formula_path | 
					
						
							|  |  |  |         "formula" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       alias generic_formula_path formula_path | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  |       def analytics_path | 
					
						
							|  |  |  |         "analytics" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       alias generic_analytics_path analytics_path | 
					
						
							| 
									
										
										
										
											2020-05-17 01:38:11 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2020-05-17 01:38:11 +05:30
										 |  |  |       def cask_path | 
					
						
							|  |  |  |         "cask" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-05-03 14:21:08 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2016-03-28 09:16:40 +01:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2018-09-06 16:58:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-22 09:08:31 +00:00
										 |  |  | require "extend/os/utils/analytics" |