package _VisualDVM.Passes.All;
import Common.Passes.PassException;
import Common.Utils.Utils_;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.ProcessPass;
import _VisualDVM.Repository.Component.Component;

import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class BuildComponent extends ProcessPass<Component> {
    @Override
    protected PassCode necessary() {
        return PassCode.DownloadRepository;
    }
    @Override
    protected boolean resetsNecessary() {
        return true;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        target = Global.mainModule.getComponent();
        return true;
    }
    @Override
    protected void performPreparation() throws Exception {
        Utils_.forceDeleteWithCheck(target.getAssemblyFile());
    }
    @Override
    protected void body() throws Exception {
        ShowMessage1("Сборка " + target.getComponentType().getDescription());
        PerformScript(target.getAssemblyCommand());
        if (!target.getAssemblyFile().exists())
            throw new PassException("Сборка не найдена");
        Files.copy(target.getAssemblyFile().toPath(),
                target.getNewFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
        if (!target.getNewFile().exists())
            throw new PassException("Не удалось скопировать сборку для установки");
    }
}
