cleanupPackage method

Future<void> cleanupPackage(
  1. String path, {
  2. bool isPackageDir = false,
})

Implementation

Future<void> cleanupPackage(String path, {bool isPackageDir = false}) async {
  final packageDirStr = isPackageDir ? path : packageDir(path);
  final ownersDir = io.Directory(p.join(packageDirStr, "owners"));

  var liveOwners = 0;
  if (await ownersDir.exists()) {
    await for (final entity in ownersDir.list()) {
      if (entity is! io.File || !entity.path.endsWith('.owner')) continue;

      final pidString = p.basename(entity.path).replaceAll('.owner', '');
      final pid = int.tryParse(pidString);

      if (pid != null && await _isProcessAlive(pid)) {
        liveOwners++;
      } else {
        await entity.delete();
      }
    }
  }

  if (liveOwners > 0) return;

  final pidsFile = io.File(p.join(packageDirStr, "build_runner.pids"));
  if (await pidsFile.exists()) {
    final lines = await pidsFile.readAsLines();
    for (final line in lines) {
      final pid = int.tryParse(line.trim());
      if (pid != null) {
        try {
          io.Process.killPid(pid);
        } catch (_) {}
      }
    }
    await pidsFile.delete();
  }
}