Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Unified Diff: pkg/analysis_server/lib/src/package_map_provider.dart

Issue 341893010: Use "pub list-package-dirs" to resolve package URIs in analysis server. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/analysis_server/lib/src/analysis_server.dart ('k') | pkg/analysis_server/lib/src/socket_server.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analysis_server/lib/src/package_map_provider.dart
diff --git a/pkg/analysis_server/lib/src/package_map_provider.dart b/pkg/analysis_server/lib/src/package_map_provider.dart
new file mode 100644
index 0000000000000000000000000000000000000000..dfe229a3f7f679126643406f4f18f09c8bb7ea26
--- /dev/null
+++ b/pkg/analysis_server/lib/src/package_map_provider.dart
@@ -0,0 +1,100 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library package.map.provider;
+
+import 'dart:convert';
+import 'dart:io' as io;
+
+import 'package:analysis_server/src/analysis_server.dart';
+import 'package:analysis_server/src/resource.dart';
+import 'package:analyzer/src/generated/engine.dart';
+
+/**
+ * A PackageMapProvider is an entity capable of determining the mapping from
+ * package name to source directory for a given folder.
+ */
+abstract class PackageMapProvider {
+ /**
+ * Compute a package map for the given folder, if possible.
+ *
+ * If a package map can't be computed, return null.
+ */
+ Map<String, Folder> computePackageMap(Folder folder);
+}
+
+/**
+ * Implementation of PackageMapProvider that operates by executing pub.
+ */
+class PubPackageMapProvider implements PackageMapProvider {
+ static const String PUB_LIST_COMMAND = 'list-package-dirs';
+
+ /**
+ * [ResourceProvider] that is used to create the [Folder]s that populate the
+ * package map.
+ */
+ final ResourceProvider resourceProvider;
+
+ PubPackageMapProvider(this.resourceProvider);
+
+ @override
+ Map<String, Folder> computePackageMap(Folder folder) {
+ // TODO(paulberry) make this asynchronous so that we can (a) do other
+ // analysis while it's in progress, and (b) time out if it takes too long
+ // to respond.
+ String executable = SHARED_SDK.pubExecutable.getAbsolutePath();
+ io.ProcessResult result;
+ try {
+ result = io.Process.runSync(
+ executable, [PUB_LIST_COMMAND], workingDirectory: folder.path);
+ } on io.ProcessException catch (exception, stackTrace) {
+ AnalysisEngine.instance.logger.logInformation(
Brian Wilkerson 2014/06/20 14:42:46 These errors seems like something we should report
+ "Error running pub $PUB_LIST_COMMAND\n${exception}\n${stackTrace}");
+ }
+ if (result.exitCode != 0) {
+ AnalysisEngine.instance.logger.logInformation(
+ "pub $PUB_LIST_COMMAND failed: exit code ${result.exitCode}");
+ return null;
+ }
+ try {
+ return parsePackageMap(result.stdout);
+ } catch (exception, stackTrace) {
+ AnalysisEngine.instance.logger.logError(
+ "Malformed output from pub $PUB_LIST_COMMAND\n${exception}\n${stackTrace}");
+ }
+
+ return null;
+ }
+
+ /**
+ * Decode the JSON output from pub into a package map.
+ */
+ Map<String, Folder> parsePackageMap(String jsonText) {
+ // The output of pub looks like this:
+ // {
+ // "packages": {
+ // "foo": "path/to/foo",
+ // "bar": ["path/to/bar1", "path/to/bar2"],
+ // "myapp": "path/to/myapp", // self link is included
+ // },
+ // "input_files": [
+ // "path/to/myapp/pubspec.lock"
+ // ]
+ // }
+ Map<String, Folder> packageMap = <String, Folder>{};
+ Map obj = JSON.decode(jsonText);
+ Map packages = obj['packages'];
+ packages.forEach((key, value) {
+ if (value is String) {
+ Resource resource = resourceProvider.getResource(value);
+ if (resource is Folder) {
+ packageMap[key] = resource;
+ }
+ } else if (value is List) {
+ // TODO(paulberry): support string lists.
+ }
+ });
+ return packageMap;
+ }
+}
« no previous file with comments | « pkg/analysis_server/lib/src/analysis_server.dart ('k') | pkg/analysis_server/lib/src/socket_server.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698