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