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 Protobuf extends Command {
  @override
  Future execute(DataCommand command) async {
    var data = DataProtobuf.fromJson(getParam(command));

    var shell = StringBuffer(getCDPath(workspace));
    var commands = data.commands;
    for (var item in commands) {
      shell.write(' $and protoc ${Command.getPathNormal(item)}');
    }

    var process =
        await runtime.start(shell, logHandler: Application.logWithType);

    return await completeProcess(process, command);
  }
}

void registerProtobufCommand() {
  Command.register(
    CommandType.Protobuf,
    () => Protobuf(),
    spec: const CommandSpec(
      type: CommandType.Protobuf,
      category: 'proto',
      dataModel: 'DataProtobuf',
      samplePath: 'assets/yaml/sample/18_protobuf.yaml',
    ),
  );
}
