platform/packages/cli/lib/src/util.dart

78 lines
2.5 KiB
Dart
Raw Normal View History

2018-07-14 21:47:49 +00:00
import 'dart:async';
import 'dart:io';
import 'package:io/ansi.dart';
2019-07-17 15:56:52 +00:00
import 'package:path/path.dart' as p;
2018-07-14 21:47:49 +00:00
import 'package:pubspec_parse/pubspec_parse.dart';
2018-07-14 22:12:12 +00:00
//import 'package:yamlicious/yamlicious.dart';
2018-07-14 21:47:49 +00:00
2019-07-17 15:21:35 +00:00
final String checkmark = ansiOutputEnabled ? '\u2714' : '[Success]';
2018-07-14 21:47:49 +00:00
2018-07-14 22:16:14 +00:00
final String ballot = ansiOutputEnabled ? '\u2717' : '[Failure]';
2018-10-04 15:30:12 +00:00
String get homeDirPath =>
Platform.environment['HOME'] ?? Platform.environment['USERPROFILE'];
Directory get homeDir => new Directory(homeDirPath);
2019-07-17 15:56:52 +00:00
Directory get angelDir => Directory(p.join(homeDir.path, '.angel'));
2018-07-14 22:35:45 +00:00
Future<Pubspec> loadPubspec([Directory directory]) {
directory ??= Directory.current;
var file = new File.fromUri(directory.uri.resolve('pubspec.yaml'));
2018-07-14 21:47:49 +00:00
return file
.readAsString()
.then((yaml) => new Pubspec.parse(yaml, sourceUrl: file.uri));
}
2019-07-17 15:56:52 +00:00
// From: https://gist.github.com/tobischw/98dcd2563eec9a2a87bda8299055358a
Future<void> copyDirectory(Directory source, Directory destination) async {
2019-07-17 16:53:11 +00:00
// if (!topLevel) stdout.write('\r');
// print(darkGray
// .wrap('Copying dir "${source.path}" -> "${destination.path}..."'));
2019-07-17 15:56:52 +00:00
await for (var entity in source.list(recursive: false)) {
2019-07-17 16:53:11 +00:00
if (p.basename(entity.path) == '.git') continue;
2019-07-17 15:56:52 +00:00
if (entity is Directory) {
var newDirectory =
Directory(p.join(destination.absolute.path, p.basename(entity.path)));
2019-08-07 07:43:19 +00:00
await newDirectory.create(recursive: true);
2019-07-17 15:56:52 +00:00
await copyDirectory(entity.absolute, newDirectory);
} else if (entity is File) {
var newPath = p.join(destination.path, p.basename(entity.path));
2019-07-17 16:53:11 +00:00
// print(darkGray.wrap('\rCopying file "${entity.path}" -> "$newPath"'));
2019-08-07 07:43:19 +00:00
await File(newPath).create(recursive: true);
2019-07-17 15:56:52 +00:00
await entity.copy(newPath);
}
}
2019-07-17 16:53:11 +00:00
// print('\rCopied "${source.path}" -> "${destination.path}.');
2019-07-17 15:56:52 +00:00
}
2018-07-14 21:47:49 +00:00
Future savePubspec(Pubspec pubspec) async {
2018-07-14 22:12:12 +00:00
// TODO: Save pubspec for real?
//var text = toYamlString(pubspec);
2018-07-14 22:35:45 +00:00
}
Future<bool> runCommand(String exec, List<String> args) async {
var s = '$exec ${args.join(' ')}'.trim();
stdout.write(darkGray.wrap('Running `$s`... '));
try {
var p = await Process.start(exec, args);
var code = await p.exitCode;
if (code == 0) {
print(green.wrap(checkmark));
return true;
} else {
print(red.wrap(ballot));
await stdout.addStream(p.stdout);
await stderr.addStream(p.stderr);
return false;
}
} catch (e) {
print(red.wrap('$ballot Failed to run process.'));
return false;
}
}