Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(494)

Side by Side Diff: pkg/polymer/bin/new_entry.dart

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

Powered by Google App Engine
This is Rietveld 408576698