Unverified Commit 763a8d6d authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

add support to multiroot scheme to PackageUriMapper (#27207)

parent 39289b83
...@@ -78,8 +78,7 @@ class StdoutHandler { ...@@ -78,8 +78,7 @@ class StdoutHandler {
void handler(String message) { void handler(String message) {
const String kResultPrefix = 'result '; const String kResultPrefix = 'result ';
if (boundaryKey == null) { if (boundaryKey == null && message.startsWith(kResultPrefix)) {
if (message.startsWith(kResultPrefix))
boundaryKey = message.substring(kResultPrefix.length); boundaryKey = message.substring(kResultPrefix.length);
} else if (message.startsWith(boundaryKey)) { } else if (message.startsWith(boundaryKey)) {
if (message.length <= boundaryKey.length) { if (message.length <= boundaryKey.length) {
...@@ -112,36 +111,45 @@ class StdoutHandler { ...@@ -112,36 +111,45 @@ class StdoutHandler {
/// Converts filesystem paths to package URIs. /// Converts filesystem paths to package URIs.
class PackageUriMapper { class PackageUriMapper {
PackageUriMapper(String scriptPath, String packagesPath) { PackageUriMapper(String scriptPath, String packagesPath, String fileSystemScheme, List<String> fileSystemRoots) {
final Map<String, Uri> packageMap = PackageMap(fs.path.absolute(packagesPath)).map; final Map<String, Uri> packageMap = PackageMap(fs.path.absolute(packagesPath)).map;
final String scriptUri = Uri.file(scriptPath, windows: platform.isWindows).toString(); final String scriptUri = Uri.file(scriptPath, windows: platform.isWindows).toString();
for (String packageName in packageMap.keys) { for (String packageName in packageMap.keys) {
final String prefix = packageMap[packageName].toString(); final String prefix = packageMap[packageName].toString();
if (fileSystemScheme != null && fileSystemRoots != null && prefix.contains(fileSystemScheme)) {
_packageName = packageName;
_uriPrefixes = fileSystemRoots
.map((String name) => Uri.file('$name/lib/', windows: platform.isWindows).toString())
.toList();
return;
}
if (scriptUri.startsWith(prefix)) { if (scriptUri.startsWith(prefix)) {
_packageName = packageName; _packageName = packageName;
_uriPrefix = prefix; _uriPrefixes = <String>[prefix];
return; return;
} }
} }
} }
String _packageName; String _packageName;
String _uriPrefix; List<String> _uriPrefixes;
Uri map(String scriptPath) { Uri map(String scriptPath) {
if (_packageName == null) if (_packageName == null) {
return null; return null;
}
final String scriptUri = Uri.file(scriptPath, windows: platform.isWindows).toString(); final String scriptUri = Uri.file(scriptPath, windows: platform.isWindows).toString();
if (scriptUri.startsWith(_uriPrefix)) { for (String uriPrefix in _uriPrefixes) {
return Uri.parse('package:$_packageName/${scriptUri.substring(_uriPrefix.length)}'); if (scriptUri.startsWith(uriPrefix)) {
return Uri.parse('package:$_packageName/${scriptUri.substring(uriPrefix.length)}');
}
} }
return null; return null;
} }
static Uri findUri(String scriptPath, String packagesPath) { static Uri findUri(String scriptPath, String packagesPath, String fileSystemScheme, List<String> fileSystemRoots) {
return PackageUriMapper(scriptPath, packagesPath).map(scriptPath); return PackageUriMapper(scriptPath, packagesPath, fileSystemScheme, fileSystemRoots).map(scriptPath);
} }
} }
...@@ -223,7 +231,7 @@ class KernelCompiler { ...@@ -223,7 +231,7 @@ class KernelCompiler {
Uri mainUri; Uri mainUri;
if (packagesPath != null) { if (packagesPath != null) {
command.addAll(<String>['--packages', packagesPath]); command.addAll(<String>['--packages', packagesPath]);
mainUri = PackageUriMapper.findUri(mainPath, packagesPath); mainUri = PackageUriMapper.findUri(mainPath, packagesPath, fileSystemScheme, fileSystemRoots);
} }
if (outputFilePath != null) { if (outputFilePath != null) {
command.addAll(<String>['--output-dill', outputFilePath]); command.addAll(<String>['--output-dill', outputFilePath]);
...@@ -256,7 +264,7 @@ class KernelCompiler { ...@@ -256,7 +264,7 @@ class KernelCompiler {
server.stderr server.stderr
.transform<String>(utf8.decoder) .transform<String>(utf8.decoder)
.listen((String message) { printError(message); }); .listen(printError);
server.stdout server.stdout
.transform<String>(utf8.decoder) .transform<String>(utf8.decoder)
.transform<String>(const LineSplitter()) .transform<String>(const LineSplitter())
...@@ -400,15 +408,20 @@ class ResidentCompiler { ...@@ -400,15 +408,20 @@ class ResidentCompiler {
// First time recompile is called we actually have to compile the app from // First time recompile is called we actually have to compile the app from
// scratch ignoring list of invalidated files. // scratch ignoring list of invalidated files.
PackageUriMapper packageUriMapper; PackageUriMapper packageUriMapper;
if (request.packagesFilePath != null) { if (request.packagesFilePath != null || _packagesPath != null) {
packageUriMapper = PackageUriMapper(request.mainPath, request.packagesFilePath); packageUriMapper = PackageUriMapper(
request.mainPath,
request.packagesFilePath ?? _packagesPath,
_fileSystemScheme,
_fileSystemRoots,
);
} }
if (_server == null) { if (_server == null) {
return _compile( return _compile(
_mapFilename(request.mainPath, packageUriMapper), _mapFilename(request.mainPath, packageUriMapper),
request.outputPath, request.outputPath,
_mapFilename(request.packagesFilePath, /* packageUriMapper= */ null) _mapFilename(request.packagesFilePath ?? _packagesPath, /* packageUriMapper= */ null)
); );
} }
......
...@@ -22,6 +22,7 @@ final Generator _kNoColorTerminalPlatform = () => FakePlatform.fromPlatform(cons ...@@ -22,6 +22,7 @@ final Generator _kNoColorTerminalPlatform = () => FakePlatform.fromPlatform(cons
void main() { void main() {
group(PackageUriMapper, () { group(PackageUriMapper, () {
group('single-root', () {
const String packagesContents = r''' const String packagesContents = r'''
xml:file:///Users/flutter_user/.pub-cache/hosted/pub.dartlang.org/xml-3.2.3/lib/ xml:file:///Users/flutter_user/.pub-cache/hosted/pub.dartlang.org/xml-3.2.3/lib/
yaml:file:///Users/flutter_user/.pub-cache/hosted/pub.dartlang.org/yaml-2.1.15/lib/ yaml:file:///Users/flutter_user/.pub-cache/hosted/pub.dartlang.org/yaml-2.1.15/lib/
...@@ -32,29 +33,50 @@ example:file:///example/lib/ ...@@ -32,29 +33,50 @@ example:file:///example/lib/
when(mockFileSystem.path).thenReturn(fs.path); when(mockFileSystem.path).thenReturn(fs.path);
when(mockFileSystem.file(any)).thenReturn(mockFile); when(mockFileSystem.file(any)).thenReturn(mockFile);
when(mockFile.readAsBytesSync()).thenReturn(utf8.encode(packagesContents)); when(mockFile.readAsBytesSync()).thenReturn(utf8.encode(packagesContents));
testUsingContext('Can map main.dart to correct package', () async { testUsingContext('Can map main.dart to correct package', () async {
final PackageUriMapper packageUriMapper = PackageUriMapper('/example/lib/main.dart', '.packages'); final PackageUriMapper packageUriMapper = PackageUriMapper('/example/lib/main.dart', '.packages', null, null);
expect(packageUriMapper.map('/example/lib/main.dart').toString(), 'package:example/main.dart'); expect(packageUriMapper.map('/example/lib/main.dart').toString(), 'package:example/main.dart');
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => mockFileSystem, FileSystem: () => mockFileSystem,
}); });
testUsingContext('Maps file from other package to null', () async { testUsingContext('Maps file from other package to null', () async {
final PackageUriMapper packageUriMapper = PackageUriMapper('/example/lib/main.dart', '.packages'); final PackageUriMapper packageUriMapper = PackageUriMapper('/example/lib/main.dart', '.packages', null, null);
expect(packageUriMapper.map('/xml/lib/xml.dart'), null); expect(packageUriMapper.map('/xml/lib/xml.dart'), null);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => mockFileSystem, FileSystem: () => mockFileSystem,
}); });
testUsingContext('Maps non-main file from same package', () async { testUsingContext('Maps non-main file from same package', () async {
final PackageUriMapper packageUriMapper = PackageUriMapper('/example/lib/main.dart', '.packages'); final PackageUriMapper packageUriMapper = PackageUriMapper('/example/lib/main.dart', '.packages', null, null);
expect(packageUriMapper.map('/example/lib/src/foo.dart').toString(), 'package:example/src/foo.dart'); expect(packageUriMapper.map('/example/lib/src/foo.dart').toString(), 'package:example/src/foo.dart');
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => mockFileSystem, FileSystem: () => mockFileSystem,
}); });
}); });
group('multi-root', () {
final MockFileSystem mockFileSystem = MockFileSystem();
final MockFile mockFile = MockFile();
when(mockFileSystem.path).thenReturn(fs.path);
when(mockFileSystem.file(any)).thenReturn(mockFile);
const String multiRootPackagesContents = r'''
xml:file:///Users/flutter_user/.pub-cache/hosted/pub.dartlang.org/xml-3.2.3/lib/
yaml:file:///Users/flutter_user/.pub-cache/hosted/pub.dartlang.org/yaml-2.1.15/lib/
example:org-dartlang-app:///lib/
''';
when(mockFile.readAsBytesSync()).thenReturn(utf8.encode(multiRootPackagesContents));
testUsingContext('Maps main file from same package on multiroot scheme', () async {
final PackageUriMapper packageUriMapper = PackageUriMapper('/example/lib/main.dart', '.packages', 'org-dartlang-app', <String>['/example', '/gen']);
expect(packageUriMapper.map('/example/lib/main.dart').toString(), 'package:example/main.dart');
}, overrides: <Type, Generator>{
FileSystem: () => mockFileSystem,
});
});
});
test(StdoutHandler, () async { test(StdoutHandler, () async {
final StdoutHandler stdoutHandler = StdoutHandler(); final StdoutHandler stdoutHandler = StdoutHandler();
stdoutHandler.handler('result 12345'); stdoutHandler.handler('result 12345');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment