ensurePruning method

Future<void> ensurePruning()

Implementation

Future<void> ensurePruning() async {
  if (!Utils.isPruneEnabled) return;

  final database = await _database;

  Logger.i('Prunning is enabled - checking pubpsec.lock');

  final pubspecLockPath = path.join(Utils.projectDirectory, Constants.pubpsecLockFileName);
  final pubspecLock = File(pubspecLockPath);

  final fileExists = pubspecLock.existsSync();

  if (!fileExists) {
    Logger.e('No ${Constants.pubpsecLockFileName} exits');

    return;
  }

  final digest = DigestUtils.generateDigestForSingleFile(pubspecLockPath);

  final existingDigest = await database.getEntryByKey(Constants.pubpsecLockFileName);

  Logger.v('Pubspec.lock digest: $digest');
  Logger.v('Existing Pubspec.lock digest: $digest');
  Logger.v('Will prune? ${digest != existingDigest ? 'YES' : 'NO'}');

  if (existingDigest != null && digest != existingDigest) {
    Logger.i('!!! Pruning cache as pubspec.lock was changed from last time !!!');
    await database.prune(keysToKeep: [Constants.pubpsecLockFileName]);
  }

  await _dbOperation((db) => db.createCustomEntry(Constants.pubpsecLockFileName, digest));
}