OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /// This library generates Mojo bindings for a Dart package. | 5 /// This library generates Mojo bindings for a Dart package. |
6 | 6 |
7 library generate; | 7 library generate; |
8 | 8 |
9 import 'dart:async'; | 9 import 'dart:async'; |
10 import 'dart:convert'; | 10 import 'dart:convert'; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 oldestModTime = modTime; | 88 oldestModTime = modTime; |
89 } | 89 } |
90 mojomDartCount++; | 90 mojomDartCount++; |
91 } | 91 } |
92 return [oldestModTime, mojomDartCount]; | 92 return [oldestModTime, mojomDartCount]; |
93 } | 93 } |
94 | 94 |
95 // Delete .mojom.dart files under [package] that are [olderThanThis]. | 95 // Delete .mojom.dart files under [package] that are [olderThanThis]. |
96 _deleteOldMojomDart(Directory package, DateTime olderThanThis) async { | 96 _deleteOldMojomDart(Directory package, DateTime olderThanThis) async { |
97 Directory libDir = new Directory(path.join(package.path, 'lib')); | 97 Directory libDir = new Directory(path.join(package.path, 'lib')); |
98 assert(await libDir.exists()); | 98 if (!await libDir.exists()) { |
99 return; | |
100 } | |
99 await for (var file in libDir.list(recursive: true, followLinks: false)) { | 101 await for (var file in libDir.list(recursive: true, followLinks: false)) { |
100 if (file is! File) continue; | 102 if (file is! File) continue; |
101 if (!isMojomDart(file.path)) continue; | 103 if (!isMojomDart(file.path)) continue; |
102 DateTime modTime = (await file.stat()).modified; | 104 DateTime modTime = (await file.stat()).modified; |
103 if (modTime.isBefore(olderThanThis)) { | 105 if (modTime.isBefore(olderThanThis)) { |
104 log.warning("Deleting stale .mojom.dart: $file"); | 106 log.warning("Deleting stale .mojom.dart: $file"); |
105 await file.delete(); | 107 await file.delete(); |
106 } | 108 } |
107 } | 109 } |
108 } | 110 } |
109 | 111 |
110 /// If the .mojoms file or the newest .mojom is newer than the oldest | 112 /// If the .mojoms file or the newest .mojom is newer than the oldest |
111 /// .mojom.dart, then regenerate everything. | 113 /// .mojom.dart, then regenerate everything. |
112 bool _shouldRegenerate(DateTime mojomTime, DateTime mojomDartTime) { | 114 bool _shouldRegenerate(DateTime mojomTime, DateTime mojomDartTime) { |
113 return (mojomTime == null) || | 115 return (mojomTime == null) || |
114 (mojomDartTime == null) || | 116 (mojomDartTime == null) || |
115 mojomTime.isAfter(mojomDartTime); | 117 mojomTime.isAfter(mojomDartTime); |
116 } | 118 } |
117 | 119 |
118 _runBindingsGeneration(String script, List<String> arguments) async { | 120 _runBindingsGeneration(String script, List<String> arguments) async { |
119 var result; | 121 var result; |
120 var stopwatch = new Stopwatch()..start(); | 122 var stopwatch = new Stopwatch()..start(); |
121 result = await Process.run(script, arguments); | 123 result = await Process.run(script, arguments); |
122 stopwatch.stop(); | 124 stopwatch.stop(); |
123 _genMs.value += stopwatch.elapsedMilliseconds; | 125 _genMs.value += stopwatch.elapsedMilliseconds; |
124 return result; | 126 return result; |
125 } | 127 } |
126 | 128 |
129 // This is a hack until we can express import paths in .mojom files. | |
130 // This checks the mojom path for '/mojo/services/' and if found assumes | |
131 // that is an import path. | |
jamesr
2015/11/17 01:35:07
i think you accidentally a word or few. maybe "..a
Cutch
2015/11/17 18:17:15
Done.
| |
132 String _sniffForMojoServicesInclude(String mojomPath) { | |
133 List<String> pathComponents = path.split(mojomPath); | |
134 while (pathComponents.length > 2) { | |
135 int last = pathComponents.length; | |
136 if ((pathComponents[last - 1] == 'services') && | |
137 (pathComponents[last - 2] == 'mojo')) { | |
138 return path.joinAll(pathComponents); | |
139 } | |
140 // Remove the last element and try again. | |
141 pathComponents.removeLast(); | |
142 } | |
143 return null; | |
144 } | |
145 | |
127 _generateForMojom(File mojom, Directory importDir, Directory destination, | 146 _generateForMojom(File mojom, Directory importDir, Directory destination, |
128 String packageName) async { | 147 String packageName) async { |
129 if (!isMojom(mojom.path)) return; | 148 if (!isMojom(mojom.path)) return; |
130 log.info("_generateForMojom($mojom)"); | 149 log.info("_generateForMojom($mojom)"); |
131 | 150 |
132 final script = path.join( | 151 final script = path.join( |
133 _mojoSdk.path, 'tools', 'bindings', 'mojom_bindings_generator.py'); | 152 _mojoSdk.path, 'tools', 'bindings', 'mojom_bindings_generator.py'); |
134 final sdkInc = path.normalize(path.join(_mojoSdk.path, '..', '..')); | 153 final sdkInc = path.normalize(path.join(_mojoSdk.path, '..', '..')); |
135 final outputDir = await destination.createTemp(); | 154 final outputDir = await destination.createTemp(); |
136 final output = outputDir.path; | 155 final output = outputDir.path; |
156 | |
157 final servicesPath = _sniffForMojoServicesInclude(mojom.path); | |
158 | |
137 final arguments = [ | 159 final arguments = [ |
138 '--use_bundled_pylibs', | 160 '--use_bundled_pylibs', |
139 '-g', | 161 '-g', |
140 'dart', | 162 'dart', |
141 '-o', | 163 '-o', |
142 output, | 164 output, |
143 // TODO(zra): Are other include paths needed? | |
144 '-I', | 165 '-I', |
145 sdkInc, | 166 sdkInc, |
146 '-I', | 167 '-I', |
147 importDir.path, | 168 importDir.path |
148 mojom.path | |
149 ]; | 169 ]; |
170 // TODO(zra): Are other include paths needed? | |
jamesr
2015/11/17 01:35:07
i think we can nuke this for now
Cutch
2015/11/17 18:17:15
Done.
| |
171 if (servicesPath != null) { | |
172 arguments.add('-I'); | |
173 arguments.add(servicesPath); | |
174 } | |
175 arguments.add(mojom.path); | |
150 | 176 |
151 log.info('Generating $mojom'); | 177 log.info('Generating $mojom'); |
152 log.info('$script ${arguments.join(" ")}'); | 178 log.info('$script ${arguments.join(" ")}'); |
153 log.info('dryRun = $_dryRun'); | 179 log.info('dryRun = $_dryRun'); |
154 if (!_dryRun) { | 180 if (!_dryRun) { |
155 final result = await _runBindingsGeneration(script, arguments); | 181 final result = await _runBindingsGeneration(script, arguments); |
156 if (result.exitCode != 0) { | 182 if (result.exitCode != 0) { |
157 log.info("bindings generation result = ${result.exitCode}"); | 183 log.info("bindings generation result = ${result.exitCode}"); |
158 await outputDir.delete(recursive: true); | 184 await outputDir.delete(recursive: true); |
159 throw new GenerationError("$script failed:\n" | 185 throw new GenerationError("$script failed:\n" |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
405 '\t$dart $packRoot $scriptPath single -m $mojoSdk -r $root ' | 431 '\t$dart $packRoot $scriptPath single -m $mojoSdk -r $root ' |
406 '-p $packagePath $skips'); | 432 '-p $packagePath $skips'); |
407 _errors++; | 433 _errors++; |
408 return; | 434 return; |
409 } | 435 } |
410 } | 436 } |
411 } | 437 } |
412 | 438 |
413 bool _shouldSkip(File f) => containsPrefix(f.path, _skip); | 439 bool _shouldSkip(File f) => containsPrefix(f.path, _skip); |
414 } | 440 } |
OLD | NEW |