OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino 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.md file. | |
4 | |
5 library fletchc.verbs.servicec_verb; | |
6 | |
7 import 'dart:io' show | |
8 File, | |
9 Directory, | |
10 Platform; | |
11 | |
12 import 'package:path/path.dart' show join, dirname; | |
13 | |
14 import 'infrastructure.dart'; | |
15 | |
16 import '../hub/exit_codes.dart' show | |
17 DART_VM_EXITCODE_COMPILE_TIME_ERROR; | |
18 | |
19 import 'package:servicec/compiler.dart' as servicec; | |
20 | |
21 import 'package:servicec/errors.dart' show | |
22 CompilationError, | |
23 ErrorReporter; | |
24 | |
25 import 'documentation.dart' show | |
26 servicecDocumentation; | |
27 | |
28 import "package:compiler/src/util/uri_extras.dart" show | |
29 relativize; | |
30 | |
31 import "package:fletchc/src/guess_configuration.dart" show | |
32 executable; | |
33 | |
34 const Action servicecAction = const Action( | |
35 // A session is required for a worker. | |
36 servicecAct, | |
37 servicecDocumentation, | |
38 requiresSession: true, | |
39 requiredTarget: TargetKind.FILE, | |
40 allowsTrailing: true); | |
41 | |
42 Future<int> servicecAct(AnalyzedSentence sentence, VerbContext context) { | |
43 return context.performTaskInWorker( | |
44 new CompileTask(sentence.targetUri, sentence.base, sentence.trailing)); | |
45 } | |
46 | |
47 class CompileTask extends SharedTask { | |
48 // Keep this class simple, see note in superclass. | |
49 | |
50 final Uri base; | |
51 final Uri targetUri; | |
52 final List<String> trailing; | |
53 | |
54 const CompileTask(this.targetUri, this.base, this.trailing); | |
55 | |
56 Future<int> call( | |
57 CommandSender commandSender, | |
58 StreamIterator<ClientCommand> commandIterator) { | |
59 return compileTask(targetUri, base, trailing); | |
60 } | |
61 } | |
62 | |
63 // TODO(stanm): test after issue #244 is resolved | |
64 const String _SERVICEC_DIR = const String.fromEnvironment("servicec-dir"); | |
65 | |
66 bool _looksLikeServicecDir(Uri uri) { | |
67 if (!new Directory.fromUri(uri).existsSync()) return false; | |
68 String expectedDirectory = join(uri.path, 'lib', 'src', 'resources'); | |
69 return new Directory(expectedDirectory).existsSync(); | |
70 } | |
71 | |
72 Uri guessServicecDir(Uri base) { | |
73 Uri servicecDirectory; | |
74 if (_SERVICEC_DIR != null) { | |
75 // Use Uri.base here because _SERVICEC_DIR is a constant relative to the | |
76 // location of where fletch was called from, not relative to the C++ | |
77 // client. | |
78 servicecDirectory = base.resolve(_SERVICEC_DIR); | |
79 } else { | |
80 Uri uri = executable.resolve(join('..', '..', 'tools', 'servicec')); | |
81 if (new Directory.fromUri(uri).existsSync()) { | |
82 servicecDirectory = uri; | |
83 } | |
84 } | |
85 if (servicecDirectory == null) { | |
86 throw new StateError(""" | |
87 Unable to guess the location of the service compiler (servicec). | |
88 Try adding command-line option '-Dservicec-dir=<path to service compiler>."""); | |
89 } else if (!_looksLikeServicecDir(servicecDirectory)) { | |
90 throw new StateError(""" | |
91 No resources directory in '$servicecDirectory'. | |
92 Try adding command-line option '-Dservicec-dir=<path to service compiler>."""); | |
93 } | |
94 return servicecDirectory; | |
95 } | |
96 | |
97 Future<int> compileTask(Uri targetUri, Uri base, List<String> arguments) async { | |
98 Uri servicecUri = guessServicecDir(base); | |
99 String resourcesDirectory = join(servicecUri.path, 'lib', 'src', 'resources'); | |
100 String outputDirectory; | |
101 if (null != arguments && arguments.length == 2 && arguments[0] == "out") { | |
102 outputDirectory = base.resolve(arguments[1]).toFilePath(); | |
103 } else { | |
104 print("Bad arguments: $arguments; expected 'out <out-dir>'."); | |
105 return DART_VM_EXITCODE_COMPILE_TIME_ERROR; | |
106 } | |
107 | |
108 String relativeName = relativize(base, targetUri, false); | |
109 print("Compiling $relativeName..."); | |
110 | |
111 String fileName = targetUri.toFilePath(); | |
112 bool success = await servicec.compileAndReportErrors( | |
113 fileName, relativeName, resourcesDirectory, outputDirectory); | |
114 | |
115 print("Compiled $relativeName to $outputDirectory"); | |
116 | |
117 return success ? 0 : DART_VM_EXITCODE_COMPILE_TIME_ERROR; | |
118 } | |
OLD | NEW |