oto/lib/oto/commands/shell/shell.dart

63 lines
1.8 KiB
Dart

import 'dart:async';
import 'dart:io';
import 'package:dart_framework/platform/process.dart';
import 'package:dart_framework/utils/system_util.dart';
import 'package:oto/oto/application.dart';
import 'package:oto/oto/commands/command.dart';
import 'package:oto/oto/data/command_data.dart';
class Shell extends Command {
@override
Future execute(DataCommand command) async {
var data = DataShell.fromJson(getParam(command));
var shell = StringBuffer(getCDPath(workspace));
var commands = data.commands;
for (var item in commands) {
shell.write(' $and $item');
}
var process =
await ProcessExecutor.start(shell, logHandler: Application.logWithType);
if (data.setMessage == null) {
return await completeProcess(process, command);
} else {
await process.waitForExit();
var str = process.stdout.toString();
var length = str.length;
str = str.substring(0, length - 1);
await setProperty(data.setMessage!, str);
return await complete(process.exitCode!, command);
}
}
}
class ShellFile extends Command {
@override
Future execute(DataCommand command) async {
var data = DataShellFile.fromJson(getParam(command));
var path = Command.getPathNormal(data.path);
var shell = StringBuffer();
if (Platform.isWindows) {
shell.writeln('call $path');
} else {
shell.writeln('source $path');
}
var process =
await ProcessExecutor.start(shell, logHandler: Application.logWithType);
return await completeProcess(process, command);
}
bool isSuccess(int exitCode) {
var success = false;
if (exitCode == 0 || exitCode == 128) {
success = true;
}
return success;
}
}
void registerShellCommands() {
Command.register(CommandType.Shell, () => Shell());
Command.register(CommandType.ShellFile, () => ShellFile());
}