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

import cn.reghao.autodop.common.msg.rpc.dto.app.PackType;
import cn.reghao.autodop.common.util.compression.ZipFiles;
import cn.reghao.autodop.dmaster.app.model.po.config.build.LocalBuildDir;
import cn.reghao.autodop.dmaster.app.model.po.config.build.PackerConfig;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

/**
 * @author reghao
 * @date 2020-03-11 13:37:36
 */
@Slf4j
public class ZipPack implements CodePacker {
    private final String targetPath;

    public ZipPack(PackerConfig packerConfig) {
        this.targetPath = packerConfig.getType().equals(PackType.zip.getName()) ?
                LocalBuildDir.packDir : packerConfig.getTargetPath();
    }

    @Override
    public String pack(String appId, String commitId, String appCompileHome) throws Exception {
        String filename = String.format("%s-%s.zip", appId, commitId);
        String dst = targetPath + "/" + filename;
        if (new File(dst).exists()) {
            throw new Exception(dst + " 已存在");
        }
        ZipFiles.zip(new File(appCompileHome), dst);

        return String.format("http://localhost:4020/api/sys/package/%s", filename);
    }
}
