Skip to content

Commit b58aa3b

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
Add PubWorkspace, integrate it into ContextBuilder, and therefore fix sealed test
Also add a superclass to BasicWorkspace and PubWorkspace, since the bulk of their implementation is shared; Only find() and findPackageFor() need to be separate; the package mapping and resolving can be shared. Change-Id: I13932d6947d6dc28fc7223594e5bd2526f12f573 Reviewed-on: https://dart-review.googlesource.com/c/89167 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent a60a06f commit b58aa3b

File tree

10 files changed

+384
-73
lines changed

10 files changed

+384
-73
lines changed

pkg/analyzer/lib/src/context/builder.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import 'package:analyzer/src/workspace/basic.dart';
4040
import 'package:analyzer/src/workspace/bazel.dart';
4141
import 'package:analyzer/src/workspace/gn.dart';
4242
import 'package:analyzer/src/workspace/package_build.dart';
43+
import 'package:analyzer/src/workspace/pub.dart';
4344
import 'package:analyzer/src/workspace/workspace.dart';
4445
import 'package:args/args.dart';
4546
import 'package:package_config/packages.dart';
@@ -672,16 +673,19 @@ class ContextBuilder {
672673
static Workspace createWorkspace(ResourceProvider resourceProvider,
673674
String rootPath, ContextBuilder contextBuilder) {
674675
if (_hasPackageFileInPath(resourceProvider, rootPath)) {
675-
// Bazel workspaces that include package files are treated like normal
676-
// (non-Bazel) directories. But may still use package:build.
676+
// A Bazel or Gn workspace that includes a '.packages' file is treated
677+
// like a normal (non-Bazel/Gn) directory. But may still use
678+
// package:build or Pub.
677679
return PackageBuildWorkspace.find(
678680
resourceProvider, rootPath, contextBuilder) ??
681+
PubWorkspace.find(resourceProvider, rootPath, contextBuilder) ??
679682
BasicWorkspace.find(resourceProvider, rootPath, contextBuilder);
680683
}
681684
Workspace workspace = BazelWorkspace.find(resourceProvider, rootPath);
682685
workspace ??= GnWorkspace.find(resourceProvider, rootPath);
683686
workspace ??=
684687
PackageBuildWorkspace.find(resourceProvider, rootPath, contextBuilder);
688+
workspace ??= PubWorkspace.find(resourceProvider, rootPath, contextBuilder);
685689
return workspace ??
686690
BasicWorkspace.find(resourceProvider, rootPath, contextBuilder);
687691
}

pkg/analyzer/lib/src/workspace/basic.dart

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:analyzer/src/generated/sdk.dart';
88
import 'package:analyzer/src/generated/source.dart';
99
import 'package:analyzer/src/source/package_map_resolver.dart';
1010
import 'package:analyzer/src/summary/package_bundle_reader.dart';
11+
import 'package:analyzer/src/workspace/simple.dart';
1112
import 'package:analyzer/src/workspace/workspace.dart';
1213
import 'package:package_config/packages.dart';
1314

@@ -16,61 +17,17 @@ import 'package:package_config/packages.dart';
1617
*
1718
* A BasicWorkspace should only be used when no other workspace type is valid.
1819
*/
19-
class BasicWorkspace extends Workspace {
20-
/**
21-
* The [ResourceProvider] by which paths are converted into [Resource]s.
22-
*/
23-
final ResourceProvider provider;
24-
25-
/**
26-
* The absolute workspace root path.
27-
*/
28-
final String root;
29-
30-
final ContextBuilder _builder;
31-
32-
Map<String, List<Folder>> _packageMap;
33-
34-
Packages _packages;
35-
20+
class BasicWorkspace extends SimpleWorkspace {
3621
/**
3722
* The singular package in this workspace.
3823
*
3924
* Each basic workspace is itself one package.
4025
*/
4126
BasicWorkspacePackage _theOnlyPackage;
4227

43-
BasicWorkspace._(this.provider, this.root, this._builder);
44-
45-
@override
46-
Map<String, List<Folder>> get packageMap {
47-
_packageMap ??= _builder.convertPackagesToMap(packages);
48-
return _packageMap;
49-
}
50-
51-
Packages get packages {
52-
_packages ??= _builder.createPackageMap(root);
53-
return _packages;
54-
}
55-
56-
@override
57-
UriResolver get packageUriResolver =>
58-
new PackageMapUriResolver(provider, packageMap);
59-
60-
@override
61-
SourceFactory createSourceFactory(DartSdk sdk, SummaryDataStore summaryData) {
62-
if (summaryData != null) {
63-
throw new UnsupportedError(
64-
'Summary files are not supported in a basic workspace.');
65-
}
66-
List<UriResolver> resolvers = <UriResolver>[];
67-
if (sdk != null) {
68-
resolvers.add(new DartUriResolver(sdk));
69-
}
70-
resolvers.add(packageUriResolver);
71-
resolvers.add(new ResourceUriResolver(provider));
72-
return new SourceFactory(resolvers, packages, provider);
73-
}
28+
BasicWorkspace._(
29+
ResourceProvider provider, String root, ContextBuilder builder)
30+
: super(provider, root, builder);
7431

7532
@override
7633
WorkspacePackage findPackageFor(String filePath) {

pkg/analyzer/lib/src/workspace/bazel.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,12 @@ class BazelWorkspace extends Workspace {
287287
/**
288288
* Find the Bazel workspace that contains the given [filePath].
289289
*
290-
* Return `null` if a workspace markers, such as the `WORKSPACE` file, or
290+
* Return `null` if a workspace marker, such as the `WORKSPACE` file, or
291291
* the sibling `READONLY` folder cannot be found.
292292
*
293293
* Return `null` if the workspace does not have `bazel-genfiles` or
294-
* `blaze-genfiles` folders, so we don't know where to search generated files.
294+
* `blaze-genfiles` folders, since we don't know where to search generated
295+
* files.
295296
*
296297
* Return `null` if there is a folder 'foo' with the sibling `READONLY`
297298
* folder, but there is corresponding folder 'foo' in `READONLY`, i.e. the

pkg/analyzer/lib/src/workspace/gn.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ class GnWorkspace extends Workspace {
198198
* file must be found in [filePath]'s output directory.
199199
*/
200200
static GnWorkspace find(ResourceProvider provider, String filePath) {
201+
Resource resource = provider.getResource(filePath);
202+
if (resource is File) {
203+
filePath = resource.parent.path;
204+
}
201205
Folder folder = provider.getFolder(filePath);
202206
while (true) {
203207
Folder parent = folder.parent;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
import 'package:analyzer/file_system/file_system.dart';
6+
import 'package:analyzer/src/context/builder.dart';
7+
import 'package:analyzer/src/generated/sdk.dart';
8+
import 'package:analyzer/src/generated/source.dart';
9+
import 'package:analyzer/src/source/package_map_resolver.dart';
10+
import 'package:analyzer/src/summary/package_bundle_reader.dart';
11+
import 'package:analyzer/src/workspace/simple.dart';
12+
import 'package:analyzer/src/workspace/workspace.dart';
13+
import 'package:package_config/packages.dart';
14+
15+
/// Information about a Pub workspace.
16+
class PubWorkspace extends SimpleWorkspace {
17+
/// The name of the file that identifies the root of the workspace.
18+
static const String _pubspecName = 'pubspec.yaml';
19+
20+
/// The singular package in this workspace.
21+
///
22+
/// Each Pub workspace is itself one package.
23+
PubWorkspacePackage _theOnlyPackage;
24+
25+
PubWorkspace._(ResourceProvider provider, String root, ContextBuilder builder)
26+
: super(provider, root, builder);
27+
28+
@override
29+
WorkspacePackage findPackageFor(String filePath) {
30+
final Folder folder = provider.getFolder(filePath);
31+
if (provider.pathContext.isWithin(root, folder.path)) {
32+
_theOnlyPackage ??= new PubWorkspacePackage(root, this);
33+
return _theOnlyPackage;
34+
} else {
35+
return null;
36+
}
37+
}
38+
39+
/// Find the pub workspace that contains the given [path].
40+
static PubWorkspace find(
41+
ResourceProvider provider, String filePath, ContextBuilder builder) {
42+
Resource resource = provider.getResource(filePath);
43+
if (resource is File) {
44+
filePath = resource.parent.path;
45+
}
46+
Folder folder = provider.getFolder(filePath);
47+
while (true) {
48+
Folder parent = folder.parent;
49+
if (parent == null) {
50+
return null;
51+
}
52+
53+
if (folder.getChildAssumingFile(_pubspecName).exists) {
54+
// Found the pubspec.yaml file; this is our root.
55+
String root = folder.path;
56+
return new PubWorkspace._(provider, root, builder);
57+
}
58+
59+
// Go up a folder.
60+
folder = parent;
61+
}
62+
}
63+
}
64+
65+
/// Information about a package defined in a [PubWorkspace].
66+
///
67+
/// Separate from [Packages] or package maps, this class is designed to simply
68+
/// understand whether arbitrary file paths represent libraries declared within
69+
/// a given package in a [PubWorkspace].
70+
class PubWorkspacePackage extends WorkspacePackage {
71+
final String root;
72+
73+
final PubWorkspace workspace;
74+
75+
PubWorkspacePackage(this.root, this.workspace);
76+
77+
@override
78+
bool contains(String path) {
79+
// There is a 1-1 relationship between [PubWorkspace]s and
80+
// [PubWorkspacePackage]s. If a file is in a package's workspace, then it
81+
// is in the package as well.
82+
return workspace.provider.pathContext.isWithin(root, path);
83+
}
84+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
import 'package:analyzer/file_system/file_system.dart';
6+
import 'package:analyzer/src/context/builder.dart';
7+
import 'package:analyzer/src/generated/sdk.dart';
8+
import 'package:analyzer/src/generated/source.dart';
9+
import 'package:analyzer/src/source/package_map_resolver.dart';
10+
import 'package:analyzer/src/summary/package_bundle_reader.dart';
11+
import 'package:analyzer/src/workspace/workspace.dart';
12+
import 'package:package_config/packages.dart';
13+
14+
/// An abstract class for simple workspaces which do not feature any build
15+
/// artifacts or generated files.
16+
///
17+
/// The [packageMap] and [packageUrlResolver] are simple derivations from the
18+
/// [ContextBuilder] and [ResourceProvider] required for the class.
19+
abstract class SimpleWorkspace extends Workspace {
20+
/// The [ResourceProvider] by which paths are converted into [Resource]s.
21+
final ResourceProvider provider;
22+
23+
/// The absolute workspace root path.
24+
final String root;
25+
26+
final ContextBuilder _builder;
27+
28+
Map<String, List<Folder>> _packageMap;
29+
30+
Packages _packages;
31+
32+
SimpleWorkspace(this.provider, this.root, this._builder);
33+
34+
@override
35+
Map<String, List<Folder>> get packageMap {
36+
_packageMap ??= _builder.convertPackagesToMap(packages);
37+
return _packageMap;
38+
}
39+
40+
Packages get packages {
41+
_packages ??= _builder.createPackageMap(root);
42+
return _packages;
43+
}
44+
45+
@override
46+
UriResolver get packageUriResolver =>
47+
new PackageMapUriResolver(provider, packageMap);
48+
49+
@override
50+
SourceFactory createSourceFactory(DartSdk sdk, SummaryDataStore summaryData) {
51+
if (summaryData != null) {
52+
throw new UnsupportedError(
53+
'Summary files are not supported in a Pub workspace.');
54+
}
55+
List<UriResolver> resolvers = <UriResolver>[];
56+
if (sdk != null) {
57+
resolvers.add(new DartUriResolver(sdk));
58+
}
59+
resolvers.add(packageUriResolver);
60+
resolvers.add(new ResourceUriResolver(provider));
61+
return new SourceFactory(resolvers, packages, provider);
62+
}
63+
}

0 commit comments

Comments
 (0)