| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library source.pub_package_map_provider; | 5 library analyzer.source.pub_package_map_provider; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:core' hide Resource; | 9 import 'dart:core'; |
| 10 import 'dart:io' as io; | 10 import 'dart:io' as io; |
| 11 | 11 |
| 12 import 'package:analyzer/file_system/file_system.dart'; | 12 import 'package:analyzer/file_system/file_system.dart'; |
| 13 import 'package:analyzer/source/package_map_provider.dart'; | 13 import 'package:analyzer/source/package_map_provider.dart'; |
| 14 import 'package:analyzer/src/dart/sdk/sdk.dart'; |
| 14 import 'package:analyzer/src/generated/engine.dart'; | 15 import 'package:analyzer/src/generated/engine.dart'; |
| 15 import 'package:analyzer/src/generated/sdk_io.dart'; | |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * The function used to run pub list. | 18 * The function used to run pub list. |
| 19 */ | 19 */ |
| 20 typedef io.ProcessResult RunPubList(Folder folder); | 20 typedef io.ProcessResult RunPubList(Folder folder); |
| 21 | 21 |
| 22 /** | 22 /** |
| 23 * Implementation of PackageMapProvider that operates by executing pub. | 23 * Implementation of PackageMapProvider that operates by executing pub. |
| 24 */ | 24 */ |
| 25 class PubPackageMapProvider implements PackageMapProvider { | 25 class PubPackageMapProvider implements PackageMapProvider { |
| 26 static const String PUB_LIST_COMMAND = 'list-package-dirs'; | 26 static const String PUB_LIST_COMMAND = 'list-package-dirs'; |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * The name of the 'pubspec.lock' file, which we assume is the dependency | 29 * The name of the 'pubspec.lock' file, which we assume is the dependency |
| 30 * in the event that [PUB_LIST_COMMAND] fails. | 30 * in the event that [PUB_LIST_COMMAND] fails. |
| 31 */ | 31 */ |
| 32 static const String PUBSPEC_LOCK_NAME = 'pubspec.lock'; | 32 static const String PUBSPEC_LOCK_NAME = 'pubspec.lock'; |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * [ResourceProvider] that is used to create the [Folder]s that populate the | 35 * [ResourceProvider] that is used to create the [Folder]s that populate the |
| 36 * package map. | 36 * package map. |
| 37 */ | 37 */ |
| 38 final ResourceProvider resourceProvider; | 38 final ResourceProvider resourceProvider; |
| 39 | 39 |
| 40 /** | 40 /** |
| 41 * Sdk that we use to find the pub executable. | 41 * Sdk that we use to find the pub executable. |
| 42 */ | 42 */ |
| 43 final DirectoryBasedDartSdk sdk; | 43 final FolderBasedDartSdk sdk; |
| 44 | 44 |
| 45 /** | 45 /** |
| 46 * The function used to run pub list. | 46 * The function used to run pub list. |
| 47 */ | 47 */ |
| 48 RunPubList _runPubList; | 48 RunPubList _runPubList; |
| 49 | 49 |
| 50 /** | 50 /** |
| 51 * Construct a new instance. | 51 * Construct a new instance. |
| 52 * A [RunPubList] implementation may be injected for testing | 52 * A [RunPubList] implementation may be injected for testing |
| 53 */ | 53 */ |
| 54 PubPackageMapProvider(this.resourceProvider, this.sdk, [this._runPubList]) { | 54 PubPackageMapProvider(this.resourceProvider, this.sdk, [this._runPubList]) { |
| 55 if (_runPubList == null) { | 55 if (_runPubList == null) { |
| 56 _runPubList = _runPubListDefault; | 56 _runPubList = _runPubListDefault; |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 | 59 |
| 60 @override | 60 @override |
| 61 PackageMapInfo computePackageMap(Folder folder) { | 61 PackageMapInfo computePackageMap(Folder folder) { |
| 62 // If the pubspec.lock file does not exist, no need to run anything. |
| 63 { |
| 64 String lockPath = getPubspecLockPath(folder); |
| 65 if (!resourceProvider.getFile(lockPath).exists) { |
| 66 return computePackageMapError(folder); |
| 67 } |
| 68 } |
| 62 // TODO(paulberry) make this asynchronous so that we can (a) do other | 69 // TODO(paulberry) make this asynchronous so that we can (a) do other |
| 63 // analysis while it's in progress, and (b) time out if it takes too long | 70 // analysis while it's in progress, and (b) time out if it takes too long |
| 64 // to respond. | 71 // to respond. |
| 65 io.ProcessResult result; | 72 io.ProcessResult result; |
| 66 try { | 73 try { |
| 67 result = _runPubList(folder); | 74 result = _runPubList(folder); |
| 68 } on io.ProcessException catch (exception, stackTrace) { | 75 } on io.ProcessException catch (exception, stackTrace) { |
| 69 AnalysisEngine.instance.logger.logInformation( | 76 AnalysisEngine.instance.logger.logInformation( |
| 70 "Error running pub $PUB_LIST_COMMAND\n$exception\n$stackTrace"); | 77 "Error running pub $PUB_LIST_COMMAND\n$exception\n$stackTrace"); |
| 71 } | 78 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 89 } | 96 } |
| 90 | 97 |
| 91 /** | 98 /** |
| 92 * Create a PackageMapInfo object representing an error condition. | 99 * Create a PackageMapInfo object representing an error condition. |
| 93 */ | 100 */ |
| 94 PackageMapInfo computePackageMapError(Folder folder) { | 101 PackageMapInfo computePackageMapError(Folder folder) { |
| 95 // Even if an error occurs, we still need to know the dependencies, so that | 102 // Even if an error occurs, we still need to know the dependencies, so that |
| 96 // we'll know when to try running "pub list-package-dirs" again. | 103 // we'll know when to try running "pub list-package-dirs" again. |
| 97 // Unfortunately, "pub list-package-dirs" doesn't tell us dependencies when | 104 // Unfortunately, "pub list-package-dirs" doesn't tell us dependencies when |
| 98 // an error occurs, so just assume there is one dependency, "pubspec.lock". | 105 // an error occurs, so just assume there is one dependency, "pubspec.lock". |
| 99 List<String> dependencies = <String>[ | 106 String lockPath = getPubspecLockPath(folder); |
| 100 resourceProvider.pathContext.join(folder.path, PUBSPEC_LOCK_NAME) | 107 List<String> dependencies = <String>[lockPath]; |
| 101 ]; | |
| 102 return new PackageMapInfo(null, dependencies.toSet()); | 108 return new PackageMapInfo(null, dependencies.toSet()); |
| 103 } | 109 } |
| 104 | 110 |
| 105 /** | 111 /** |
| 112 * Return the path to the `pubspec.lock` file in the given [folder]. |
| 113 */ |
| 114 String getPubspecLockPath(Folder folder) => |
| 115 resourceProvider.pathContext.join(folder.path, PUBSPEC_LOCK_NAME); |
| 116 |
| 117 /** |
| 106 * Decode the JSON output from pub into a package map. Paths in the | 118 * Decode the JSON output from pub into a package map. Paths in the |
| 107 * output are considered relative to [folder]. | 119 * output are considered relative to [folder]. |
| 108 */ | 120 */ |
| 109 PackageMapInfo parsePackageMap(Map obj, Folder folder) { | 121 PackageMapInfo parsePackageMap(Map obj, Folder folder) { |
| 110 // The output of pub looks like this: | 122 // The output of pub looks like this: |
| 111 // { | 123 // { |
| 112 // "packages": { | 124 // "packages": { |
| 113 // "foo": "path/to/foo", | 125 // "foo": "path/to/foo", |
| 114 // "bar": ["path/to/bar1", "path/to/bar2"], | 126 // "bar": ["path/to/bar1", "path/to/bar2"], |
| 115 // "myapp": "path/to/myapp", // self link is included | 127 // "myapp": "path/to/myapp", // self link is included |
| (...skipping 11 matching lines...) Expand all Loading... |
| 127 Resource resource = folder.getChildAssumingFolder(path); | 139 Resource resource = folder.getChildAssumingFolder(path); |
| 128 if (resource is Folder) { | 140 if (resource is Folder) { |
| 129 folders.add(resource); | 141 folders.add(resource); |
| 130 } | 142 } |
| 131 } | 143 } |
| 132 } | 144 } |
| 133 if (folders.isNotEmpty) { | 145 if (folders.isNotEmpty) { |
| 134 packageMap[packageName] = folders; | 146 packageMap[packageName] = folders; |
| 135 } | 147 } |
| 136 } | 148 } |
| 149 |
| 137 packages.forEach((key, value) { | 150 packages.forEach((key, value) { |
| 138 if (value is String) { | 151 if (value is String) { |
| 139 processPaths(key, [value]); | 152 processPaths(key, [value]); |
| 140 } else if (value is List) { | 153 } else if (value is List) { |
| 141 processPaths(key, value); | 154 processPaths(key, value); |
| 142 } | 155 } |
| 143 }); | 156 }); |
| 144 Set<String> dependencies = new Set<String>(); | 157 Set<String> dependencies = new Set<String>(); |
| 145 List inputFiles = obj['input_files']; | 158 List inputFiles = obj['input_files']; |
| 146 if (inputFiles != null) { | 159 if (inputFiles != null) { |
| 147 for (var path in inputFiles) { | 160 for (var path in inputFiles) { |
| 148 if (path is String) { | 161 if (path is String) { |
| 149 dependencies.add(folder.canonicalizePath(path)); | 162 dependencies.add(folder.canonicalizePath(path)); |
| 150 } | 163 } |
| 151 } | 164 } |
| 152 } | 165 } |
| 153 return new PackageMapInfo(packageMap, dependencies); | 166 return new PackageMapInfo(packageMap, dependencies); |
| 154 } | 167 } |
| 155 | 168 |
| 156 /** | 169 /** |
| 157 * Run pub list to determine the packages and input files. | 170 * Run pub list to determine the packages and input files. |
| 158 */ | 171 */ |
| 159 io.ProcessResult _runPubListDefault(Folder folder) { | 172 io.ProcessResult _runPubListDefault(Folder folder) { |
| 160 String executablePath = sdk.pubExecutable.getAbsolutePath(); | 173 String executablePath = sdk.pubExecutable.path; |
| 161 List<String> arguments = [PUB_LIST_COMMAND]; | 174 List<String> arguments = [PUB_LIST_COMMAND]; |
| 162 String workingDirectory = folder.path; | 175 String workingDirectory = folder.path; |
| 163 int subprocessId = AnalysisEngine.instance.instrumentationService | 176 int subprocessId = AnalysisEngine.instance.instrumentationService |
| 164 .logSubprocessStart(executablePath, arguments, workingDirectory); | 177 .logSubprocessStart(executablePath, arguments, workingDirectory); |
| 165 io.ProcessResult result = io.Process | 178 io.ProcessResult result = io.Process |
| 166 .runSync(executablePath, arguments, workingDirectory: workingDirectory); | 179 .runSync(executablePath, arguments, workingDirectory: workingDirectory); |
| 167 AnalysisEngine.instance.instrumentationService.logSubprocessResult( | 180 AnalysisEngine.instance.instrumentationService.logSubprocessResult( |
| 168 subprocessId, result.exitCode, result.stdout, result.stderr); | 181 subprocessId, result.exitCode, result.stdout, result.stderr); |
| 169 return result; | 182 return result; |
| 170 } | 183 } |
| 171 } | 184 } |
| OLD | NEW |