copyAndGenerateFonts static method

Future<void> copyAndGenerateFonts(
  1. ArgResults argResults,
  2. ArgParser parser
)

Implementation

static Future<void> copyAndGenerateFonts(
    ArgResults argResults, ArgParser parser) async {
  final configFilePath = argResults['config'] as String?;
  final pubspecPath = argResults['pubspec'] as String?;

  if (configFilePath == null || pubspecPath == null) {
    throw OptionException('Missing required options.');
  }

  final fontCopier = Copier(configFilePath);

  try {
    await fontCopier.loadConfig();
    final fontFamilies = await fontCopier.loadFontFamilies();
    final packagePath = await findPackagePath();
    print(AnsiStyles.magentaBright("\nUsing package path: `$packagePath`"));

    const assetFontDir = 'assets/fonts'; // Fixed output directory

    final pubspecFile = await getPubspecFile(packagePath);
    final pubspecContent = await pubspecFile.readAsString();
    final pubspecYaml = loadYaml(pubspecContent) as YamlMap;

    for (var fontFamily in fontFamilies) {
      final fontFiles =
          fontCopier.findFontFiles(fontFamily, packagePath, pubspecYaml);
      fontCopier.copyFonts(fontFiles, fontFamily, assetFontDir);
    }

    await fontCopier.updatePubspec(fontFamilies, assetFontDir, pubspecPath);
    print(AnsiStyles.greenBright
        .bold("\n✔ Fonts copied and pubspec.yaml updated successfully.\n"));
  } catch (e) {
    if (e is PathNotFoundException) {
      print(AnsiStyles.redBright(
          '\n✖ The system cannot find the file `$pubspecPath` specified.'));
      print(AnsiStyles.yellowBright('\nRecommendation:\n'
          '1. Ensure that the `-p` or `--pubspec` option points to the correct location of your `pubspec.yaml` file.\n'
          '2. If you don\'t want to modify the `pubspec.yaml` file, you can create a custom `.yaml` file manually and \n'
          'copy the generated font map into your `pubspec.yaml` file, which is where the generated font yaml are supposed to be written.\n'));
    } else {
      print(AnsiStyles.redBright("\n✖ $e"));
    }
  }
}