blob: 9182ec6d48a3ee4efba22646dffb275a649e0619 [file] [log] [blame]
// Copyright (c) 2014, 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 pub.excluding_transformer;
import 'dart:async';
import 'package:barback/barback.dart';
/// Decorates an inner [Transformer] and handles including and excluding
/// primary inputs.
class ExcludingTransformer extends Transformer {
/// If [includes] or [excludes] is non-null, wraps [inner] in an
/// [ExcludingTransformer] that handles those.
///
/// Otherwise, just returns [inner] unmodified.
static Transformer wrap(Transformer inner, Set<String> includes,
Set<String> excludes) {
if (includes == null && excludes == null) return inner;
if (inner is LazyTransformer) {
return new _LazyExcludingTransformer(
inner as LazyTransformer, includes, excludes);
} else if (inner is DeclaringTransformer) {
return new _DeclaringExcludingTransformer(
inner as DeclaringTransformer, includes, excludes);
} else {
return new ExcludingTransformer._(inner, includes, excludes);
}
}
final Transformer _inner;
/// The set of asset paths which should be included.
///
/// If `null`, all non-excluded assets are allowed. Otherwise, only included
/// assets are allowed.
final Set<String> _includes;
/// The set of assets which should be excluded.
///
/// Exclusions are applied after inclusions.
final Set<String> _excludes;
ExcludingTransformer._(this._inner, this._includes, this._excludes);
isPrimary(AssetId id) {
// TODO(rnystrom): Support globs in addition to paths. See #17093.
if (_includes != null) {
// If there are any includes, it must match one of them.
if (!_includes.contains(id.path)) return false;
}
// It must not be excluded.
if (_excludes != null && _excludes.contains(id.path)) {
return false;
}
return _inner.isPrimary(id);
}
Future apply(Transform transform) => _inner.apply(transform);
String toString() => _inner.toString();
}
class _DeclaringExcludingTransformer extends ExcludingTransformer
implements DeclaringTransformer {
_DeclaringExcludingTransformer(DeclaringTransformer inner,
Set<String> includes, Set<String> excludes)
: super._(inner as Transformer, includes, excludes);
Future declareOutputs(DeclaringTransform transform) =>
(_inner as DeclaringTransformer).declareOutputs(transform);
}
class _LazyExcludingTransformer extends _DeclaringExcludingTransformer
implements LazyTransformer {
_LazyExcludingTransformer(DeclaringTransformer inner,
Set<String> includes, Set<String> excludes)
: super(inner, includes, excludes);
}