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:glob/glob.dart'; | 7 import 'package:glob/glob.dart'; |
8 import 'package:path/path.dart' as p; | 8 import 'package:path/path.dart' as p; |
9 import 'package:source_span/source_span.dart'; | 9 import 'package:source_span/source_span.dart'; |
10 import 'package:yaml/yaml.dart'; | 10 import 'package:yaml/yaml.dart'; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 bool get hasExclusions => includes != null || excludes != null; | 56 bool get hasExclusions => includes != null || excludes != null; |
57 | 57 |
58 /// Returns whether this transformer might transform a file that's visible to | 58 /// Returns whether this transformer might transform a file that's visible to |
59 /// the package's dependers. | 59 /// the package's dependers. |
60 bool get canTransformPublicFiles { | 60 bool get canTransformPublicFiles { |
61 if (includes == null) return true; | 61 if (includes == null) return true; |
62 return includes.any((glob) { | 62 return includes.any((glob) { |
63 // Check whether the first path component of the glob is "lib", "bin", or | 63 // Check whether the first path component of the glob is "lib", "bin", or |
64 // contains wildcards that may cause it to match "lib" or "bin". | 64 // contains wildcards that may cause it to match "lib" or "bin". |
65 var first = p.posix.split(glob.toString()).first; | 65 var first = p.posix.split(glob.toString()).first; |
66 if (first.contains('{') || first.contains('*') || first.contains('[') || | 66 if (first.contains('{') || |
| 67 first.contains('*') || |
| 68 first.contains('[') || |
67 first.contains('?')) { | 69 first.contains('?')) { |
68 return true; | 70 return true; |
69 } | 71 } |
70 | 72 |
71 return first == 'lib' || first == 'bin'; | 73 return first == 'lib' || first == 'bin'; |
72 }); | 74 }); |
73 } | 75 } |
74 | 76 |
75 /// Parses [identifier] as a [TransformerId] with [configuration]. | 77 /// Parses [identifier] as a [TransformerId] with [configuration]. |
76 /// | 78 /// |
77 /// [identifierSpan] is the source span for [identifier]. | 79 /// [identifierSpan] is the source span for [identifier]. |
78 factory TransformerConfig.parse(String identifier, SourceSpan identifierSpan, | 80 factory TransformerConfig.parse(String identifier, SourceSpan identifierSpan, |
79 YamlMap configuration) => | 81 YamlMap configuration) => |
80 new TransformerConfig(new TransformerId.parse(identifier, identifierSpan), | 82 new TransformerConfig( |
| 83 new TransformerId.parse(identifier, identifierSpan), |
81 configuration); | 84 configuration); |
82 | 85 |
83 factory TransformerConfig(TransformerId id, YamlMap configurationNode) { | 86 factory TransformerConfig(TransformerId id, YamlMap configurationNode) { |
84 parseField(key) { | 87 parseField(key) { |
85 if (!configurationNode.containsKey(key)) return null; | 88 if (!configurationNode.containsKey(key)) return null; |
86 var fieldNode = configurationNode.nodes[key]; | 89 var fieldNode = configurationNode.nodes[key]; |
87 var field = fieldNode.value; | 90 var field = fieldNode.value; |
88 | 91 |
89 if (field is String) { | 92 if (field is String) { |
90 return new Set.from([new Glob(field, context: p.url, recursive: true)]); | 93 return new Set.from([new Glob(field, context: p.url, recursive: true)]); |
91 } | 94 } |
92 | 95 |
93 if (field is! List) { | 96 if (field is! List) { |
94 throw new SourceSpanFormatException( | 97 throw new SourceSpanFormatException( |
95 '"$key" field must be a string or list.', fieldNode.span); | 98 '"$key" field must be a string or list.', |
| 99 fieldNode.span); |
96 } | 100 } |
97 | 101 |
98 return new Set.from(field.nodes.map((node) { | 102 return new Set.from(field.nodes.map((node) { |
99 if (node.value is String) { | 103 if (node.value is String) { |
100 return new Glob(node.value, context: p.url, recursive: true); | 104 return new Glob(node.value, context: p.url, recursive: true); |
101 } | 105 } |
102 | 106 |
103 throw new SourceSpanFormatException( | 107 throw new SourceSpanFormatException( |
104 '"$key" field may contain only strings.', node.span); | 108 '"$key" field may contain only strings.', |
| 109 node.span); |
105 })); | 110 })); |
106 } | 111 } |
107 | 112 |
108 var includes = null; | 113 var includes = null; |
109 var excludes = null; | 114 var excludes = null; |
110 | 115 |
111 var configuration; | 116 var configuration; |
112 var span; | 117 var span; |
113 if (configurationNode == null) { | 118 if (configurationNode == null) { |
114 configuration = {}; | 119 configuration = {}; |
115 span = id.span; | 120 span = id.span; |
116 } else { | 121 } else { |
117 // Don't write to the immutable YAML map. | 122 // Don't write to the immutable YAML map. |
118 configuration = new Map.from(configurationNode); | 123 configuration = new Map.from(configurationNode); |
119 span = configurationNode.span; | 124 span = configurationNode.span; |
120 | 125 |
121 // Pull out the exclusions/inclusions. | 126 // Pull out the exclusions/inclusions. |
122 includes = parseField("\$include"); | 127 includes = parseField("\$include"); |
123 configuration.remove("\$include"); | 128 configuration.remove("\$include"); |
124 excludes = parseField("\$exclude"); | 129 excludes = parseField("\$exclude"); |
125 configuration.remove("\$exclude"); | 130 configuration.remove("\$exclude"); |
126 | 131 |
127 // All other keys starting with "$" are unexpected. | 132 // All other keys starting with "$" are unexpected. |
128 for (var key in configuration.keys) { | 133 for (var key in configuration.keys) { |
129 if (key is! String || !key.startsWith(r'$')) continue; | 134 if (key is! String || !key.startsWith(r'$')) continue; |
130 throw new SourceSpanFormatException( | 135 throw new SourceSpanFormatException( |
131 'Unknown reserved field.', configurationNode.nodes[key].span); | 136 'Unknown reserved field.', |
| 137 configurationNode.nodes[key].span); |
132 } | 138 } |
133 } | 139 } |
134 | 140 |
135 return new TransformerConfig._(id, configuration, span, includes, excludes); | 141 return new TransformerConfig._(id, configuration, span, includes, excludes); |
136 } | 142 } |
137 | 143 |
138 TransformerConfig._( | 144 TransformerConfig._(this.id, this.configuration, this.span, this.includes, |
139 this.id, this.configuration, this.span, this.includes, this.excludes); | 145 this.excludes); |
140 | 146 |
141 String toString() => id.toString(); | 147 String toString() => id.toString(); |
142 | 148 |
143 /// Returns whether the include/exclude rules allow the transformer to run on | 149 /// Returns whether the include/exclude rules allow the transformer to run on |
144 /// [pathWithinPackage]. | 150 /// [pathWithinPackage]. |
145 /// | 151 /// |
146 /// [pathWithinPackage] must be a URL-style path relative to the containing | 152 /// [pathWithinPackage] must be a URL-style path relative to the containing |
147 /// package's root directory. | 153 /// package's root directory. |
148 bool canTransform(String pathWithinPackage) { | 154 bool canTransform(String pathWithinPackage) { |
149 if (excludes != null) { | 155 if (excludes != null) { |
150 // If there are any excludes, it must not match any of them. | 156 // If there are any excludes, it must not match any of them. |
151 for (var exclude in excludes) { | 157 for (var exclude in excludes) { |
152 if (exclude.matches(pathWithinPackage)) return false; | 158 if (exclude.matches(pathWithinPackage)) return false; |
153 } | 159 } |
154 } | 160 } |
155 | 161 |
156 // If there are any includes, it must match one of them. | 162 // If there are any includes, it must match one of them. |
157 return includes == null || | 163 return includes == null || |
158 includes.any((include) => include.matches(pathWithinPackage)); | 164 includes.any((include) => include.matches(pathWithinPackage)); |
159 } | 165 } |
160 } | 166 } |
OLD | NEW |