OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016, 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 import 'dart:html' show HttpRequest; | |
5 import 'dart:convert' show BASE64; | |
6 | |
7 import 'package:args/command_runner.dart'; | |
8 import 'package:analyzer/src/generated/source.dart' | |
9 show Source, DartUriResolver; | |
10 import 'package:analyzer/src/summary/idl.dart' show PackageBundle; | |
11 import 'package:dev_compiler/src/compiler/compiler.dart' | |
12 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; | |
13 import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions; | |
14 | |
15 import 'package:analyzer/src/generated/engine.dart' | |
16 show AnalysisContext, AnalysisEngine, TimestampedData; | |
17 import 'package:analyzer/src/generated/sdk.dart' | |
18 show DartSdk, SdkLibrary, SdkLibraryImpl; | |
19 import 'package:analyzer/src/generated/source.dart' | |
20 show DartUriResolver, Source, SourceFactory, UriKind; | |
21 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; | |
22 import 'package:analyzer/src/context/cache.dart' | |
23 show AnalysisCache, CachePartition; | |
24 import 'package:analyzer/file_system/memory_file_system.dart' | |
25 show MemoryResourceProvider; | |
26 import 'package:analyzer/file_system/file_system.dart' | |
27 show ResourceProvider, ResourceUriResolver; | |
28 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; | |
29 | |
30 typedef void MessageHandler(Object message); | |
31 | |
32 /// The command for invoking the modular compiler. | |
33 class CompileCommand extends Command { | |
Jennifer Messerly
2016/07/27 21:38:33
Maybe rename this WebCompileCommand?
priscillalee
2016/07/27 23:48:33
Done.
| |
34 get name => 'compile'; | |
35 get description => 'Compile a set of Dart files into a JavaScript module.'; | |
36 final MessageHandler messageHandler; | |
37 final Function onload; | |
Jennifer Messerly
2016/07/27 21:38:33
suggestion, declare a strong type here. `Function`
priscillalee
2016/07/27 23:48:33
Typedefs are a really good idea! Thanks for that s
| |
38 | |
39 CompileCommand(this.onload, {MessageHandler messageHandler}) | |
40 : this.messageHandler = messageHandler ?? print { | |
41 CompilerOptions.addArguments(argParser); | |
42 AnalyzerOptions.addArguments(argParser); | |
43 } | |
44 | |
45 @override | |
46 String run() { | |
47 HttpRequest request = new HttpRequest(); | |
Jennifer Messerly
2016/07/27 21:38:33
style: I would probably write this as:
var re
priscillalee
2016/07/27 23:48:33
Done.
| |
48 | |
49 request.onReadyStateChange.listen((_) { | |
Jennifer Messerly
2016/07/27 21:38:33
one way to convert this to async/await:
Future<C
priscillalee
2016/07/27 23:48:33
Wow, I love how the async syntax reads, and it'd b
Jennifer Messerly
2016/07/28 05:23:10
Ah, maybe not worth worrying about then. Offhand I
priscillalee
2016/07/28 17:15:52
Acknowledged.
| |
50 if (request.readyState == HttpRequest.DONE && | |
51 (request.status == 200 || request.status == 0)) { | |
52 var response = request.responseText; | |
53 var sdkBytes = BASE64.decode(response); | |
54 Function result = setUpCompile(sdkBytes); | |
55 onload(result); | |
56 } | |
57 }); | |
58 | |
59 request.open('get', 'dart_sdk.sum'); | |
60 request.send(); | |
61 | |
62 return ""; | |
63 } | |
64 | |
65 Function setUpCompile(List<int> sdkBytes) { | |
Jennifer Messerly
2016/07/27 21:38:33
would be good to use a typedef for this function t
priscillalee
2016/07/27 23:48:33
Done.
| |
66 var resourceProvider = new MemoryResourceProvider(); | |
67 var packageBundle = new PackageBundle.fromBuffer(sdkBytes); | |
68 var webDartSdk = | |
69 new SummaryBasedDartSdk(true, packageBundle, resourceProvider); | |
70 | |
71 var sdkResolver = new DartUriResolver(webDartSdk); | |
72 var fileResolvers = [new ResourceUriResolver(resourceProvider)]; | |
73 | |
74 var compiler = new ModuleCompiler( | |
75 new AnalyzerOptions(dartSdkPath: '/dart-sdk'), | |
76 sdkResolver: sdkResolver, | |
77 fileResolvers: fileResolvers, | |
78 resourceProvider: resourceProvider); | |
79 | |
80 var compilerOptions = new CompilerOptions.fromArguments(argResults); | |
81 | |
82 var number = 0; | |
83 | |
84 return (String dart, Function callback) { | |
85 //Create a new virtual File that contains the given Dart source. | |
86 number++; | |
87 resourceProvider.newFile("/expression$number.dart", dart); | |
88 | |
89 var unit = | |
90 new BuildUnit("", "", ["file:///expression$number.dart"], null); | |
91 | |
92 JSModuleFile module = compiler.compile(unit, compilerOptions); | |
93 module.errors.forEach(messageHandler); | |
94 | |
95 if (!module.isValid) throw new CompileErrorException(); | |
96 callback(module.code); //module.summaryBytes; | |
Jennifer Messerly
2016/07/27 21:38:33
could this be a return value instead of a callback
priscillalee
2016/07/27 23:48:33
Done.
| |
97 }; | |
98 } | |
99 } | |
100 | |
101 /// Thrown when the input source code has errors. | |
102 class CompileErrorException implements Exception { | |
103 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | |
104 } | |
105 | |
106 class WebDartSdk implements DartSdk { | |
Jennifer Messerly
2016/07/27 21:38:33
we didn't end up using this right? So we could del
priscillalee
2016/07/27 23:48:33
Done.
| |
107 final Map<Uri, _WebSdkSource> _sources = {}; | |
108 final bool reportMissing; | |
109 final Map<String, SdkLibrary> _libs = {}; | |
110 final String sdkVersion = '0'; | |
111 List<String> get uris => _sources.keys.map((uri) => '$uri').toList(); | |
112 | |
113 AnalysisContext context; | |
114 DartUriResolver _resolver; | |
115 DartUriResolver get resolver => _resolver; | |
116 PackageBundle packageBundle; | |
117 | |
118 WebDartSdk(Map<String, String> sources, ResourceProvider resourceProvider, | |
119 this.packageBundle, | |
120 {this.reportMissing}) { | |
121 context = new _SdkAnalysisContext(this); | |
122 sources.forEach((uriString, contents) { | |
123 var uri = Uri.parse(uriString); | |
124 _sources[uri] = new _WebSdkSource(uri, contents); | |
125 _libs[uriString] = new SdkLibraryImpl(uri.path) | |
126 ..setDart2JsLibrary() | |
127 ..setVmLibrary(); | |
128 }); | |
129 _resolver = new DartUriResolver(this); | |
130 | |
131 context.sourceFactory = | |
132 new SourceFactory([_resolver], null, resourceProvider); | |
133 } | |
134 | |
135 @override | |
136 PackageBundle getSummarySdkBundle(bool strongMode) { | |
137 return packageBundle; | |
138 } | |
139 | |
140 List<SdkLibrary> get sdkLibraries => _libs.values.toList(); | |
141 SdkLibrary getSdkLibrary(String dartUri) => _libs[dartUri]; | |
142 Source mapDartUri(String dartUri) => _getSource(Uri.parse(dartUri)); | |
143 | |
144 Source fromEncoding(UriKind kind, Uri uri) { | |
145 if (kind != UriKind.DART_URI) { | |
146 throw new UnsupportedError('expected dart: uri kind, got $kind.'); | |
147 } | |
148 return _getSource(uri); | |
149 } | |
150 | |
151 Source _getSource(Uri uri) { | |
152 var src = _sources[uri]; | |
153 if (src == null) { | |
154 if (reportMissing) print('warning: missing mock for $uri.'); | |
155 _sources[uri] = src = new _WebSdkSource(uri, 'library dart.${uri.path};'); | |
156 } | |
157 return src; | |
158 } | |
159 | |
160 @override | |
161 Source fromFileUri(Uri uri) { | |
162 throw new UnsupportedError('WebDartSdk.fromFileUri'); | |
163 } | |
164 } | |
165 | |
166 class _WebSdkSource implements Source { | |
167 /// Absolute URI which this source can be imported from. | |
168 final Uri uri; | |
169 final String _contents; | |
170 | |
171 _WebSdkSource(this.uri, this._contents); | |
172 | |
173 bool exists() => true; | |
174 | |
175 int get hashCode => uri.hashCode; | |
176 | |
177 final int modificationStamp = 1; | |
178 | |
179 TimestampedData<String> get contents => | |
180 new TimestampedData(modificationStamp, _contents); | |
181 | |
182 String get encoding => "${uriKind.encoding}$uri"; | |
183 | |
184 Source get source => this; | |
185 | |
186 String get fullName => shortName; | |
187 | |
188 String get shortName => uri.path; | |
189 | |
190 UriKind get uriKind => UriKind.DART_URI; | |
191 | |
192 bool get isInSystemLibrary => true; | |
193 | |
194 Source resolveRelative(Uri relativeUri) => | |
195 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | |
196 | |
197 Uri resolveRelativeUri(Uri relativeUri) => | |
198 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | |
199 } | |
200 | |
201 /// An [AnalysisContextImpl] that only contains sources for a Dart SDK. | |
202 class _SdkAnalysisContext extends AnalysisContextImpl { | |
203 final DartSdk sdk; | |
204 | |
205 _SdkAnalysisContext(this.sdk); | |
206 | |
207 @override | |
208 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) { | |
209 if (factory == null) { | |
210 return super.createCacheFromSourceFactory(factory); | |
211 } | |
212 return new AnalysisCache( | |
213 <CachePartition>[AnalysisEngine.instance.partitionManager.forSdk(sdk)]); | |
214 } | |
215 } | |
OLD | NEW |