blob: cc3e39e603c94933dc24d0fa8256c14fde7fb8dc [file] [log] [blame]
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -07001// Copyright (C) 2013 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Shawn Pearcedaa19772013-05-16 15:35:31 -070015import com.googlecode.prolog_cafe.compiler.Compiler;
Shawn Pearcecc34990492015-03-06 12:11:16 -080016import com.googlecode.prolog_cafe.exceptions.CompileException;
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070017import java.io.File;
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070018import java.io.IOException;
Hector Oswaldo Caballerodb21e3a2017-04-23 22:00:48 -040019import java.io.InputStream;
20import java.nio.file.Files;
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070021import java.util.jar.JarEntry;
22import java.util.jar.JarOutputStream;
23
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070024public class BuckPrologCompiler {
Shawn Pearce4e1a8bc2013-11-28 18:38:30 -080025 private static File tmpdir;
26
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070027 public static void main(String[] argv) throws IOException, CompileException {
Shawn Pearce4e1a8bc2013-11-28 18:38:30 -080028 int i = 0;
29 tmpdir = new File(argv[i++]);
30 File out = new File(argv[i++]);
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070031 File java = tmpdir("java");
Shawn Pearce4e1a8bc2013-11-28 18:38:30 -080032 for (; i < argv.length; i++) {
33 new Compiler().prologToJavaSource(argv[i], java.getPath());
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070034 }
Shawn Pearce11d27c82013-07-24 08:09:31 -070035 jar(out, java);
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070036 }
37
38 private static File tmpdir(String name) throws IOException {
Shawn Pearce4e1a8bc2013-11-28 18:38:30 -080039 File d = File.createTempFile(name + "_", "", tmpdir);
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070040 if (!d.delete() || !d.mkdir()) {
41 throw new IOException("Cannot mkdir " + d);
42 }
43 return d;
44 }
45
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070046 private static void jar(File jar, File classes) throws IOException {
Shawn Pearce4e1a8bc2013-11-28 18:38:30 -080047 File tmp = File.createTempFile("prolog", ".jar", tmpdir);
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070048 try {
Hector Oswaldo Caballerodb21e3a2017-04-23 22:00:48 -040049 try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(tmp.toPath()))) {
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070050 add(out, classes, "");
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070051 }
52 if (!tmp.renameTo(jar)) {
53 throw new IOException("Cannot create " + jar);
54 }
55 } finally {
56 tmp.delete();
57 }
58 }
59
Dave Borowitz292fa152016-11-13 09:56:32 -080060 private static void add(JarOutputStream out, File classes, String prefix) throws IOException {
David Pursehouse1e2458a2015-03-20 14:39:36 +090061 String[] list = classes.list();
62 if (list == null) {
63 return;
64 }
65 for (String name : list) {
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070066 File f = new File(classes, name);
67 if (f.isDirectory()) {
68 add(out, f, prefix + name + "/");
69 continue;
70 }
71
72 JarEntry e = new JarEntry(prefix + name);
Hector Oswaldo Caballerodb21e3a2017-04-23 22:00:48 -040073 try (InputStream in = Files.newInputStream(f.toPath())) {
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070074 e.setTime(f.lastModified());
75 out.putNextEntry(e);
76 byte[] buf = new byte[16 << 10];
77 int n;
78 while (0 < (n = in.read(buf))) {
79 out.write(buf, 0, n);
80 }
81 } finally {
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070082 out.closeEntry();
83 }
84 }
85 }
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070086}