Skip to content

Commit 43a691c

Browse files
authored
[infra] Move package:record_use here (#2812)
Moves `package:record_use` from the Dart SDK to this repo. * Fixes example in pkg/hooks to use the new API. * Fixes license headers. * Bump version to `0.5.0-wip`, changing the `Location`s to be optional to align with dart2js was a breaking change. https://dart-review.googlesource.com/c/sdk/+/416000 (We didn't publish a new version of the package.) * Set lints to the set used in this repo and address them. Also un-ignore the examples. * Skipping public members doc for now, we'll likely change everything. * Add the test data to the pub workspace so it analyzes against the latest versions of the package. * This will likely require a manual roll into the Dart SDK to make the tests work there again with the changed pubspecs. https://dart-review.googlesource.com/c/sdk/+/463662 Closes #2677
1 parent 73b32c4 commit 43a691c

54 files changed

Lines changed: 2314 additions & 4 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
name: "package:record_use"
3+
about: "Create a bug or file a feature request against package:record_use."
4+
labels: "package:record_use"
5+
---

.github/pr-title-checker-config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"[native_toolchain_c] ",
1818
"[objective_c] ",
1919
"[pub_formats] ",
20+
"[record_use] ",
2021
"[swift2objc] ",
2122
"[swiftgen] "
2223
],

pkgs/hooks/example/link/package_with_assets/hook/link.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ void main(List<String> args) async {
2020
await link(args, (input, output) async {
2121
final usages = input.usages;
2222

23-
final usedAssets = (usages.instancesOf(multiplyIdentifier) ?? []).map(
24-
(e) => (e.instanceConstant.fields.values.first as StringConstant).value,
25-
);
23+
final usedAssets = usages
24+
.constantsOf(multiplyIdentifier)
25+
.map((e) => e['assetName'] as String);
2626

2727
output.assets.data.addAll(
2828
input.assets.data.where(

pkgs/hooks/example/link/package_with_assets/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ dependencies:
1414
hooks: any
1515
logging: ^1.3.0
1616
meta: ^1.16.0
17-
record_use: ^0.3.0
17+
record_use: any
1818

1919
dev_dependencies:
2020
lints: ^6.0.0

pkgs/record_use/CHANGELOG.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## 0.5.0-wip
2+
3+
- Made locations optional to accomodate for dart2js compiler not providing
4+
source locations for constant instances.
5+
6+
## 0.4.2
7+
8+
- Fix empty instance parsing.
9+
10+
## 0.4.1
11+
12+
- Fix bug in signature parsing.
13+
14+
## 0.4.0
15+
16+
- Update SDK constraint to `^3.5.0`.
17+
- Rewrite API to expose less symbols.
18+
- Remove locations for easier caching.
19+
20+
## 0.3.0
21+
22+
- Make `InstanceConstant` a `Constant`.
23+
- Separate import from location uri.
24+
25+
## 0.2.0
26+
27+
- Use maps instead of lists in serialization.
28+
29+
## 0.1.1
30+
31+
- Fix repository link.
32+
33+
## 0.1.0
34+
35+
- Initial version.

pkgs/record_use/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2024, the Dart project authors.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following
11+
disclaimer in the documentation and/or other materials provided
12+
with the distribution.
13+
* Neither the name of Google LLC nor the names of its
14+
contributors may be used to endorse or promote products derived
15+
from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pkgs/record_use/README.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
> [!CAUTION]
2+
> This is an experimental package, and it's API can break at any time. Use at
3+
> your own discretion.
4+
5+
This package provides the data classes for the usage recording feature in the
6+
Dart SDK.
7+
8+
Dart objects with the `@RecordUse` annotation are being recorded at compile
9+
time, providing the user with information. The information depends on the object
10+
being recorded.
11+
12+
- If placed on a static method, the annotation means that arguments passed to
13+
the method will be recorded, as far as they can be inferred at compile time.
14+
- If placed on a class with a constant constructor, the annotation means that
15+
any constant instance of the class will be recorded. This is particularly useful
16+
when using the class as an annotation.
17+
18+
## Example
19+
20+
```dart
21+
import 'package:meta/meta.dart' show RecordUse;
22+
23+
void main() {
24+
print(SomeClass.stringMetadata(42));
25+
print(SomeClass.doubleMetadata(42));
26+
print(SomeClass.intMetadata(42));
27+
print(SomeClass.boolMetadata(42));
28+
}
29+
30+
class SomeClass {
31+
@RecordMetadata('leroyjenkins')
32+
@RecordUse()
33+
static stringMetadata(int i) {
34+
return i + 1;
35+
}
36+
37+
@RecordMetadata(3.14)
38+
@RecordUse()
39+
static doubleMetadata(int i) {
40+
return i + 1;
41+
}
42+
43+
@RecordMetadata(42)
44+
@RecordUse()
45+
static intMetadata(int i) {
46+
return i + 1;
47+
}
48+
49+
@RecordMetadata(true)
50+
@RecordUse()
51+
static boolMetadata(int i) {
52+
return i + 1;
53+
}
54+
}
55+
56+
@RecordUse()
57+
class RecordMetadata {
58+
final Object metadata;
59+
60+
const RecordMetadata(this.metadata);
61+
}
62+
63+
```
64+
This code will generate a data file that contains both the `metadata` values of
65+
the `RecordMetadata` instances, as well as the arguments for the different
66+
methods annotated with `@RecordUse()`.
67+
68+
This information can then be accessed in a link hook as follows:
69+
```dart
70+
import 'dart:convert';
71+
72+
import 'package:hooks/hooks.dart';
73+
import 'package:record_use/record_use_internal.dart';
74+
75+
final methodId = Identifier(
76+
uri: 'myfile.dart',
77+
name: 'myMethod',
78+
);
79+
80+
final classId = Identifier(
81+
uri: 'myfile.dart',
82+
name: 'myClass',
83+
);
84+
85+
void main(List<String> arguments){
86+
link(arguments, (config, output) async {
87+
final usesUri = config.recordedUses;
88+
final usesJson = await File,fromUri(usesUri).readAsString();
89+
final uses = UsageRecord.fromJson(jsonDecode(usesJson));
90+
91+
final args = uses.argumentsTo(methodId));
92+
//[args] is an iterable of arguments, in this case containing "42"
93+
94+
final fields = uses.instancesOf(classId);
95+
//[fields] is an iterable of the fields of the class, in this case
96+
//containing
97+
// {"arguments": "leroyjenkins"}
98+
// {"arguments": 3.14}
99+
// {"arguments": 42}
100+
// {"arguments": true}
101+
102+
... // Do something with the information, such as tree-shaking native assets
103+
});
104+
}
105+
```
106+
107+
## Limitations
108+
As this is designed to work on both web and native platforms, we have to adapt
109+
to the platform pecularities. One of them is that javascript does not support
110+
named arguments, so the dart2js compiler rewrites functions to only accept named
111+
parameters.
112+
While you can use named parameters to record functions, we advise caution as the
113+
retrieval behavior might change once we work around this dart2js limitation and
114+
implement separate positional and named parameters.
115+
116+
## Contributing
117+
Contributions are welcome! Please open an issue or submit a pull request.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
include: package:dart_flutter_team_lints/analysis_options.yaml
2+
3+
analyzer:
4+
language:
5+
strict-raw-types: true
6+
7+
linter:
8+
rules:
9+
- avoid_positional_boolean_parameters
10+
# TODO(dcharkes): Enable this lint once the API has stabilized.
11+
# - public_member_api_docs
12+
- prefer_const_declarations
13+
- prefer_expression_function_bodies
14+
- prefer_final_in_for_each
15+
- prefer_final_locals
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
export 'src/identifier.dart' show Identifier;
6+
export 'src/metadata.dart' show Metadata, MetadataExt;
7+
export 'src/record_use.dart' show ConstantInstance, RecordedUsages;
8+
export 'src/recorded_usage_from_file.dart' show parseFromFile;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
export 'src/constant.dart'
6+
show
7+
BoolConstant,
8+
Constant,
9+
InstanceConstant,
10+
IntConstant,
11+
ListConstant,
12+
MapConstant,
13+
NullConstant,
14+
PrimitiveConstant,
15+
StringConstant;
16+
export 'src/definition.dart' show Definition;
17+
export 'src/identifier.dart' show Identifier;
18+
export 'src/location.dart' show Location;
19+
export 'src/metadata.dart' show Metadata, MetadataExt;
20+
export 'src/record_use.dart' show RecordedUsages;
21+
export 'src/recordings.dart'
22+
show FlattenConstantsExtension, MapifyIterableExtension, Recordings;
23+
export 'src/reference.dart'
24+
show CallReference, CallTearOff, CallWithArguments, InstanceReference;
25+
export 'src/version.dart' show version;

0 commit comments

Comments
 (0)