2017-07-07 02:36:15 +02:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2018-06-15 22:42:36 +02:00
|
|
|
import 'dart:convert';
|
2017-07-07 02:36:15 +02:00
|
|
|
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
/// Creates a directory in the system temp directory and returns its path.
|
|
|
|
Future<String> createTempDir() async => (await runHybridExpression(
|
2021-03-10 02:04:09 +01:00
|
|
|
'(await Directory.systemTemp.createTemp("dart_sass_")).path'))
|
|
|
|
as String /*!*/;
|
2017-07-07 02:36:15 +02:00
|
|
|
|
|
|
|
/// Writes [text] to [path].
|
2019-05-31 17:01:49 +02:00
|
|
|
Future<void> writeTextFile(String path, String text) => runHybridExpression(
|
2017-07-07 02:36:15 +02:00
|
|
|
'new File(message[0]).writeAsString(message[1])', [path, text]);
|
|
|
|
|
2019-10-11 00:33:36 +02:00
|
|
|
/// Creates a directory at [path].
|
2019-05-31 17:01:49 +02:00
|
|
|
Future<void> createDirectory(String path) =>
|
2017-07-07 23:50:12 +02:00
|
|
|
runHybridExpression('new Directory(message).create()', path);
|
|
|
|
|
2019-10-11 00:33:36 +02:00
|
|
|
/// Recursively deletes the directory at [path].
|
2019-05-31 17:01:49 +02:00
|
|
|
Future<void> deleteDirectory(String path) =>
|
2017-07-07 02:36:15 +02:00
|
|
|
runHybridExpression('new Directory(message).delete(recursive: true)', path);
|
|
|
|
|
|
|
|
/// Runs [expression], which may be asynchronous, in a hybrid isolate.
|
|
|
|
///
|
|
|
|
/// Returns the result of [expression] if it's JSON-serializable.
|
2019-11-06 01:28:26 +01:00
|
|
|
Future<Object> runHybridExpression(String expression, [Object message]) async {
|
2017-07-07 03:03:49 +02:00
|
|
|
var channel = spawnHybridCode('''
|
2017-07-07 02:36:15 +02:00
|
|
|
import 'dart:async';
|
2018-06-15 22:42:36 +02:00
|
|
|
import 'dart:convert';
|
2017-07-07 02:36:15 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:stream_channel/stream_channel.dart';
|
|
|
|
|
|
|
|
hybridMain(StreamChannel channel, message) async {
|
|
|
|
var result = await ${expression};
|
2018-06-15 22:42:36 +02:00
|
|
|
channel.sink.add(_isJsonSafe(result) ? jsonEncode(result) : 'null');
|
2017-07-07 02:36:15 +02:00
|
|
|
channel.sink.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _isJsonSafe(object) {
|
|
|
|
if (object == null) return true;
|
|
|
|
if (object is String) return true;
|
|
|
|
if (object is num) return true;
|
|
|
|
if (object is bool) return true;
|
|
|
|
if (object is List) return object.every(_isJsonSafe);
|
|
|
|
if (object is Map) {
|
|
|
|
return object.keys.every(_isJsonSafe) &&
|
|
|
|
object.values.every(_isJsonSafe);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-07 03:03:49 +02:00
|
|
|
''', message: message);
|
2017-07-07 02:36:15 +02:00
|
|
|
|
2018-06-15 22:42:36 +02:00
|
|
|
return jsonDecode((await channel.stream.first) as String);
|
2017-07-07 02:36:15 +02:00
|
|
|
}
|