Skip to content

Commit

Permalink
add brew analytics command (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
xu-cheng committed May 1, 2016
1 parent 90d3317 commit 98aff27
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 11 deletions.
46 changes: 46 additions & 0 deletions Library/Homebrew/cmd/analytics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#: * `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.

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
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/update-report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def update_report
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:"
Expand Down
13 changes: 6 additions & 7 deletions Library/Homebrew/utils/analytics.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down Expand Up @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion Library/brew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOS
Cowardly refusing to 'sudo brew $HOMEBREW_COMMAND'
You can use brew with sudo, but only if the brew executable is owned by root.
Expand Down
2 changes: 1 addition & 1 deletion share/doc/homebrew/Analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ export HOMEBREW_NO_ANALYTICS=1
Alternatively, this will prevent analytics from ever being sent:

```sh
git config --file="$(brew --repository)/.git/config" --replace-all homebrew.analyticsdisabled true
brew analytics off
```
4 changes: 4 additions & 0 deletions share/doc/homebrew/brew.1.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ <h2 id="ESSENTIAL-COMMANDS">ESSENTIAL COMMANDS</h2>
<h2 id="COMMANDS">COMMANDS</h2>

<dl>
<dt><code>analytics</code> [<code>state</code>]</dt><dd><p>Display anonymous user behaviour analytics state.
Read more at <a href="https://git.io/brew-analytics" data-bare-link="true">https://git.io/brew-analytics</a>.</p></dd>
<dt><code>analytics</code> (<code>on</code>|<code>off</code>)</dt><dd><p>Turn on/off Homebrew's analytics.</p></dd>
<dt><code>analytics</code> <code>regenerate-uuid</code></dt><dd><p>Regenerate UUID used in Homebrew's analytics.</p></dd>
<dt><code>audit</code> [<code>--strict</code>] [<code>--online</code>] [<code>--display-cop-names</code>] [<var>formulae</var>]</dt><dd><p>Check <var>formulae</var> for Homebrew coding style violations. This should be
run before submitting a new formula.</p>

Expand Down
14 changes: 13 additions & 1 deletion share/man/man1/brew.1
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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\.
.
Expand Down

0 comments on commit 98aff27

Please sign in to comment.