From 98aff27317eaf50c869341e04562062ecd09bd85 Mon Sep 17 00:00:00 2001 From: Xu Cheng Date: Sun, 1 May 2016 22:04:46 +0800 Subject: [PATCH] add `brew analytics` command (#173) --- Library/Homebrew/cmd/analytics.rb | 46 +++++++++++++++++++++++++++ Library/Homebrew/cmd/update-report.rb | 2 +- Library/Homebrew/utils/analytics.sh | 13 ++++---- Library/brew.sh | 2 +- share/doc/homebrew/Analytics.md | 2 +- share/doc/homebrew/brew.1.html | 4 +++ share/man/man1/brew.1 | 14 +++++++- 7 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 Library/Homebrew/cmd/analytics.rb diff --git a/Library/Homebrew/cmd/analytics.rb b/Library/Homebrew/cmd/analytics.rb new file mode 100644 index 0000000000..649c537107 --- /dev/null +++ b/Library/Homebrew/cmd/analytics.rb @@ -0,0 +1,46 @@ +#: * `analytics` [`state`]: +#: Display anonymous user behaviour analytics state. +#: Read more at . +#: +#: * `analytics` (`on`|`off`): +#: Turn on/off Homebrew's analytics. +#: +#: * `analytics` `regenerate-uuid`: +#: Regenerate UUID used in Homebrew's analytics. + +module Homebrew + def analytics + config_file = HOMEBREW_REPOSITORY/".git/config" + + raise UsageError if ARGV.named.size > 1 + case ARGV.named.first + when nil, "state" + analyticsdisabled = \ + Utils.popen_read("git config --file=#{config_file} --get homebrew.analyticsdisabled").chuzzle + uuid = \ + Utils.popen_read("git config --file=#{config_file} --get homebrew.analyticsuuid").chuzzle + if ENV["HOMEBREW_NO_ANALYTICS"] + puts "Analytics is disabled (by HOMEBREW_NO_ANALYTICS)." + elsif analyticsdisabled == "true" + puts "Analytics is disabled." + else + puts "Analytics is enabled." + puts "UUID: #{uuid}" if uuid + end + when "on" + safe_system "git", "config", "--file=#{config_file}", + "--replace-all", "homebrew.analyticsdisabled", "false" + safe_system "git", "config", "--file=#{config_file}", + "--replace-all", "homebrew.analyticsmessage", "true" + when "off" + safe_system "git", "config", "--file=#{config_file}", + "--replace-all", "homebrew.analyticsdisabled", "true" + system "git", "config", "--file=#{config_file}", "--unset-all", "homebrew.analyticsuuid" + when "regenerate-uuid" + # it will be regenerated in next run. + system "git", "config", "--file=#{config_file}", "--unset-all", "homebrew.analyticsuuid" + else + raise UsageError + end + end +end diff --git a/Library/Homebrew/cmd/update-report.rb b/Library/Homebrew/cmd/update-report.rb index 53b9d639ae..1d9822ae57 100644 --- a/Library/Homebrew/cmd/update-report.rb +++ b/Library/Homebrew/cmd/update-report.rb @@ -17,7 +17,7 @@ module Homebrew Utils.popen_read("git", "config", "--local", "--get", "homebrew.analyticsmessage").chuzzle analytics_disabled = \ Utils.popen_read("git", "config", "--local", "--get", "homebrew.analyticsdisabled").chuzzle - if analytics_message_displayed != "true" && analytics_disabled != "true" + if analytics_message_displayed != "true" && analytics_disabled != "true" && !ENV["HOMEBREW_NO_ANALYTICS"] ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "1" ohai "Homebrew has enabled anonymous aggregate user behaviour analytics" puts "Read the analytics documentation (and how to opt-out) here:" diff --git a/Library/Homebrew/utils/analytics.sh b/Library/Homebrew/utils/analytics.sh index 47716e5e56..2ca1c72e75 100644 --- a/Library/Homebrew/utils/analytics.sh +++ b/Library/Homebrew/utils/analytics.sh @@ -18,11 +18,9 @@ setup-analytics() { migrate-legacy-uuid-file - # Make disabling anlytics sticky if [[ -n "$HOMEBREW_NO_ANALYTICS" ]] then - git config --file="$git_config_file" --replace-all homebrew.analyticsdisabled true - git config --file="$git_config_file" --unset-all homebrew.analyticsuuid + return fi local message_seen="$(git config --file="$git_config_file" --get homebrew.analyticsmessage)" @@ -72,10 +70,11 @@ report-analytics-screenview-command() { # Don't report commands used mostly by our scripts and not users. # TODO: list more e.g. shell completion things here perhaps using a single # script as a shell-completion entry point. - if [[ "$HOMEBREW_COMMAND" = "commands" ]] - then - return - fi + case "$HOMEBREW_COMMAND" in + --prefix|analytics|command|commands) + return + ;; + esac local args=( --max-time 3 \ diff --git a/Library/brew.sh b/Library/brew.sh index 54f713dd98..33f4465036 100644 --- a/Library/brew.sh +++ b/Library/brew.sh @@ -190,7 +190,7 @@ fi if [[ "$(id -u)" = "0" && "$(/usr/bin/stat -f%u "$HOMEBREW_BREW_FILE")" != "0" ]] then case "$HOMEBREW_COMMAND" in - install|reinstall|postinstall|link|pin|update|upgrade|create|migrate|tap|tap-pin|switch) + analytics|install|reinstall|postinstall|link|pin|update|upgrade|create|migrate|tap|tap-pin|switch) odie <

COMMANDS

+
analytics [state]

Display anonymous user behaviour analytics state. +Read more at https://git.io/brew-analytics.

+
analytics (on|off)

Turn on/off Homebrew's analytics.

+
analytics regenerate-uuid

Regenerate UUID used in Homebrew's analytics.

audit [--strict] [--online] [--display-cop-names] [formulae]

Check formulae for Homebrew coding style violations. This should be run before submitting a new formula.

diff --git a/share/man/man1/brew.1 b/share/man/man1/brew.1 index 577dd95c82..3e6ba709c3 100644 --- a/share/man/man1/brew.1 +++ b/share/man/man1/brew.1 @@ -1,7 +1,7 @@ .\" generated with Ronn/v0.7.3 .\" http://github.com/rtomayko/ronn/tree/0.7.3 . -.TH "BREW" "1" "April 2016" "Homebrew" "brew" +.TH "BREW" "1" "May 2016" "Homebrew" "brew" . .SH "NAME" \fBbrew\fR \- The missing package manager for OS X @@ -44,6 +44,18 @@ Perform a substring search of formula names for \fItext\fR\. If \fItext\fR is su .SH "COMMANDS" . .TP +\fBanalytics\fR [\fBstate\fR] +Display anonymous user behaviour analytics state\. Read more at \fIhttps://git\.io/brew\-analytics\fR\. +. +.TP +\fBanalytics\fR (\fBon\fR|\fBoff\fR) +Turn on/off Homebrew\'s analytics\. +. +.TP +\fBanalytics\fR \fBregenerate\-uuid\fR +Regenerate UUID used in Homebrew\'s analytics\. +. +.TP \fBaudit\fR [\fB\-\-strict\fR] [\fB\-\-online\fR] [\fB\-\-display\-cop\-names\fR] [\fIformulae\fR] Check \fIformulae\fR for Homebrew coding style violations\. This should be run before submitting a new formula\. .