plist_parser.dart 2.79 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 6 7
import 'package:meta/meta.dart';
import 'package:process/process.dart';

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

class PlistParser {
16 17 18 19 20 21 22 23 24 25 26
  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;
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

  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';
42
    if (!_fileSystem.isFileSync(executable)) {
43
      throw const FileNotFoundException(executable);
44
    }
45
    if (!_fileSystem.isFileSync(plistFilePath)) {
46
      return const <String, dynamic>{};
47
    }
48

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

    try {
      final List<String> args = <String>[
        executable, '-convert', 'json', '-o', '-', normalizedPlistPath,
      ];
55
      final String jsonContent = _processUtils.runSync(
56 57 58
        args,
        throwOnError: true,
      ).stdout.trim();
59
      return castStringKeyedMap(json.decode(jsonContent));
60
    } on ProcessException catch (error) {
61
      _logger.printTrace('$error');
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
      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.
  String getValueFromFile(String plistFilePath, String key) {
    assert(key != null);
    final Map<String, dynamic> parsed = parseFile(plistFilePath);
78
    return parsed[key] as String;
79 80
  }
}