-
Notifications
You must be signed in to change notification settings - Fork 212
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
Initial api #11
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
45984e6
initial api and basic ability to run on local packages
jakemac53 e5c1fc3
move example to its own sub-package, and add a transformer wrapper
jakemac53 7cb46a2
add transformer to e2e_example, rename output dir to
jakemac53 79e2339
add package_graph and use it in CopyBuilder
jakemac53 8c0aadb
cleanup
jakemac53 1dd1014
code review updates
jakemac53 c3573f6
Move BuildStep to an interface and add BuildStepImpl with the gory de…
jakemac53 de90a36
remove old comment
jakemac53 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ packages | |
build/ | ||
.pub/ | ||
pubspec.lock | ||
|
||
generated |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(_); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'''); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// 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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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