@@ -14,93 +14,128 @@ import 'package:shelf_packages_handler/shelf_packages_handler.dart';
14
14
import 'package:shelf_static/shelf_static.dart' ;
15
15
16
16
void main (List <String > args) async {
17
- watch.start ();
18
- if (args.isNotEmpty) {
19
- throw ArgumentError ('No command line args are supported' );
20
- }
17
+ try {
18
+ watch.start ();
19
+ if (args.isNotEmpty) {
20
+ throw ArgumentError ('No command line args are supported' );
21
+ }
21
22
22
- var client = await DartDevcFrontendServerClient .start (
23
- 'org-dartlang-root:///$app ' , outputDill,
24
- fileSystemRoots: [p.current],
25
- fileSystemScheme: 'org-dartlang-root' ,
26
- verbose: true );
27
-
28
- _print ('compiling $app ' );
29
- await client.compile ([]);
30
- client.accept ();
31
- _print ('done compiling $app ' );
32
-
33
- _print ('starting shelf server' );
34
- var cascade = Cascade ()
35
- .add (_clientHandler (client))
36
- .add (createStaticHandler (p.current))
37
- .add (createFileHandler (
38
- p.join (sdkDir, 'lib' , 'dev_compiler' , 'kernel' , 'amd' , 'dart_sdk.js' ),
39
- url: 'example/app/dart_sdk.js' ))
40
- .add (createFileHandler (
41
- p.join (sdkDir, 'lib' , 'dev_compiler' , 'web' ,
42
- 'dart_stack_trace_mapper.js' ),
43
- url: 'example/app/dart_stack_trace_mapper.js' ))
44
- .add (createFileHandler (
45
- p.join (sdkDir, 'lib' , 'dev_compiler' , 'kernel' , 'amd' , 'require.js' ),
46
- url: 'example/app/require.js' ))
47
- .add (packagesDirHandler ());
48
- final server = await shelf_io.serve (cascade.handler, 'localhost' , 8080 );
49
- _print ('server ready' );
50
-
51
- // The file we will be editing in the repl
52
- var appFile = File (app);
53
- var originalContent = await appFile.readAsString ();
54
- var appLines = const LineSplitter ().convert (originalContent);
55
- var getterText = 'String get message =>' ;
56
- var messageLine = appLines.indexWhere ((line) => line.startsWith (getterText));
57
-
58
- var stdinQueue = StreamQueue (
59
- stdin.transform (utf8.decoder).transform (const LineSplitter ()));
60
- _prompt ();
61
- while (await stdinQueue.hasNext) {
62
- var newMessage = await stdinQueue.next;
63
- if (newMessage == 'quit' ) {
64
- await server.close ();
65
- await stdinQueue.cancel ();
66
- break ;
67
- } else if (newMessage == 'reset' ) {
68
- print ('resetting' );
69
- client.reset ();
70
- _print ('restoring $app ' );
71
- await appFile.writeAsString (originalContent);
72
- } else {
73
- _print ('editing $app ' );
74
- appLines[messageLine] = '$getterText "$newMessage ";' ;
75
- var newContent = appLines.join ('\n ' );
76
- await appFile.writeAsString (newContent);
77
-
78
- _print ('recompiling $app with edits' );
79
- var result =
80
- await client.compile ([Uri .parse ('org-dartlang-root:///$app ' )]);
81
- if (result.errorCount > 0 ) {
82
- print ('Compile errors: \n ${result .compilerOutputLines .join ('\n ' )}' );
83
- await client.reject ();
23
+ _print ('compiling the dart sdk' );
24
+ var sdkCompileResult = await Process .run (Platform .resolvedExecutable, [
25
+ p.join (sdkDir, 'bin' , 'snapshots' , 'dartdevc.dart.snapshot' ),
26
+ '--multi-root-scheme=org-dartlang-sdk' ,
27
+ '--modules=amd' ,
28
+ '--module-name=dart_sdk' ,
29
+ '--sound-null-safety' ,
30
+ '-o' ,
31
+ dartSdkJs,
32
+ p.url.join (sdkDir, sdkKernelPath),
33
+ ]);
34
+ if (sdkCompileResult.exitCode != 0 ) {
35
+ _print ('Failed to compile the dart sdk to JS:\n '
36
+ '${sdkCompileResult .stdout }\n '
37
+ '${sdkCompileResult .stderr }' );
38
+ exit (sdkCompileResult.exitCode);
39
+ }
40
+
41
+ _print ('starting frontend server' );
42
+ var client = await DartDevcFrontendServerClient .start (
43
+ 'org-dartlang-root:///$app ' , outputDill,
44
+ fileSystemRoots: [p.current],
45
+ fileSystemScheme: 'org-dartlang-root' ,
46
+ platformKernel: p.toUri (sdkKernelPath).toString (),
47
+ verbose: true );
48
+
49
+ _print ('compiling $app ' );
50
+ await client.compile ([]);
51
+ client.accept ();
52
+ _print ('done compiling $app ' );
53
+
54
+ _print ('starting shelf server' );
55
+ var cascade = Cascade ()
56
+ .add (_clientHandler (client))
57
+ .add (createStaticHandler (p.current))
58
+ .add (createFileHandler (dartSdkJs, url: 'example/app/dart_sdk.js' ))
59
+ .add (createFileHandler (
60
+ p.join (sdkDir, 'lib' , 'dev_compiler' , 'web' ,
61
+ 'dart_stack_trace_mapper.js' ),
62
+ url: 'example/app/dart_stack_trace_mapper.js' ))
63
+ .add (createFileHandler (
64
+ p.join (
65
+ sdkDir, 'lib' , 'dev_compiler' , 'kernel' , 'amd' , 'require.js' ),
66
+ url: 'example/app/require.js' ))
67
+ .add (packagesDirHandler ());
68
+ final server = await shelf_io.serve (cascade.handler, 'localhost' , 8080 );
69
+ _print ('server ready' );
70
+
71
+ // The file we will be editing in the repl
72
+ var appFile = File (app);
73
+ var originalContent = await appFile.readAsString ();
74
+ var appLines = const LineSplitter ().convert (originalContent);
75
+ var getterText = 'String get message =>' ;
76
+ var messageLine =
77
+ appLines.indexWhere ((line) => line.startsWith (getterText));
78
+
79
+ var stdinQueue = StreamQueue (
80
+ stdin.transform (utf8.decoder).transform (const LineSplitter ()));
81
+ _prompt ();
82
+ while (await stdinQueue.hasNext) {
83
+ var newMessage = await stdinQueue.next;
84
+ if (newMessage == 'quit' ) {
85
+ await server.close ();
86
+ await stdinQueue.cancel ();
87
+ break ;
88
+ } else if (newMessage == 'reset' ) {
89
+ print ('resetting' );
90
+ client.reset ();
91
+ _print ('restoring $app ' );
92
+ await appFile.writeAsString (originalContent);
84
93
} else {
85
- _print ('Recompile succeeded for $app ' );
86
- client.accept ();
87
- // TODO: support hot restart
88
- print ('reload app to see the new message' );
94
+ _print ('editing $app ' );
95
+ appLines[messageLine] = '$getterText "$newMessage ";' ;
96
+ var newContent = appLines.join ('\n ' );
97
+ await appFile.writeAsString (newContent);
98
+
99
+ _print ('recompiling $app with edits' );
100
+ var result =
101
+ await client.compile ([Uri .parse ('org-dartlang-root:///$app ' )]);
102
+ if (result.errorCount > 0 ) {
103
+ print ('Compile errors: \n ${result .compilerOutputLines .join ('\n ' )}' );
104
+ await client.reject ();
105
+ } else {
106
+ _print ('Recompile succeeded for $app ' );
107
+ client.accept ();
108
+ // TODO: support hot restart
109
+ print ('reload app to see the new message' );
110
+ }
89
111
}
112
+
113
+ _prompt ();
90
114
}
91
115
92
- _prompt ();
116
+ _print ('restoring $app ' );
117
+ await appFile.writeAsString (originalContent);
118
+ _print ('exiting' );
119
+ await client.shutdown ();
120
+ } finally {
121
+ Directory (p.join ('.dart_tool' , 'out' )).deleteSync (recursive: true );
93
122
}
94
-
95
- _print ('restoring $app ' );
96
- await appFile.writeAsString (originalContent);
97
- _print ('exiting' );
98
- await client.shutdown ();
99
123
}
100
124
101
125
Handler _clientHandler (DartDevcFrontendServerClient client) {
102
126
return (Request request) {
103
- var assetBytes = client.assetBytes (request.requestedUri.path);
127
+ var path = request.url.path;
128
+ var packagesIndex = path.indexOf ('/packages/' );
129
+ if (packagesIndex > 0 ) {
130
+ path = request.url.path.substring (packagesIndex);
131
+ } else {
132
+ path = request.url.path;
133
+ }
134
+ if (! path.startsWith ('/' )) path = '/$path ' ;
135
+ if (path.endsWith ('.dart.js' ) && path != '/example/app/main.dart.js' ) {
136
+ path = path.replaceFirst ('.dart.js' , '.dart.lib.js' , path.length - 8 );
137
+ }
138
+ var assetBytes = client.assetBytes (path);
104
139
if (assetBytes == null ) return Response .notFound ('path not found' );
105
140
return Response .ok (assetBytes,
106
141
headers: {HttpHeaders .contentTypeHeader: 'application/javascript' });
@@ -115,6 +150,9 @@ void _prompt() => stdout.write(
115
150
'Enter a new message to print and recompile, or type `quit` to exit:' );
116
151
117
152
final app = 'example/app/main.dart' ;
153
+ final dartSdkJs = p.join ('.dart_tool' , 'out' , 'dart_sdk.js' );
118
154
final outputDill = p.join ('.dart_tool' , 'out' , 'example_app.dill' );
119
155
final sdkDir = p.dirname (p.dirname (Platform .resolvedExecutable));
156
+ final sdkKernelPath =
157
+ p.join (sdkDir, 'lib' , '_internal' , 'ddc_platform_sound.dill' );
120
158
final watch = Stopwatch ();
0 commit comments