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 MapProperty<String, String> launchers; 41 private ListProperty<String> options; 42 43 @Inject 44 public LaunchersTask(ObjectFactory factory) { 45 toDir = factory.property(Path.class); 46 os = factory.property(String.class); 47 launchers = factory.mapProperty(String.class, String.class); 48 options = factory.listProperty(String.class); 49 } 50 51 @Input 52 ListProperty<String> getOptions() { 53 return options; 54 } 55 56 @OutputDirectory 57 Property<Path> getToDir() { 58 return toDir; 59 } 60 61 @Input 62 Property<String> getOS() { 63 return os; 64 } 65 66 @Input 67 MapProperty<String, String> getLaunchers() { 68 return launchers; 69 } 70 71 private static void clearDirectory(Path directory) throws IOException { 72 Files.walk(directory) 73 .map(Path::toFile) 74 .sorted(Comparator.reverseOrder()) 75 .forEach(File::delete); 76 } 77 78 @TaskAction 79 void generate() throws IOException { 80 var dest = toDir.get().resolve(os.get()); 81 if (Files.isDirectory(dest)) { 82 clearDirectory(dest); 83 } 84 Files.createDirectories(dest); 85 var optionString = String.join(" ", options.get()); 86 for (var entry : launchers.get().entrySet()) { 87 var filename = entry.getKey(); 88 var clazz = entry.getValue(); 89 90 if (os.get().equals("windows")) { 91 var file = dest.resolve(filename + ".bat"); 92 try (var w = Files.newBufferedWriter(file)) { 93 w.write("@echo off\r\n"); 94 w.write("set DIR=%~dp0\r\n"); 95 w.write("set JAVA_HOME=%DIR%..\\image\r\n"); 96 w.write("\"%JAVA_HOME%\\bin\\java.exe\" " + optionString + " --module " + clazz + " %*\r\n"); 97 } 98 } else { 99 var file = dest.resolve(filename); 100 try (var w = Files.newBufferedWriter(file)) { 101 w.write("#!/bin/sh\n"); 102 w.write("DIR=$(dirname \"$0\")\n"); 103 w.write("export JAVA_HOME=\"${DIR}/../image\"\n"); 104 w.write("\"${JAVA_HOME}/bin/java\" " + optionString + " --module " + clazz + " \"$@\"\n"); 105 } 106 if (file.getFileSystem().supportedFileAttributeViews().contains("posix")) { 107 var permissions = PosixFilePermissions.fromString("rwxr-xr-x"); 108 Files.setPosixFilePermissions(file, permissions); 109 } 110 } 111 } 112 } 113 }