addDependencies static method

Future<bool> addDependencies(
  1. String package, {
  2. String? version,
  3. bool isDev = false,
  4. bool runPubGet = true,
  5. bool isPubSiteCN = true,
})

Implementation

static Future<bool> addDependencies(String package,
    {String? version,
    bool isDev = false,
    bool runPubGet = true,
    bool isPubSiteCN = true}) async {
  var pubSpec = PubSpec.fromYamlString(_pubspecFile.readAsStringSync());

  if (containsPackage(package, isDev)) {
    LogService.info(
        LocaleKeys.ask_package_already_installed.trArgs([package]),
        false,
        false);
    final menu = Menu(
      [
        LocaleKeys.options_yes.tr,
        LocaleKeys.options_no.tr,
      ],
    );
    final result = menu.choose();
    if (result.index != 0) {
      return false;
    }
  }

  version = version == null || version.isEmpty
      ? await PubDevApi.getLatestVersionFromPackage(package,
          isPubSiteCN: isPubSiteCN)
      : '^$version';
  if (version == null) return false;
  if (isDev) {
    pubSpec.devDependencies[package] = HostedReference.fromJson(version);
  } else {
    pubSpec.dependencies[package] = HostedReference.fromJson(version);
  }
  _savePub(pubSpec);
  if (runPubGet) await ShellUtils.pubGet();
  LogService.success(LocaleKeys.success_package_installed.trArgs([package]));
  return true;
}