-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Bundle assets used in packages #11751
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
Conversation
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.
The possibly-null package parameter seems complicated enough to warrant a lot of dartdoc. Can't we do anything simpler? Like e.g. adding the packages/my_package/ prefix automatically in package code?
I'm not sure the term "package" (=Flutter package) would be readily understood by the average reader.
Added some style nits below.
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.
final
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.
final
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.
You could make this final, if you remove line 134 and then use packageManifestDescriptor['flutter'] directly in line 138.
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.
=> hashValues(base, assetEntry, relativePath, source);
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.
final
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.
Add trailing ,
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.
Add trailing ,
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.
I'm fine with adding trailing , but this file does not seem to adopt that convention...
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.
Our conventions change over time and we slowly transition the entire codebase over time. Always aim for the most up to date conventions (which ideally are described in the style guide).
You should feel empowered to apply new conventions to old code aggressively. For example I will often pepper trailing commas around the codebase as I'm doing other things.
You should feel empowered to proposed PRs for the Flutter style guide, especially for updating it to match explicit conventions you hear about in reviews, or implicit conventions you observe in the codebase. The style guide isn't complete.
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.
Add trailing ,
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.
Looking at other Flutter implementations of == it seems the convention is to do:
@override
bool operator ==(dynamic other) {
if (identical(other, this))
return true;
if (other.runtimeType != runtimeType)
return false;
final _Asset typedOther = other;
return typedOther.base == base
&& typedOther.assetEntry == assetEntry
&& typedOther.relativePath == relativePath
&& typedOther.source == source;
}
jakobr-google
left a comment
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.
LGTM.
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.
Nit: I'd remove the 'only'. The package argument can also be used in the app implementation, to refer to an asset in an imported package, but it must be specified in the package implementation.
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.
Yes, true.
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.
final flutterManifest = ... ?
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.
IIRC, we usually do something fancier to combine the hashcodes (multiply, xor, or shift), and make sure we stay in the 30-bit range.
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.
I think if you did if (other is! _Asset) return false;, then the analyzer should be smart enough to know that other is an _Asset below, so you wouldn't need the extra variable and cast.
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
|
I'm not entirely happy about the |
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.
nit: space before ?
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.
trailing comma
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.
update the docs
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.
this should include more details about:
(a) what this does
(b) how you write pubspec.yaml files to use this
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.
(c) how to know what string to use for "package"
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.
same nits for this class as the other class
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.
nit: trailing comma here and elsewhere
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.
include the same information here as earlier regarding what package does, how to use it, etc.
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.
ditto
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.
nit: indent by two less
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.
nit: compare runtimeType
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.
nit: vertically align the expressions (you can put the && at the start of the line too to make it prettier)
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.
nit: never break a line that contains =>
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.
use hashValues, not xor.
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.
I cannot use hashValues from dart:ui since the library is not available on the stand-alone VM. Do we have something else I can use instead?
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.
Hm, good point. I guess xor is probably fine. dart-lang/sdk#25217 is the bug for getting hashValues into dart:core... :-)
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.
style: if there's a line break after the (, there should be one before the )
|
This approach looks good! At some point we should probably convert the Flutter gallery to use this since its assets come from a package too. |
c6b6534 to
441bf7e
Compare
|
Thanks a lot for the comments! Could you please have another quick look? |
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.
Use of => with multi-line body should be avoided, e.g. by replacing with
@override
int get hashCode => base.hashCode ^ assetEntry.hashCode ^ relativePath.hashCode ^ source.hashCode;or
@override
int get hashCode {
return base.hashCode
^ assetEntry.hashCode
^ relativePath.hashCode
^ source.hashCode;
}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.
Indent by 2 less.
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.
You can improve vertical alignment and consistency by always mentioning other first.
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.
Cut extra space before ##...
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.
... and paste it here.
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.
Remove extra line?
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.
Above -- where?
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.
assets
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.
'packages/<package_name>/'
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.
nitty nit nit: we really prefer it when the code is vertically aligned cleanly, as in:
return base.hashCode
 ^ assetEntry.hashCode
^ relativePath.hashCode
^ source.hashCode;
```|
This approach looks great. Just needs tests. Thanks! |
|
Thanks! I am working on the tests. |
46ef4e4 to
81bbac8
Compare
mravn-google
left a comment
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.
LGTM
| }); | ||
|
|
||
| testUsingContext('One package with asset variants', () async { | ||
| // // Setting flutterRoot here so that it picks up the MemoryFileSystem's |
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.
Remove extra //.
| testUsingContext('One package with no assets', () async { | ||
| // Setting flutterRoot here so that it picks up the MemoryFileSystem's | ||
| // path separator. | ||
| Cache.flutterRoot = getFlutterRoot(); |
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.
Maybe move this to an establishFlutterRoot function so you don't have to duplicate the comment in every test.
| fs.file('p/p/$asset') | ||
| ..createSync(recursive: true) | ||
| ..writeAsStringSync(asset); | ||
| } |
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.
Maybe extract this loop as a writeAssets function too?
|
|
||
| writePubspecFile('pubspec.yaml', 'test'); | ||
| writePackagesFile('test_package:p/p/lib/\ntest_package2:p2/p/lib/'); | ||
| writePubspecFile('p/p/pubspec.yaml', 'test_package', |
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.
Move first and second parameter to their own lines.
Update to reflect #11751 and flutter/website#663
Update to reflect #11751 and flutter/website#663
This PR solves the problem of including package code that makes use of assets.
pubspec.yamlof the package.For instance if a package
my_packagecontains a directory with an image:assets/kitten.pngand its
pubspec.yamlhasthen we add an entry called
packages/my_package/assets/kitten.png.packageparameter. The idea is that package code should use this extra parameter to specify the name of the package so that it can be used when resolving the image.For instance the package code could be:
As an alternative to 2) we could require package developers to write
new Image.asset('packages/my_package/assets/kitten.jpg')I will work on some tests for this but would love some comments on the design.