author | herrick |
Mon, 10 Dec 2018 16:48:25 -0500 | |
branch | JDK-8200758-branch |
changeset 57067 | 9c17d779808e |
parent 57059 | 9bb2a4dc3af7 |
child 57080 | bd4ce7f9ea2c |
permissions | -rw-r--r-- |
57038 | 1 |
/* |
2 |
* Copyright (c) 2012, 2018, 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
57039 | 26 |
package jdk.jpackage.internal; |
57038 | 27 |
|
57039 | 28 |
import jdk.jpackage.internal.resources.LinuxResources; |
57038 | 29 |
|
30 |
import javax.imageio.ImageIO; |
|
31 |
import java.awt.image.BufferedImage; |
|
32 |
import java.io.*; |
|
33 |
import java.nio.file.Files; |
|
34 |
import java.nio.file.attribute.PosixFilePermission; |
|
35 |
import java.nio.file.attribute.PosixFilePermissions; |
|
36 |
import java.text.MessageFormat; |
|
37 |
import java.util.*; |
|
38 |
import java.util.logging.Level; |
|
39 |
import java.util.logging.Logger; |
|
40 |
import java.util.regex.Matcher; |
|
41 |
import java.util.regex.Pattern; |
|
42 |
||
57039 | 43 |
import static jdk.jpackage.internal.StandardBundlerParam.*; |
44 |
import static jdk.jpackage.internal.LinuxAppBundler.LINUX_INSTALL_DIR; |
|
57067
9c17d779808e
8213425: Analyze output from Source code scanner and fix where needed.
herrick
parents:
57059
diff
changeset
|
45 |
import static jdk.jpackage.internal.LinuxAppBundler.LINUX_PACKAGE_DEPENDENCIES; |
57038 | 46 |
|
47 |
public class LinuxRpmBundler extends AbstractBundler { |
|
48 |
||
49 |
private static final ResourceBundle I18N = ResourceBundle.getBundle( |
|
57059 | 50 |
"jdk.jpackage.internal.resources.LinuxResources"); |
57038 | 51 |
|
52 |
public static final BundlerParamInfo<LinuxAppBundler> APP_BUNDLER = |
|
53 |
new StandardBundlerParam<>( |
|
57059 | 54 |
I18N.getString("param.rpm-app-bundler.name"), |
55 |
I18N.getString("param.rpm-app-bundler.description"), |
|
57038 | 56 |
"linux.app.bundler", |
57 |
LinuxAppBundler.class, |
|
58 |
params -> new LinuxAppBundler(), |
|
59 |
null); |
|
60 |
||
61 |
public static final BundlerParamInfo<File> RPM_IMAGE_DIR = |
|
62 |
new StandardBundlerParam<>( |
|
63 |
I18N.getString("param.image-dir.name"), |
|
64 |
I18N.getString("param.image-dir.description"), |
|
65 |
"linux.rpm.imageDir", |
|
66 |
File.class, |
|
67 |
params -> { |
|
68 |
File imagesRoot = IMAGES_ROOT.fetchFrom(params); |
|
69 |
if (!imagesRoot.exists()) imagesRoot.mkdirs(); |
|
70 |
return new File(imagesRoot, "linux-rpm.image"); |
|
71 |
}, |
|
72 |
(s, p) -> new File(s)); |
|
73 |
||
74 |
public static final BundlerParamInfo<File> CONFIG_ROOT = |
|
75 |
new StandardBundlerParam<>( |
|
76 |
I18N.getString("param.config-root.name"), |
|
77 |
I18N.getString("param.config-root.description"), |
|
78 |
"configRoot", |
|
79 |
File.class, |
|
80 |
params -> new File(BUILD_ROOT.fetchFrom(params), "linux"), |
|
81 |
(s, p) -> new File(s)); |
|
82 |
||
83 |
// Fedora rules for package naming are used here |
|
84 |
// https://fedoraproject.org/wiki/Packaging:NamingGuidelines?rd=Packaging/NamingGuidelines |
|
85 |
// |
|
86 |
// all Fedora packages must be named using only the following ASCII |
|
87 |
// characters. These characters are displayed here: |
|
88 |
// |
|
89 |
// abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._+ |
|
90 |
// |
|
91 |
private static final Pattern RPM_BUNDLE_NAME_PATTERN = |
|
92 |
Pattern.compile("[a-z\\d\\+\\-\\.\\_]+", Pattern.CASE_INSENSITIVE); |
|
93 |
||
94 |
public static final BundlerParamInfo<String> BUNDLE_NAME = |
|
95 |
new StandardBundlerParam<> ( |
|
96 |
I18N.getString("param.bundle-name.name"), |
|
97 |
I18N.getString("param.bundle-name.description"), |
|
98 |
Arguments.CLIOptions.LINUX_BUNDLE_NAME.getId(), |
|
99 |
String.class, |
|
100 |
params -> { |
|
101 |
String nm = APP_NAME.fetchFrom(params); |
|
102 |
if (nm == null) return null; |
|
103 |
||
104 |
// make sure to lower case and spaces become dashes |
|
105 |
nm = nm.toLowerCase().replaceAll("[ ]", "-"); |
|
106 |
||
107 |
return nm; |
|
108 |
}, |
|
109 |
(s, p) -> { |
|
110 |
if (!RPM_BUNDLE_NAME_PATTERN.matcher(s).matches()) { |
|
111 |
String msgKey = "error.invalid-value-for-package-name"; |
|
112 |
throw new IllegalArgumentException( |
|
113 |
new ConfigException(MessageFormat.format( |
|
114 |
I18N.getString(msgKey), s), |
|
115 |
I18N.getString(msgKey + ".advice"))); |
|
116 |
} |
|
117 |
||
118 |
return s; |
|
119 |
} |
|
120 |
); |
|
121 |
||
122 |
public static final BundlerParamInfo<String> LICENSE_TYPE = |
|
123 |
new StandardBundlerParam<>( |
|
124 |
I18N.getString("param.license-type.name"), |
|
125 |
I18N.getString("param.license-type.description"), |
|
126 |
Arguments.CLIOptions.LINUX_RPM_LICENSE_TYPE.getId(), |
|
127 |
String.class, |
|
128 |
params -> I18N.getString("param.license-type.default"), |
|
129 |
(s, p) -> s |
|
130 |
); |
|
131 |
||
132 |
public static final BundlerParamInfo<String> XDG_FILE_PREFIX = |
|
133 |
new StandardBundlerParam<> ( |
|
134 |
I18N.getString("param.xdg-prefix.name"), |
|
135 |
I18N.getString("param.xdg-prefix.description"), |
|
136 |
"linux.xdg-prefix", |
|
137 |
String.class, |
|
138 |
params -> { |
|
139 |
try { |
|
140 |
String vendor; |
|
141 |
if (params.containsKey(VENDOR.getID())) { |
|
142 |
vendor = VENDOR.fetchFrom(params); |
|
143 |
} else { |
|
57039 | 144 |
vendor = "jpackage"; |
57038 | 145 |
} |
146 |
String appName = APP_FS_NAME.fetchFrom(params); |
|
147 |
||
148 |
return (vendor + "-" + appName).replaceAll("\\s", ""); |
|
149 |
} catch (Exception e) { |
|
150 |
if (Log.isDebug()) { |
|
151 |
e.printStackTrace(); |
|
152 |
} |
|
153 |
} |
|
154 |
return "unknown-MimeInfo.xml"; |
|
155 |
}, |
|
156 |
(s, p) -> s); |
|
157 |
||
158 |
private final static String DEFAULT_ICON = "javalogo_white_32.png"; |
|
159 |
private final static String DEFAULT_SPEC_TEMPLATE = "template.spec"; |
|
160 |
private final static String DEFAULT_DESKTOP_FILE_TEMPLATE = |
|
161 |
"template.desktop"; |
|
162 |
||
163 |
public final static String TOOL_RPMBUILD = "rpmbuild"; |
|
164 |
public final static double TOOL_RPMBUILD_MIN_VERSION = 4.0d; |
|
165 |
||
166 |
public LinuxRpmBundler() { |
|
167 |
super(); |
|
168 |
baseResourceLoader = LinuxResources.class; |
|
169 |
} |
|
170 |
||
171 |
public static boolean testTool(String toolName, double minVersion) { |
|
172 |
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
173 |
PrintStream ps = new PrintStream(baos)) { |
|
174 |
ProcessBuilder pb = new ProcessBuilder(toolName, "--version"); |
|
175 |
IOUtils.exec(pb, Log.isDebug(), false, ps); |
|
176 |
//not interested in the above's output |
|
177 |
String content = new String(baos.toByteArray()); |
|
178 |
Pattern pattern = Pattern.compile(" (\\d+\\.\\d+)"); |
|
179 |
Matcher matcher = pattern.matcher(content); |
|
180 |
||
181 |
if (matcher.find()) { |
|
182 |
String v = matcher.group(1); |
|
183 |
double version = Double.parseDouble(v); |
|
184 |
return minVersion <= version; |
|
185 |
} else { |
|
186 |
return false; |
|
187 |
} |
|
188 |
} catch (Exception e) { |
|
189 |
Log.verbose(MessageFormat.format(I18N.getString( |
|
190 |
"message.test-for-tool"), toolName, e.getMessage())); |
|
191 |
return false; |
|
192 |
} |
|
193 |
} |
|
194 |
||
195 |
@Override |
|
196 |
public boolean validate(Map<String, ? super Object> p) |
|
197 |
throws UnsupportedPlatformException, ConfigException { |
|
198 |
try { |
|
199 |
if (p == null) throw new ConfigException( |
|
200 |
I18N.getString("error.parameters-null"), |
|
201 |
I18N.getString("error.parameters-null.advice")); |
|
202 |
||
203 |
// run basic validation to ensure requirements are met |
|
204 |
// we are not interested in return code, only possible exception |
|
57067
9c17d779808e
8213425: Analyze output from Source code scanner and fix where needed.
herrick
parents:
57059
diff
changeset
|
205 |
APP_BUNDLER.fetchFrom(p).validate(p); |
57038 | 206 |
|
207 |
// validate license file, if used, exists in the proper place |
|
208 |
if (p.containsKey(LICENSE_FILE.getID())) { |
|
209 |
List<RelativeFileSet> appResourcesList = |
|
210 |
APP_RESOURCES_LIST.fetchFrom(p); |
|
211 |
for (String license : LICENSE_FILE.fetchFrom(p)) { |
|
212 |
boolean found = false; |
|
213 |
for (RelativeFileSet appResources : appResourcesList) { |
|
214 |
found = found || appResources.contains(license); |
|
215 |
} |
|
216 |
if (!found) { |
|
217 |
throw new ConfigException( |
|
218 |
I18N.getString("error.license-missing"), |
|
219 |
MessageFormat.format( |
|
220 |
I18N.getString("error.license-missing.advice"), |
|
221 |
license)); |
|
222 |
} |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
// validate presense of required tools |
|
227 |
if (!testTool(TOOL_RPMBUILD, TOOL_RPMBUILD_MIN_VERSION)){ |
|
228 |
throw new ConfigException( |
|
229 |
MessageFormat.format( |
|
230 |
I18N.getString("error.cannot-find-rpmbuild"), |
|
231 |
TOOL_RPMBUILD_MIN_VERSION), |
|
232 |
MessageFormat.format( |
|
233 |
I18N.getString("error.cannot-find-rpmbuild.advice"), |
|
234 |
TOOL_RPMBUILD_MIN_VERSION)); |
|
235 |
} |
|
236 |
||
237 |
// only one mime type per association, at least one file extension |
|
238 |
List<Map<String, ? super Object>> associations = |
|
239 |
FILE_ASSOCIATIONS.fetchFrom(p); |
|
240 |
if (associations != null) { |
|
241 |
for (int i = 0; i < associations.size(); i++) { |
|
242 |
Map<String, ? super Object> assoc = associations.get(i); |
|
243 |
List<String> mimes = FA_CONTENT_TYPE.fetchFrom(assoc); |
|
244 |
if (mimes == null || mimes.isEmpty()) { |
|
245 |
String msgKey = |
|
246 |
"error.no-content-types-for-file-association"; |
|
247 |
throw new ConfigException( |
|
248 |
MessageFormat.format(I18N.getString(msgKey), i), |
|
249 |
I18N.getString(msgKey + ".advice")); |
|
250 |
} else if (mimes.size() > 1) { |
|
251 |
String msgKey = |
|
252 |
"error.no-content-types-for-file-association"; |
|
253 |
throw new ConfigException( |
|
254 |
MessageFormat.format(I18N.getString(msgKey), i), |
|
255 |
I18N.getString(msgKey + ".advice")); |
|
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
||
260 |
// bundle name has some restrictions |
|
261 |
// the string converter will throw an exception if invalid |
|
262 |
BUNDLE_NAME.getStringConverter().apply(BUNDLE_NAME.fetchFrom(p), p); |
|
263 |
||
264 |
return true; |
|
265 |
} catch (RuntimeException re) { |
|
266 |
if (re.getCause() instanceof ConfigException) { |
|
267 |
throw (ConfigException) re.getCause(); |
|
268 |
} else { |
|
269 |
throw new ConfigException(re); |
|
270 |
} |
|
271 |
} |
|
272 |
} |
|
273 |
||
274 |
private boolean prepareProto(Map<String, ? super Object> p) |
|
275 |
throws IOException { |
|
276 |
File appImage = StandardBundlerParam.getPredefinedAppImage(p); |
|
277 |
File appDir = null; |
|
278 |
||
279 |
// we either have an application image or need to build one |
|
280 |
if (appImage != null) { |
|
281 |
appDir = new File(RPM_IMAGE_DIR.fetchFrom(p), |
|
282 |
APP_NAME.fetchFrom(p)); |
|
283 |
// copy everything from appImage dir into appDir/name |
|
284 |
IOUtils.copyRecursive(appImage.toPath(), appDir.toPath()); |
|
285 |
} else { |
|
286 |
appDir = APP_BUNDLER.fetchFrom(p).doBundle(p, |
|
287 |
RPM_IMAGE_DIR.fetchFrom(p), true); |
|
288 |
} |
|
289 |
return appDir != null; |
|
290 |
} |
|
291 |
||
292 |
public File bundle(Map<String, ? super Object> p, File outdir) { |
|
293 |
if (!outdir.isDirectory() && !outdir.mkdirs()) { |
|
294 |
throw new RuntimeException(MessageFormat.format( |
|
295 |
I18N.getString("error.cannot-create-output-dir"), |
|
296 |
outdir.getAbsolutePath())); |
|
297 |
} |
|
298 |
if (!outdir.canWrite()) { |
|
299 |
throw new RuntimeException(MessageFormat.format( |
|
300 |
I18N.getString("error.cannot-write-to-output-dir"), |
|
301 |
outdir.getAbsolutePath())); |
|
302 |
} |
|
303 |
||
304 |
File imageDir = RPM_IMAGE_DIR.fetchFrom(p); |
|
305 |
try { |
|
306 |
||
307 |
imageDir.mkdirs(); |
|
308 |
||
309 |
if (prepareProto(p) && prepareProjectConfig(p)) { |
|
310 |
return buildRPM(p, outdir); |
|
311 |
} |
|
312 |
return null; |
|
313 |
} catch (IOException ex) { |
|
314 |
ex.printStackTrace(); |
|
315 |
return null; |
|
316 |
} finally { |
|
317 |
try { |
|
318 |
if (imageDir != null && |
|
319 |
PREDEFINED_APP_IMAGE.fetchFrom(p) == null && |
|
320 |
(PREDEFINED_RUNTIME_IMAGE.fetchFrom(p) == null || |
|
321 |
!Arguments.CREATE_JRE_INSTALLER.fetchFrom(p)) && |
|
322 |
!Log.isDebug() && |
|
323 |
!Log.isVerbose()) { |
|
324 |
IOUtils.deleteRecursive(imageDir); |
|
325 |
} else if (imageDir != null) { |
|
326 |
Log.verbose(MessageFormat.format(I18N.getString( |
|
327 |
"message.debug-working-directory"), |
|
328 |
imageDir.getAbsolutePath())); |
|
329 |
} |
|
330 |
} catch (IOException ex) { |
|
331 |
// noinspection ReturnInsideFinallyBlock |
|
332 |
Log.debug(ex.getMessage()); |
|
333 |
return null; |
|
334 |
} |
|
335 |
} |
|
336 |
} |
|
337 |
||
338 |
/* |
|
339 |
* set permissions with a string like "rwxr-xr-x" |
|
340 |
* |
|
341 |
* This cannot be directly backport to 22u which is built with 1.6 |
|
342 |
*/ |
|
343 |
private void setPermissions(File file, String permissions) { |
|
344 |
Set<PosixFilePermission> filePermissions = |
|
345 |
PosixFilePermissions.fromString(permissions); |
|
346 |
try { |
|
347 |
if (file.exists()) { |
|
348 |
Files.setPosixFilePermissions(file.toPath(), filePermissions); |
|
349 |
} |
|
350 |
} catch (IOException ex) { |
|
351 |
Logger.getLogger(LinuxDebBundler.class.getName()).log( |
|
352 |
Level.SEVERE, null, ex); |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
private String getLicenseFileString(Map<String, ? super Object> params) { |
|
357 |
StringBuilder sb = new StringBuilder(); |
|
358 |
for (String f: LICENSE_FILE.fetchFrom(params)) { |
|
359 |
if (sb.length() != 0) { |
|
360 |
sb.append("\n"); |
|
361 |
} |
|
362 |
sb.append("%doc "); |
|
363 |
sb.append(LINUX_INSTALL_DIR.fetchFrom(params)); |
|
364 |
sb.append("/"); |
|
365 |
sb.append(APP_FS_NAME.fetchFrom(params)); |
|
366 |
sb.append("/app/"); |
|
367 |
sb.append(f); |
|
368 |
} |
|
369 |
return sb.toString(); |
|
370 |
} |
|
371 |
||
372 |
private boolean prepareProjectConfig(Map<String, ? super Object> params) |
|
373 |
throws IOException { |
|
374 |
Map<String, String> data = createReplacementData(params); |
|
375 |
File rootDir = |
|
376 |
LinuxAppBundler.getRootDir(RPM_IMAGE_DIR.fetchFrom(params), params); |
|
377 |
||
378 |
// prepare installer icon |
|
379 |
File iconTarget = getConfig_IconFile(rootDir, params); |
|
380 |
File icon = LinuxAppBundler.ICON_PNG.fetchFrom(params); |
|
381 |
if (!Arguments.CREATE_JRE_INSTALLER.fetchFrom(params)) { |
|
382 |
if (icon == null || !icon.exists()) { |
|
383 |
fetchResource(LinuxAppBundler.BUNDLER_PREFIX |
|
384 |
+ iconTarget.getName(), |
|
385 |
I18N.getString("resource.menu-icon"), |
|
386 |
DEFAULT_ICON, |
|
387 |
iconTarget, |
|
388 |
VERBOSE.fetchFrom(params), |
|
389 |
DROP_IN_RESOURCES_ROOT.fetchFrom(params)); |
|
390 |
} else { |
|
391 |
fetchResource(LinuxAppBundler.BUNDLER_PREFIX |
|
392 |
+ iconTarget.getName(), |
|
393 |
I18N.getString("resource.menu-icon"), |
|
394 |
icon, |
|
395 |
iconTarget, |
|
396 |
VERBOSE.fetchFrom(params), |
|
397 |
DROP_IN_RESOURCES_ROOT.fetchFrom(params)); |
|
398 |
} |
|
399 |
} |
|
400 |
||
401 |
StringBuilder installScripts = new StringBuilder(); |
|
402 |
StringBuilder removeScripts = new StringBuilder(); |
|
403 |
for (Map<String, ? super Object> secondaryLauncher : |
|
404 |
SECONDARY_LAUNCHERS.fetchFrom(params)) { |
|
405 |
Map<String, String> secondaryLauncherData = |
|
406 |
createReplacementData(secondaryLauncher); |
|
407 |
secondaryLauncherData.put("APPLICATION_FS_NAME", |
|
408 |
data.get("APPLICATION_FS_NAME")); |
|
409 |
secondaryLauncherData.put("DESKTOP_MIMES", ""); |
|
410 |
||
411 |
// prepare desktop shortcut |
|
412 |
Writer w = new BufferedWriter(new FileWriter( |
|
413 |
getConfig_DesktopShortcutFile(rootDir, secondaryLauncher))); |
|
414 |
String content = preprocessTextResource( |
|
415 |
LinuxAppBundler.BUNDLER_PREFIX |
|
416 |
+ getConfig_DesktopShortcutFile(rootDir, |
|
417 |
secondaryLauncher).getName(), |
|
418 |
I18N.getString("resource.menu-shortcut-descriptor"), |
|
419 |
DEFAULT_DESKTOP_FILE_TEMPLATE, secondaryLauncherData, |
|
420 |
VERBOSE.fetchFrom(params), |
|
421 |
DROP_IN_RESOURCES_ROOT.fetchFrom(params)); |
|
422 |
w.write(content); |
|
423 |
w.close(); |
|
424 |
||
425 |
// prepare installer icon |
|
426 |
iconTarget = getConfig_IconFile(rootDir, secondaryLauncher); |
|
427 |
icon = LinuxAppBundler.ICON_PNG.fetchFrom(secondaryLauncher); |
|
428 |
if (icon == null || !icon.exists()) { |
|
429 |
fetchResource(LinuxAppBundler.BUNDLER_PREFIX |
|
430 |
+ iconTarget.getName(), |
|
431 |
I18N.getString("resource.menu-icon"), |
|
432 |
DEFAULT_ICON, |
|
433 |
iconTarget, |
|
434 |
VERBOSE.fetchFrom(params), |
|
435 |
DROP_IN_RESOURCES_ROOT.fetchFrom(params)); |
|
436 |
} else { |
|
437 |
fetchResource(LinuxAppBundler.BUNDLER_PREFIX |
|
438 |
+ iconTarget.getName(), |
|
439 |
I18N.getString("resource.menu-icon"), |
|
440 |
icon, |
|
441 |
iconTarget, |
|
442 |
VERBOSE.fetchFrom(params), |
|
443 |
DROP_IN_RESOURCES_ROOT.fetchFrom(params)); |
|
444 |
} |
|
445 |
||
446 |
// post copying of desktop icon |
|
447 |
installScripts.append("xdg-desktop-menu install --novendor "); |
|
448 |
installScripts.append(LINUX_INSTALL_DIR.fetchFrom(params)); |
|
449 |
installScripts.append("/"); |
|
450 |
installScripts.append(data.get("APPLICATION_FS_NAME")); |
|
451 |
installScripts.append("/"); |
|
452 |
installScripts.append(secondaryLauncherData.get( |
|
453 |
"APPLICATION_LAUNCHER_FILENAME")); |
|
454 |
installScripts.append(".desktop\n"); |
|
455 |
||
456 |
// preun cleanup of desktop icon |
|
457 |
removeScripts.append("xdg-desktop-menu uninstall --novendor "); |
|
458 |
removeScripts.append(LINUX_INSTALL_DIR.fetchFrom(params)); |
|
459 |
removeScripts.append("/"); |
|
460 |
removeScripts.append(data.get("APPLICATION_FS_NAME")); |
|
461 |
removeScripts.append("/"); |
|
462 |
removeScripts.append(secondaryLauncherData.get( |
|
463 |
"APPLICATION_LAUNCHER_FILENAME")); |
|
464 |
removeScripts.append(".desktop\n"); |
|
465 |
||
466 |
} |
|
467 |
data.put("SECONDARY_LAUNCHERS_INSTALL", installScripts.toString()); |
|
468 |
data.put("SECONDARY_LAUNCHERS_REMOVE", removeScripts.toString()); |
|
469 |
||
470 |
StringBuilder cdsScript = new StringBuilder(); |
|
471 |
||
472 |
data.put("APP_CDS_CACHE", cdsScript.toString()); |
|
473 |
||
474 |
List<Map<String, ? super Object>> associations = |
|
475 |
FILE_ASSOCIATIONS.fetchFrom(params); |
|
476 |
data.put("FILE_ASSOCIATION_INSTALL", ""); |
|
477 |
data.put("FILE_ASSOCIATION_REMOVE", ""); |
|
478 |
data.put("DESKTOP_MIMES", ""); |
|
479 |
if (associations != null) { |
|
480 |
String mimeInfoFile = XDG_FILE_PREFIX.fetchFrom(params) |
|
481 |
+ "-MimeInfo.xml"; |
|
482 |
StringBuilder mimeInfo = new StringBuilder( |
|
483 |
"<?xml version=\"1.0\"?>\n<mime-info xmlns=" |
|
484 |
+"'http://www.freedesktop.org/standards/shared-mime-info'>\n"); |
|
485 |
StringBuilder registrations = new StringBuilder(); |
|
486 |
StringBuilder deregistrations = new StringBuilder(); |
|
487 |
StringBuilder desktopMimes = new StringBuilder("MimeType="); |
|
488 |
boolean addedEntry = false; |
|
489 |
||
490 |
for (Map<String, ? super Object> assoc : associations) { |
|
491 |
// <mime-type type="application/x-vnd.awesome"> |
|
492 |
// <comment>Awesome document</comment> |
|
493 |
// <glob pattern="*.awesome"/> |
|
494 |
// <glob pattern="*.awe"/> |
|
495 |
// </mime-type> |
|
496 |
||
497 |
if (assoc == null) { |
|
498 |
continue; |
|
499 |
} |
|
500 |
||
501 |
String description = FA_DESCRIPTION.fetchFrom(assoc); |
|
502 |
File faIcon = FA_ICON.fetchFrom(assoc); //TODO FA_ICON_PNG |
|
503 |
List<String> extensions = FA_EXTENSIONS.fetchFrom(assoc); |
|
504 |
if (extensions == null) { |
|
505 |
Log.verbose(I18N.getString( |
|
506 |
"message.creating-association-with-null-extension")); |
|
507 |
} |
|
508 |
||
509 |
List<String> mimes = FA_CONTENT_TYPE.fetchFrom(assoc); |
|
510 |
if (mimes == null || mimes.isEmpty()) { |
|
511 |
continue; |
|
512 |
} |
|
513 |
String thisMime = mimes.get(0); |
|
514 |
String dashMime = thisMime.replace('/', '-'); |
|
515 |
||
516 |
mimeInfo.append(" <mime-type type='") |
|
517 |
.append(thisMime) |
|
518 |
.append("'>\n"); |
|
519 |
if (description != null && !description.isEmpty()) { |
|
520 |
mimeInfo.append(" <comment>") |
|
521 |
.append(description) |
|
522 |
.append("</comment>\n"); |
|
523 |
} |
|
524 |
||
525 |
if (extensions != null) { |
|
526 |
for (String ext : extensions) { |
|
527 |
mimeInfo.append(" <glob pattern='*.") |
|
528 |
.append(ext) |
|
529 |
.append("'/>\n"); |
|
530 |
} |
|
531 |
} |
|
532 |
||
533 |
mimeInfo.append(" </mime-type>\n"); |
|
534 |
if (!addedEntry) { |
|
535 |
registrations.append("xdg-mime install ") |
|
536 |
.append(LINUX_INSTALL_DIR.fetchFrom(params)) |
|
537 |
.append("/") |
|
538 |
.append(data.get("APPLICATION_FS_NAME")) |
|
539 |
.append("/") |
|
540 |
.append(mimeInfoFile) |
|
541 |
.append("\n"); |
|
542 |
||
543 |
deregistrations.append("xdg-mime uninstall ") |
|
544 |
.append(LINUX_INSTALL_DIR.fetchFrom(params)) |
|
545 |
.append("/") |
|
546 |
.append(data.get("APPLICATION_FS_NAME")) |
|
547 |
.append("/") |
|
548 |
.append(mimeInfoFile) |
|
549 |
.append("\n"); |
|
550 |
addedEntry = true; |
|
551 |
} else { |
|
552 |
desktopMimes.append(";"); |
|
553 |
} |
|
554 |
desktopMimes.append(thisMime); |
|
555 |
||
556 |
if (faIcon != null && faIcon.exists()) { |
|
557 |
int size = getSquareSizeOfImage(faIcon); |
|
558 |
||
559 |
if (size > 0) { |
|
560 |
File target = new File(rootDir, |
|
561 |
APP_FS_NAME.fetchFrom(params) |
|
562 |
+ "_fa_" + faIcon.getName()); |
|
563 |
IOUtils.copyFile(faIcon, target); |
|
564 |
||
565 |
// xdg-icon-resource install --context mimetypes |
|
566 |
// --size 64 awesomeapp_fa_1.png |
|
567 |
// application-x.vnd-awesome |
|
568 |
registrations.append( |
|
569 |
"xdg-icon-resource install " |
|
570 |
+ "--context mimetypes --size ") |
|
571 |
.append(size) |
|
572 |
.append(" ") |
|
573 |
.append(LINUX_INSTALL_DIR.fetchFrom(params)) |
|
574 |
.append("/") |
|
575 |
.append(data.get("APPLICATION_FS_NAME")) |
|
576 |
.append("/") |
|
577 |
.append(target.getName()) |
|
578 |
.append(" ") |
|
579 |
.append(dashMime) |
|
580 |
.append("\n"); |
|
581 |
||
582 |
// xdg-icon-resource uninstall --context mimetypes |
|
583 |
// --size 64 awesomeapp_fa_1.png |
|
584 |
// application-x.vnd-awesome |
|
585 |
deregistrations.append( |
|
586 |
"xdg-icon-resource uninstall " |
|
587 |
+ "--context mimetypes --size ") |
|
588 |
.append(size) |
|
589 |
.append(" ") |
|
590 |
.append(LINUX_INSTALL_DIR.fetchFrom(params)) |
|
591 |
.append("/") |
|
592 |
.append(data.get("APPLICATION_FS_NAME")) |
|
593 |
.append("/") |
|
594 |
.append(target.getName()) |
|
595 |
.append(" ") |
|
596 |
.append(dashMime) |
|
597 |
.append("\n"); |
|
598 |
} |
|
599 |
} |
|
600 |
} |
|
601 |
mimeInfo.append("</mime-info>"); |
|
602 |
||
603 |
if (addedEntry) { |
|
604 |
Writer w = new BufferedWriter(new FileWriter( |
|
605 |
new File(rootDir, mimeInfoFile))); |
|
606 |
w.write(mimeInfo.toString()); |
|
607 |
w.close(); |
|
608 |
data.put("FILE_ASSOCIATION_INSTALL", registrations.toString()); |
|
609 |
data.put("FILE_ASSOCIATION_REMOVE", deregistrations.toString()); |
|
610 |
data.put("DESKTOP_MIMES", desktopMimes.toString()); |
|
611 |
} |
|
612 |
} |
|
613 |
||
614 |
if (!Arguments.CREATE_JRE_INSTALLER.fetchFrom(params)) { |
|
615 |
//prepare desktop shortcut |
|
616 |
Writer w = new BufferedWriter(new FileWriter( |
|
617 |
getConfig_DesktopShortcutFile(rootDir, params))); |
|
618 |
String content = preprocessTextResource( |
|
619 |
LinuxAppBundler.BUNDLER_PREFIX |
|
620 |
+ getConfig_DesktopShortcutFile(rootDir, |
|
621 |
params).getName(), |
|
622 |
I18N.getString("resource.menu-shortcut-descriptor"), |
|
623 |
DEFAULT_DESKTOP_FILE_TEMPLATE, data, |
|
624 |
VERBOSE.fetchFrom(params), |
|
625 |
DROP_IN_RESOURCES_ROOT.fetchFrom(params)); |
|
626 |
w.write(content); |
|
627 |
w.close(); |
|
628 |
} |
|
629 |
||
630 |
// prepare spec file |
|
631 |
Writer w = new BufferedWriter( |
|
632 |
new FileWriter(getConfig_SpecFile(params))); |
|
633 |
String content = preprocessTextResource( |
|
634 |
LinuxAppBundler.BUNDLER_PREFIX |
|
635 |
+ getConfig_SpecFile(params).getName(), |
|
636 |
I18N.getString("resource.rpm-spec-file"), |
|
637 |
DEFAULT_SPEC_TEMPLATE, data, |
|
638 |
VERBOSE.fetchFrom(params), |
|
639 |
DROP_IN_RESOURCES_ROOT.fetchFrom(params)); |
|
640 |
w.write(content); |
|
641 |
w.close(); |
|
642 |
||
643 |
return true; |
|
644 |
} |
|
645 |
||
646 |
private Map<String, String> createReplacementData( |
|
647 |
Map<String, ? super Object> params) { |
|
648 |
Map<String, String> data = new HashMap<>(); |
|
649 |
||
650 |
data.put("APPLICATION_NAME", APP_NAME.fetchFrom(params)); |
|
651 |
data.put("APPLICATION_FS_NAME", APP_FS_NAME.fetchFrom(params)); |
|
652 |
data.put("APPLICATION_PACKAGE", BUNDLE_NAME.fetchFrom(params)); |
|
653 |
data.put("APPLICATION_VENDOR", VENDOR.fetchFrom(params)); |
|
654 |
data.put("APPLICATION_VERSION", VERSION.fetchFrom(params)); |
|
655 |
data.put("APPLICATION_LAUNCHER_FILENAME", |
|
656 |
APP_FS_NAME.fetchFrom(params)); |
|
657 |
data.put("INSTALLATION_DIRECTORY", LINUX_INSTALL_DIR.fetchFrom(params)); |
|
658 |
data.put("XDG_PREFIX", XDG_FILE_PREFIX.fetchFrom(params)); |
|
659 |
data.put("DEPLOY_BUNDLE_CATEGORY", CATEGORY.fetchFrom(params)); |
|
660 |
// TODO rpm categories |
|
661 |
data.put("APPLICATION_DESCRIPTION", DESCRIPTION.fetchFrom(params)); |
|
662 |
data.put("APPLICATION_SUMMARY", TITLE.fetchFrom(params)); |
|
663 |
data.put("APPLICATION_LICENSE_TYPE", LICENSE_TYPE.fetchFrom(params)); |
|
664 |
data.put("APPLICATION_LICENSE_FILE", getLicenseFileString(params)); |
|
665 |
String deps = LINUX_PACKAGE_DEPENDENCIES.fetchFrom(params); |
|
666 |
data.put("PACKAGE_DEPENDENCIES", |
|
667 |
deps.isEmpty() ? "" : "Requires: " + deps); |
|
668 |
data.put("CREATE_JRE_INSTALLER", |
|
669 |
Arguments.CREATE_JRE_INSTALLER.fetchFrom(params).toString()); |
|
670 |
return data; |
|
671 |
} |
|
672 |
||
673 |
private File getConfig_DesktopShortcutFile(File rootDir, |
|
674 |
Map<String, ? super Object> params) { |
|
675 |
return new File(rootDir, |
|
676 |
APP_FS_NAME.fetchFrom(params) + ".desktop"); |
|
677 |
} |
|
678 |
||
679 |
private File getConfig_IconFile(File rootDir, |
|
680 |
Map<String, ? super Object> params) { |
|
681 |
return new File(rootDir, |
|
682 |
APP_FS_NAME.fetchFrom(params) + ".png"); |
|
683 |
} |
|
684 |
||
685 |
private File getConfig_SpecFile(Map<String, ? super Object> params) { |
|
686 |
return new File(RPM_IMAGE_DIR.fetchFrom(params), |
|
687 |
APP_FS_NAME.fetchFrom(params) + ".spec"); |
|
688 |
} |
|
689 |
||
690 |
private File buildRPM(Map<String, ? super Object> params, |
|
691 |
File outdir) throws IOException { |
|
692 |
Log.verbose(MessageFormat.format(I18N.getString( |
|
693 |
"message.outputting-bundle-location"), |
|
694 |
outdir.getAbsolutePath())); |
|
695 |
||
696 |
File broot = new File(BUILD_ROOT.fetchFrom(params), "rmpbuildroot"); |
|
697 |
||
698 |
outdir.mkdirs(); |
|
699 |
||
700 |
//run rpmbuild |
|
701 |
ProcessBuilder pb = new ProcessBuilder( |
|
702 |
TOOL_RPMBUILD, |
|
703 |
"-bb", getConfig_SpecFile(params).getAbsolutePath(), |
|
704 |
"--define", "%_sourcedir " |
|
705 |
+ RPM_IMAGE_DIR.fetchFrom(params).getAbsolutePath(), |
|
706 |
// save result to output dir |
|
707 |
"--define", "%_rpmdir " + outdir.getAbsolutePath(), |
|
708 |
// do not use other system directories to build as current user |
|
709 |
"--define", "%_topdir " + broot.getAbsolutePath() |
|
710 |
); |
|
711 |
pb = pb.directory(RPM_IMAGE_DIR.fetchFrom(params)); |
|
712 |
IOUtils.exec(pb, false); |
|
713 |
||
714 |
if (!Log.isDebug() && !Log.isVerbose()) { |
|
715 |
IOUtils.deleteRecursive(broot); |
|
716 |
} |
|
717 |
||
718 |
Log.verbose(MessageFormat.format( |
|
719 |
I18N.getString("message.output-bundle-location"), |
|
720 |
outdir.getAbsolutePath())); |
|
721 |
||
722 |
// presume the result is the ".rpm" file with the newest modified time |
|
723 |
// not the best solution, but it is the most reliable |
|
724 |
File result = null; |
|
725 |
long lastModified = 0; |
|
726 |
File[] list = outdir.listFiles(); |
|
727 |
if (list != null) { |
|
728 |
for (File f : list) { |
|
729 |
if (f.getName().endsWith(".rpm") && |
|
730 |
f.lastModified() > lastModified) { |
|
731 |
result = f; |
|
732 |
lastModified = f.lastModified(); |
|
733 |
} |
|
734 |
} |
|
735 |
} |
|
736 |
||
737 |
return result; |
|
738 |
} |
|
739 |
||
740 |
@Override |
|
741 |
public String getName() { |
|
57059 | 742 |
return I18N.getString("rpm.bundler.name"); |
57038 | 743 |
} |
744 |
||
745 |
@Override |
|
746 |
public String getDescription() { |
|
57059 | 747 |
return I18N.getString("rpm.bundler.description"); |
57038 | 748 |
} |
749 |
||
750 |
@Override |
|
751 |
public String getID() { |
|
752 |
return "rpm"; |
|
753 |
} |
|
754 |
||
755 |
@Override |
|
756 |
public String getBundleType() { |
|
757 |
return "INSTALLER"; |
|
758 |
} |
|
759 |
||
760 |
@Override |
|
761 |
public Collection<BundlerParamInfo<?>> getBundleParameters() { |
|
762 |
Collection<BundlerParamInfo<?>> results = new LinkedHashSet<>(); |
|
763 |
results.addAll(LinuxAppBundler.getAppBundleParameters()); |
|
764 |
results.addAll(getRpmBundleParameters()); |
|
765 |
return results; |
|
766 |
} |
|
767 |
||
768 |
public static Collection<BundlerParamInfo<?>> getRpmBundleParameters() { |
|
769 |
return Arrays.asList( |
|
770 |
BUNDLE_NAME, |
|
771 |
CATEGORY, |
|
772 |
DESCRIPTION, |
|
773 |
LinuxAppBundler.ICON_PNG, |
|
774 |
LICENSE_FILE, |
|
775 |
LICENSE_TYPE, |
|
776 |
TITLE, |
|
777 |
VENDOR |
|
778 |
); |
|
779 |
} |
|
780 |
||
781 |
@Override |
|
782 |
public File execute( |
|
783 |
Map<String, ? super Object> params, File outputParentDir) { |
|
784 |
return bundle(params, outputParentDir); |
|
785 |
} |
|
786 |
||
787 |
@Override |
|
788 |
public boolean supported() { |
|
789 |
return (Platform.getPlatform() == Platform.LINUX); |
|
790 |
} |
|
791 |
||
792 |
public int getSquareSizeOfImage(File f) { |
|
793 |
try { |
|
794 |
BufferedImage bi = ImageIO.read(f); |
|
795 |
if (bi.getWidth() == bi.getHeight()) { |
|
796 |
return bi.getWidth(); |
|
797 |
} else { |
|
798 |
return 0; |
|
799 |
} |
|
800 |
} catch (Exception e) { |
|
801 |
e.printStackTrace(); |
|
802 |
return 0; |
|
803 |
} |
|
804 |
} |
|
805 |
} |