installFromDirectory method

Directory? installFromDirectory({
  1. required Directory directoryPackage,
  2. required String? archiveSha256,
  3. bool deleteIfExist = true,
})

Implementation

Directory? installFromDirectory({
  required Directory directoryPackage,
  required String? archiveSha256,
  bool deleteIfExist = true,
}) {
  if (Dart.isWeb) {
    return null;
  }
  final File file_pubspec =
      File(path.join(directoryPackage.path, "pubspec.yaml"));
  if (file_pubspec.existsSync() == false) {
    return null;
  }
  final Map yaml_code =
      (yaml.loadYaml(file_pubspec.readAsStringSync(), recover: true) as Map);
  // /home/galaxeus/.pub-cache/hosted/pub.dev/.cache/telegram_client-versions.json
  {
    final File file_pub_dev_versions = File(path.join(hosted_directory.path,
        "pub.dev", ".cache", "${yaml_code["name"]}-versions.json"));
    if (file_pub_dev_versions.parent.existsSync() == false) {
      file_pub_dev_versions.parent.createSync(recursive: true);
    }
    final Map jsonPubDevVersion = () {
      try {
        return json.decode((file_pub_dev_versions.readAsStringSync())) as Map;
      } catch (e) {}
      return {};
    }();
    if (jsonPubDevVersion["name"] != yaml_code["name"]) {
      jsonPubDevVersion["name"] = yaml_code["name"];
    }
    {
      try {
        final fetchedAt =
            DateTime.tryParse(jsonPubDevVersion["_fetchedAt"]) ??
                DateTime.now();
        jsonPubDevVersion["_fetchedAt"] = fetchedAt.toIso8601String();
      } catch (e) {
        jsonPubDevVersion["_fetchedAt"] = DateTime.now().toIso8601String();
      }
    }

    final String archive_sha256 = () {
      final String archive_sha256 = (archiveSha256 ?? "").trim();
      if (jsonPubDevVersion["latest"] is Map) {
        if (jsonPubDevVersion["latest"]["archive_sha256"] is String &&
            (jsonPubDevVersion["latest"]["archive_sha256"] as String)
                .isNotEmpty) {
          return jsonPubDevVersion["latest"]["archive_sha256"];
        }
      }
      if (archive_sha256.isEmpty) {
        return "b52f94533ec4a8ab5d1769fd7fca391fe6b79e19d9c207ac7a93a64dfb3c8be0";
      }
      return archive_sha256;
    }();
    {
      final File file_pub_dev_hash = File(path.join(
          hosted_hashes_directory.path,
          "pub.dev",
          "${yaml_code["name"]}-${yaml_code["version"]}.sha256"));
      if (file_pub_dev_hash.parent.existsSync() == false) {
        file_pub_dev_hash.parent.createSync(recursive: true);
      }
      file_pub_dev_hash.writeAsStringSync(archive_sha256);
    }
    final String published = () {
      final DateTime dateTimeBefore =
          DateTime.now().subtract(Duration(minutes: 10));
      if (jsonPubDevVersion["latest"] is Map) {
        if (jsonPubDevVersion["latest"]["published"] is String &&
            (jsonPubDevVersion["latest"]["published"] as String).isNotEmpty) {
          return jsonPubDevVersion["latest"]["published"];
        }
      }
      return dateTimeBefore.toIso8601String();
    }();

    final Map latest_pub_dev_version = {
      "version": yaml_code["version"],
      "pubspec": yaml_code,
      "archive_url":
          "https://pub.dev/api/archives/${yaml_code["name"]}-${yaml_code["version"]}.tar.gz",
      "archive_sha256": archive_sha256,
      "published": published,
    };

    if (jsonPubDevVersion["latest"] is Map == false) {
      jsonPubDevVersion["latest"] = latest_pub_dev_version;
    } else {
      jsonPubDevVersion["latest"] = latest_pub_dev_version;
    }
    if (jsonPubDevVersion["versions"] is List == false) {
      jsonPubDevVersion["versions"] = [];
    }
    final List pub_dev_package_versions = jsonPubDevVersion["versions"];
    bool isNotFoundUpdate = true;
    for (int i = 0; i < pub_dev_package_versions.length; i++) {
      final pub_dev_package_version = pub_dev_package_versions[i];
      if (pub_dev_package_version is Map) {
        if (pub_dev_package_version["version"] ==
            latest_pub_dev_version["version"]) {
          pub_dev_package_versions[i] = latest_pub_dev_version;

          isNotFoundUpdate = false;
          break;
        }
      }
    }

    if (isNotFoundUpdate) {
      pub_dev_package_versions.add(latest_pub_dev_version);
    }

    jsonPubDevVersion["versions"] = pub_dev_package_versions;
    file_pub_dev_versions.writeAsStringSync(json.encode(jsonPubDevVersion));
  }
  final Directory directory_pub_dev = Directory(path.join(
      hosted_directory.path,
      "pub.dev",
      "${yaml_code["name"]}-${yaml_code["version"]}"));
  if (deleteIfExist && directory_pub_dev.existsSync()) {
    directory_pub_dev.deleteSync(recursive: true);
    directory_pub_dev.createSync(recursive: true);
  }
  directoryPackage.copyTo(directory_pub_dev);
  return directory_pub_dev;
}