DependencyChecker with tests (#7268)
- [x] Introduce DependencyChecker which can determine if any dependencies have been modified. - [x] Move the DartDependencyBuilder into a separate file. - [x] Add unit tests for DartDependencyBuilder. - [x] Add unit tets for DependencyChecker Part of #7014
This commit is contained in:
46
packages/flutter_tools/lib/src/dart/dependencies.dart
Normal file
46
packages/flutter_tools/lib/src/dart/dependencies.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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 'dart:convert';
|
||||
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import '../base/process.dart';
|
||||
import '../toolchain.dart';
|
||||
|
||||
class DartDependencySetBuilder {
|
||||
DartDependencySetBuilder(this.mainScriptPath,
|
||||
this.projectRootPath,
|
||||
this.packagesFilePath);
|
||||
|
||||
final String mainScriptPath;
|
||||
final String projectRootPath;
|
||||
final String packagesFilePath;
|
||||
|
||||
Set<String> build() {
|
||||
final String skySnapshotPath =
|
||||
ToolConfiguration.instance.getHostToolPath(HostTool.SkySnapshot);
|
||||
|
||||
final List<String> args = <String>[
|
||||
skySnapshotPath,
|
||||
'--packages=$packagesFilePath',
|
||||
'--print-deps',
|
||||
mainScriptPath
|
||||
];
|
||||
|
||||
String output = runSyncAndThrowStdErrOnError(args);
|
||||
|
||||
final List<String> lines = LineSplitter.split(output).toList();
|
||||
final Set<String> minimalDependencies = new Set<String>();
|
||||
for (String line in lines) {
|
||||
if (!line.startsWith('package:')) {
|
||||
// We convert the uris so that they are relative to the project
|
||||
// root.
|
||||
line = path.relative(line, from: projectRootPath);
|
||||
}
|
||||
minimalDependencies.add(line);
|
||||
}
|
||||
return minimalDependencies;
|
||||
}
|
||||
}
|
||||
@@ -29,12 +29,27 @@ class PackageMap {
|
||||
|
||||
final String packagesPath;
|
||||
|
||||
Map<String, Uri> get map {
|
||||
/// Load and parses the .packages file.
|
||||
void load() {
|
||||
_map ??= _parse(packagesPath);
|
||||
}
|
||||
|
||||
Map<String, Uri> get map {
|
||||
load();
|
||||
return _map;
|
||||
}
|
||||
Map<String, Uri> _map;
|
||||
|
||||
/// Returns the path to [packageUri].
|
||||
String pathForPackage(Uri packageUri) {
|
||||
assert(packageUri.scheme == 'package');
|
||||
List<String> pathSegments = packageUri.pathSegments.toList();
|
||||
String packageName = pathSegments.removeAt(0);
|
||||
Uri packageBase = map[packageName];
|
||||
String packageRelativePath = path.joinAll(pathSegments);
|
||||
return packageBase.resolve(packageRelativePath).path;
|
||||
}
|
||||
|
||||
String checkValid() {
|
||||
if (FileSystemEntity.isFileSync(packagesPath))
|
||||
return null;
|
||||
|
||||
67
packages/flutter_tools/lib/src/dependency_checker.dart
Normal file
67
packages/flutter_tools/lib/src/dependency_checker.dart
Normal file
@@ -0,0 +1,67 @@
|
||||
// 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 'dart:io';
|
||||
|
||||
import 'globals.dart';
|
||||
|
||||
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) {
|
||||
File file = new File(path);
|
||||
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;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
@@ -14,15 +13,15 @@ import 'application_package.dart';
|
||||
import 'asset.dart';
|
||||
import 'base/context.dart';
|
||||
import 'base/logger.dart';
|
||||
import 'base/process.dart';
|
||||
import 'base/utils.dart';
|
||||
import 'build_info.dart';
|
||||
import 'dart/package_map.dart';
|
||||
import 'dart/dependencies.dart';
|
||||
import 'devfs.dart';
|
||||
import 'device.dart';
|
||||
import 'globals.dart';
|
||||
import 'resident_runner.dart';
|
||||
import 'toolchain.dart';
|
||||
|
||||
import 'vmservice.dart';
|
||||
import 'usage.dart';
|
||||
|
||||
@@ -37,47 +36,6 @@ HotRunnerConfig get hotRunnerConfig => context[HotRunnerConfig];
|
||||
|
||||
const bool kHotReloadDefault = true;
|
||||
|
||||
class DartDependencySetBuilder {
|
||||
DartDependencySetBuilder(this.mainScriptPath,
|
||||
this.projectRootPath,
|
||||
this.packagesFilePath);
|
||||
|
||||
final String mainScriptPath;
|
||||
final String projectRootPath;
|
||||
final String packagesFilePath;
|
||||
|
||||
Set<String> build() {
|
||||
final String skySnapshotPath =
|
||||
ToolConfiguration.instance.getHostToolPath(HostTool.SkySnapshot);
|
||||
|
||||
final List<String> args = <String>[
|
||||
skySnapshotPath,
|
||||
'--packages=$packagesFilePath',
|
||||
'--print-deps',
|
||||
mainScriptPath
|
||||
];
|
||||
|
||||
String output = runSyncAndThrowStdErrOnError(args);
|
||||
|
||||
final List<String> lines = LineSplitter.split(output).toList();
|
||||
final Set<String> minimalDependencies = new Set<String>();
|
||||
for (String line in lines) {
|
||||
// We need to convert the uris so that they are relative to the project
|
||||
// root and tweak package: uris so that they reflect their devFS location.
|
||||
if (line.startsWith('package:')) {
|
||||
// Swap out package: for packages/ because we place all package sources
|
||||
// under packages/.
|
||||
line = line.replaceFirst('package:', 'packages/');
|
||||
} else {
|
||||
// Ensure paths are relative to the project root.
|
||||
line = path.relative(line, from: projectRootPath);
|
||||
}
|
||||
minimalDependencies.add(line);
|
||||
}
|
||||
return minimalDependencies;
|
||||
}
|
||||
}
|
||||
|
||||
class HotRunner extends ResidentRunner {
|
||||
HotRunner(
|
||||
Device device, {
|
||||
@@ -151,7 +109,18 @@ class HotRunner extends ResidentRunner {
|
||||
new DartDependencySetBuilder(
|
||||
_mainPath, _projectRootPath, _packagesFilePath);
|
||||
try {
|
||||
_dartDependencies = dartDependencySetBuilder.build();
|
||||
Set<String> dependencies = dartDependencySetBuilder.build();
|
||||
_dartDependencies = new Set<String>();
|
||||
for (String path in dependencies) {
|
||||
// We need to tweak package: uris so that they reflect their devFS
|
||||
// location.
|
||||
if (path.startsWith('package:')) {
|
||||
// Swap out package: for packages/ because we place all package
|
||||
// sources under packages/.
|
||||
path = path.replaceFirst('package:', 'packages/');
|
||||
}
|
||||
_dartDependencies.add(path);
|
||||
}
|
||||
} catch (error) {
|
||||
printStatus('Error detected in application source code:', emphasis: true);
|
||||
printError(error);
|
||||
|
||||
Reference in New Issue
Block a user