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