plist_parser.dart 2.78 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:process/process.dart';
6

7
import '../base/file_system.dart';
8
import '../base/io.dart';
9
import '../base/logger.dart';
10
import '../base/process.dart';
11
import '../base/utils.dart';
12 13 14
import '../convert.dart';

class PlistParser {
15
  PlistParser({
16 17 18
    required FileSystem fileSystem,
    required Logger logger,
    required ProcessManager processManager,
19 20 21 22 23 24 25
  }) : _fileSystem = fileSystem,
       _logger = logger,
       _processUtils = ProcessUtils(logger: logger, processManager: processManager);

  final FileSystem _fileSystem;
  final Logger _logger;
  final ProcessUtils _processUtils;
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

  static const String kCFBundleIdentifierKey = 'CFBundleIdentifier';
  static const String kCFBundleShortVersionStringKey = 'CFBundleShortVersionString';
  static const String kCFBundleExecutable = 'CFBundleExecutable';

  /// Parses the plist file located at [plistFilePath] and returns the
  /// associated map of key/value property list pairs.
  ///
  /// If [plistFilePath] points to a non-existent file or a file that's not a
  /// valid property list file, this will return an empty map.
  ///
  /// The [plistFilePath] argument must not be null.
  Map<String, dynamic> parseFile(String plistFilePath) {
    assert(plistFilePath != null);
    const String executable = '/usr/bin/plutil';
41
    if (!_fileSystem.isFileSync(executable)) {
42
      throw const FileNotFoundException(executable);
43
    }
44
    if (!_fileSystem.isFileSync(plistFilePath)) {
45
      return const <String, dynamic>{};
46
    }
47

48
    final String normalizedPlistPath = _fileSystem.path.absolute(plistFilePath);
49 50 51 52 53

    try {
      final List<String> args = <String>[
        executable, '-convert', 'json', '-o', '-', normalizedPlistPath,
      ];
54
      final String jsonContent = _processUtils.runSync(
55 56 57
        args,
        throwOnError: true,
      ).stdout.trim();
58
      return castStringKeyedMap(json.decode(jsonContent)) ?? const <String, dynamic>{};
59
    } on ProcessException catch (error) {
60
      _logger.printTrace('$error');
61 62 63 64 65 66 67 68 69 70 71 72 73
      return const <String, dynamic>{};
    }
  }

  /// Parses the Plist file located at [plistFilePath] and returns the value
  /// that's associated with the specified [key] within the property list.
  ///
  /// If [plistFilePath] points to a non-existent file or a file that's not a
  /// valid property list file, this will return null.
  ///
  /// If [key] is not found in the property list, this will return null.
  ///
  /// The [plistFilePath] and [key] arguments must not be null.
74
  String? getValueFromFile(String plistFilePath, String key) {
75 76
    assert(key != null);
    final Map<String, dynamic> parsed = parseFile(plistFilePath);
77
    return parsed[key] as String;
78 79
  }
}