Bundle assets used in packages#11751
Conversation
There was a problem hiding this comment.
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.
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.
=> hashValues(base, assetEntry, relativePath, source);
There was a problem hiding this comment.
I'm fine with adding trailing , but this file does not seem to adopt that convention...
There was a problem hiding this comment.
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.
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;
}There was a problem hiding this comment.
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.
final flutterManifest = ... ?
There was a problem hiding this comment.
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.
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.
|
I'm not entirely happy about the |
There was a problem hiding this comment.
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.
(c) how to know what string to use for "package"
There was a problem hiding this comment.
same nits for this class as the other class
There was a problem hiding this comment.
nit: trailing comma here and elsewhere
There was a problem hiding this comment.
include the same information here as earlier regarding what package does, how to use it, etc.
There was a problem hiding this comment.
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.
nit: never break a line that contains =>
There was a problem hiding this comment.
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.
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.
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.
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.
You can improve vertical alignment and consistency by always mentioning other first.
There was a problem hiding this comment.
Cut extra space before ##...
There was a problem hiding this comment.
... and paste it here.
There was a problem hiding this comment.
'packages/<package_name>/'
There was a problem hiding this comment.
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
| }); | ||
|
|
||
| testUsingContext('One package with asset variants', () async { | ||
| // // Setting flutterRoot here so that it picks up the MemoryFileSystem's |
| 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.
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.
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.
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.