1 /* 2 * Copyright (c) 2019, 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 24 package org.openjdk.skara.gradle.images; 25 26 import org.gradle.api.DefaultTask; 27 import org.gradle.api.model.ObjectFactory; 28 import org.gradle.api.provider.*; 29 import org.gradle.api.tasks.*; 30 31 import javax.inject.Inject; 32 import java.io.*; 33 import java.nio.file.*; 34 import java.nio.file.attribute.PosixFilePermissions; 35 import java.util.Comparator; 36 37 public class LaunchersTask extends DefaultTask { 38 private Property<Path> toDir; 39 private Property<String> os; 40 private Property<String> cpu; 41 private MapProperty<String, String> launchers; 42 private ListProperty<String> options; 43 44 @Inject 45 public LaunchersTask(ObjectFactory factory) { 46 toDir = factory.property(Path.class); 47 os = factory.property(String.class); 48 cpu = factory.property(String.class); 49 launchers = factory.mapProperty(String.class, String.class); 50 options = factory.listProperty(String.class); 51 } 52 53 @Input 54 ListProperty<String> getOptions() { 55 return options; 56 } 57 58 @OutputDirectory 59 Property<Path> getToDir() { 60 return toDir; 61 } 62 63 @Input 64 Property<String> getOS() { 65 return os; 66 } 67 68 @Input 69 Property<String> getCPU() { 70 return cpu; 71 } 72 73 @Input 74 MapProperty<String, String> getLaunchers() { 75 return launchers; 76 } 77 78 private static void clearDirectory(Path directory) throws IOException { 79 Files.walk(directory) 80 .map(Path::toFile) 81 .sorted(Comparator.reverseOrder()) 82 .forEach(File::delete); 83 } 84 85 @TaskAction 86 void generate() throws IOException { 87 var dest = toDir.get().resolve(os.get() + "-" + cpu.get()); 88 if (Files.isDirectory(dest)) { 89 clearDirectory(dest); 90 } 91 Files.createDirectories(dest); 92 var optionString = String.join(" ", options.get()); 93 for (var entry : launchers.get().entrySet()) { 94 var filename = entry.getKey(); 95 var clazz = entry.getValue(); 96 97 if (os.get().equals("windows")) { 98 var file = dest.resolve(filename + ".bat"); 99 try (var w = Files.newBufferedWriter(file)) { 100 w.write("@echo off\r\n"); 101 w.write("set DIR=%~dp0\r\n"); 102 w.write("set JAVA_HOME=%DIR%..\\image\r\n"); 103 w.write("\"%JAVA_HOME%\\bin\\java.exe\" " + optionString + " --module " + clazz + " %*\r\n"); 104 } 105 } else { 106 var file = dest.resolve(filename); 107 try (var w = Files.newBufferedWriter(file)) { 108 w.write("#!/bin/sh\n"); 109 w.write("DIR=$(dirname \"$0\")\n"); 110 w.write("export JAVA_HOME=\"${DIR}/../image\"\n"); 111 w.write("\"${JAVA_HOME}/bin/java\" " + optionString + " --module " + clazz + " \"$@\"\n"); 112 } 113 if (file.getFileSystem().supportedFileAttributeViews().contains("posix")) { 114 var permissions = PosixFilePermissions.fromString("rwxr-xr-x"); 115 Files.setPosixFilePermissions(file, permissions); 116 } 117 } 118 } 119 } 120 }