author | herrick |
Thu, 06 Jun 2019 19:51:17 -0400 | |
branch | JDK-8200758-branch |
changeset 57396 | 3944e4c2f779 |
parent 57392 | 46d4b0aa4542 |
child 57397 | 89549ecec1c7 |
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 { |
57038 | 256 |
if (!outdir.isDirectory() && !outdir.mkdirs()) { |
57140 | 257 |
throw new PackagerException( |
258 |
"error.cannot-create-output-dir", |
|
259 |
outdir.getAbsolutePath()); |
|
57038 | 260 |
} |
261 |
if (!outdir.canWrite()) { |
|
57140 | 262 |
throw new PackagerException( |
263 |
"error.cannot-write-to-output-dir", |
|
264 |
outdir.getAbsolutePath()); |
|
57038 | 265 |
} |
266 |
||
57396
3944e4c2f779
8223586: remove jpackage dead code and other cleanup
herrick
parents:
57392
diff
changeset
|
267 |
File imageDir = RPM_IMAGE_DIR.fetchFrom(params); |
57038 | 268 |
try { |
269 |
||
270 |
imageDir.mkdirs(); |
|
271 |
||
57396
3944e4c2f779
8223586: remove jpackage dead code and other cleanup
herrick
parents:
57392
diff
changeset
|
272 |
if (prepareProto(params) && prepareProjectConfig(params)) { |
3944e4c2f779
8223586: remove jpackage dead code and other cleanup
herrick
parents:
57392
diff
changeset
|
273 |
return buildRPM(params, outdir); |
57038 | 274 |
} |
275 |
return null; |
|
276 |
} catch (IOException ex) { |
|
57140 | 277 |
Log.verbose(ex); |
278 |
throw new PackagerException(ex); |
|
57038 | 279 |
} |
280 |
} |
|
281 |
||
57080 | 282 |
private String getLicenseFileString(Map<String, ? super Object> params) |
283 |
throws IOException { |
|
284 |
StringBuilder sb = new StringBuilder(); |
|
57038 | 285 |
|
57080 | 286 |
String licenseStr = LICENSE_FILE.fetchFrom(params); |
287 |
if (licenseStr != null) { |
|
288 |
File licenseFile = new File(licenseStr); |
|
289 |
File rootDir = |
|
290 |
LinuxAppBundler.getRootDir(RPM_IMAGE_DIR.fetchFrom(params), |
|
291 |
params); |
|
292 |
File target = new File(rootDir + File.separator + "app" |
|
293 |
+ File.separator + licenseFile.getName()); |
|
294 |
Files.copy(licenseFile.toPath(), target.toPath()); |
|
295 |
||
296 |
sb.append("%license "); |
|
57038 | 297 |
sb.append(LINUX_INSTALL_DIR.fetchFrom(params)); |
298 |
sb.append("/"); |
|
57119 | 299 |
sb.append(APP_NAME.fetchFrom(params)); |
57038 | 300 |
sb.append("/app/"); |
57080 | 301 |
sb.append(licenseFile.getName()); |
57038 | 302 |
} |
57080 | 303 |
|
57038 | 304 |
return sb.toString(); |
305 |
} |
|
306 |
||
307 |
private boolean prepareProjectConfig(Map<String, ? super Object> params) |
|
308 |
throws IOException { |
|
309 |
Map<String, String> data = createReplacementData(params); |
|
310 |
File rootDir = |
|
311 |
LinuxAppBundler.getRootDir(RPM_IMAGE_DIR.fetchFrom(params), params); |
|
312 |
||
313 |
// prepare installer icon |
|
314 |
File iconTarget = getConfig_IconFile(rootDir, params); |
|
315 |
File icon = LinuxAppBundler.ICON_PNG.fetchFrom(params); |
|
57256 | 316 |
if (!StandardBundlerParam.isRuntimeInstaller(params)) { |
57038 | 317 |
if (icon == null || !icon.exists()) { |
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 |
DEFAULT_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 |
} else { |
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
325 |
fetchResource(iconTarget.getName(), |
57038 | 326 |
I18N.getString("resource.menu-icon"), |
327 |
icon, |
|
328 |
iconTarget, |
|
329 |
VERBOSE.fetchFrom(params), |
|
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
330 |
RESOURCE_DIR.fetchFrom(params)); |
57038 | 331 |
} |
332 |
} |
|
333 |
||
334 |
StringBuilder installScripts = new StringBuilder(); |
|
335 |
StringBuilder removeScripts = new StringBuilder(); |
|
57256 | 336 |
for (Map<String, ? super Object> addLauncher : |
337 |
ADD_LAUNCHERS.fetchFrom(params)) { |
|
338 |
Map<String, String> addLauncherData = |
|
339 |
createReplacementData(addLauncher); |
|
340 |
addLauncherData.put("APPLICATION_FS_NAME", |
|
57038 | 341 |
data.get("APPLICATION_FS_NAME")); |
57256 | 342 |
addLauncherData.put("DESKTOP_MIMES", ""); |
57038 | 343 |
|
344 |
// prepare desktop shortcut |
|
57390 | 345 |
try (Writer w = Files.newBufferedWriter( |
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
346 |
getConfig_DesktopShortcutFile(rootDir, |
57390 | 347 |
addLauncher).toPath())) { |
348 |
String content = preprocessTextResource( |
|
349 |
getConfig_DesktopShortcutFile(rootDir, |
|
350 |
addLauncher).getName(), |
|
351 |
I18N.getString("resource.menu-shortcut-descriptor"), |
|
352 |
DEFAULT_DESKTOP_FILE_TEMPLATE, addLauncherData, |
|
353 |
VERBOSE.fetchFrom(params), |
|
354 |
RESOURCE_DIR.fetchFrom(params)); |
|
355 |
w.write(content); |
|
356 |
} |
|
57038 | 357 |
|
358 |
// prepare installer icon |
|
57256 | 359 |
iconTarget = getConfig_IconFile(rootDir, addLauncher); |
360 |
icon = LinuxAppBundler.ICON_PNG.fetchFrom(addLauncher); |
|
57038 | 361 |
if (icon == null || !icon.exists()) { |
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 |
DEFAULT_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 |
} else { |
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
369 |
fetchResource(iconTarget.getName(), |
57038 | 370 |
I18N.getString("resource.menu-icon"), |
371 |
icon, |
|
372 |
iconTarget, |
|
373 |
VERBOSE.fetchFrom(params), |
|
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
374 |
RESOURCE_DIR.fetchFrom(params)); |
57038 | 375 |
} |
376 |
||
377 |
// post copying of desktop icon |
|
378 |
installScripts.append("xdg-desktop-menu install --novendor "); |
|
379 |
installScripts.append(LINUX_INSTALL_DIR.fetchFrom(params)); |
|
380 |
installScripts.append("/"); |
|
381 |
installScripts.append(data.get("APPLICATION_FS_NAME")); |
|
382 |
installScripts.append("/"); |
|
57256 | 383 |
installScripts.append(addLauncherData.get( |
57038 | 384 |
"APPLICATION_LAUNCHER_FILENAME")); |
385 |
installScripts.append(".desktop\n"); |
|
386 |
||
387 |
// preun cleanup of desktop icon |
|
388 |
removeScripts.append("xdg-desktop-menu uninstall --novendor "); |
|
389 |
removeScripts.append(LINUX_INSTALL_DIR.fetchFrom(params)); |
|
390 |
removeScripts.append("/"); |
|
391 |
removeScripts.append(data.get("APPLICATION_FS_NAME")); |
|
392 |
removeScripts.append("/"); |
|
57256 | 393 |
removeScripts.append(addLauncherData.get( |
57038 | 394 |
"APPLICATION_LAUNCHER_FILENAME")); |
395 |
removeScripts.append(".desktop\n"); |
|
396 |
||
397 |
} |
|
57256 | 398 |
data.put("ADD_LAUNCHERS_INSTALL", installScripts.toString()); |
399 |
data.put("ADD_LAUNCHERS_REMOVE", removeScripts.toString()); |
|
57038 | 400 |
|
401 |
StringBuilder cdsScript = new StringBuilder(); |
|
402 |
||
403 |
data.put("APP_CDS_CACHE", cdsScript.toString()); |
|
404 |
||
405 |
List<Map<String, ? super Object>> associations = |
|
406 |
FILE_ASSOCIATIONS.fetchFrom(params); |
|
407 |
data.put("FILE_ASSOCIATION_INSTALL", ""); |
|
408 |
data.put("FILE_ASSOCIATION_REMOVE", ""); |
|
409 |
data.put("DESKTOP_MIMES", ""); |
|
410 |
if (associations != null) { |
|
411 |
String mimeInfoFile = XDG_FILE_PREFIX.fetchFrom(params) |
|
412 |
+ "-MimeInfo.xml"; |
|
413 |
StringBuilder mimeInfo = new StringBuilder( |
|
414 |
"<?xml version=\"1.0\"?>\n<mime-info xmlns=" |
|
415 |
+"'http://www.freedesktop.org/standards/shared-mime-info'>\n"); |
|
416 |
StringBuilder registrations = new StringBuilder(); |
|
417 |
StringBuilder deregistrations = new StringBuilder(); |
|
418 |
StringBuilder desktopMimes = new StringBuilder("MimeType="); |
|
419 |
boolean addedEntry = false; |
|
420 |
||
421 |
for (Map<String, ? super Object> assoc : associations) { |
|
422 |
// <mime-type type="application/x-vnd.awesome"> |
|
423 |
// <comment>Awesome document</comment> |
|
424 |
// <glob pattern="*.awesome"/> |
|
425 |
// <glob pattern="*.awe"/> |
|
426 |
// </mime-type> |
|
427 |
||
428 |
if (assoc == null) { |
|
429 |
continue; |
|
430 |
} |
|
431 |
||
432 |
String description = FA_DESCRIPTION.fetchFrom(assoc); |
|
433 |
File faIcon = FA_ICON.fetchFrom(assoc); //TODO FA_ICON_PNG |
|
434 |
List<String> extensions = FA_EXTENSIONS.fetchFrom(assoc); |
|
435 |
if (extensions == null) { |
|
436 |
Log.verbose(I18N.getString( |
|
437 |
"message.creating-association-with-null-extension")); |
|
438 |
} |
|
439 |
||
440 |
List<String> mimes = FA_CONTENT_TYPE.fetchFrom(assoc); |
|
441 |
if (mimes == null || mimes.isEmpty()) { |
|
442 |
continue; |
|
443 |
} |
|
444 |
String thisMime = mimes.get(0); |
|
445 |
String dashMime = thisMime.replace('/', '-'); |
|
446 |
||
447 |
mimeInfo.append(" <mime-type type='") |
|
448 |
.append(thisMime) |
|
449 |
.append("'>\n"); |
|
450 |
if (description != null && !description.isEmpty()) { |
|
451 |
mimeInfo.append(" <comment>") |
|
452 |
.append(description) |
|
453 |
.append("</comment>\n"); |
|
454 |
} |
|
455 |
||
456 |
if (extensions != null) { |
|
457 |
for (String ext : extensions) { |
|
458 |
mimeInfo.append(" <glob pattern='*.") |
|
459 |
.append(ext) |
|
460 |
.append("'/>\n"); |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
mimeInfo.append(" </mime-type>\n"); |
|
465 |
if (!addedEntry) { |
|
466 |
registrations.append("xdg-mime install ") |
|
467 |
.append(LINUX_INSTALL_DIR.fetchFrom(params)) |
|
468 |
.append("/") |
|
469 |
.append(data.get("APPLICATION_FS_NAME")) |
|
470 |
.append("/") |
|
471 |
.append(mimeInfoFile) |
|
472 |
.append("\n"); |
|
473 |
||
474 |
deregistrations.append("xdg-mime uninstall ") |
|
475 |
.append(LINUX_INSTALL_DIR.fetchFrom(params)) |
|
476 |
.append("/") |
|
477 |
.append(data.get("APPLICATION_FS_NAME")) |
|
478 |
.append("/") |
|
479 |
.append(mimeInfoFile) |
|
480 |
.append("\n"); |
|
481 |
addedEntry = true; |
|
482 |
} else { |
|
483 |
desktopMimes.append(";"); |
|
484 |
} |
|
485 |
desktopMimes.append(thisMime); |
|
486 |
||
487 |
if (faIcon != null && faIcon.exists()) { |
|
488 |
int size = getSquareSizeOfImage(faIcon); |
|
489 |
||
490 |
if (size > 0) { |
|
491 |
File target = new File(rootDir, |
|
57119 | 492 |
APP_NAME.fetchFrom(params) |
493 |
+ "_fa_" + faIcon.getName()); |
|
57038 | 494 |
IOUtils.copyFile(faIcon, target); |
495 |
||
496 |
// xdg-icon-resource install --context mimetypes |
|
497 |
// --size 64 awesomeapp_fa_1.png |
|
498 |
// application-x.vnd-awesome |
|
499 |
registrations.append( |
|
500 |
"xdg-icon-resource install " |
|
501 |
+ "--context mimetypes --size ") |
|
502 |
.append(size) |
|
503 |
.append(" ") |
|
504 |
.append(LINUX_INSTALL_DIR.fetchFrom(params)) |
|
505 |
.append("/") |
|
506 |
.append(data.get("APPLICATION_FS_NAME")) |
|
507 |
.append("/") |
|
508 |
.append(target.getName()) |
|
509 |
.append(" ") |
|
510 |
.append(dashMime) |
|
511 |
.append("\n"); |
|
512 |
||
513 |
// xdg-icon-resource uninstall --context mimetypes |
|
514 |
// --size 64 awesomeapp_fa_1.png |
|
515 |
// application-x.vnd-awesome |
|
516 |
deregistrations.append( |
|
517 |
"xdg-icon-resource uninstall " |
|
518 |
+ "--context mimetypes --size ") |
|
519 |
.append(size) |
|
520 |
.append(" ") |
|
521 |
.append(LINUX_INSTALL_DIR.fetchFrom(params)) |
|
522 |
.append("/") |
|
523 |
.append(data.get("APPLICATION_FS_NAME")) |
|
524 |
.append("/") |
|
525 |
.append(target.getName()) |
|
526 |
.append(" ") |
|
527 |
.append(dashMime) |
|
528 |
.append("\n"); |
|
529 |
} |
|
530 |
} |
|
531 |
} |
|
532 |
mimeInfo.append("</mime-info>"); |
|
533 |
||
534 |
if (addedEntry) { |
|
57390 | 535 |
try (Writer w = Files.newBufferedWriter( |
536 |
new File(rootDir, mimeInfoFile).toPath())) { |
|
537 |
w.write(mimeInfo.toString()); |
|
538 |
} |
|
57038 | 539 |
data.put("FILE_ASSOCIATION_INSTALL", registrations.toString()); |
540 |
data.put("FILE_ASSOCIATION_REMOVE", deregistrations.toString()); |
|
541 |
data.put("DESKTOP_MIMES", desktopMimes.toString()); |
|
542 |
} |
|
543 |
} |
|
544 |
||
57256 | 545 |
if (!StandardBundlerParam.isRuntimeInstaller(params)) { |
57038 | 546 |
//prepare desktop shortcut |
57390 | 547 |
try (Writer w = Files.newBufferedWriter( |
548 |
getConfig_DesktopShortcutFile(rootDir, params).toPath())) { |
|
549 |
String content = preprocessTextResource( |
|
550 |
getConfig_DesktopShortcutFile(rootDir, |
|
551 |
params).getName(), |
|
552 |
I18N.getString("resource.menu-shortcut-descriptor"), |
|
553 |
DEFAULT_DESKTOP_FILE_TEMPLATE, data, |
|
554 |
VERBOSE.fetchFrom(params), |
|
555 |
RESOURCE_DIR.fetchFrom(params)); |
|
556 |
w.write(content); |
|
557 |
} |
|
558 |
} |
|
559 |
||
560 |
// prepare spec file |
|
561 |
try (Writer w = Files.newBufferedWriter( |
|
562 |
getConfig_SpecFile(params).toPath())) { |
|
57038 | 563 |
String content = preprocessTextResource( |
57390 | 564 |
getConfig_SpecFile(params).getName(), |
565 |
I18N.getString("resource.rpm-spec-file"), |
|
566 |
DEFAULT_SPEC_TEMPLATE, data, |
|
57038 | 567 |
VERBOSE.fetchFrom(params), |
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
568 |
RESOURCE_DIR.fetchFrom(params)); |
57038 | 569 |
w.write(content); |
570 |
} |
|
571 |
||
572 |
return true; |
|
573 |
} |
|
574 |
||
575 |
private Map<String, String> createReplacementData( |
|
57080 | 576 |
Map<String, ? super Object> params) throws IOException { |
57038 | 577 |
Map<String, String> data = new HashMap<>(); |
578 |
||
579 |
data.put("APPLICATION_NAME", APP_NAME.fetchFrom(params)); |
|
57119 | 580 |
data.put("APPLICATION_FS_NAME", APP_NAME.fetchFrom(params)); |
57038 | 581 |
data.put("APPLICATION_PACKAGE", BUNDLE_NAME.fetchFrom(params)); |
582 |
data.put("APPLICATION_VENDOR", VENDOR.fetchFrom(params)); |
|
583 |
data.put("APPLICATION_VERSION", VERSION.fetchFrom(params)); |
|
57119 | 584 |
data.put("APPLICATION_LAUNCHER_FILENAME", APP_NAME.fetchFrom(params)); |
57038 | 585 |
data.put("INSTALLATION_DIRECTORY", LINUX_INSTALL_DIR.fetchFrom(params)); |
586 |
data.put("XDG_PREFIX", XDG_FILE_PREFIX.fetchFrom(params)); |
|
57256 | 587 |
data.put("DEPLOY_BUNDLE_CATEGORY", MENU_GROUP.fetchFrom(params)); |
57038 | 588 |
// TODO rpm categories |
589 |
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
|
590 |
data.put("APPLICATION_SUMMARY", APP_NAME.fetchFrom(params)); |
57038 | 591 |
data.put("APPLICATION_LICENSE_TYPE", LICENSE_TYPE.fetchFrom(params)); |
592 |
data.put("APPLICATION_LICENSE_FILE", getLicenseFileString(params)); |
|
593 |
String deps = LINUX_PACKAGE_DEPENDENCIES.fetchFrom(params); |
|
594 |
data.put("PACKAGE_DEPENDENCIES", |
|
595 |
deps.isEmpty() ? "" : "Requires: " + deps); |
|
57256 | 596 |
data.put("RUNTIME_INSTALLER", "" + |
597 |
StandardBundlerParam.isRuntimeInstaller(params)); |
|
57038 | 598 |
return data; |
599 |
} |
|
600 |
||
601 |
private File getConfig_DesktopShortcutFile(File rootDir, |
|
602 |
Map<String, ? super Object> params) { |
|
57119 | 603 |
return new File(rootDir, APP_NAME.fetchFrom(params) + ".desktop"); |
57038 | 604 |
} |
605 |
||
606 |
private File getConfig_IconFile(File rootDir, |
|
607 |
Map<String, ? super Object> params) { |
|
57119 | 608 |
return new File(rootDir, APP_NAME.fetchFrom(params) + ".png"); |
57038 | 609 |
} |
610 |
||
611 |
private File getConfig_SpecFile(Map<String, ? super Object> params) { |
|
612 |
return new File(RPM_IMAGE_DIR.fetchFrom(params), |
|
57119 | 613 |
APP_NAME.fetchFrom(params) + ".spec"); |
57038 | 614 |
} |
615 |
||
616 |
private File buildRPM(Map<String, ? super Object> params, |
|
617 |
File outdir) throws IOException { |
|
618 |
Log.verbose(MessageFormat.format(I18N.getString( |
|
619 |
"message.outputting-bundle-location"), |
|
620 |
outdir.getAbsolutePath())); |
|
621 |
||
57256 | 622 |
File broot = new File(TEMP_ROOT.fetchFrom(params), "rmpbuildroot"); |
57038 | 623 |
|
624 |
outdir.mkdirs(); |
|
625 |
||
626 |
//run rpmbuild |
|
627 |
ProcessBuilder pb = new ProcessBuilder( |
|
628 |
TOOL_RPMBUILD, |
|
629 |
"-bb", getConfig_SpecFile(params).getAbsolutePath(), |
|
630 |
"--define", "%_sourcedir " |
|
631 |
+ RPM_IMAGE_DIR.fetchFrom(params).getAbsolutePath(), |
|
632 |
// save result to output dir |
|
633 |
"--define", "%_rpmdir " + outdir.getAbsolutePath(), |
|
634 |
// do not use other system directories to build as current user |
|
635 |
"--define", "%_topdir " + broot.getAbsolutePath() |
|
636 |
); |
|
637 |
pb = pb.directory(RPM_IMAGE_DIR.fetchFrom(params)); |
|
57391 | 638 |
IOUtils.exec(pb); |
57038 | 639 |
|
640 |
Log.verbose(MessageFormat.format( |
|
641 |
I18N.getString("message.output-bundle-location"), |
|
642 |
outdir.getAbsolutePath())); |
|
643 |
||
644 |
// presume the result is the ".rpm" file with the newest modified time |
|
645 |
// not the best solution, but it is the most reliable |
|
646 |
File result = null; |
|
647 |
long lastModified = 0; |
|
648 |
File[] list = outdir.listFiles(); |
|
649 |
if (list != null) { |
|
650 |
for (File f : list) { |
|
651 |
if (f.getName().endsWith(".rpm") && |
|
652 |
f.lastModified() > lastModified) { |
|
653 |
result = f; |
|
654 |
lastModified = f.lastModified(); |
|
655 |
} |
|
656 |
} |
|
657 |
} |
|
658 |
||
659 |
return result; |
|
660 |
} |
|
661 |
||
662 |
@Override |
|
663 |
public String getName() { |
|
57059 | 664 |
return I18N.getString("rpm.bundler.name"); |
57038 | 665 |
} |
666 |
||
667 |
@Override |
|
668 |
public String getDescription() { |
|
57059 | 669 |
return I18N.getString("rpm.bundler.description"); |
57038 | 670 |
} |
671 |
||
672 |
@Override |
|
673 |
public String getID() { |
|
674 |
return "rpm"; |
|
675 |
} |
|
676 |
||
677 |
@Override |
|
678 |
public String getBundleType() { |
|
679 |
return "INSTALLER"; |
|
680 |
} |
|
681 |
||
682 |
@Override |
|
683 |
public Collection<BundlerParamInfo<?>> getBundleParameters() { |
|
684 |
Collection<BundlerParamInfo<?>> results = new LinkedHashSet<>(); |
|
685 |
results.addAll(LinuxAppBundler.getAppBundleParameters()); |
|
686 |
results.addAll(getRpmBundleParameters()); |
|
687 |
return results; |
|
688 |
} |
|
689 |
||
690 |
public static Collection<BundlerParamInfo<?>> getRpmBundleParameters() { |
|
691 |
return Arrays.asList( |
|
692 |
BUNDLE_NAME, |
|
57256 | 693 |
MENU_GROUP, |
57038 | 694 |
DESCRIPTION, |
695 |
LinuxAppBundler.ICON_PNG, |
|
696 |
LICENSE_FILE, |
|
697 |
LICENSE_TYPE, |
|
698 |
VENDOR |
|
699 |
); |
|
700 |
} |
|
701 |
||
702 |
@Override |
|
57140 | 703 |
public File execute(Map<String, ? super Object> params, |
704 |
File outputParentDir) throws PackagerException { |
|
57038 | 705 |
return bundle(params, outputParentDir); |
706 |
} |
|
707 |
||
708 |
@Override |
|
57150 | 709 |
public boolean supported(boolean runtimeInstaller) { |
57038 | 710 |
return (Platform.getPlatform() == Platform.LINUX); |
711 |
} |
|
712 |
||
713 |
public int getSquareSizeOfImage(File f) { |
|
714 |
try { |
|
715 |
BufferedImage bi = ImageIO.read(f); |
|
716 |
if (bi.getWidth() == bi.getHeight()) { |
|
717 |
return bi.getWidth(); |
|
718 |
} else { |
|
719 |
return 0; |
|
720 |
} |
|
721 |
} catch (Exception e) { |
|
722 |
e.printStackTrace(); |
|
723 |
return 0; |
|
724 |
} |
|
725 |
} |
|
726 |
} |