OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 /// Script to create boilerplate for a Polymer element. | |
6 /// Produces new .html entry point for a polymer app and updates the | |
7 /// pubspec.yaml to reflect it. | |
8 /// | |
9 /// Run this script with pub run: | |
10 /// | |
11 /// pub run polymer:new_entry <html_file> | |
12 /// | |
13 library polymer.bin.new_entry; | |
14 | |
15 import 'dart:io'; | |
16 import 'package:args/args.dart'; | |
17 import 'package:path/path.dart' as path; | |
18 import 'package:yaml/yaml.dart'; | |
19 import 'package:source_span/source_span.dart'; | |
20 | |
21 void printUsage() { | |
22 print('pub run polymer:new_entry entry_point_file.html'); | |
23 } | |
24 | |
25 void main(List<String> args) { | |
26 var parser = new ArgParser(allowTrailingOptions: true); | |
27 parser.addFlag('help', abbr: 'h'); | |
28 var entryPoint; | |
29 | |
30 try { | |
31 var options = parser.parse(args); | |
32 if (options['help']) { | |
33 printUsage(); | |
34 return; | |
35 } | |
36 entryPoint = options.rest[0]; | |
37 } catch (e) { | |
38 print('$e\n'); | |
39 printUsage(); | |
40 exitCode = 1; | |
41 return; | |
42 } | |
43 | |
44 // If the entrypoint file has no extension, add .html to it. | |
45 if (path.extension(entryPoint) == '') { | |
46 entryPoint = '${entryPoint}.html'; | |
47 } | |
48 | |
49 var outputDir = path.dirname(entryPoint); | |
50 var outputDirLocation = new Directory(outputDir); | |
51 | |
52 if (!outputDirLocation.existsSync()) { | |
53 outputDirLocation.createSync(recursive: true); | |
54 } | |
55 | |
56 outputDir = outputDirLocation.resolveSymbolicLinksSync(); | |
57 var pubspecDir = _findDirWithFile(outputDir, 'pubspec.yaml'); | |
58 | |
59 if (pubspecDir == null) { | |
60 print('Could not find pubspec.yaml when walking up from $outputDir'); | |
61 exitCode = 1; | |
62 return; | |
63 } | |
64 | |
65 var relativeEntryPoint = path.relative( | |
66 path.join(outputDir, path.basename(entryPoint)), from: pubspecDir); | |
67 | |
68 try { | |
69 if (_createBoilerPlate(relativeEntryPoint, pubspecDir)) { | |
70 print('Added $entryPoint to ${path.join(pubspecDir, "pubspec.yaml")}'); | |
71 } | |
72 print('Successfully created:'); | |
73 print(' ' + path.join(pubspecDir, entryPoint)); | |
74 } catch (e, t) { | |
75 print('Exception: $e\n$t'); | |
76 print('Error creating files in $outputDir'); | |
77 exitCode = 1; | |
78 } | |
79 | |
80 return; | |
81 } | |
82 | |
83 String _findDirWithFile(String dir, String filename) { | |
84 while (!new File(path.join(dir, filename)).existsSync()) { | |
85 var parentDir = path.dirname(dir); | |
86 // If we reached root and failed to find it, bail. | |
87 if (parentDir == dir) return null; | |
88 dir = parentDir; | |
89 } | |
90 return dir; | |
91 } | |
92 | |
93 // Returns true if the pubspec file was modified. It might not be modified if | |
94 // there was a monolithic polymer transformer in the pubspec, or if the entry | |
95 // point for some reason already existed in the pubspec. | |
96 bool _createBoilerPlate(String entryPoint, String pubspecDir) { | |
97 String html = ''' | |
98 <!doctype html> | |
99 <html> | |
100 <head> | |
101 <!-- link rel="import" href="path_to_html_import.html" --> | |
102 </head> | |
103 <body> | |
104 <!-- HTML for body here --> | |
105 <script type="application/dart">export 'package:polymer/init.dart';</script> | |
106 </body> | |
107 </html> | |
108 '''; | |
109 | |
110 new File(path.join(pubspecDir, entryPoint)).writeAsStringSync(html); | |
111 | |
112 var pubspecPath = path.join(pubspecDir, 'pubspec.yaml'); | |
113 var pubspecText = new File(pubspecPath).readAsStringSync(); | |
114 var transformers = loadYaml(pubspecText)['transformers']; | |
115 var entryPoints; | |
116 | |
117 var insertionPoint; | |
118 var textToInsert = ''; | |
119 | |
120 if (transformers != null) { | |
121 // If there are transformers in the pubspec, look for the polymer | |
122 // transformers, get the entry points, and delete the old entry points. | |
123 SourceSpan transformersSourceSpan = transformers.span; | |
124 | |
125 SourceSpan polymerTransformerSourceSpan; | |
126 SourceSpan entryPointsSourceSpan; | |
127 for (var e in transformers) { | |
128 if (e == 'polymer') { | |
129 // If they had an empty polymer transformer, just get rid of it (we will | |
130 // replace it with our own map style one). | |
131 var polymerRegex = new RegExp(r'\n\s*-\spolymer\s*'); | |
132 // Insert right after the newline. | |
133 insertionPoint = pubspecText.indexOf(polymerRegex) + 1; | |
134 pubspecText = pubspecText.replaceFirst(polymerRegex, '\n'); | |
135 } else if (e is YamlMap && e['polymer'] != null) { | |
136 polymerTransformerSourceSpan = e['polymer'].span; | |
137 | |
138 var existing = e['polymer']['entry_points']; | |
139 if (existing == null && e['polymer'].containsKey('entry_points')) { | |
140 if (path.split(entryPoint)[0] != 'web') { | |
141 print('WARNING: Did not add entry_point $entryPoint to pubspec.yaml' | |
142 ' because of existing empty `entry_points` field in polymer' | |
143 ' transformer. This defaults to treating all files under `web/`' | |
144 ' as entry points, but you tried to add an entry point outside' | |
145 ' of the `web/` folder. You will need to explicitly list all' | |
146 ' entrypoints that you care about into your pubspec in order to' | |
147 ' include any outside of `web/`.'); | |
148 } | |
149 return false; | |
150 } | |
151 entryPoints = (existing == null | |
152 ? [] | |
153 : (existing is String ? [existing] : existing.toList())); | |
154 | |
155 if (entryPoints.contains(entryPoint)) return false; | |
156 entryPoints.add(entryPoint); | |
157 | |
158 if (existing != null) { | |
159 entryPointsSourceSpan = existing.span; | |
160 } | |
161 } | |
162 } | |
163 | |
164 if (polymerTransformerSourceSpan == null) { | |
165 if (insertionPoint == null) { | |
166 insertionPoint = transformersSourceSpan.start.offset; | |
167 } | |
168 textToInsert = '- polymer:\n entry_points:\n'; | |
169 } else if (entryPointsSourceSpan == null) { | |
170 insertionPoint = polymerTransformerSourceSpan.start.offset; | |
171 textToInsert = ' entry_points:\n'; | |
172 } else { | |
173 insertionPoint = entryPointsSourceSpan.start.offset; | |
174 pubspecText = '${pubspecText.substring(0, insertionPoint)}' | |
175 '${pubspecText.substring(entryPointsSourceSpan.end.offset)}'; | |
176 } | |
177 } else { | |
178 // There were no transformers at all. | |
179 insertionPoint = pubspecText.length; | |
180 var optionalNewline = pubspecText.endsWith('\n') ? '' : '\n'; | |
181 textToInsert = ''' | |
182 ${optionalNewline}transformers: | |
183 - polymer: | |
184 entry_points: | |
185 '''; | |
186 entryPoints = [entryPoint]; | |
187 } | |
188 | |
189 if (entryPoints == null) entryPoints = [entryPoint]; | |
190 // TODO(dgrove): Once dartbug.com/20409 is addressed, use that here. | |
191 var entryPointsText = entryPoints.map((e) => ' - $e').join('\n'); | |
192 | |
193 textToInsert += entryPointsText; | |
194 if (insertionPoint == pubspecText.length) { | |
195 pubspecText = '${pubspecText}${textToInsert}'; | |
196 } else { | |
197 pubspecText = '${pubspecText.substring(0, insertionPoint)}' | |
198 '${textToInsert}\n${pubspecText.substring(insertionPoint)}'; | |
199 } | |
200 | |
201 _writePubspec(pubspecPath, pubspecText); | |
202 return true; | |
203 } | |
204 | |
205 _writePubspec(String pubspecPath, String text) { | |
206 new File(pubspecPath).writeAsStringSync(text); | |
207 } | |
OLD | NEW |