-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Placeholder package config reader. #29
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) 2024, 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. | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:dart_model/dart_model.dart'; | ||
import 'package:package_config/package_config.dart'; | ||
|
||
/// Reads a package config to determine information about macros. | ||
class MacroPackageConfig { | ||
final PackageConfig packageConfig; | ||
|
||
MacroPackageConfig({required this.packageConfig}); | ||
|
||
factory MacroPackageConfig.readFromUri(Uri uri) => MacroPackageConfig( | ||
packageConfig: | ||
PackageConfig.parseBytes(File.fromUri(uri).readAsBytesSync(), uri)); | ||
|
||
/// Checks whether [name] is a macro annotation. | ||
/// | ||
/// If so, returns the qualified name of the macro implementation. | ||
/// | ||
/// If not, returns `null`. | ||
/// | ||
/// This is a placeholder implementation until `language/3728` is | ||
/// implemented. It expects macros to be marked by a comment in the | ||
/// annotation package `pubspec.yaml` that looks like this: | ||
/// | ||
/// ``` | ||
/// # macro <annotation name> <implementation name> | ||
/// ``` | ||
/// | ||
/// For example: | ||
/// | ||
/// ``` | ||
/// # macro lib/declare_x_macro.dart#DeclareX package:_test_macros/declare_x_macro.dart#DeclareXImplementation | ||
/// ``` | ||
QualifiedName? lookupMacroImplementation(QualifiedName name) { | ||
var packageName = name.uri; | ||
if (packageName.startsWith('dart:') || | ||
packageName.startsWith('org-dartlang-sdk:')) { | ||
return null; | ||
} | ||
// TODO(davidmorgan): error handling when lookup fails. | ||
if (packageName.startsWith('file:')) { | ||
packageName = | ||
packageConfig.toPackageUri(Uri.parse(packageName)).toString(); | ||
} | ||
final libraryPathAndName = | ||
'lib/${packageName.substring(packageName.indexOf('/') + 1)}#${name.name}'; | ||
if (packageName.startsWith('package:') && packageName.contains('/')) { | ||
packageName = packageName.substring('package:'.length); | ||
packageName = packageName.substring(0, packageName.indexOf('/')); | ||
} else { | ||
// TODO(davidmorgan): support macros outside lib dirs. | ||
throw ArgumentError('Name must start "package:" and have a path: $name'); | ||
} | ||
|
||
final matchingPackage = packageConfig[packageName]; | ||
if (matchingPackage == null) { | ||
throw StateError('Package "$packageName" not found in package config.'); | ||
} | ||
|
||
// TODO(language/3728): read macro annotation identifiers from package | ||
// config. Until then, check the pubsec, to simulate what that feature will | ||
// do. | ||
final packageUri = matchingPackage.root; | ||
final pubspecUri = packageUri.resolve('pubspec.yaml'); | ||
final lines = File.fromUri(pubspecUri).readAsLinesSync(); | ||
|
||
final implsByLibraryQualifiedName = <String, String>{}; | ||
for (final line in lines) { | ||
if (!line.startsWith('# macro ')) continue; | ||
final items = line.split(' '); | ||
// The rest of the line should be the library qualified name of the | ||
// annotation then the fully qualified name of the implementation. | ||
implsByLibraryQualifiedName[items[2]] = items[3]; | ||
} | ||
|
||
final result = implsByLibraryQualifiedName[libraryPathAndName]; | ||
return result == null ? null : QualifiedName(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) 2024, 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. | ||
|
||
import 'dart:io'; | ||
import 'dart:isolate'; | ||
|
||
import 'package:_macro_host/src/package_config.dart'; | ||
import 'package:dart_model/dart_model.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group(MacroPackageConfig, () { | ||
test('can look up macro implementations from package URIs', () async { | ||
final packageConfig = | ||
MacroPackageConfig.readFromUri(Isolate.packageConfigSync!); | ||
|
||
expect( | ||
packageConfig | ||
.lookupMacroImplementation(QualifiedName( | ||
'package:_test_macros/declare_x_macro.dart#DeclareX'))! | ||
.string, | ||
'package:_test_macros/declare_x_macro.dart#DeclareXImplementation'); | ||
}); | ||
|
||
test('can look up macro implementations from file URIs', () async { | ||
final packageConfig = | ||
MacroPackageConfig.readFromUri(Isolate.packageConfigSync!); | ||
|
||
final sourceFileUri = Directory.current.uri | ||
.resolve('../_test_macros/lib/declare_x_macro.dart'); | ||
|
||
expect( | ||
packageConfig | ||
.lookupMacroImplementation( | ||
QualifiedName('$sourceFileUri#DeclareX'))! | ||
.string, | ||
'package:_test_macros/declare_x_macro.dart#DeclareXImplementation'); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have a TODO here to look up the package name by URI in the package config, to support macros defined outside of
lib
dirs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.