Skip to content
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 2 commits into from
Aug 12, 2024

Conversation

davidmorgan
Copy link
Contributor

@davidmorgan davidmorgan commented Aug 7, 2024

So we can identify macros based on pubspecs while we wait for that to be implemented properly.

Not used yet as that would overlap with other pending PRs, but it should be easy to plug in afterwards.

packageName = packageName.substring('package:'.length);
packageName = packageName.substring(0, packageName.indexOf('/'));
} else {
throw ArgumentError('Name must start "package:" and have a path: $name');
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

import 'package:package_config/package_config.dart' as package_config;

/// Reads a package config to determine information about macros.
class PackageConfig {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets also call this something other than PackageConfig. Really, it is something that reads a package config and extracts information about macros from it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess: MacroPackageConfig :) done.


/// Reads a package config to determine information about macros.
class PackageConfig {
final Uri uri;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably just take in a PackageConfig instance - and have a factory constructor or static method which can construct that from a URI. That will make it more testable and avoid re-reading/parsing it on every call to lookupMacroImplementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines 62 to 64
final pubspecUri = packageUri.replace(
pathSegments:
packageUri.pathSegments.followedBy(['pubspec.yaml']).toList());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to just do:

Suggested change
final pubspecUri = packageUri.replace(
pathSegments:
packageUri.pathSegments.followedBy(['pubspec.yaml']).toList());
final pubspecUri = packageUri.resolve('pubspec.yaml');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely tried that! But now I try it again it works. Weird. I wonder how I screwed up :)

Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly you didn't have a trailing / originally for the root URI. Afaik though the package config spec requires this trailing slash.

}
final packageConfig = package_config.PackageConfig.parseBytes(
File.fromUri(uri).readAsBytesSync(), uri);
final libraryQualifiedName =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This is really a path not just a name. It also doesn't include the lib/ which it should, to allow macros to be defined outside of lib/ (and below, I would expect the full path not the lib relative path in the # macro comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@davidmorgan davidmorgan force-pushed the read-pubspec branch 4 times, most recently from ffa1ad0 to a5f0f58 Compare August 8, 2024 10:34
@davidmorgan
Copy link
Contributor Author

Thanks!

Comments addressed, and I added another commit that actually uses the new code, PTAL.

Comment on lines 52 to 55
final matchingPackage = packageConfig.packages
.where((p) => p.name == packageName)
.toList()
.singleOrNull;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sure it doesn't matter all that much, but creating a closure + iterable + list here does bug me :).

Luckily, PackageConfig implements the [] operator :).

Suggested change
final matchingPackage = packageConfig.packages
.where((p) => p.name == packageName)
.toList()
.singleOrNull;
final matchingPackage = packageConfig[packageName];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@davidmorgan
Copy link
Contributor Author

Rebasing on top of the CFE PR it turns out the package config class has exactly what I need to remove the hack over there, resolving file:// URIs based on package config. So, updated to add that and also work for the CFE :) PTAL.

@davidmorgan davidmorgan merged commit 0b951a9 into dart-lang:main Aug 12, 2024
39 checks passed
@davidmorgan davidmorgan deleted the read-pubspec branch August 12, 2024 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants