package cn.reghao.bnt.web.devops.builder.tool.compiler;

import cn.reghao.bnt.web.devops.builder.model.CompilerDto;
import cn.reghao.jutil.jdk.shell.ShellExecutor;
import cn.reghao.jutil.jdk.shell.ShellResult;

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

    public ShellCompiler(CompilerDto compilerDto) {
        this.compileCmds = compilerDto.getCompileCmd().split(" && ");
        this.shell = new ShellExecutor();
    }

    @Override
    public void compile(String appId, String appCompileHome) throws Exception {
        ShellResult result;
        for (String compileScript : compileCmds) {
            result = shell.exec(appCompileHome, compileScript.split("\\s+"));
            if (!result.isSuccess()) {
                throw new Exception(result.getResult());
                //throw new Exception(JsonConverter.objectToJson(result.getResult()));
            }
        }
    }
}
