dependency_checker.dart 2.18 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'globals.dart';

7
import 'base/file_system.dart';
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
import 'dart/dependencies.dart';
import 'dart/package_map.dart';
import 'asset.dart';

import 'package:path/path.dart' as pathos;

class DependencyChecker {
  final DartDependencySetBuilder builder;
  final Set<String> _dependencies = new Set<String>();
  final AssetBundle assets;
  DependencyChecker(this.builder, this.assets);

  /// Returns [true] if any components have been modified after [threshold] or
  /// if it cannot be determined.
  bool check(DateTime threshold) {
    _dependencies.clear();
    PackageMap packageMap;
    // Parse the package map.
    try {
      packageMap = new PackageMap(builder.packagesFilePath)..load();
      _dependencies.add(builder.packagesFilePath);
    } catch (e, st) {
      printTrace('DependencyChecker: could not parse .packages file:\n$e\n$st');
      return true;
    }
    // Build the set of Dart dependencies.
    try {
      Set<String> dependencies = builder.build();
      for (String path in dependencies) {
        // Ensure all paths are absolute.
        if (path.startsWith('package:')) {
          path = packageMap.pathForPackage(Uri.parse(path));
        } else {
          path = pathos.join(builder.projectRootPath, path);
        }
        _dependencies.add(path);
      }
    } catch (e, st) {
      printTrace('DependencyChecker: error determining .dart dependencies:\n$e\n$st');
      return true;
    }
    // TODO(johnmccutchan): Extract dependencies from the AssetBundle too.

    // Check all dependency modification times.
    for (String path in _dependencies) {
53
      File file = fs.file(path);
54 55 56 57 58 59 60 61 62 63
      FileStat stat = file.statSync();
      if (stat.type == FileSystemEntityType.NOT_FOUND) {
        printTrace('DependencyChecker: Error stating $path.');
        return true;
      }
      if (stat.modified.isAfter(threshold)) {
        printTrace('DependencyChecker: $path is newer than $threshold');
        return true;
      }
    }
64
    printTrace('DependencyChecker: nothing is modified after $threshold.');
65 66 67
    return false;
  }
}