diff --git a/lib/src/builder/build_step.dart b/lib/src/builder/build_step.dart index 2811a8ca1..f62cb1e02 100644 --- a/lib/src/builder/build_step.dart +++ b/lib/src/builder/build_step.dart @@ -15,7 +15,7 @@ abstract class BuildStep { /// The primary input for this build step. Asset get input; - // A [Logger] for this [BuildStep]. + /// A [Logger] for this [BuildStep]. Logger get logger; /// Checks if an [Asset] by [id] exists as an input for this [BuildStep]. diff --git a/lib/src/package_graph/package_graph.dart b/lib/src/package_graph/package_graph.dart index b5b4162c5..3f41aa3e7 100644 --- a/lib/src/package_graph/package_graph.dart +++ b/lib/src/package_graph/package_graph.dart @@ -17,6 +17,24 @@ class PackageGraph { PackageGraph._(this.root, Map allPackages) : allPackages = new Map.unmodifiable(allPackages); + /// Creates a [PackageGraph] given the [root] [PackageNode]. + factory PackageGraph.fromRoot(PackageNode root) { + final allPackages = { + root.name: root, + }; + + addDeps(PackageNode package) { + for (var dep in package.dependencies) { + if (allPackages.containsKey(dep.name)) continue; + allPackages[dep.name] = dep; + addDeps(dep); + } + } + addDeps(root); + + return new PackageGraph._(root, allPackages); + } + /// Creates a [PackageGraph] for the package whose top level directory lives /// at [packagePath] (no trailing slash). factory PackageGraph.forPath(String packagePath) { @@ -62,7 +80,7 @@ class PackageGraph { {bool isRoot: false}) { var name = yaml['name']; assert(!nodes.containsKey(name)); - var node = new PackageNode._( + var node = new PackageNode( name, yaml['version'], type, packageLocations[name]); nodes[name] = node; @@ -74,7 +92,7 @@ class PackageGraph { var pubspec = _pubspecForUri(packageLocations[name]); dep = addNodeAndDeps(pubspec, _dependencyType(rootDeps[name])); } - node._dependencies.add(dep); + node.dependencies.add(dep); }); return node; @@ -113,13 +131,12 @@ class PackageNode { final PackageDependencyType dependencyType; /// All the packages that this package directly depends on. - final List _dependencies = []; - Iterable get dependencies => _dependencies.toList(); + final List dependencies = []; /// The location of the current version of this package. final Uri location; - PackageNode._(this.name, this.version, this.dependencyType, this.location); + PackageNode(this.name, this.version, this.dependencyType, this.location); String toString() => ''' $name: diff --git a/test/package_graph/package_graph_test.dart b/test/package_graph/package_graph_test.dart index 0efa8509f..9351b4d8a 100644 --- a/test/package_graph/package_graph_test.dart +++ b/test/package_graph/package_graph_test.dart @@ -9,61 +9,76 @@ import 'package:build/build.dart'; main() { PackageGraph graph; - group('forThisPackage ', () { - setUp(() async { - graph = await new PackageGraph.forThisPackage(); - }); + group('PackageGraph', () { + group('forThisPackage ', () { + setUp(() async { + graph = await new PackageGraph.forThisPackage(); + }); - test('root', () { - expectPkg( - graph.root, 'build', isNotEmpty, PackageDependencyType.Path, './'); + test('root', () { + expectPkg( + graph.root, 'build', isNotEmpty, PackageDependencyType.Path, './'); + }); }); - }); - group('basic package ', () { - var basicPkgPath = 'test/fixtures/basic_pkg'; + group('basic package ', () { + var basicPkgPath = 'test/fixtures/basic_pkg'; - setUp(() async { - graph = await new PackageGraph.forPath(basicPkgPath); - }); + setUp(() async { + graph = await new PackageGraph.forPath(basicPkgPath); + }); - test('allPackages', () { - expect(graph.allPackages, { - 'a': graph['a'], - 'b': graph['b'], - 'c': graph['c'], - 'basic_pkg': graph['basic_pkg'], + test('allPackages', () { + expect(graph.allPackages, { + 'a': graph['a'], + 'b': graph['b'], + 'c': graph['c'], + 'basic_pkg': graph['basic_pkg'], + }); }); - }); - test('root', () { - expectPkg(graph.root, 'basic_pkg', '1.0.0', PackageDependencyType.Path, - basicPkgPath, [graph['a'], graph['b'], graph['c']]); - }); + test('root', () { + expectPkg(graph.root, 'basic_pkg', '1.0.0', PackageDependencyType.Path, + basicPkgPath, [graph['a'], graph['b'], graph['c']]); + }); - test('pub dependency', () { - expectPkg(graph['a'], 'a', '2.0.0', PackageDependencyType.Pub, - '$basicPkgPath/pkg/a/', [graph['b'], graph['c']]); - }); + test('pub dependency', () { + expectPkg(graph['a'], 'a', '2.0.0', PackageDependencyType.Pub, + '$basicPkgPath/pkg/a/', [graph['b'], graph['c']]); + }); - test('git dependency', () { - expectPkg(graph['b'], 'b', '3.0.0', PackageDependencyType.Github, - '$basicPkgPath/pkg/b/', [graph['c']]); + test('git dependency', () { + expectPkg(graph['b'], 'b', '3.0.0', PackageDependencyType.Github, + '$basicPkgPath/pkg/b/', [graph['c']]); + }); + + test('path dependency', () { + expectPkg(graph['c'], 'c', '4.0.0', PackageDependencyType.Path, + '$basicPkgPath/pkg/c/', [graph['basic_pkg']]); + }); }); - test('path dependency', () { - expectPkg(graph['c'], 'c', '4.0.0', PackageDependencyType.Path, - '$basicPkgPath/pkg/c/', [graph['basic_pkg']]); + test('custom creation via fromRoot', () { + var a = new PackageNode('a', '1.0.0', PackageDependencyType.Path, null); + var b = new PackageNode('b', '1.0.0', PackageDependencyType.Pub, null); + var c = new PackageNode('c', '1.0.0', PackageDependencyType.Pub, null); + var d = new PackageNode('d', '1.0.0', PackageDependencyType.Pub, null); + a.dependencies.addAll([b, d]); + b.dependencies.add(c); + var graph = new PackageGraph.fromRoot(a); + expect(graph.root, a); + expect(graph.allPackages, {'a': a, 'b': b, 'c': c, 'd': d}); }); - }); - test('missing pubspec throws on create', () { - expect(() => new PackageGraph.forPath('test/fixtures/no_pubspec'), throws); - }); + test('missing pubspec throws on create', () { + expect( + () => new PackageGraph.forPath('test/fixtures/no_pubspec'), throws); + }); - test('missing .packages file throws on create', () { - expect(() => new PackageGraph.forPath('test/fixtures/no_packages_file'), - throws); + test('missing .packages file throws on create', () { + expect(() => new PackageGraph.forPath('test/fixtures/no_packages_file'), + throws); + }); }); }