File tree Expand file tree Collapse file tree 8 files changed +74
-54
lines changed
hooks/example/link/package_with_assets Expand file tree Collapse file tree 8 files changed +74
-54
lines changed Original file line number Diff line number Diff line change @@ -11,18 +11,31 @@ import 'package:data_assets/data_assets.dart';
1111import 'package:hooks/hooks.dart' ;
1212import 'package:record_use/record_use.dart' ;
1313
14- const multiplyIdentifier = Identifier (
14+ const someMethodIdentifier = Identifier (
1515 importUri: 'package:package_with_assets/package_with_assets.dart' ,
16- name: 'AssetUsed ' ,
16+ name: 'someMethod ' ,
1717);
1818
19+ const someOtherMethodIdentifier = Identifier (
20+ importUri: 'package:package_with_assets/package_with_assets.dart' ,
21+ name: 'someOtherMethod' ,
22+ );
23+
24+ final assetMapping = {
25+ someMethodIdentifier: 'assets/used_asset.json' ,
26+ someOtherMethodIdentifier: 'assets/unused_asset.json' ,
27+ };
28+
1929void main (List <String > args) async {
2030 await link (args, (input, output) async {
2131 final usages = input.usages;
2232
23- final usedAssets = usages
24- .constantsOf (multiplyIdentifier)
25- .map ((e) => e['assetName' ] as String );
33+ final usedAssets = [
34+ for (final entry in assetMapping.entries)
35+ if (usages.constArgumentsFor (entry.key).isNotEmpty ||
36+ usages.hasNonConstArguments (entry.key))
37+ entry.value,
38+ ];
2639
2740 output.assets.data.addAll (
2841 input.assets.data.where (
Original file line number Diff line number Diff line change @@ -10,19 +10,9 @@ import 'package:meta/meta.dart';
1010//also https://github.com/dart-lang/sdk/issues/54003.
1111
1212/// A method that uses an asset.
13- @AssetUsed ( 'assets/used_asset.json' )
13+ @RecordUse ( )
1414String someMethod () => 'Using used_asset' ;
1515
1616/// Another method that uses an asset.
17- @AssetUsed ('assets/unused_asset.json' )
18- String someOtherMethod () => 'Using unused_asset' ;
19-
20- /// An annotation to mark that an asset is used.
2117@RecordUse ()
22- class AssetUsed {
23- /// The name of the asset being used.
24- final String assetName;
25-
26- /// Creates an [AssetUsed] annotation.
27- const AssetUsed (this .assetName);
28- }
18+ String someOtherMethod () => 'Using unused_asset' ;
Original file line number Diff line number Diff line change 55import 'package:drop_data_asset/drop_data_asset.dart' ;
66
77void main (List <String > arguments) {
8- print ('Hello world: ${MyMath . double ( 3 )}!' );
8+ print ('Hello world: ${const Double ( 3 ). run ( )}!' );
99}
Original file line number Diff line number Diff line change @@ -46,17 +46,21 @@ void main(List<String> arguments) async {
4646 }
4747 }
4848
49- // Tree-shake unused assets
50- final instances = usages.constantsOf (
51- Identifier (
52- importUri: 'package:${input .packageName }/src/${input .packageName }.dart' ,
53- name: 'RecordCallToC' ,
54- ),
55- );
56- for (final instance in instances) {
57- final symbol = instance['symbol' ] as String ;
58- print ('An instance of "$instance " was found with the field "$symbol "' );
59- symbols.add (symbol);
49+ // Tree-shake unused assets using instances
50+ for (final className in ['Double' , 'Square' ]) {
51+ final instances = usages.constantsOf (
52+ Identifier (
53+ importUri:
54+ 'package:${input .packageName }/src/${input .packageName }.dart' ,
55+ name: className,
56+ ),
57+ );
58+ print ('Checking instances of $className ...' );
59+ for (final instance in instances) {
60+ print ('An instance of "$className " was found: $instance ' );
61+ // Map class name to asset symbol (lowercase)
62+ symbols.add (className.toLowerCase ());
63+ }
6064 }
6165
6266 final neededCodeAssets = [
Original file line number Diff line number Diff line change @@ -12,17 +12,20 @@ class MyMath {
1212
1313 @RecordUse ()
1414 static int multiply (int a, int b) => a * b;
15+ }
1516
16- @RecordCallToC ('double' )
17- static int double (int a) => a + a;
17+ @RecordUse ()
18+ class Double {
19+ final int value;
20+ const Double (this .value);
1821
19- @RecordCallToC ('square' )
20- static int square (int a) => a * a;
22+ int run () => value + value;
2123}
2224
2325@RecordUse ()
24- class RecordCallToC {
25- final String symbol;
26+ class Square {
27+ final int value;
28+ const Square (this .value);
2629
27- const RecordCallToC ( this .symbol) ;
30+ int run () => value * value ;
2831}
Original file line number Diff line number Diff line change 55import 'package:drop_dylib_recording/drop_dylib_recording.dart' ;
66
77void main (List <String > arguments) {
8- print ('Hello world: ${MyMath . double ( 3 )}!' );
8+ print ('Hello world: ${const Double ( 3 ). run ( )}!' );
99}
Original file line number Diff line number Diff line change @@ -51,16 +51,23 @@ void main(List<String> arguments) async {
5151
5252 argumentsFile.writeAsStringSync (dataLines.join ('\n ' ));
5353
54- // Tree-shake unused assets
55- final instances = usages.constantsOf (
56- const Identifier (
57- importUri: 'package:drop_dylib_recording/src/drop_dylib_recording.dart' ,
58- name: 'RecordCallToC' ,
59- ),
60- );
61- for (final instance in instances) {
62- final symbol = instance['symbol' ] as String ;
63- symbols.add (symbol);
54+ // Tree-shake unused assets using instances
55+ for (final className in ['Double' , 'Square' ]) {
56+ final instances = usages.constantsOf (
57+ Identifier (
58+ importUri:
59+ 'package:drop_dylib_recording/src/drop_dylib_recording.dart' ,
60+ name: className,
61+ ),
62+ );
63+ for (final instance in instances) {
64+ print ('An instance of "$className " was found: $instance ' );
65+ if (className == 'Double' ) {
66+ symbols.add ('add' );
67+ } else if (className == 'Square' ) {
68+ symbols.add ('multiply' );
69+ }
70+ }
6471 }
6572
6673 final neededCodeAssets = [
Original file line number Diff line number Diff line change @@ -14,17 +14,20 @@ class MyMath {
1414
1515 @RecordUse ()
1616 static int multiply (int a, int b) => bindings.multiply (a, b);
17+ }
1718
18- @RecordCallToC ('add' )
19- static int double (int a) => bindings.add (a, a);
19+ @RecordUse ()
20+ class Double {
21+ final int value;
22+ const Double (this .value);
2023
21- @RecordCallToC ('multiply' )
22- static int square (int a) => bindings.multiply (a, a);
24+ int run () => bindings.add (value, value);
2325}
2426
2527@RecordUse ()
26- class RecordCallToC {
27- final String symbol;
28+ class Square {
29+ final int value;
30+ const Square (this .value);
2831
29- const RecordCallToC ( this .symbol );
32+ int run () => bindings. multiply (value, value );
3033}
You can’t perform that action at this time.
0 commit comments