generate static method

Future<void> generate({
  1. String projectPath = '',
  2. String imagePath = 'assets/images',
  3. String codePath = 'lib/app_res',
  4. String codeName = 'app_image',
  5. String className = 'AppImages',
  6. bool sortByLength = false,
  7. bool verbose = true,
  8. void logger(
    1. String message
    )?,
})

Implementation

static Future<void> generate({
  String projectPath = '',
  String imagePath = 'assets/images',
  String codePath = 'lib/app_res',
  String codeName = 'app_image',
  String className = 'AppImages',
  bool sortByLength = false,
  bool verbose = true,
  void Function(String message)? logger,
}) async {
  _log(verbose, logger, 'BetterAssets: generation started.');

  final projectDir = _resolveProjectDirectory(projectPath);
  final imageDir = Directory(_joinPath(projectDir.path, imagePath));
  final outputFile = File(
    _joinPath(projectDir.path, '$codePath/$codeName.dart'),
  );

  _log(verbose, logger, 'BetterAssets: project path: ${projectDir.path}');
  _log(verbose, logger, 'BetterAssets: image path: ${imageDir.path}');
  _log(verbose, logger, 'BetterAssets: output file: ${outputFile.path}');

  if (!await imageDir.exists()) {
    _log(verbose, logger, 'BetterAssets: image directory not found.');
    throw FileSystemException('Image directory not found', imageDir.path);
  }

  final imageFiles = await imageDir
      .list(recursive: true, followLinks: false)
      .where((entity) => entity is File)
      .cast<File>()
      .toList();

  _log(
    verbose,
    logger,
    'BetterAssets: found ${imageFiles.length} image file(s).',
  );

  imageFiles.sort((a, b) {
    final aPath = _relativePath(a.path, imageDir.path);
    final bPath = _relativePath(b.path, imageDir.path);
    if (sortByLength) {
      final lengthCompare = aPath.length.compareTo(bPath.length);
      if (lengthCompare != 0) return lengthCompare;
    }
    return aPath.compareTo(bPath);
  });

  final entries = _buildEntries(imageFiles, imageDir.path, imagePath);
  for (final entry in entries) {
    _log(verbose, logger, 'BetterAssets: ${entry.name} -> ${entry.value}');
  }

  final output = _buildClass(className, imagePath, entries);

  await outputFile.parent.create(recursive: true);
  await outputFile.writeAsString(output);

  _log(
    verbose,
    logger,
    'BetterAssets: generation finished. Generated ${entries.length} asset constant(s) in ${outputFile.path}.',
  );
}