Path: blob/master/test/langtools/jdk/jshell/Compiler.java
40931 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedWriter;24import java.io.IOException;25import java.io.UncheckedIOException;26import java.nio.file.Files;27import java.nio.file.Path;28import java.nio.file.Paths;29import java.util.jar.Attributes;30import java.util.jar.Manifest;3132import toolbox.JarTask;33import toolbox.JavacTask;34import toolbox.ToolBox;3536public class Compiler {3738private final ToolBox tb = new ToolBox();3940public Path getClassDir() {41String classes = ToolBox.testClasses;42if (classes == null) {43return Paths.get("build");44} else {45return Paths.get(classes);46}47}4849public Path getPath(String path) {50return getPath(Paths.get(path));51}5253public Path getPath(Path path) {54return getClassDir().resolve(path);55}5657public void compile(String...sources) {58compile(Paths.get("."), sources);59}6061public void compile(Path directory, String...sources) {62Path classDir = getClassDir();63new JavacTask(tb)64.options("-d", classDir.resolve(directory).toString())65.sources(sources)66.run();67}6869public void jar(String jarName, String...files) {70jar(Paths.get("."), jarName, files);71}7273public void jar(Path directory, String jarName, String...files) {74Manifest manifest = new Manifest();75manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");76Path classDirPath = getClassDir();77Path baseDir = classDirPath.resolve(directory);78Path jarPath = baseDir.resolve(jarName);79new JarTask(tb, jarPath.toString())80.manifest(manifest)81.baseDir(baseDir.toString())82.files(files).run();83}8485public void writeToFile(Path path, String...sources) {86try {87if (path.getParent() != null) {88Files.createDirectories(path.getParent());89}90} catch (IOException e) {91throw new UncheckedIOException(e);92}93try (BufferedWriter writer = Files.newBufferedWriter(path)) {94for (String source : sources) {95writer.append(source);96writer.append('\n');97}98} catch (IOException e) {99throw new UncheckedIOException(e);100}101}102}103104105