| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:io'; | 5 import 'dart:io'; |
| 6 | 6 |
| 7 import 'package:path/path.dart' as path; | 7 import 'package:path/path.dart' as path; |
| 8 import 'package:usage/src/usage_impl.dart'; | 8 import 'package:usage/src/usage_impl.dart'; |
| 9 import 'package:usage/src/usage_impl_io.dart'; | 9 import 'package:usage/src/usage_impl_io.dart'; |
| 10 import 'package:usage/src/usage_impl_io.dart' as usage_io show getDartVersion; | 10 import 'package:usage/src/usage_impl_io.dart' as usage_io show getDartVersion; |
| 11 import 'package:usage/usage.dart'; | 11 import 'package:usage/usage.dart'; |
| 12 import 'package:usage/usage_io.dart'; | 12 import 'package:usage/usage_io.dart'; |
| 13 | 13 |
| 14 export 'package:usage/usage.dart' show Analytics; |
| 15 |
| 16 // TODO(devoncarew): Hard-coded to off for now. Remove when we're ready to ship. |
| 17 final bool _HARD_CODE_OFF = true; |
| 18 |
| 14 final String _dartDirectoryName = '.dart'; | 19 final String _dartDirectoryName = '.dart'; |
| 15 final String _settingsFileName = 'analytics.json'; | 20 final String _settingsFileName = 'analytics.json'; |
| 16 | 21 |
| 17 /// Dart SDK tools with analytics should display this notice. | 22 /// Dart SDK tools with analytics should display this notice. |
| 18 /// | 23 /// |
| 19 /// In addition, they should support displaying the analytics' status, and have | 24 /// In addition, they should support displaying the analytics' status, and have |
| 20 /// a flag to toggle analytics. This may look something like: | 25 /// a flag to toggle analytics. This may look something like: |
| 21 /// | 26 /// |
| 22 /// `Analytics are currently enabled (and can be disabled with --no-analytics).` | 27 /// `Analytics are currently enabled (and can be disabled with --no-analytics).` |
| 23 final String analyticsNotice = | 28 final String analyticsNotice = |
| 24 "Dart SDK tools anonymously report feature usage statistics and basic " | 29 "Dart SDK tools anonymously report feature usage statistics and basic crash\
n" |
| 25 "crash reports to help improve Dart tools over time. See Google's privacy " | 30 "reports to help improve Dart tools over time. See Google's privacy policy:\
n" |
| 26 "policy: https://www.google.com/intl/en/policies/privacy/."; | 31 "https://www.google.com/intl/en/policies/privacy/."; |
| 32 |
| 33 /// Return a customized message for command-line tools to display about the |
| 34 /// state of analytics, and how users can enabled or disable analytics. |
| 35 /// |
| 36 /// An example return value might be `'Analytics are currently enabled (and can |
| 37 /// be disabled with --no-analytics).'` |
| 38 String createAnalyticsStatusMessage(bool enabled, |
| 39 {String command: 'analytics'}) { |
| 40 String currentState = enabled ? 'enabled' : 'disabled'; |
| 41 String toggleState = enabled ? 'disabled' : 'enabled'; |
| 42 String commandToggle = enabled ? 'no-$command' : command; |
| 43 |
| 44 return 'Analytics are currently $currentState ' |
| 45 '(and can be $toggleState with --$commandToggle).'; |
| 46 } |
| 27 | 47 |
| 28 /// Create an [Analytics] instance with the given trackingID and | 48 /// Create an [Analytics] instance with the given trackingID and |
| 29 /// applicationName. | 49 /// applicationName. |
| 30 /// | 50 /// |
| 31 /// This analytics instance will share a common enablement state with the rest | 51 /// This analytics instance will share a common enablement state with the rest |
| 32 /// of the Dart SDK tools. | 52 /// of the Dart SDK tools. |
| 33 Analytics createAnalyticsInstance(String trackingId, String applicationName, | 53 Analytics createAnalyticsInstance(String trackingId, String applicationName, |
| 34 {bool disableForSession: false}) { | 54 {bool disableForSession: false}) { |
| 35 Directory dir = getDartStorageDirectory(); | 55 Directory dir = getDartStorageDirectory(); |
| 36 if (!dir.existsSync()) { | 56 if (!dir.existsSync()) { |
| 37 dir.createSync(); | 57 dir.createSync(); |
| 38 } | 58 } |
| 39 | 59 |
| 60 if (_HARD_CODE_OFF) { |
| 61 disableForSession = true; |
| 62 } |
| 63 |
| 40 File file = new File(path.join(dir.path, _settingsFileName)); | 64 File file = new File(path.join(dir.path, _settingsFileName)); |
| 41 return new _TelemetryAnalytics( | 65 return new _TelemetryAnalytics( |
| 42 trackingId, applicationName, getDartVersion(), file, disableForSession); | 66 trackingId, applicationName, getDartVersion(), file, disableForSession); |
| 43 } | 67 } |
| 44 | 68 |
| 45 /// The directory used to store the analytics settings file. | 69 /// The directory used to store the analytics settings file. |
| 46 /// | 70 /// |
| 47 /// Typically, the directory is `~/.dart/` (and the settings file is | 71 /// Typically, the directory is `~/.dart/` (and the settings file is |
| 48 /// `analytics.json`). | 72 /// `analytics.json`). |
| 49 Directory getDartStorageDirectory() => | 73 Directory getDartStorageDirectory() => |
| (...skipping 20 matching lines...) Expand all Loading... |
| 70 applicationVersion: applicationVersion, | 94 applicationVersion: applicationVersion, |
| 71 ) { | 95 ) { |
| 72 final String locale = getPlatformLocale(); | 96 final String locale = getPlatformLocale(); |
| 73 if (locale != null) { | 97 if (locale != null) { |
| 74 setSessionValue('ul', locale); | 98 setSessionValue('ul', locale); |
| 75 } | 99 } |
| 76 } | 100 } |
| 77 | 101 |
| 78 @override | 102 @override |
| 79 bool get enabled { | 103 bool get enabled { |
| 80 if (disableForSession || _isRunningOnBot()) { | 104 if (disableForSession || isRunningOnBot()) { |
| 81 return false; | 105 return false; |
| 82 } | 106 } |
| 83 return super.enabled; | 107 return super.enabled; |
| 84 } | 108 } |
| 85 } | 109 } |
| 86 | 110 |
| 87 bool _isRunningOnBot() { | 111 bool isRunningOnBot() { |
| 88 // - https://docs.travis-ci.com/user/environment-variables/ | 112 // - https://docs.travis-ci.com/user/environment-variables/ |
| 89 // - https://www.appveyor.com/docs/environment-variables/ | 113 // - https://www.appveyor.com/docs/environment-variables/ |
| 90 // - CHROME_HEADLESS and BUILDBOT_BUILDERNAME are properties on Chrome infra | 114 // - CHROME_HEADLESS and BUILDBOT_BUILDERNAME are properties on Chrome infra |
| 91 // bots. | 115 // bots. |
| 92 return Platform.environment['TRAVIS'] == 'true' || | 116 return Platform.environment['TRAVIS'] == 'true' || |
| 93 Platform.environment['BOT'] == 'true' || | 117 Platform.environment['BOT'] == 'true' || |
| 94 Platform.environment['CONTINUOUS_INTEGRATION'] == 'true' || | 118 Platform.environment['CONTINUOUS_INTEGRATION'] == 'true' || |
| 95 Platform.environment['CHROME_HEADLESS'] == '1' || | 119 Platform.environment['CHROME_HEADLESS'] == '1' || |
| 96 Platform.environment.containsKey('BUILDBOT_BUILDERNAME') || | 120 Platform.environment.containsKey('BUILDBOT_BUILDERNAME') || |
| 97 Platform.environment.containsKey('APPVEYOR') || | 121 Platform.environment.containsKey('APPVEYOR') || |
| 98 Platform.environment.containsKey('CI'); | 122 Platform.environment.containsKey('CI'); |
| 99 } | 123 } |
| OLD | NEW |