1// Copyright 2016 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import '../base/context.dart'; 6import '../base/file_system.dart'; 7import '../base/process.dart'; 8import '../convert.dart'; 9import '../globals.dart'; 10 11class PlistParser { 12 const PlistParser(); 13 14 static const String kCFBundleIdentifierKey = 'CFBundleIdentifier'; 15 static const String kCFBundleShortVersionStringKey = 'CFBundleShortVersionString'; 16 static const String kCFBundleExecutable = 'CFBundleExecutable'; 17 18 static PlistParser get instance => context.get<PlistParser>() ?? const PlistParser(); 19 20 /// Parses the plist file located at [plistFilePath] and returns the 21 /// associated map of key/value property list pairs. 22 /// 23 /// If [plistFilePath] points to a non-existent file or a file that's not a 24 /// valid property list file, this will return an empty map. 25 /// 26 /// The [plistFilePath] argument must not be null. 27 Map<String, dynamic> parseFile(String plistFilePath) { 28 assert(plistFilePath != null); 29 const String executable = '/usr/bin/plutil'; 30 if (!fs.isFileSync(executable)) 31 throw const FileNotFoundException(executable); 32 if (!fs.isFileSync(plistFilePath)) 33 return const <String, dynamic>{}; 34 35 final String normalizedPlistPath = fs.path.absolute(plistFilePath); 36 37 try { 38 final List<String> args = <String>[ 39 executable, '-convert', 'json', '-o', '-', normalizedPlistPath, 40 ]; 41 final String jsonContent = runCheckedSync(args); 42 return json.decode(jsonContent); 43 } catch (error) { 44 printTrace('$error'); 45 return const <String, dynamic>{}; 46 } 47 } 48 49 /// Parses the Plist file located at [plistFilePath] and returns the value 50 /// that's associated with the specified [key] within the property list. 51 /// 52 /// If [plistFilePath] points to a non-existent file or a file that's not a 53 /// valid property list file, this will return null. 54 /// 55 /// If [key] is not found in the property list, this will return null. 56 /// 57 /// The [plistFilePath] and [key] arguments must not be null. 58 String getValueFromFile(String plistFilePath, String key) { 59 assert(key != null); 60 final Map<String, dynamic> parsed = parseFile(plistFilePath); 61 return parsed[key]; 62 } 63} 64