updateLauncherName method

  1. @override
void updateLauncherName()
override

Implementation

@override
void updateLauncherName() {
  final ohosRoot = Directory(context.ohosPath);
  if (!ohosRoot.existsSync()) {
    common.logSkipped(
      "OHOS launcher name",
      context.ohosPath,
      "directory does not exist",
    );
    return;
  }

  final defaultName = common.fetchLauncherName(context);
  final overrides = _fetchOhosNameOverrides();
  final baseName = overrides["base"] ?? defaultName;

  final ohPackagePath = _joinPath(ohosRoot.path, "oh-package.json5");
  if (common.fileExists(ohPackagePath)) {
    final ohPackageData = common.readFile(ohPackagePath);
    final currentOhosName =
        _extractCurrentNameFromJson5(ohPackageData, ohPackagePath);
    final updatedOhPackageData = _replaceTopLevelNameValue(
      ohPackageData,
      defaultName,
      ohPackagePath,
    );
    common.overwriteFile(ohPackagePath, updatedOhPackageData);

    final appJson5Path = _joinPath(ohosRoot.path, "AppScope/app.json5");
    if (common.fileExists(appJson5Path)) {
      final appJson5Data = common.readFile(appJson5Path);
      final updatedAppJson5Data = _updateBundleName(
        appJson5Data,
        currentOhosName,
        defaultName,
        appJson5Path,
      );
      common.overwriteFile(appJson5Path, updatedAppJson5Data);
    } else {
      common.logSkipped(
          "OHOS bundleName", appJson5Path, "file does not exist");
    }
  } else {
    common.logSkipped(
      "OHOS package name",
      ohPackagePath,
      "file does not exist",
    );
  }

  final appScopeBaseStringPath = _joinPath(
    ohosRoot.path,
    "AppScope/resources/base/element/string.json",
  );
  _updateStringResourceValueIfPresent(
    appScopeBaseStringPath,
    "app_name",
    baseName,
  );

  final entryResourcesRoot =
      _joinPath(ohosRoot.path, "entry/src/main/resources");
  final existingResourceFiles =
      _collectExistingEntryStringFiles(entryResourcesRoot);

  for (final entry in existingResourceFiles.entries) {
    final folderName = entry.key;
    final filePath = entry.value;
    final targetName =
        _resolveNameForFolder(folderName, defaultName, overrides);
    _updateStringResourceValueIfPresent(
      filePath,
      "EntryAbility_label",
      targetName,
    );
  }

  for (final localeEntry in overrides.entries) {
    final normalizedLocale = localeEntry.key;
    final targetName = localeEntry.value;
    final folderName = _formatLocaleFolder(normalizedLocale);
    final expectedFilePath = _joinPath(
      entryResourcesRoot,
      "$folderName${Platform.pathSeparator}element${Platform.pathSeparator}string.json",
    );

    if (common.fileExists(expectedFilePath)) {
      common.logSkipped(
        "EntryAbility_label for locale $normalizedLocale",
        expectedFilePath,
        "resource file already exists",
      );
      continue;
    }

    _createEntryStringFile(expectedFilePath, targetName);
    common.logReplaced(
      "EntryAbility_label for locale $normalizedLocale",
      expectedFilePath,
      matchCount: 1,
    );
  }
}