ForTestCompileSomething.java 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2010-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.jet.codegen.forTestCompile;

import com.google.common.io.Files;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils;
S
Stepan Koltsov 已提交
24
import org.jetbrains.jet.TimeUtils;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Stack;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

/**
 * @author Stepan Koltsov
 */
abstract class ForTestCompileSomething {

    @NotNull
    private final String jarName;
    private Throwable error;
    private File jarFile;

    ForTestCompileSomething(@NotNull String jarName) {
44 45
        System.out.println("Compiling " + jarName + "...");
        long start = System.currentTimeMillis();
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        this.jarName = jarName;
        try {
            File tmpDir = JetTestUtils.tmpDir("runtimejar");

            jarFile = new File(tmpDir, "runtime.jar");

            File classesDir = new File(tmpDir, "classes");

            FileUtil.createParentDirs(new File(classesDir, "dummy"));

            doCompile(classesDir);

            FileOutputStream stdlibJar = new FileOutputStream(jarFile);
            try {
                JarOutputStream jarOutputStream = new JarOutputStream(stdlibJar);
                try {
                    copyToJar(classesDir, jarOutputStream);
                }
                finally {
                    jarOutputStream.close();
                }
            }
            finally {
                stdlibJar.close();
            }

            FileUtil.delete(classesDir);
73
            long end = System.currentTimeMillis();
S
Stepan Koltsov 已提交
74
            System.out.println("Compiling " + jarName + " done in " + TimeUtils.millisecondsToSecondsString(end - start) + "s");
75 76 77 78 79 80 81 82 83 84 85 86 87 88
        } catch (Throwable e) {
            error = e;
        }
    }

    private static void copyToJar(File root, JarOutputStream os) throws IOException {
        Stack<Pair<String, File>> toCopy = new Stack<Pair<String, File>>();
        toCopy.add(new Pair<String, File>("", root));
        while (!toCopy.empty()) {
            Pair<String, File> pop = toCopy.pop();
            File file = pop.getSecond();
            if (file.isFile()) {
                os.putNextEntry(new JarEntry(pop.getFirst()));
                Files.copy(file, os);
S
Stepan Koltsov 已提交
89 90
            }
            else if (file.isDirectory()) {
91 92 93 94
                for (File child : file.listFiles()) {
                    String path = pop.getFirst().isEmpty() ? child.getName() : pop.getFirst() + "/" + child.getName();
                    toCopy.add(new Pair<String, File>(path, child));
                }
S
Stepan Koltsov 已提交
95 96
            }
            else {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                throw new IllegalStateException();
            }
        }
    }

    protected abstract void doCompile(@NotNull File classesDir) throws Exception;

    @NotNull
    public File getJarFile() {
        if (error != null) {
            throw new IllegalStateException("compilation of " + jarName + " failed: " + error, error);
        }
        return jarFile;
    }
}