loadEnv function

void loadEnv({
  1. String path = defaultConfigEnvPath,
  2. YamlMap? parsedDoc,
})

Loads the configuration file from path.

Implementation

void loadEnv({
  String path = defaultConfigEnvPath,
  YamlMap? parsedDoc,
}) {
  late YamlMap doc;
  if (parsedDoc == null) {
    var data = openString(path);
    if (data.isEmpty) {
      trace(
          'ERROR: "$defaultConfigEnvPath" file not found, creating from template.');
      createSampleConfig();
      exit(0);
    }
    configPath = p.canonicalize(path);
    doc = loadYaml(data);
  } else {
    doc = parsedDoc;
  }

  // config.intlEnabled = doc['intl']?['enabled'] ?? false;
  // config.outputJsonDir = doc['output_json_dir'] ?? '';
  if (doc['output_fts_utils'] != null && doc['output_fts_utils'] is! bool) {
    error('Invalid value for `output_fts_utils` in $path, must be a boolean.');
    exit(1);
  }
  config.outputJsonTemplate = doc['output_json_template'] ?? '';
  config.outputArbTemplate = doc['output_arb_template'] ?? '';
  config._configOutputTemplates();

  ///'output/assets/l10n'
  config.outputAndroidLocales = doc['output_android_locales'] ?? true;
  config.entryFile = doc['entry_file'] ?? '';
  config.dartOutputDir = doc['dart']?['output_dir'] ?? '';
  config.dartOutputFtsUtils = doc['dart']['output_fts_utils'] ?? false;
  config.dartTKeysId = doc['dart']?['keys_id'] ?? '';
  config.useDartMaps = doc['dart']?['use_maps'] ?? false;
  config.dartFormatLineLength = doc['dart']?['format_line_length'] ?? 0;
  config.dartTranslationsId = doc['dart']?['translations_id'] ?? '';
  config.paramOutputPattern = doc['param_output_pattern'] ?? '{*}';
  config.resolveLinkedKeys = doc['resolve_linked_keys'] ?? false;
  config.paramFtsUtilsArgsPattern =
      doc['dart']?['fts_utils_args_pattern'] ?? '%s';

  _configParamOutput();
  if (config.dartOutputDir.isNotEmpty) {
    config.dartOutputDir = p.canonicalize(config.dartOutputDir);
  }
  if (config.entryFile.isNotEmpty) {
    /// clean the URL now.
    config.entryFile = p.canonicalize(config.entryFile);
    config.inputYamlDir = p.dirname(config.entryFile);
    var f = File(config.entryFile);
    if (!f.existsSync()) {
      trace(
          '''ERROR: $defaultConfigEnvPath, [entry_file: "${config.entryFile}"] doesn't exists.
Please, create your data tree.''');
      exit(32);
    }
  }
  if (doc['locales'] != null) {
    final l = doc['locales'];
    if (l is YamlList) {
      config.locales = List<String>.from(l).toSet().toList(growable: false);
      config.locales =
          config.locales.map((e) => normLocale(e)).toList(growable: false);

      /// validate locale existence?
      print('Using config locales:');
      const sep = ' - ';
      for (var locale in config.locales) {
        trace(sep, langInfoFromKey(locale));
      }
      // config.locales.removeWhere((element) => element.isEmpty);
      // trace('config locales: ', config.locales);
    }
  }
  if (config.locales.isEmpty) {
    trace('''ERROR: $defaultConfigEnvPath: [locales:] not defined.
Add at least 1, like "en" or "es", reflecting the master language locale you use in [entry_file].
Remember, the first item in the [locales:] is the master locale... the one you use to translate to other locales.
This list takes all the locales you will process in GoogleSheet, and has to be valid locale names.
See https://cloud.google.com/translate/docs/languages for a list of supported translation locales.
''');
    exit(2);
  }

  if (doc['gsheets'] != null) {
    _parseSheets(doc['gsheets']);
    if (config.sheetId == null) {
      trace(
          'ERROR: $defaultConfigEnvPath: [gsheets:spreadsheet_id] not defined, get it from the GoogleSheet url: '
          'https://docs.google.com/spreadsheets/d/{HERE}');
      exit(2);
    }
    if (config.tableId == null) {
      config.tableId = 'Sheet1';
      trace(
          '$defaultConfigEnvPath: [gsheets:worksheet] not defined, will default to "Sheet1", make sure it matches.');
    }
    var sheetUrl =
        'https://docs.google.com/spreadsheets/d/${config.sheetId}/edit#gid=0';
    print('spreadsheet id:\n - ${magenta(config.sheetId!)}');
    // trace('Worksheet title: "', config.tableId, '"');
    trace('worksheet title:\n - ${magenta(config.tableId!)}');
    trace('🔗 click to edit sheet:\n - $sheetUrl');
  } else {
    trace(
        'ERROR: $defaultConfigEnvPath: [ghseets] configuration not found, please edit $defaultConfigEnvPath.');
    exit(1);
  }

  if (config.useIterativeCache) {
    warning('[config:gsheet:use_iterative_cache:] is enabled.');
//     warning(
//         '''WARNING: [config:gsheet:use_iterative_cache:] is enabled.
// For a performance boost on big datasets, the values inserted tries to use the GoogleTranslate formula once.
//
// Enable "iterative calculations" manually in your worksheet to avoid the #VALUE error.
//
// Go to:
// File > Spreadsheet Settings > Calculation > set "Iterative calculation" to "On"
//
// Or check:
// https://support.google.com/docs/answer/58515?hl=en&co=GENIE.Platform%3DDesktop#zippy=%2Cchoose-how-often-formulas-calculate
// ''');
  } else {
    trace('[config:gsheet:use_iterative_cache:] is disabled.');
  }
  // config.locales = doc?['locales'] ?? ['en'];
  // trace('env output path: ', config.outputDir);
  // if (config.outputDir.isEmpty) {
  //   throw 'config::outputDir is empty, please check _config_env.yaml file';
  // }
  if (config.entryFile.isEmpty) {
    trace('ERROR: $defaultConfigEnvPath: [entryFile] is empty, please add it.');
    exit(3);
  }
  // if (config.outputJsonDir.isEmpty) {
  //   trace(
  //       'ERROR: $defaultConfigEnvPath: [outputJsonDir] is empty, please add it.');
  //   exit(3);
  // }
  if (!config._isValidDartConfig()) {
    exit(3);
  }
}