-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run cli compilations in parallel dart isolates (#2078)
Co-authored-by: Natalie Weizenbaum <[email protected]>
- Loading branch information
Showing
13 changed files
with
273 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2023 Google Inc. Use of this source code is governed by an | ||
// MIT-style license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
import 'dart:math' as math; | ||
|
||
import '../io.dart'; | ||
import '../stylesheet_graph.dart'; | ||
import '../util/map.dart'; | ||
import 'compile_stylesheet.dart'; | ||
import 'concurrent/vm.dart' | ||
// Never load the isolate library when compiling to JS. | ||
if (dart.library.js) 'concurrent/js.dart'; | ||
import 'options.dart'; | ||
|
||
/// Compiles the stylesheets concurrently and returns whether all stylesheets are compiled | ||
/// successfully. | ||
Future<bool> compileStylesheets(ExecutableOptions options, | ||
StylesheetGraph graph, Map<String?, String?> sourcesToDestinations, | ||
{bool ifModified = false}) async { | ||
var errorsWithStackTraces = switch ([...sourcesToDestinations.pairs]) { | ||
// Concurrency does add some overhead, so avoid it in the common case of | ||
// compiling a single stylesheet. | ||
[(var source, var destination)] => [ | ||
await compileStylesheet(options, graph, source, destination, | ||
ifModified: ifModified) | ||
], | ||
var pairs => await Future.wait([ | ||
for (var (source, destination) in pairs) | ||
compileStylesheetConcurrently(options, graph, source, destination, | ||
ifModified: ifModified) | ||
], eagerError: options.stopOnError) | ||
}; | ||
|
||
var printedError = false; | ||
|
||
// Print all errors in deterministic order. | ||
for (var errorWithStackTrace in errorsWithStackTraces) { | ||
if (errorWithStackTrace == null) continue; | ||
var (code, error, stackTrace) = errorWithStackTrace; | ||
|
||
// We let the highest exitCode take precedence for deterministic behavior. | ||
exitCode = math.max(exitCode, code); | ||
|
||
_printError(error, stackTrace, printedError); | ||
printedError = true; | ||
} | ||
|
||
return !printedError; | ||
} | ||
|
||
// Prints [error] to stderr, along with a preceding newline if anything else | ||
// has been printed to stderr. | ||
// | ||
// If [stackTrace] is passed, it is printed after the error. | ||
void _printError(String error, String? stackTrace, bool printedError) { | ||
var buffer = StringBuffer(); | ||
if (printedError) buffer.writeln(); | ||
buffer.write(error); | ||
if (stackTrace != null) { | ||
buffer.writeln(); | ||
buffer.writeln(); | ||
buffer.write(stackTrace); | ||
} | ||
printError(buffer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright 2023 Google Inc. Use of this source code is governed by an | ||
// MIT-style license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
import '../compile_stylesheet.dart'; | ||
|
||
/// We don't currently support concurrent compilation in JS. | ||
/// | ||
/// In the future, we could add support using web workers. | ||
final compileStylesheetConcurrently = compileStylesheet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2023 Google Inc. Use of this source code is governed by an | ||
// MIT-style license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
import 'dart:isolate'; | ||
|
||
import 'package:term_glyph/term_glyph.dart' as term_glyph; | ||
|
||
import '../options.dart'; | ||
import '../../stylesheet_graph.dart'; | ||
import '../compile_stylesheet.dart'; | ||
|
||
/// Compiles the stylesheet at [source] to [destination]. | ||
/// | ||
/// Runs in a new Dart Isolate, unless [source] is `null`. | ||
Future<(int, String, String?)?> compileStylesheetConcurrently( | ||
ExecutableOptions options, | ||
StylesheetGraph graph, | ||
String? source, | ||
String? destination, | ||
{bool ifModified = false}) { | ||
// Reading from stdin does not work properly in dart isolate. | ||
if (source == null) { | ||
return compileStylesheet(options, graph, source, destination, | ||
ifModified: ifModified); | ||
} | ||
|
||
return Isolate.run(() { | ||
term_glyph.ascii = !options.unicode; | ||
return compileStylesheet(options, graph, source, destination, | ||
ifModified: ifModified); | ||
}); | ||
} |
Oops, something went wrong.