[flutter_tools] remove globals from plist parser and update tests (#51444)

This commit is contained in:
Jonah Williams
2020-02-26 18:31:42 -08:00
committed by GitHub
parent d4226566d4
commit 80619f100f
12 changed files with 131 additions and 105 deletions

View File

@@ -41,14 +41,14 @@ class AndroidStudio implements Comparable<AndroidStudio> {
factory AndroidStudio.fromMacOSBundle(String bundlePath) {
String studioPath = globals.fs.path.join(bundlePath, 'Contents');
String plistFile = globals.fs.path.join(studioPath, 'Info.plist');
Map<String, dynamic> plistValues = PlistParser.instance.parseFile(plistFile);
Map<String, dynamic> plistValues = globals.plistParser.parseFile(plistFile);
// As AndroidStudio managed by JetBrainsToolbox could have a wrapper pointing to the real Android Studio.
// Check if we've found a JetBrainsToolbox wrapper and deal with it properly.
final String jetBrainsToolboxAppBundlePath = plistValues['JetBrainsToolboxApp'] as String;
if (jetBrainsToolboxAppBundlePath != null) {
studioPath = globals.fs.path.join(jetBrainsToolboxAppBundlePath, 'Contents');
plistFile = globals.fs.path.join(studioPath, 'Info.plist');
plistValues = PlistParser.instance.parseFile(plistFile);
plistValues = globals.plistParser.parseFile(plistFile);
}
final String versionString = plistValues[PlistParser.kCFBundleShortVersionStringKey] as String;

View File

@@ -312,7 +312,7 @@ abstract class IOSApp extends ApplicationPackage {
globals.printError('Invalid prebuilt iOS app. Does not contain Info.plist.');
return null;
}
final String id = PlistParser.instance.getValueFromFile(
final String id = globals.plistParser.getValueFromFile(
plistPath,
PlistParser.kCFBundleIdentifierKey,
);

View File

@@ -862,7 +862,7 @@ class IntelliJValidatorOnMac extends IntelliJValidator {
@override
String get version {
_version ??= PlistParser.instance.getValueFromFile(
_version ??= globals.plistParser.getValueFromFile(
plistFile,
PlistParser.kCFBundleShortVersionStringKey,
) ?? 'unknown';
@@ -876,7 +876,8 @@ class IntelliJValidatorOnMac extends IntelliJValidator {
return _pluginsPath;
}
final String altLocation = PlistParser.instance.getValueFromFile(plistFile, 'JetBrainsToolboxApp');
final String altLocation = globals.plistParser
.getValueFromFile(plistFile, 'JetBrainsToolboxApp');
if (altLocation != null) {
_pluginsPath = altLocation + '.plugins';

View File

@@ -22,6 +22,7 @@ import 'base/user_messages.dart';
import 'cache.dart';
import 'ios/ios_deploy.dart';
import 'ios/mac.dart';
import 'ios/plist_parser.dart';
import 'macos/xcode.dart';
import 'persistent_tool_state.dart';
import 'version.dart';
@@ -150,5 +151,12 @@ final AnsiTerminal _defaultAnsiTerminal = AnsiTerminal(
/// The global Stdio wrapper.
Stdio get stdio => context.get<Stdio>() ?? const Stdio();
PlistParser get plistParser => context.get<PlistParser>() ?? (_defaultInstance ??= PlistParser(
fileSystem: fs,
processManager: processManager,
logger: logger,
));
PlistParser _defaultInstance;
/// The [ChromeLauncher] instance.
ChromeLauncher get chromeLauncher => context.get<ChromeLauncher>();

View File

@@ -9,7 +9,6 @@ import '../base/process.dart';
import '../base/version.dart';
import '../build_info.dart';
import '../globals.dart' as globals;
import '../ios/plist_parser.dart';
import '../macos/xcode.dart';
const bool kBitcodeEnabledDefault = false;
@@ -28,7 +27,7 @@ Future<void> validateBitcode(BuildMode buildMode, TargetPlatform targetPlatform)
final RunResult clangResult = await xcode.clang(<String>['--version']);
final String clangVersion = clangResult.stdout.split('\n').first;
final String engineClangVersion = PlistParser.instance.getValueFromFile(
final String engineClangVersion = globals.plistParser.getValueFromFile(
globals.fs.path.join(flutterFrameworkPath, 'Info.plist'),
'ClangVersion',
);

View File

@@ -2,23 +2,33 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '../base/context.dart';
import 'package:meta/meta.dart';
import 'package:process/process.dart';
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/process.dart';
import '../base/utils.dart';
import '../convert.dart';
import '../globals.dart' as globals;
class PlistParser {
const PlistParser();
PlistParser({
@required FileSystem fileSystem,
@required Logger logger,
@required ProcessManager processManager,
}) : _fileSystem = fileSystem,
_logger = logger,
_processUtils = ProcessUtils(logger: logger, processManager: processManager);
final FileSystem _fileSystem;
final Logger _logger;
final ProcessUtils _processUtils;
static const String kCFBundleIdentifierKey = 'CFBundleIdentifier';
static const String kCFBundleShortVersionStringKey = 'CFBundleShortVersionString';
static const String kCFBundleExecutable = 'CFBundleExecutable';
static PlistParser get instance => context.get<PlistParser>() ?? const PlistParser();
/// Parses the plist file located at [plistFilePath] and returns the
/// associated map of key/value property list pairs.
///
@@ -29,26 +39,26 @@ class PlistParser {
Map<String, dynamic> parseFile(String plistFilePath) {
assert(plistFilePath != null);
const String executable = '/usr/bin/plutil';
if (!globals.fs.isFileSync(executable)) {
if (!_fileSystem.isFileSync(executable)) {
throw const FileNotFoundException(executable);
}
if (!globals.fs.isFileSync(plistFilePath)) {
if (!_fileSystem.isFileSync(plistFilePath)) {
return const <String, dynamic>{};
}
final String normalizedPlistPath = globals.fs.path.absolute(plistFilePath);
final String normalizedPlistPath = _fileSystem.path.absolute(plistFilePath);
try {
final List<String> args = <String>[
executable, '-convert', 'json', '-o', '-', normalizedPlistPath,
];
final String jsonContent = processUtils.runSync(
final String jsonContent = _processUtils.runSync(
args,
throwOnError: true,
).stdout.trim();
return castStringKeyedMap(json.decode(jsonContent));
} on ProcessException catch (error) {
globals.printTrace('$error');
_logger.printTrace('$error');
return const <String, dynamic>{};
}
}

View File

@@ -392,7 +392,7 @@ class IOSSimulator extends Device {
// parsing the xcodeproj or configuration files.
// See https://github.com/flutter/flutter/issues/31037 for more information.
final String plistPath = globals.fs.path.join(package.simulatorBundlePath, 'Info.plist');
final String bundleIdentifier = PlistParser.instance.getValueFromFile(plistPath, PlistParser.kCFBundleIdentifierKey);
final String bundleIdentifier = globals.plistParser.getValueFromFile(plistPath, PlistParser.kCFBundleIdentifierKey);
await SimControl.instance.launch(id, bundleIdentifier, args);
} catch (error) {

View File

@@ -66,7 +66,7 @@ abstract class MacOSApp extends ApplicationPackage {
globals.printError('Invalid prebuilt macOS app. Does not contain Info.plist.');
return null;
}
final Map<String, dynamic> propertyValues = PlistParser.instance.parseFile(plistPath);
final Map<String, dynamic> propertyValues = globals.plistParser.parseFile(plistPath);
final String id = propertyValues[PlistParser.kCFBundleIdentifierKey] as String;
final String executableName = propertyValues[PlistParser.kCFBundleExecutable] as String;
if (id == null) {

View File

@@ -402,7 +402,7 @@ class IosProject extends FlutterProjectPlatform implements XcodeBasedProject {
// Try parsing the default, first.
if (defaultInfoPlist.existsSync()) {
try {
fromPlist = PlistParser.instance.getValueFromFile(
fromPlist = globals.plistParser.getValueFromFile(
defaultHostInfoPlist.path,
PlistParser.kCFBundleIdentifierKey,
);