library dartdoc_runner.cleaners.package_cleaner;

import 'dart:async';
import 'dart:io';

import 'package:dartdoc_runner/config.dart';
import 'package:dartdoc_runner/package.dart';
import 'package:logging/logging.dart';

var _logger = new Logger("package_cleaner");

class PackageCleaner {
  final Config config;
  PackageCleaner(this.config);

  Future<Null> delete(Iterable packages) async {
    var futures = packages.map((Package package) async {
      _logger.info("Deleting pubcache and output directory for the package $package");
      await new Directory(package.pubCacheDir(config)).delete(recursive: true);
      await new Directory(package.outputDir(config)).delete(recursive: true);
    });
    await Future.wait(futures);
  }
}

library dartdoc_runner.cleaners.package_cleaner;

import 'dart:async';
import 'dart:io';

import 'package:dartdoc_runner/config.dart';
import 'package:dartdoc_runner/package.dart';
import 'package:logging/logging.dart';
import 'package:yaml/yaml.dart' as yaml;
import 'package:dartdoc_runner/version.dart';

var _logger = new Logger("package_cleaner");

class PackageCleaner {
  final Config config;
  PackageCleaner(this.config);

  Future<Null> delete(Iterable packages) async {
    var futures = packages.map((Package package) async {
      _logger.info("Deleting output directory for the package $package");
      await new Directory(package.outputDir(config)).delete(recursive: true);
      if (!_usedByDartDocGeneratorPackages.contains(package)) {
        _logger.info("Deleting pubcache directory for the package $package");
        await new Directory(package.pubCacheDir(config)).delete(recursive: true);
      }
    });
    await Future.wait(futures);
  }

  Set<Package> _usedByDartDocGeneratorPackagesMemoizer;
  Set<Package> get _usedByDartDocGeneratorPackages {
    if (_usedByDartDocGeneratorPackagesMemoizer == null) {
      Map<String, Map<String, String>> lockfile = yaml.loadYaml(new File("pubspec.lock").readAsStringSync())["packages"];
      var packages = new Set();
      lockfile.forEach((String key, Map<String, String> values) {
        packages.add(new Package(key, new Version(values["version"])));
      });
      _usedByDartDocGeneratorPackagesMemoizer = packages;
    }
    return _usedByDartDocGeneratorPackagesMemoizer;
  }
}

Make sure we don't delete the packages dartdocs-generator depends on
Make sure we don't delete the packages dartdocs-generator depends on
