OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library pub.barback.transformer_config; | 5 library pub.barback.transformer_config; |
6 | 6 |
7 import 'package:path/path.dart' as p; | 7 import 'package:path/path.dart' as p; |
8 import 'package:source_maps/source_maps.dart'; | 8 import 'package:source_span/source_span.dart'; |
9 import 'package:yaml/yaml.dart'; | 9 import 'package:yaml/yaml.dart'; |
10 | 10 |
11 import 'transformer_id.dart'; | 11 import 'transformer_id.dart'; |
12 | 12 |
13 /// The configuration for a transformer. | 13 /// The configuration for a transformer. |
14 /// | 14 /// |
15 /// This corresponds to the transformers listed in a pubspec, which have both an | 15 /// This corresponds to the transformers listed in a pubspec, which have both an |
16 /// [id] indicating the location of the transformer and configuration specific | 16 /// [id] indicating the location of the transformer and configuration specific |
17 /// to that use of the transformer. | 17 /// to that use of the transformer. |
18 class TransformerConfig { | 18 class TransformerConfig { |
19 /// The [id] of the transformer [this] is configuring. | 19 /// The [id] of the transformer [this] is configuring. |
20 final TransformerId id; | 20 final TransformerId id; |
21 | 21 |
22 /// The configuration to pass to the transformer. | 22 /// The configuration to pass to the transformer. |
23 /// | 23 /// |
24 /// Any pub-specific configuration (i.e. keys starting with "$") will have | 24 /// Any pub-specific configuration (i.e. keys starting with "$") will have |
25 /// been stripped out of this and handled separately. This will be an empty | 25 /// been stripped out of this and handled separately. This will be an empty |
26 /// map if no configuration was provided. | 26 /// map if no configuration was provided. |
27 final Map configuration; | 27 final Map configuration; |
28 | 28 |
29 /// The source span from which this configuration was parsed. | 29 /// The source span from which this configuration was parsed. |
30 final Span span; | 30 final SourceSpan span; |
31 | 31 |
32 /// The primary input inclusions. | 32 /// The primary input inclusions. |
33 /// | 33 /// |
34 /// Each inclusion is an asset path. If this set is non-empty, then *only* | 34 /// Each inclusion is an asset path. If this set is non-empty, then *only* |
35 /// matching assets are allowed as a primary input by this transformer. If | 35 /// matching assets are allowed as a primary input by this transformer. If |
36 /// `null`, all assets are included. | 36 /// `null`, all assets are included. |
37 /// | 37 /// |
38 /// This is processed before [excludes]. If a transformer has both includes | 38 /// This is processed before [excludes]. If a transformer has both includes |
39 /// and excludes, then the set of included assets is determined and assets | 39 /// and excludes, then the set of included assets is determined and assets |
40 /// are excluded from that resulting set. | 40 /// are excluded from that resulting set. |
(...skipping 17 matching lines...) Expand all Loading... |
58 /// the package's dependers. | 58 /// the package's dependers. |
59 bool get canTransformPublicFiles { | 59 bool get canTransformPublicFiles { |
60 if (includes == null) return true; | 60 if (includes == null) return true; |
61 return includes.any((path) => | 61 return includes.any((path) => |
62 p.url.isWithin('lib', path) || p.url.isWithin('bin', path)); | 62 p.url.isWithin('lib', path) || p.url.isWithin('bin', path)); |
63 } | 63 } |
64 | 64 |
65 /// Parses [identifier] as a [TransformerId] with [configuration]. | 65 /// Parses [identifier] as a [TransformerId] with [configuration]. |
66 /// | 66 /// |
67 /// [identifierSpan] is the source span for [identifier]. | 67 /// [identifierSpan] is the source span for [identifier]. |
68 factory TransformerConfig.parse(String identifier, Span identifierSpan, | 68 factory TransformerConfig.parse(String identifier, SourceSpan identifierSpan, |
69 YamlMap configuration) => | 69 YamlMap configuration) => |
70 new TransformerConfig(new TransformerId.parse(identifier, identifierSpan), | 70 new TransformerConfig(new TransformerId.parse(identifier, identifierSpan), |
71 configuration); | 71 configuration); |
72 | 72 |
73 factory TransformerConfig(TransformerId id, YamlMap configurationNode) { | 73 factory TransformerConfig(TransformerId id, YamlMap configurationNode) { |
74 parseField(key) { | 74 parseField(key) { |
75 if (!configurationNode.containsKey(key)) return null; | 75 if (!configurationNode.containsKey(key)) return null; |
76 var fieldNode = configurationNode.nodes[key]; | 76 var fieldNode = configurationNode.nodes[key]; |
77 var field = fieldNode.value; | 77 var field = fieldNode.value; |
78 | 78 |
79 if (field is String) return new Set.from([field]); | 79 if (field is String) return new Set.from([field]); |
80 | 80 |
81 if (field is List) { | 81 if (field is List) { |
82 for (var node in field.nodes) { | 82 for (var node in field.nodes) { |
83 if (node.value is String) continue; | 83 if (node.value is String) continue; |
84 throw new SpanFormatException( | 84 throw new SourceSpanFormatException( |
85 '"$key" field may contain only strings.', node.span); | 85 '"$key" field may contain only strings.', node.span); |
86 } | 86 } |
87 | 87 |
88 return new Set.from(field); | 88 return new Set.from(field); |
89 } else { | 89 } else { |
90 throw new SpanFormatException( | 90 throw new SourceSpanFormatException( |
91 '"$key" field must be a string or list.', fieldNode.span); | 91 '"$key" field must be a string or list.', fieldNode.span); |
92 } | 92 } |
93 } | 93 } |
94 | 94 |
95 var includes = null; | 95 var includes = null; |
96 var excludes = null; | 96 var excludes = null; |
97 | 97 |
98 var configuration; | 98 var configuration; |
99 var span; | 99 var span; |
100 if (configurationNode == null) { | 100 if (configurationNode == null) { |
101 configuration = {}; | 101 configuration = {}; |
102 span = id.span; | 102 span = id.span; |
103 } else { | 103 } else { |
104 // Don't write to the immutable YAML map. | 104 // Don't write to the immutable YAML map. |
105 configuration = new Map.from(configurationNode); | 105 configuration = new Map.from(configurationNode); |
106 span = configurationNode.span; | 106 span = configurationNode.span; |
107 | 107 |
108 // Pull out the exclusions/inclusions. | 108 // Pull out the exclusions/inclusions. |
109 includes = parseField("\$include"); | 109 includes = parseField("\$include"); |
110 configuration.remove("\$include"); | 110 configuration.remove("\$include"); |
111 excludes = parseField("\$exclude"); | 111 excludes = parseField("\$exclude"); |
112 configuration.remove("\$exclude"); | 112 configuration.remove("\$exclude"); |
113 | 113 |
114 // All other keys starting with "$" are unexpected. | 114 // All other keys starting with "$" are unexpected. |
115 for (var key in configuration.keys) { | 115 for (var key in configuration.keys) { |
116 if (key is! String || !key.startsWith(r'$')) continue; | 116 if (key is! String || !key.startsWith(r'$')) continue; |
117 throw new SpanFormatException( | 117 throw new SourceSpanFormatException( |
118 'Unknown reserved field.', configurationNode.nodes[key].span); | 118 'Unknown reserved field.', configurationNode.nodes[key].span); |
119 } | 119 } |
120 } | 120 } |
121 | 121 |
122 return new TransformerConfig._(id, configuration, span, includes, excludes); | 122 return new TransformerConfig._(id, configuration, span, includes, excludes); |
123 } | 123 } |
124 | 124 |
125 TransformerConfig._( | 125 TransformerConfig._( |
126 this.id, this.configuration, this.span, this.includes, this.excludes); | 126 this.id, this.configuration, this.span, this.includes, this.excludes); |
127 | 127 |
128 String toString() => id.toString(); | 128 String toString() => id.toString(); |
129 | 129 |
130 /// Returns whether the include/exclude rules allow the transformer to run on | 130 /// Returns whether the include/exclude rules allow the transformer to run on |
131 /// [pathWithinPackage]. | 131 /// [pathWithinPackage]. |
132 /// | 132 /// |
133 /// [pathWithinPackage] must be a URL-style path relative to the containing | 133 /// [pathWithinPackage] must be a URL-style path relative to the containing |
134 /// package's root directory. | 134 /// package's root directory. |
135 bool canTransform(String pathWithinPackage) { | 135 bool canTransform(String pathWithinPackage) { |
136 // TODO(rnystrom): Support globs in addition to paths. See #17093. | 136 // TODO(rnystrom): Support globs in addition to paths. See #17093. |
137 if (excludes != null) { | 137 if (excludes != null) { |
138 // If there are any excludes, it must not match any of them. | 138 // If there are any excludes, it must not match any of them. |
139 if (excludes.contains(pathWithinPackage)) return false; | 139 if (excludes.contains(pathWithinPackage)) return false; |
140 } | 140 } |
141 | 141 |
142 // If there are any includes, it must match one of them. | 142 // If there are any includes, it must match one of them. |
143 return includes == null || includes.contains(pathWithinPackage); | 143 return includes == null || includes.contains(pathWithinPackage); |
144 } | 144 } |
145 } | 145 } |
OLD | NEW |