OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, 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 #library('frog_leg'); | |
6 | |
7 #import('../../lib/uri/uri.dart'); | |
8 #import('source_file.dart'); | |
9 #import('../lang.dart', prefix: 'frog'); | |
10 #import('api.dart', prefix: 'api'); | |
11 #import('io/io.dart', prefix: 'io'); | |
12 | |
13 String relativize(Uri base, Uri uri) { | |
14 if (base.scheme == 'file' && | |
15 base.scheme == uri.scheme && | |
16 base.userInfo == uri.userInfo && | |
17 base.domain == uri.domain && | |
18 base.port == uri.port && | |
19 uri.query == "" && uri.fragment == "") { | |
20 if (uri.path.startsWith(base.path)) { | |
21 return uri.path.substring(base.path.length); | |
22 } | |
23 List<String> uriParts = uri.path.split('/'); | |
24 List<String> baseParts = base.path.split('/'); | |
25 int common = 0; | |
26 int length = Math.min(uriParts.length, baseParts.length); | |
27 while (common < length && uriParts[common] == baseParts[common]) { | |
28 common++; | |
29 } | |
30 StringBuffer sb = new StringBuffer(); | |
31 for (int i = common + 1; i < baseParts.length; i++) { | |
32 sb.add('../'); | |
33 } | |
34 for (int i = common; i < uriParts.length - 1; i++) { | |
35 sb.add('${uriParts[i]}/'); | |
36 } | |
37 sb.add('${uriParts.last()}'); | |
38 return sb.toString(); | |
39 } | |
40 return uri.toString(); | |
41 } | |
42 | |
43 bool compile(frog.World world) { | |
44 final throwOnError = frog.options.throwOnErrors; | |
45 final showWarnings = frog.options.showWarnings; | |
46 final allowMockCompilation = frog.options.allowMockCompilation; | |
47 // final compiler = new WorldCompiler(world, throwOnError); | |
48 Uri cwd = new Uri(scheme: 'file', path: io.getCurrentDirectory()); | |
49 Uri uri = cwd.resolve(frog.options.dartScript); | |
50 String frogLibDir = frog.options.libDir; | |
51 if (!frogLibDir.endsWith("/")) frogLibDir = "$frogLibDir/"; | |
52 Uri frogLib = new Uri(scheme: 'file', path: frogLibDir); | |
53 Uri libraryRoot = frogLib.resolve('../leg/lib/'); | |
54 Map<String, SourceFile> sourceFiles = <SourceFile>{}; | |
55 | |
56 Future<String> provider(Uri uri) { | |
57 if (uri.scheme != 'file') { | |
58 throw new IllegalArgumentException(uri); | |
59 } | |
60 String source = world.files.readAll(uri.path); | |
61 world.dartBytesRead += source.length; | |
62 sourceFiles[uri.toString()] = | |
63 new SourceFile(relativize(cwd, uri), source); | |
64 Completer<String> completer = new Completer<String>(); | |
65 completer.complete(source); | |
66 return completer.future; | |
67 } | |
68 | |
69 void handler(Uri uri, int begin, int end, String message, bool fatal) { | |
70 if (uri === null && !fatal) { | |
71 world.info('[leg] $message'); | |
72 return; | |
73 } | |
74 if (uri === null) { | |
75 assert(fatal); | |
76 print(message); | |
77 } else if (fatal || showWarnings) { | |
78 SourceFile file = sourceFiles[uri.toString()]; | |
79 print(file.getLocationMessage(message, begin, end, true)); | |
80 } | |
81 if (fatal && throwOnError) throw new AbortLeg(message); | |
82 } | |
83 | |
84 List<String> options = new List<String>(); | |
85 if (allowMockCompilation) options.add('--allow-mock-compilation'); | |
86 | |
87 // TODO(ahe): We expect the future to be complete and call value | |
88 // directly. In effect, we don't support truly asynchronous API. | |
89 String code = api.compile(uri, libraryRoot, provider, handler, options).value; | |
90 if (code === null) return false; | |
91 world.legCode = code; | |
92 world.jsBytesWritten = code.length; | |
93 return true; | |
94 } | |
95 | |
96 class AbortLeg { | |
97 final message; | |
98 AbortLeg(this.message); | |
99 toString() => 'Aborted due to --throw-on-error: $message'; | |
100 } | |
OLD | NEW |