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