Files
sdk/pkg/record_use
Sigurd Meldgaard 2fe05bd568 Reland "Migrate to use pub workspace"
This is a reland of commit b9b77058a9

Original change's description:
> Migrate to use pub workspace
>
> Use `pub get` to generate `.dart_tool/package_config.json` on gclient sync.
>
> All pkg/ (and a few third_party) packages that are developed inside the sdk repo are included in the workspace from the root `pubspec.yaml`.
>
> All dependencies that are pulled in via DEPS are added as path dependencies via `dependency_overrides` in the root `pubspec.yaml`.
>
> Bug: https://github.com/dart-lang/sdk/issues/56220
> Change-Id: I38c12b608c68da54c57821116cf9aa6696936746
> Tested: relies on CQ of existing tests. Should have no effect on functionality
> CoreLibraryReviewExempt: only core library change is adding a `// ignore:` comment. Should have no influence on functionality
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/397164
> Commit-Queue: Sigurd Meldgaard <sigurdm@google.com>
> Reviewed-by: Alexander Thomas <athom@google.com>

Bug: https://github.com/dart-lang/sdk/issues/56220
Change-Id: I29afabade2d2447dea05121cb87ff50bb21a4b76
Cq-Include-Trybots: luci.dart.try:flutter-linux-try,flutter-web-try
Tested: relies on CQ of existing tests. Should have no effect on functionality
CoreLibraryReviewExempt: only core library change is adding a `//
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/415561
Commit-Queue: Sigurd Meldgaard <sigurdm@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
2025-03-20 06:19:16 -07:00
..
2025-01-08 20:05:37 -08:00
2024-09-12 08:47:53 +00:00

Caution

This is an experimental package, and it's API can break at any time. Use at your own discretion.

This package provides the data classes for the usage recording feature in the Dart SDK.

Dart objects with the @RecordUse annotation are being recorded at compile time, providing the user with information. The information depends on the object being recorded.

  • If placed on a static method, the annotation means that arguments passed to the method will be recorded, as far as they can be inferred at compile time.
  • If placed on a class with a constant constructor, the annotation means that any constant instance of the class will be recorded. This is particularly useful when using the class as an annotation.

Example

import 'package:meta/meta.dart' show RecordUse;

void main() {
  print(SomeClass.stringMetadata(42));
  print(SomeClass.doubleMetadata(42));
  print(SomeClass.intMetadata(42));
  print(SomeClass.boolMetadata(42));
}

class SomeClass {
  @RecordMetadata('leroyjenkins')
  @RecordUse()
  static stringMetadata(int i) {
    return i + 1;
  }

  @RecordMetadata(3.14)
  @RecordUse()
  static doubleMetadata(int i) {
    return i + 1;
  }

  @RecordMetadata(42)
  @RecordUse()
  static intMetadata(int i) {
    return i + 1;
  }

  @RecordMetadata(true)
  @RecordUse()
  static boolMetadata(int i) {
    return i + 1;
  }
}

@RecordUse()
class RecordMetadata {
  final Object metadata;

  const RecordMetadata(this.metadata);
}

This code will generate a data file that contains both the metadata values of the RecordMetadata instances, as well as the arguments for the different methods annotated with @RecordUse().

This information can then be accessed in a link hook as follows:

import 'dart:convert';

import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:record_use/record_use_internal.dart';

final methodId = Identifier(
  uri: 'myfile.dart',
  name: 'myMethod',
);

final classId = Identifier(
  uri: 'myfile.dart',
  name: 'myClass',
);

void main(List<String> arguments){
  link(arguments, (config, output) async {
    final usesUri = config.recordedUses;
    final usesJson = await File,fromUri(usesUri).readAsString();
    final uses = UsageRecord.fromJson(jsonDecode(usesJson));

    final args = uses.argumentsTo(methodId));
    //[args] is an iterable of arguments, in this case containing "42"

    final fields = uses.instancesOf(classId);
    //[fields] is an iterable of the fields of the class, in this case
    //containing
    // {"arguments": "leroyjenkins"}
    // {"arguments": 3.14}
    // {"arguments": 42}
    // {"arguments": true}

    ... // Do something with the information, such as tree-shaking native assets
  });
}

Contributing

Contributions are welcome! Please open an issue or submit a pull request.