[analyzer] fix an exception when run on platforms w/o home dirs

Bug: https://github.com/dart-lang/sdk/issues/37308
Change-Id: I2e117a678bfc99dcc3f48d4a58ea5895d0079261
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107580
Commit-Queue: Devon Carew <devoncarew@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
This commit is contained in:
Devon Carew
2019-06-28 18:31:49 +00:00
committed by commit-bot@chromium.org
parent 881ca948fd
commit 983447f95a
2 changed files with 41 additions and 3 deletions
+40 -3
View File
@@ -4,6 +4,7 @@
import 'dart:io';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
import 'package:usage/src/usage_impl.dart';
import 'package:usage/src/usage_impl_io.dart';
@@ -52,14 +53,26 @@ String createAnalyticsStatusMessage(
///
/// This analytics instance will share a common enablement state with the rest
/// of the Dart SDK tools.
_TelemetryAnalytics createAnalyticsInstance(
Analytics createAnalyticsInstance(
String trackingId,
String applicationName, {
bool disableForSession: false,
}) {
Directory dir = getDartStorageDirectory();
if (dir == null) {
// Some systems don't support user home directories; for those, fail
// gracefully by returning a disabled analytics object.
return new _DisabledAnalytics(trackingId, applicationName);
}
if (!dir.existsSync()) {
dir.createSync();
try {
dir.createSync();
} catch (e) {
// If we can't create the directory for the analytics settings, fail
// gracefully by returning a disabled analytics object.
return new _DisabledAnalytics(trackingId, applicationName);
}
}
File file = new File(path.join(dir.path, _settingsFileName));
@@ -71,8 +84,15 @@ _TelemetryAnalytics createAnalyticsInstance(
///
/// Typically, the directory is `~/.dart/` (and the settings file is
/// `analytics.json`).
///
/// This can return null under some conditions, including when the user's home
/// directory does not exist.
@visibleForTesting
Directory getDartStorageDirectory() {
return new Directory(path.join(userHomeDir(), _dartDirectoryName));
Directory homeDirectory = new Directory(userHomeDir());
if (!homeDirectory.existsSync()) return null;
return new Directory(path.join(homeDirectory.path, _dartDirectoryName));
}
/// Return the version of the Dart SDK.
@@ -111,6 +131,23 @@ class _TelemetryAnalytics extends AnalyticsImpl {
}
}
class _DisabledAnalytics extends AnalyticsMock {
@override
final String trackingId;
@override
final String applicationName;
_DisabledAnalytics(this.trackingId, this.applicationName);
@override
bool get enabled => false;
}
/// Detect whether we're running on a bot or in a continuous testing
/// environment.
///
/// We should periodically keep this code up to date with
/// https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/base/utils.dart#L20.
bool isRunningOnBot() {
final Map<String, String> env = Platform.environment;
+1
View File
@@ -8,6 +8,7 @@ environment:
dependencies:
http: ^0.12.0
meta: ^1.0.2
path: ^1.4.0
stack_trace: ^1.7.0
usage: ^3.2.0+1