1/*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.io.BufferedWriter;
25import java.io.IOException;
26import java.io.UncheckedIOException;
27import java.nio.file.Files;
28import java.nio.file.Path;
29import java.nio.file.Paths;
30import java.util.jar.Attributes;
31import java.util.jar.Manifest;
32
33import toolbox.JarTask;
34import toolbox.JavacTask;
35import toolbox.ToolBox;
36
37public class Compiler {
38
39    private final ToolBox tb = new ToolBox();
40
41    public Path getClassDir() {
42        String classes = ToolBox.testClasses;
43        if (classes == null) {
44            return Paths.get("build");
45        } else {
46            return Paths.get(classes);
47        }
48    }
49
50    public Path getPath(String path) {
51        return getPath(Paths.get(path));
52    }
53
54    public Path getPath(Path path) {
55        return getClassDir().resolve(path);
56    }
57
58    public void compile(String...sources) {
59        compile(Paths.get("."), sources);
60    }
61
62    public void compile(Path directory, String...sources) {
63        Path classDir = getClassDir();
64        new JavacTask(tb)
65                .options("-d", classDir.resolve(directory).toString())
66                .sources(sources)
67                .run();
68    }
69
70    public void jar(String jarName, String...files) {
71        jar(Paths.get("."), jarName, files);
72    }
73
74    public void jar(Path directory, String jarName, String...files) {
75        Manifest manifest = new Manifest();
76        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
77        Path classDirPath = getClassDir();
78        Path baseDir = classDirPath.resolve(directory);
79        Path jarPath = baseDir.resolve(jarName);
80        new JarTask(tb, jarPath.toString())
81                .manifest(manifest)
82                .baseDir(baseDir.toString())
83                .files(files).run();
84    }
85
86    public void writeToFile(Path path, String...sources) {
87        try {
88            if (path.getParent() != null) {
89                Files.createDirectories(path.getParent());
90            }
91        } catch (IOException e) {
92            throw new UncheckedIOException(e);
93        }
94        try (BufferedWriter writer = Files.newBufferedWriter(path)) {
95            for (String source : sources) {
96                writer.append(source);
97                writer.append('\n');
98            }
99        } catch (IOException e) {
100            throw new UncheckedIOException(e);
101        }
102    }
103}
104