|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.netbeans.nbpackage.tar; |
| 20 | + |
| 21 | +import org.apache.netbeans.nbpackage.AbstractPackagerTask; |
| 22 | +import org.apache.netbeans.nbpackage.Architecture; |
| 23 | +import org.apache.netbeans.nbpackage.ExecutionContext; |
| 24 | +import org.apache.netbeans.nbpackage.FileUtils; |
| 25 | +import org.apache.netbeans.nbpackage.NBPackage; |
| 26 | +import org.apache.netbeans.nbpackage.StringUtils; |
| 27 | +import java.io.IOException; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.nio.file.attribute.PosixFilePermissions; |
| 31 | +import java.util.Locale; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.Optional; |
| 34 | +import static org.apache.netbeans.nbpackage.Architecture.AARCH64; |
| 35 | +import static org.apache.netbeans.nbpackage.Architecture.X86_64; |
| 36 | + |
| 37 | +class TarScriptPackageTask extends AbstractPackagerTask { |
| 38 | + |
| 39 | + private static final String ARCH_AARCH64 = "aarch64"; |
| 40 | + private static final String ARCH_X86_64 = "x86_64"; |
| 41 | + private static final String ARCH_NOARCH = "noarch"; |
| 42 | + |
| 43 | + private String packageArch; |
| 44 | + |
| 45 | + TarScriptPackageTask(ExecutionContext context) { |
| 46 | + super(context); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void customizeImage(Path image) throws Exception { |
| 51 | + String appDir = findLauncher( |
| 52 | + image.resolve("APPDIR").resolve("bin")) |
| 53 | + .getFileName().toString(); |
| 54 | + |
| 55 | + Path launcherDir = calculateAppPath(image).resolve("launcher"); |
| 56 | + Files.createDirectories(launcherDir); |
| 57 | + |
| 58 | + Path icon = context().getValue(TarScriptPackager.ICON_PATH).orElse(null); |
| 59 | + Path svg = context().getValue(TarScriptPackager.SVG_ICON_PATH).orElse(null); |
| 60 | + if (svg != null && icon == null) { |
| 61 | + context().warningHandler().accept(TarScriptPackager.MESSAGES.getString("message.svgnoicon")); |
| 62 | + svg = null; |
| 63 | + } |
| 64 | + |
| 65 | + if (icon != null) { |
| 66 | + Files.copy(icon, launcherDir.resolve(appDir + ".png")); |
| 67 | + } else { |
| 68 | + Files.copy(getClass().getResourceAsStream( |
| 69 | + "/org/apache/netbeans/nbpackage/apache-netbeans-48x48.png"), |
| 70 | + launcherDir.resolve(appDir + ".png")); |
| 71 | + } |
| 72 | + if (svg != null) { |
| 73 | + Files.copy(svg, launcherDir.resolve(appDir + ".svg")); |
| 74 | + } else if (icon == null) { |
| 75 | + Files.copy(getClass().getResourceAsStream( |
| 76 | + "/org/apache/netbeans/nbpackage/apache-netbeans.svg"), |
| 77 | + launcherDir.resolve(appDir + ".svg")); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected Path buildPackage(Path image) throws Exception { |
| 83 | + String appDir = findLauncher( |
| 84 | + image.resolve("APPDIR").resolve("bin")) |
| 85 | + .getFileName().toString(); |
| 86 | + |
| 87 | + String appName = context().getValue(NBPackage.PACKAGE_NAME).orElse(appDir); |
| 88 | + String appNameSafe = sanitize(appName); |
| 89 | + |
| 90 | + Path dst = context().destination().resolve(image.getFileName().toString() + ".sh"); |
| 91 | + |
| 92 | + String desktop = setupDesktopFile("exe", "icon"); |
| 93 | + |
| 94 | + String launcher = TarScriptPackager.LAUNCHER_TEMPLATE.load(context()); |
| 95 | + |
| 96 | + String template = TarScriptPackager.TAR_SCRIPT_TEMPLATE.load(context()); |
| 97 | + Map<String, String> tokens = Map.of("package.tar.app_name_safe", appNameSafe, |
| 98 | + "package.tar.app_name", appName, "package.tar.app_dir", appDir, |
| 99 | + "package.tar.desktop", desktop, "package.tar.launcher", launcher); |
| 100 | + String script = StringUtils.replaceTokens(template, |
| 101 | + key -> { |
| 102 | + var ret = tokens.get(key); |
| 103 | + if (ret != null) { |
| 104 | + return ret; |
| 105 | + } else { |
| 106 | + return context().tokenReplacementFor(key); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + FileUtils.createEmbeddedTarScript(script, image, dst); |
| 111 | + |
| 112 | + try { |
| 113 | + Files.setPosixFilePermissions(dst, PosixFilePermissions.fromString("rwxr-xr-x")); |
| 114 | + } catch (UnsupportedOperationException ex) { |
| 115 | + context().warningHandler().accept("UnsupportedOperationException : PosixFilePermissions"); |
| 116 | + } |
| 117 | + return dst; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + protected String calculateImageName(Path input) throws Exception { |
| 122 | + return super.calculateImageName(input) + "." + packageArch(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + protected Path calculateRuntimePath(Path image, Path application) throws Exception { |
| 127 | + return application.resolve("jdk"); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + protected Path calculateAppPath(Path image) throws IOException { |
| 132 | + return image.resolve("APPDIR"); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + private String packageArch() { |
| 137 | + if (packageArch == null) { |
| 138 | + packageArch = context().getValue(NBPackage.PACKAGE_ARCH) |
| 139 | + .orElseGet(() -> { |
| 140 | + Optional<Path> runtime = context().getValue(NBPackage.PACKAGE_RUNTIME); |
| 141 | + if (runtime.isPresent()) { |
| 142 | + return Architecture.detectFromPath( |
| 143 | + runtime.get()).map(a -> { |
| 144 | + return switch (a) { |
| 145 | + case AARCH64 -> |
| 146 | + ARCH_AARCH64; |
| 147 | + case X86_64 -> |
| 148 | + ARCH_X86_64; |
| 149 | + }; |
| 150 | + }).orElseGet(() -> { |
| 151 | + context().warningHandler().accept( |
| 152 | + TarScriptPackager.MESSAGES.getString("message.unknownarch")); |
| 153 | + return ARCH_NOARCH; |
| 154 | + }); |
| 155 | + } else { |
| 156 | + return ARCH_NOARCH; |
| 157 | + } |
| 158 | + }); |
| 159 | + } |
| 160 | + return packageArch; |
| 161 | + } |
| 162 | + |
| 163 | + private String sanitize(String text) { |
| 164 | + return text.toLowerCase(Locale.ROOT) |
| 165 | + .replaceAll("[^a-z0-9\\+\\-\\.]", "-"); |
| 166 | + } |
| 167 | + |
| 168 | + private Path findLauncher(Path binDir) throws IOException { |
| 169 | + try ( var files = Files.list(binDir)) { |
| 170 | + return files.filter(f -> !f.getFileName().toString().endsWith(".exe")) |
| 171 | + .findFirst().orElseThrow(IOException::new); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private String setupDesktopFile(String exec, String pkgName) throws IOException { |
| 176 | + String template = TarScriptPackager.DESKTOP_TEMPLATE.load(context()); |
| 177 | + return context().replaceTokens(template); |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments