|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a
|
3 | 3 | // BSD-style license that can be found in the LICENSE file.
|
4 | 4 |
|
5 |
| -import 'dart:convert'; |
6 |
| -import 'dart:io'; |
7 |
| - |
8 |
| -import 'package:dds/dds.dart'; |
9 |
| -import 'package:dds/src/arg_parser.dart'; |
10 |
| -import 'package:dds/src/bazel_uri_converter.dart'; |
11 |
| - |
12 |
| -import 'package:path/path.dart' as path; |
13 |
| - |
14 |
| -Uri _getDevToolsAssetPath() { |
15 |
| - final dartDir = File(Platform.resolvedExecutable).parent.path; |
16 |
| - final fullSdk = dartDir.endsWith('bin'); |
17 |
| - return Uri.file( |
18 |
| - fullSdk |
19 |
| - ? path.absolute( |
20 |
| - dartDir, |
21 |
| - 'resources', |
22 |
| - 'devtools', |
23 |
| - ) |
24 |
| - : path.absolute( |
25 |
| - dartDir, |
26 |
| - 'devtools', |
27 |
| - ), |
28 |
| - ); |
29 |
| -} |
| 5 | +import 'package:dds/src/dds_cli_entrypoint.dart'; |
30 | 6 |
|
31 | 7 | Future<void> main(List<String> args) async {
|
32 |
| - final argParser = DartDevelopmentServiceOptions.createArgParser( |
33 |
| - includeHelp: true, |
34 |
| - ); |
35 |
| - final argResults = argParser.parse(args); |
36 |
| - if (args.isEmpty || argResults.wasParsed('help')) { |
37 |
| - print(''' |
38 |
| -Starts a Dart Development Service (DDS) instance. |
39 |
| -
|
40 |
| -Usage: |
41 |
| -${argParser.usage} |
42 |
| - '''); |
43 |
| - return; |
44 |
| - } |
45 |
| - |
46 |
| - // This URI is provided by the VM service directly so don't bother doing a |
47 |
| - // lookup. |
48 |
| - final remoteVmServiceUri = Uri.parse( |
49 |
| - argResults[DartDevelopmentServiceOptions.vmServiceUriOption], |
50 |
| - ); |
51 |
| - |
52 |
| - // Resolve the address which is potentially provided by the user. |
53 |
| - late InternetAddress address; |
54 |
| - final bindAddress = |
55 |
| - argResults[DartDevelopmentServiceOptions.bindAddressOption]; |
56 |
| - try { |
57 |
| - final addresses = await InternetAddress.lookup(bindAddress); |
58 |
| - // Prefer IPv4 addresses. |
59 |
| - for (int i = 0; i < addresses.length; i++) { |
60 |
| - address = addresses[i]; |
61 |
| - if (address.type == InternetAddressType.IPv4) break; |
62 |
| - } |
63 |
| - } on SocketException catch (e, st) { |
64 |
| - writeErrorResponse('Invalid bind address: $bindAddress', st); |
65 |
| - return; |
66 |
| - } |
67 |
| - |
68 |
| - final portString = argResults[DartDevelopmentServiceOptions.bindPortOption]; |
69 |
| - int port; |
70 |
| - try { |
71 |
| - port = int.parse(portString); |
72 |
| - } on FormatException catch (e, st) { |
73 |
| - writeErrorResponse('Invalid port: $portString', st); |
74 |
| - return; |
75 |
| - } |
76 |
| - final serviceUri = Uri( |
77 |
| - scheme: 'http', |
78 |
| - host: address.address, |
79 |
| - port: port, |
80 |
| - ); |
81 |
| - final disableServiceAuthCodes = |
82 |
| - argResults[DartDevelopmentServiceOptions.disableServiceAuthCodesFlag]; |
83 |
| - |
84 |
| - final serveDevTools = |
85 |
| - argResults[DartDevelopmentServiceOptions.serveDevToolsFlag]; |
86 |
| - final devToolsServerAddressStr = |
87 |
| - argResults[DartDevelopmentServiceOptions.devToolsServerAddressOption]; |
88 |
| - Uri? devToolsBuildDirectory; |
89 |
| - final devToolsServerAddress = devToolsServerAddressStr == null |
90 |
| - ? null |
91 |
| - : Uri.parse(devToolsServerAddressStr); |
92 |
| - if (serveDevTools) { |
93 |
| - devToolsBuildDirectory = _getDevToolsAssetPath(); |
94 |
| - } |
95 |
| - final enableServicePortFallback = |
96 |
| - argResults[DartDevelopmentServiceOptions.enableServicePortFallbackFlag]; |
97 |
| - final cachedUserTags = |
98 |
| - argResults[DartDevelopmentServiceOptions.cachedUserTagsOption]; |
99 |
| - final google3WorkspaceRoot = |
100 |
| - argResults[DartDevelopmentServiceOptions.google3WorkspaceRootOption]; |
101 |
| - |
102 |
| - try { |
103 |
| - final dds = await DartDevelopmentService.startDartDevelopmentService( |
104 |
| - remoteVmServiceUri, |
105 |
| - serviceUri: serviceUri, |
106 |
| - enableAuthCodes: !disableServiceAuthCodes, |
107 |
| - ipv6: address.type == InternetAddressType.IPv6, |
108 |
| - devToolsConfiguration: serveDevTools && devToolsBuildDirectory != null |
109 |
| - ? DevToolsConfiguration( |
110 |
| - enable: serveDevTools, |
111 |
| - customBuildDirectoryPath: devToolsBuildDirectory, |
112 |
| - devToolsServerAddress: devToolsServerAddress, |
113 |
| - ) |
114 |
| - : null, |
115 |
| - enableServicePortFallback: enableServicePortFallback, |
116 |
| - cachedUserTags: cachedUserTags, |
117 |
| - uriConverter: google3WorkspaceRoot != null |
118 |
| - ? BazelUriConverter(google3WorkspaceRoot).uriToPath |
119 |
| - : null, |
120 |
| - ); |
121 |
| - final dtdInfo = dds.hostedDartToolingDaemon; |
122 |
| - stderr.write(json.encode({ |
123 |
| - 'state': 'started', |
124 |
| - 'ddsUri': dds.uri.toString(), |
125 |
| - if (dds.devToolsUri != null) 'devToolsUri': dds.devToolsUri.toString(), |
126 |
| - if (dtdInfo != null) |
127 |
| - 'dtd': { |
128 |
| - 'uri': dtdInfo.uri, |
129 |
| - }, |
130 |
| - })); |
131 |
| - stderr.close(); |
132 |
| - } catch (e, st) { |
133 |
| - writeErrorResponse(e, st); |
134 |
| - } finally { |
135 |
| - // Always close stderr to notify tooling that DDS has finished writing |
136 |
| - // launch details. |
137 |
| - await stderr.close(); |
138 |
| - } |
139 |
| -} |
140 |
| - |
141 |
| -void writeErrorResponse(Object e, StackTrace st) { |
142 |
| - stderr.write(json.encode({ |
143 |
| - 'state': 'error', |
144 |
| - 'error': '$e', |
145 |
| - 'stacktrace': '$st', |
146 |
| - if (e is DartDevelopmentServiceException) 'ddsExceptionDetails': e.toJson(), |
147 |
| - })); |
| 8 | + // This level of indirection is only here so DDS can be configured for |
| 9 | + // google3 specific functionality as it's not possible to import files under |
| 10 | + // a package's bin directory to wrap the entrypoint. |
| 11 | + await runDartDevelopmentServiceFromCLI(args); |
148 | 12 | }
|
0 commit comments