[flutter_tool] Make BotDetector context free (#48605)
This commit is contained in:
60
packages/flutter_tools/lib/src/base/bot_detector.dart
Normal file
60
packages/flutter_tools/lib/src/base/bot_detector.dart
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:platform/platform.dart';
|
||||
|
||||
import 'context.dart';
|
||||
|
||||
class BotDetector {
|
||||
BotDetector({
|
||||
@required Platform platform,
|
||||
}) : _platform = platform;
|
||||
|
||||
final Platform _platform;
|
||||
|
||||
bool get isRunningOnBot {
|
||||
if (
|
||||
// Explicitly stated to not be a bot.
|
||||
_platform.environment['BOT'] == 'false'
|
||||
|
||||
// Set by the IDEs to the IDE name, so a strong signal that this is not a bot.
|
||||
|| _platform.environment.containsKey('FLUTTER_HOST')
|
||||
// When set, GA logs to a local file (normally for tests) so we don't need to filter.
|
||||
|| _platform.environment.containsKey('FLUTTER_ANALYTICS_LOG_FILE')
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _platform.environment['BOT'] == 'true'
|
||||
|
||||
// https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables
|
||||
|| _platform.environment['TRAVIS'] == 'true'
|
||||
|| _platform.environment['CONTINUOUS_INTEGRATION'] == 'true'
|
||||
|| _platform.environment.containsKey('CI') // Travis and AppVeyor
|
||||
|
||||
// https://www.appveyor.com/docs/environment-variables/
|
||||
|| _platform.environment.containsKey('APPVEYOR')
|
||||
|
||||
// https://cirrus-ci.org/guide/writing-tasks/#environment-variables
|
||||
|| _platform.environment.containsKey('CIRRUS_CI')
|
||||
|
||||
// https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
|
||||
|| (_platform.environment.containsKey('AWS_REGION') &&
|
||||
_platform.environment.containsKey('CODEBUILD_INITIATOR'))
|
||||
|
||||
// https://wiki.jenkins.io/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-belowJenkinsSetEnvironmentVariables
|
||||
|| _platform.environment.containsKey('JENKINS_URL')
|
||||
|
||||
// Properties on Flutter's Chrome Infra bots.
|
||||
|| _platform.environment['CHROME_HEADLESS'] == '1'
|
||||
|| _platform.environment.containsKey('BUILDBOT_BUILDERNAME')
|
||||
|| _platform.environment.containsKey('SWARMING_TASK_ID');
|
||||
}
|
||||
}
|
||||
|
||||
bool isRunningOnBot(Platform platform) {
|
||||
final BotDetector botDetector = context.get<BotDetector>() ?? BotDetector(platform: platform);
|
||||
return botDetector.isRunningOnBot;
|
||||
}
|
||||
@@ -10,61 +10,10 @@ import 'package:meta/meta.dart';
|
||||
|
||||
import '../convert.dart';
|
||||
import '../globals.dart' as globals;
|
||||
import 'context.dart';
|
||||
import 'file_system.dart';
|
||||
import 'io.dart' as io;
|
||||
import 'terminal.dart';
|
||||
|
||||
const BotDetector _kBotDetector = BotDetector();
|
||||
|
||||
class BotDetector {
|
||||
const BotDetector();
|
||||
|
||||
bool get isRunningOnBot {
|
||||
if (
|
||||
// Explicitly stated to not be a bot.
|
||||
globals.platform.environment['BOT'] == 'false'
|
||||
|
||||
// Set by the IDEs to the IDE name, so a strong signal that this is not a bot.
|
||||
|| globals.platform.environment.containsKey('FLUTTER_HOST')
|
||||
// When set, GA logs to a local file (normally for tests) so we don't need to filter.
|
||||
|| globals.platform.environment.containsKey('FLUTTER_ANALYTICS_LOG_FILE')
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return globals.platform.environment['BOT'] == 'true'
|
||||
|
||||
// https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables
|
||||
|| globals.platform.environment['TRAVIS'] == 'true'
|
||||
|| globals.platform.environment['CONTINUOUS_INTEGRATION'] == 'true'
|
||||
|| globals.platform.environment.containsKey('CI') // Travis and AppVeyor
|
||||
|
||||
// https://www.appveyor.com/docs/environment-variables/
|
||||
|| globals.platform.environment.containsKey('APPVEYOR')
|
||||
|
||||
// https://cirrus-ci.org/guide/writing-tasks/#environment-variables
|
||||
|| globals.platform.environment.containsKey('CIRRUS_CI')
|
||||
|
||||
// https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
|
||||
|| (globals.platform.environment.containsKey('AWS_REGION') &&
|
||||
globals.platform.environment.containsKey('CODEBUILD_INITIATOR'))
|
||||
|
||||
// https://wiki.jenkins.io/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-belowJenkinsSetEnvironmentVariables
|
||||
|| globals.platform.environment.containsKey('JENKINS_URL')
|
||||
|
||||
// Properties on Flutter's Chrome Infra bots.
|
||||
|| globals.platform.environment['CHROME_HEADLESS'] == '1'
|
||||
|| globals.platform.environment.containsKey('BUILDBOT_BUILDERNAME')
|
||||
|| globals.platform.environment.containsKey('SWARMING_TASK_ID');
|
||||
}
|
||||
}
|
||||
|
||||
bool get isRunningOnBot {
|
||||
final BotDetector botDetector = context.get<BotDetector>() ?? _kBotDetector;
|
||||
return botDetector.isRunningOnBot;
|
||||
}
|
||||
|
||||
/// Convert `foo_bar` to `fooBar`.
|
||||
String camelCase(String str) {
|
||||
int index = str.indexOf('_');
|
||||
|
||||
@@ -22,7 +22,6 @@ import 'base/signals.dart';
|
||||
import 'base/terminal.dart';
|
||||
import 'base/time.dart';
|
||||
import 'base/user_messages.dart';
|
||||
import 'base/utils.dart';
|
||||
import 'build_system/build_system.dart';
|
||||
import 'cache.dart';
|
||||
import 'compile.dart';
|
||||
@@ -75,7 +74,6 @@ Future<T> runInContext<T>(
|
||||
ApplicationPackageFactory: () => ApplicationPackageFactory(),
|
||||
Artifacts: () => CachedArtifacts(),
|
||||
AssetBundleFactory: () => AssetBundleFactory.defaultInstance,
|
||||
BotDetector: () => const BotDetector(),
|
||||
BuildSystem: () => const BuildSystem(),
|
||||
Cache: () => Cache(),
|
||||
ChromeLauncher: () => const ChromeLauncher(),
|
||||
|
||||
@@ -6,13 +6,13 @@ import 'dart:async';
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
import '../base/bot_detector.dart';
|
||||
import '../base/common.dart';
|
||||
import '../base/context.dart';
|
||||
import '../base/file_system.dart';
|
||||
import '../base/io.dart' as io;
|
||||
import '../base/logger.dart';
|
||||
import '../base/process.dart';
|
||||
import '../base/utils.dart';
|
||||
import '../cache.dart';
|
||||
import '../globals.dart' as globals;
|
||||
import '../reporting/reporting.dart';
|
||||
@@ -232,7 +232,7 @@ class _DefaultPub implements Pub {
|
||||
@required bool retry,
|
||||
bool showTraceForErrors,
|
||||
}) async {
|
||||
showTraceForErrors ??= isRunningOnBot;
|
||||
showTraceForErrors ??= isRunningOnBot(globals.platform);
|
||||
|
||||
String lastPubMessage = 'no message';
|
||||
bool versionSolvingFailed = false;
|
||||
@@ -368,7 +368,7 @@ String _getPubEnvironmentValue(PubContext pubContext) {
|
||||
final String existing = globals.platform.environment[_pubEnvironmentKey];
|
||||
final List<String> values = <String>[
|
||||
if (existing != null && existing.isNotEmpty) existing,
|
||||
if (isRunningOnBot) 'flutter_bot',
|
||||
if (isRunningOnBot(globals.platform)) 'flutter_bot',
|
||||
'flutter_cli',
|
||||
...pubContext._values,
|
||||
];
|
||||
|
||||
@@ -10,12 +10,12 @@ import 'package:http/http.dart' as http;
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:usage/usage_io.dart';
|
||||
|
||||
import '../base/bot_detector.dart';
|
||||
import '../base/context.dart';
|
||||
import '../base/file_system.dart';
|
||||
import '../base/io.dart';
|
||||
import '../base/os.dart';
|
||||
import '../base/time.dart';
|
||||
import '../base/utils.dart';
|
||||
import '../doctor.dart';
|
||||
import '../features.dart';
|
||||
import '../globals.dart' as globals;
|
||||
|
||||
@@ -176,7 +176,7 @@ class _DefaultUsage implements Usage {
|
||||
// Many CI systems don't do a full git checkout.
|
||||
version.endsWith('/unknown') ||
|
||||
// Ignore bots.
|
||||
isRunningOnBot ||
|
||||
isRunningOnBot(globals.platform) ||
|
||||
// Ignore when suppressed by FLUTTER_SUPPRESS_ANALYTICS.
|
||||
suppressEnvFlag
|
||||
)) {
|
||||
|
||||
Reference in New Issue
Block a user