package cn.reghao.autodop.dmaster.app.service.bd.tools.compiler;

import cn.reghao.autodop.common.util.shell.ShellExecutor;
import cn.reghao.autodop.common.util.shell.ShellResult;
import cn.reghao.jdkutil.serializer.JsonConverter;
import cn.reghao.autodop.dmaster.app.model.po.config.build.CompilerConfig;

/**
 * 编译代码
 *
 * @author reghao
 * @date 2019-10-12 23:55:01
 */
public class ShellCompiler implements CodeCompiler {
    private final ShellExecutor shell;
    private final String compileCmd;

    public ShellCompiler(CompilerConfig compilerConfig) {
        this.compileCmd = compilerConfig.getCompileCmd();
        this.shell = new ShellExecutor();
    }

    @Override
    public void compile(String appId, String appCompileHome) throws Exception {
        ShellResult result = shell.exec(compileCmd, appCompileHome);
        if (!result.isSuccess()) {
            throw new Exception(JsonConverter.objectToJson(result.getOutput()));
        }
    }
}
