Skip to content

Initial api #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ packages
build/
.pub/
pubspec.lock

generated
31 changes: 31 additions & 0 deletions e2e_example/lib/copy_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. 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:async';

import 'package:build/build.dart';

// Makes copies of things!
class CopyBuilder extends Builder {
@override
Future build(BuildStep buildStep) async {
var input = buildStep.input;
var copy = new Asset(_copiedAssetId(input.id), input.stringContents);
await buildStep.writeAsString(copy);
}

@override
List<AssetId> declareOutputs(AssetId inputId) => [_copiedAssetId(inputId)];

/// Only runs on the root package, and copies all *.txt files.
static List<Phase> buildPhases(PackageGraph graph) {
var phase = new Phase([
new CopyBuilder()
], [
new InputSet(graph.root.name, filePatterns: ['**/*.txt'])
]);
return [phase];
}
}

AssetId _copiedAssetId(AssetId inputId) => inputId.addExtension('.copy');
12 changes: 12 additions & 0 deletions e2e_example/lib/transformer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:build/build.dart';

import 'copy_builder.dart';

class CopyTransformer extends BuilderTransformer {
final List<Builder> builders = [new CopyBuilder()];

CopyTransformer.asPlugin(_);
}
13 changes: 13 additions & 0 deletions e2e_example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: e2e_example
version: 0.1.0

environment:
sdk: '>=1.9.1 <2.0.0'

dependencies:
build:
path: ../

transformers:
- e2e_example:
$include: 'web/*.txt'
36 changes: 36 additions & 0 deletions e2e_example/tool/build.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:build/build.dart';

import 'package:e2e_example/copy_builder.dart';

main() async {
/// Builds a full package dependency graph for the current package.
var graph = new PackageGraph.forThisPackage();

/// Give [Builder]s access to a [PackageGraph] so they can choose which
/// packages to run on. This simplifies user code a lot, and helps to mitigate
/// the transitive deps issue.
var phases = CopyBuilder.buildPhases(graph);

var result = await build([phases]);

if (result.status == BuildStatus.Success) {
print('''
Build Succeeded!

Type: ${result.buildType}
Outputs: ${result.outputs}''');
} else {
print('''
Build Failed :(

Type: ${result.buildType}
Outputs: ${result.outputs}

Exception: ${result.exception}
Stack Trace:
${result.stackTrace}''');
}
}
1 change: 1 addition & 0 deletions e2e_example/web/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
13 changes: 11 additions & 2 deletions lib/build.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library build;
export 'src/asset/asset.dart';
export 'src/asset/id.dart';
export 'src/builder/build_step.dart';
export 'src/builder/builder.dart';
export 'src/generate/build_result.dart';
export 'src/generate/build.dart';
export 'src/generate/input_set.dart';
export 'src/generate/phase.dart';
export 'src/package_graph/package_graph.dart';
export 'src/transformer/transformer.dart';
17 changes: 17 additions & 0 deletions lib/src/asset/asset.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'id.dart';

/// A fully realized asset whose content is available synchronously.
class Asset {
/// The id for this asset.
final AssetId id;

/// The content for this asset.
final String stringContents;

Asset(this.id, this.stringContents);

String toString() => 'Asset: $id';
}
110 changes: 110 additions & 0 deletions lib/src/asset/id.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably ignore this file, no changes currently from the barback version

// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:path/path.dart' as pathos;

/// Forked from the `barback` package.
///
/// Identifies an asset within a package.
class AssetId implements Comparable<AssetId> {
/// The name of the package containing this asset.
final String package;

/// The path to the asset relative to the root directory of [package].
///
/// Source (i.e. read from disk) and generated (i.e. the output of a
/// [Transformer]) assets all have paths. Even intermediate assets that are
/// generated and then consumed by later transformations will still have
/// a path used to identify it.
///
/// Asset paths always use forward slashes as path separators, regardless of
/// the host platform.
final String path;

/// Gets the file extension of the asset, if it has one, including the ".".
String get extension => pathos.extension(path);

/// Creates a new AssetId at [path] within [package].
///
/// The [path] will be normalized: any backslashes will be replaced with
/// forward slashes (regardless of host OS) and "." and ".." will be removed
/// where possible.
AssetId(this.package, String path)
: path = _normalizePath(path);

/// Parses an [AssetId] string of the form "package|path/to/asset.txt".
///
/// The [path] will be normalized: any backslashes will be replaced with
/// forward slashes (regardless of host OS) and "." and ".." will be removed
/// where possible.
factory AssetId.parse(String description) {
var parts = description.split("|");
if (parts.length != 2) {
throw new FormatException('Could not parse "$description".');
}

if (parts[0].isEmpty) {
throw new FormatException(
'Cannot have empty package name in "$description".');
}

if (parts[1].isEmpty) {
throw new FormatException(
'Cannot have empty path in "$description".');
}

return new AssetId(parts[0], parts[1]);
}

/// Deserializes an [AssetId] from [data], which must be the result of
/// calling [serialize] on an existing [AssetId].
///
/// Note that this is intended for communicating ids across isolates and not
/// for persistent storage of asset identifiers. There is no guarantee of
/// backwards compatibility in serialization form across versions.
AssetId.deserialize(data)
: package = data[0],
path = data[1];

/// Returns `true` of [other] is an [AssetId] with the same package and path.
operator ==(other) =>
other is AssetId &&
package == other.package &&
path == other.path;

int get hashCode => package.hashCode ^ path.hashCode;

int compareTo(AssetId other) {
var packageComp = package.compareTo(other.package);
if (packageComp != 0) return packageComp;
return path.compareTo(other.path);
}

/// Returns a new [AssetId] with the same [package] as this one and with the
/// [path] extended to include [extension].
AssetId addExtension(String extension) =>
new AssetId(package, "$path$extension");

/// Returns a new [AssetId] with the same [package] and [path] as this one
/// but with file extension [newExtension].
AssetId changeExtension(String newExtension) =>
new AssetId(package, pathos.withoutExtension(path) + newExtension);

String toString() => "$package|$path";

/// Serializes this [AssetId] to an object that can be sent across isolates
/// and passed to [deserialize].
serialize() => [package, path];
}

String _normalizePath(String path) {
if (pathos.isAbsolute(path)) {
throw new ArgumentError('Asset paths must be relative, but got "$path".');
}

// Normalize path separators so that they are always "/" in the AssetID.
path = path.replaceAll(r"\", "/");

// Collapse "." and "..".
return pathos.posix.normalize(path);
}
11 changes: 11 additions & 0 deletions lib/src/asset/reader.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. 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:async';
import 'dart:convert';

import 'id.dart';

abstract class AssetReader {
Future<String> readAsString(AssetId id, {Encoding encoding: UTF8});
}
11 changes: 11 additions & 0 deletions lib/src/asset/writer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. 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:async';
import 'dart:convert';

import 'asset.dart';

abstract class AssetWriter {
Future writeAsString(Asset asset, {Encoding encoding: UTF8});
}
31 changes: 31 additions & 0 deletions lib/src/builder/build_step.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. 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:async';
import 'dart:convert';

import '../asset/asset.dart';
import '../asset/id.dart';

/// A single step in the build processes. This represents a single input and
/// it also handles tracking of dependencies.
class BuildStep {
/// The primary input for this build step.
final Asset input;

/// Reads an [Asset] by [id] as a [String] using [encoding].
///
/// If [trackAsDependency] is true, then [id] will be marked as a dependency
/// of all [expectedOutputs].
Future<String> readAsString(AssetId id,
{Encoding encoding: UTF8, bool trackAsDependency: true});

/// Outputs an [Asset] using the current [AssetWriter], and adds [asset] to
/// [outputs].
void writeAsString(Asset asset, {Encoding encoding: UTF8});

/// Explicitly adds [id] as a dependency of all [expectedOutputs]. This is
/// not generally necessary unless forcing `trackAsDependency: false` when
/// calling [readAsString].
void addDependency(AssetId id);
}
78 changes: 78 additions & 0 deletions lib/src/builder/build_step_impl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. 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:async';
import 'dart:collection';
import 'dart:convert';

import '../asset/asset.dart';
import '../asset/id.dart';
import '../asset/reader.dart';
import '../asset/writer.dart';
import 'build_step.dart';

/// A single step in the build processes. This represents a single input and
/// its expected and real outputs. It also handles tracking of dependencies.
class BuildStepImpl implements BuildStep {
/// The primary input for this build step.
@override
final Asset input;

/// The list of all outputs which are expected/allowed to be output from this
/// step.
final List<AssetId> expectedOutputs;

/// The actual outputs of this build step.
UnmodifiableListView<Asset> get outputs => new UnmodifiableListView(_outputs);
final List<Asset> _outputs = [];

/// A future that completes once all outputs current are done writing.
Future get outputsCompleted => _outputsCompleted;
Future _outputsCompleted = new Future(() {});

/// The dependencies read in during this build step.
UnmodifiableListView<AssetId> get dependencies =>
new UnmodifiableListView(_dependencies);
final Set<AssetId> _dependencies = new Set<AssetId>();

/// Used internally for reading files.
final AssetReader _reader;

/// Used internally for writing files.
final AssetWriter _writer;

BuildStepImpl(
this.input, List<AssetId> expectedOutputs, this._reader, this._writer)
: expectedOutputs = new List.unmodifiable(expectedOutputs) {
/// The [input] is always a dependency.
_dependencies.add(input.id);
}

/// Reads an [Asset] by [id] as a [String] using [encoding].
///
/// If [trackAsDependency] is true, then [id] will be marked as a dependency
/// of all [expectedOutputs].
@override
Future<String> readAsString(AssetId id,
{Encoding encoding: UTF8, bool trackAsDependency: true}) {
if (trackAsDependency) _dependencies.add(id);
return _reader.readAsString(id, encoding: encoding);
}

/// Outputs an [Asset] using the current [AssetWriter], and adds [asset] to
/// [outputs].
@override
void writeAsString(Asset asset, {Encoding encoding: UTF8}) {
_outputs.add(asset);
var done = _writer.writeAsString(asset, encoding: encoding);
_outputsCompleted = _outputsCompleted.then((_) => done);
}

/// Explicitly adds [id] as a dependency of all [expectedOutputs]. This is
/// not generally necessary unless forcing `trackAsDependency: false` when
/// calling [readAsString].
@override
void addDependency(AssetId id) {
_dependencies.add(id);
}
}
Loading