Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: web/main.dart

Issue 2183603003: Working compiler in browser. (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Undid unnecessary changes Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env dart 1 #!/usr/bin/env dart
2 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 2 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
3 // for details. All rights reserved. Use of this source code is governed by a 3 // for details. All rights reserved. Use of this source code is governed by a
4 // BSD-style license that can be found in the LICENSE file. 4 // BSD-style license that can be found in the LICENSE file.
5 5
6 /// Command line entry point for Dart Development Compiler (dartdevc). 6 /// Command line entry point for Dart Development Compiler (dartdevc).
7 /// 7 ///
8 /// Supported commands are 8 /// Supported commands are
9 /// * compile: builds a collection of dart libraries into a single JS module 9 /// * compile: builds a collection of dart libraries into a single JS module
10 /// 10 ///
(...skipping 17 matching lines...) Expand all
28 /// * We do not assume a `node` JS installation, so we cannot easily reuse 28 /// * We do not assume a `node` JS installation, so we cannot easily reuse
29 /// existing tools for the "link" step, or assume users have a local 29 /// existing tools for the "link" step, or assume users have a local
30 /// file server, 30 /// file server,
31 /// * We didn't have a file watcher API at first, 31 /// * We didn't have a file watcher API at first,
32 /// * We had no conventions about where compiled output should go (or even 32 /// * We had no conventions about where compiled output should go (or even
33 /// that we would be compiling at all, vs running on an in-browser Dart VM), 33 /// that we would be compiling at all, vs running on an in-browser Dart VM),
34 /// * We wanted a good first impression with our simple examples, so we used 34 /// * We wanted a good first impression with our simple examples, so we used
35 /// local file servers, and users have an expectation of it now, even though 35 /// local file servers, and users have an expectation of it now, even though
36 /// it doesn't scale to typical apps that need their own real servers. 36 /// it doesn't scale to typical apps that need their own real servers.
37 37
38 @JS()
39 library dev_compiler.web.main;
40
38 import 'dart:async'; 41 import 'dart:async';
39 import 'dart:io'; 42
40 import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine;
41 import 'package:args/command_runner.dart'; 43 import 'package:args/command_runner.dart';
42 import 'package:bazel_worker/bazel_worker.dart'; 44 import 'package:js/js.dart';
43 import 'package:dev_compiler/src/compiler/command.dart';
44 45
45 Future main(List<String> args) async { 46 import 'web_command.dart';
46 // Always returns a new modifiable list.
47 args = _preprocessArgs(args);
48 47
49 if (args.contains('--persistent_worker')) { 48 @JS()
50 new _CompilerWorker(args..remove('--persistent_worker')).run(); 49 external set compileDartExpression(Function function);
51 } else { 50
52 exitCode = await _runCommand(args); 51 Future main() async {
53 } 52 var args = ['compile'];
53 _runCommand((result) {
54 compileDartExpression = allowInterop(result);
55 }, args);
54 } 56 }
55 57
56 /// Runs a single compile command, and returns an exit code. 58 /// Runs a single compile command, and returns an exit code.
57 Future<int> _runCommand(List<String> args, 59 Future<int> _runCommand(Function onload, List<String> args,
58 {MessageHandler messageHandler}) async { 60 {MessageHandler messageHandler}) async {
59 try { 61 try {
60 if (args.isEmpty || args.first != 'compile' && args.first != 'help') {
61 // TODO(jmesserly): we should deprecate the commands. For now they are
62 // still supported for backwards compatibility.
63 args.insert(0, 'compile');
64 }
65 var runner = new CommandRunner('dartdevc', 'Dart Development Compiler'); 62 var runner = new CommandRunner('dartdevc', 'Dart Development Compiler');
66 runner.addCommand(new CompileCommand(messageHandler: messageHandler)); 63 runner
64 .addCommand(new CompileCommand(onload, messageHandler: messageHandler));
67 await runner.run(args); 65 await runner.run(args);
68 } catch (e, s) { 66 } catch (e, s) {
69 return _handleError(e, s, args, messageHandler: messageHandler); 67 return _handleError(e, s, args, messageHandler: messageHandler);
70 } 68 }
71 return EXIT_CODE_OK; 69 return 1;
72 }
73
74 /// Runs the compiler worker loop.
75 class _CompilerWorker extends AsyncWorkerLoop {
76 /// The original args supplied to the executable.
77 final List<String> _startupArgs;
78
79 _CompilerWorker(this._startupArgs) : super();
80
81 /// Performs each individual work request.
82 Future<WorkResponse> performRequest(WorkRequest request) async {
83 var args = _startupArgs.toList()..addAll(request.arguments);
84
85 var output = new StringBuffer();
86 var exitCode = await _runCommand(args, messageHandler: output.writeln);
87 AnalysisEngine.instance.clearCaches();
88 return new WorkResponse()
89 ..exitCode = exitCode
90 ..output = output.toString();
91 }
92 } 70 }
93 71
94 /// Handles [error] in a uniform fashion. Returns the proper exit code and calls 72 /// Handles [error] in a uniform fashion. Returns the proper exit code and calls
95 /// [messageHandler] with messages. 73 /// [messageHandler] with messages.
96 int _handleError(dynamic error, dynamic stackTrace, List<String> args, 74 int _handleError(dynamic error, dynamic stackTrace, List<String> args,
97 {MessageHandler messageHandler}) { 75 {MessageHandler messageHandler}) {
98 messageHandler ??= print; 76 messageHandler ??= print;
99 77
100 if (error is UsageException) { 78 if (error is UsageException) {
101 // Incorrect usage, input file not found, etc. 79 // Incorrect usage, input file not found, etc.
(...skipping 14 matching lines...) Expand all
116 messageHandler("We're sorry, you've found a bug in our compiler."); 94 messageHandler("We're sorry, you've found a bug in our compiler.");
117 messageHandler("You can report this bug at:"); 95 messageHandler("You can report this bug at:");
118 messageHandler(" https://github.com/dart-lang/dev_compiler/issues"); 96 messageHandler(" https://github.com/dart-lang/dev_compiler/issues");
119 messageHandler(""); 97 messageHandler("");
120 messageHandler( 98 messageHandler(
121 "Please include the information below in your report, along with"); 99 "Please include the information below in your report, along with");
122 messageHandler( 100 messageHandler(
123 "any other information that may help us track it down. Thanks!"); 101 "any other information that may help us track it down. Thanks!");
124 messageHandler(""); 102 messageHandler("");
125 messageHandler(" dartdevc arguments: " + args.join(' ')); 103 messageHandler(" dartdevc arguments: " + args.join(' '));
126 messageHandler(" dart --version: ${Platform.version}");
127 messageHandler(""); 104 messageHandler("");
128 messageHandler("```"); 105 messageHandler("```");
129 messageHandler(error); 106 messageHandler(error);
130 messageHandler(stackTrace); 107 messageHandler(stackTrace);
131 messageHandler("```"); 108 messageHandler("```");
132 return 70; 109 return 70;
133 } 110 }
134 } 111 }
135
136 /// Always returns a new modifiable list.
137 ///
138 /// If the final arg is `@file_path` then read in all the lines of that file
139 /// and add those as args.
140 ///
141 /// Bazel actions that support workers must provide all their per-WorkRequest
142 /// arguments in a file like this instead of as normal args.
143 List<String> _preprocessArgs(List<String> args) {
144 args = new List.from(args);
145 if (args.isNotEmpty && args.last.startsWith('@')) {
146 var fileArg = args.removeLast();
147 args.addAll(new File(fileArg.substring(1)).readAsLinesSync());
148 }
149 return args;
150 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698