Issue including files from different directories in a Dart code generator with build_runner (aggregate builder)

37 views Asked by At

First of all, I would like to emphasize that I am just learning about Aggregate Builder.I am working on a Dart code generator using build_runner, and I've encountered an issue when trying to include files from different directories in the code generation process. My goal is to examine all .dart files in the project, but it seems that buildStep.findAssets(Glob("/*.dart"))** only includes files within the conventional lib or test directories from flutter.

I have a code generator project, and a dartfrog backend that implements it

Code generator project

  • dependencies:

    • Dart SDK version: 3.0.3 (stable) (Wed May 31 15:35:05 2023 +0000) on "linux_x64"
    • build : 2.4.1
    • glob : 2.1.2
    • source_gen : 1.5.0
    • build_runner : 2.4.7
  • custom_builder.dart

// custom_builder.dart

import 'dart:async';
import 'package:build/build.dart';
import 'package:glob/glob.dart';

class CustomBuilder extends Builder {
  static final _scope = Glob('**/*.dart');

  @override
  FutureOr<void> build(BuildStep buildStep) async {
    await _findFilesAndBuild(buildStep);
    log.info("1.- ASSETS FOUND");
    await writeInFile(buildStep);
    log.info("2.- FILE WRITE SUCCESSFULL");
  }

  @override
  Map<String, List<String>> get buildExtensions => {
        r'$package$': [".custom.gen.temp"]
      };

  _findFilesAndBuild(BuildStep buildStep) async {
    await for (var fileAsset in buildStep.findAssets(_scope)) {
      log.info("CUSTOM BUILDER: file path ${fileAsset.path}");
      await _buildForFile(fileAsset, buildStep);
    }
  }

  _buildForFile(AssetId fileAsset, BuildStep buildStep) async {
    // logic for fileAsset
  }

  writeInFile(BuildStep buildStep) {}
}
  • build.yaml:
## build.yaml

builders:
  custom_generator:
    import: "package:custom_generator/custom_generator.dart"
    builder_factories: ["customBuilder"]
    build_extensions: { r'$package$': [ ".custom.gen.temp" ] }
    auto_apply: dependents
    build_to: source

Dart_frog backend

  • dependencies:
    • Dart SDK version: 3.0.3 (stable) (Wed May 31 15:35:05 2023 +0000) on "linux_x64"
    • dart_frog : ^1.0.0
    • build_runner : 2.4.7

I have the following directory tree:

|- config/
|    - config_class.dart
|- lib/
|    - lib_class1.dart
|- routes/
|    - index.dart
|    - route_class.dart
|- test/
|    - routes/
|    - test_class1.dart
|

after running the command dart run build_runner build --delete-conflicting-outputs -v on my dart frog project I get the following impressions:

[INFO] Build:Running build...
[INFO] custom_generator on $package$:CUSTOM BUILDER: file path lib/lib_class1.dart
[INFO] custom_generator on $package$:CUSTOM BUILDER: file path test/routes/index_test.dart
[INFO] custom_generator on $package$:CUSTOM BUILDER: file path test/test_class1.dart
[INFO] custom_generator on $package$:1.- ASSETS FOUND
[INFO] custom_generator on $package$:2.- FILE WRITE SUCCESSFULL testo
[INFO] Build:Running build completed, took 22ms

You can see that only printouts of files that belong to the lib or test directories appear, apparently buildStep.findAssets(_scope) does not include the other directories.

  • what am I doing wrong?
  • What configuration should I make to achieve my goal?

I attach the repository of the projects in question https://github.com/erick-ciudaz/code_generation_project

0

There are 0 answers