package org.metaborg.spoofax.generator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import org.apache.commons.io.IOUtils;
import org.metaborg.spoofax.generator.project.ProjectSettings;
import org.metaborg.spoofax.generator.util.MustacheWriter;

public abstract class BaseGenerator {
    
    private final File basedir;
    protected final MustacheWriter writer;

    public BaseGenerator(ProjectSettings projectSettings) {
        this.basedir = projectSettings.getBaseDir();
        this.writer = new MustacheWriter(projectSettings.getBaseDir(),
                new Object[]{ this, projectSettings }, getClass());
    }

    protected void unpack(String name) throws IOException {
        String dst = new File(name).getParent();
        unpack(name, dst);
    }

    protected void unpack(String srcName, String dstDir) throws IOException {
        File tmp = File.createTempFile("generator-unpack", "tmp");
        tmp.deleteOnExit();
        try (InputStream is = getClass().getResourceAsStream(srcName)) {
            try (OutputStream os = new FileOutputStream(tmp)) {
                IOUtils.copy(is,os);
            }
        }
        File dst = new File(basedir, dstDir);
        dst.mkdirs();
        ZipFile z;
        try {
            z = new ZipFile(tmp.getPath());
            z.extractAll(dst.getPath());
        } catch (ZipException ex) {
            throw new IOException("Failed te unpack archive "+srcName, ex);
        }
    }

}

@@ -59,252 +59,8 @@
 le;%0A
-import java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;%0Aimport net.lingala.zip4j.core.ZipFile;%0Aimport net.lingala.zip4j.exception.ZipException;%0Aimport org.apache.commons.io.IOUtils;%0A
 impo
@@ -560,835 +560,6 @@
  %7D%0A%0A
-    protected void unpack(String name) throws IOException %7B%0A        String dst = new File(name).getParent();%0A        unpack(name, dst);%0A    %7D%0A%0A    protected void unpack(String srcName, String dstDir) throws IOException %7B%0A        File tmp = File.createTempFile(%22generator-unpack%22, %22tmp%22);%0A        tmp.deleteOnExit();%0A        try (InputStream is = getClass().getResourceAsStream(srcName)) %7B%0A            try (OutputStream os = new FileOutputStream(tmp)) %7B%0A                IOUtils.copy(is,os);%0A            %7D%0A        %7D%0A        File dst = new File(basedir, dstDir);%0A        dst.mkdirs();%0A        ZipFile z;%0A        try %7B%0A            z = new ZipFile(tmp.getPath());%0A            z.extractAll(dst.getPath());%0A        %7D catch (ZipException ex) %7B%0A            throw new IOException(%22Failed te unpack archive %22+srcName, ex);%0A        %7D%0A    %7D%0A%0A
 %7D%0A

remove unused unzip code.