author | herrick |
Thu, 06 Jun 2019 19:18:09 -0400 | |
branch | JDK-8200758-branch |
changeset 57392 | 46d4b0aa4542 |
parent 57391 | 970f28090a06 |
child 57396 | 3944e4c2f779 |
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.Pattern; |
|
37 |
||
57039 | 38 |
import static jdk.jpackage.internal.StandardBundlerParam.*; |
39 |
import static jdk.jpackage.internal.LinuxAppBundler.ICON_PNG; |
|
40 |
import static jdk.jpackage.internal.LinuxAppBundler.LINUX_INSTALL_DIR; |
|
41 |
import static jdk.jpackage.internal.LinuxAppBundler.LINUX_PACKAGE_DEPENDENCIES; |
|
57038 | 42 |
|
43 |
public class LinuxDebBundler 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 |
(s, p) -> null); |
|
54 |
||
55 |
// Debian rules for package naming are used here |
|
56 |
// https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Source |
|
57 |
// |
|
58 |
// Package names must consist only of lower case letters (a-z), |
|
59 |
// digits (0-9), plus (+) and minus (-) signs, and periods (.). |
|
60 |
// They must be at least two characters long and |
|
61 |
// must start with an alphanumeric character. |
|
62 |
// |
|
63 |
private static final Pattern DEB_BUNDLE_NAME_PATTERN = |
|
64 |
Pattern.compile("^[a-z][a-z\\d\\+\\-\\.]+"); |
|
65 |
||
66 |
public static final BundlerParamInfo<String> BUNDLE_NAME = |
|
67 |
new StandardBundlerParam<> ( |
|
68 |
Arguments.CLIOptions.LINUX_BUNDLE_NAME.getId(), |
|
69 |
String.class, |
|
70 |
params -> { |
|
71 |
String nm = APP_NAME.fetchFrom(params); |
|
72 |
||
73 |
if (nm == null) return null; |
|
74 |
||
75 |
// make sure to lower case and spaces/underscores become dashes |
|
76 |
nm = nm.toLowerCase().replaceAll("[ _]", "-"); |
|
77 |
return nm; |
|
78 |
}, |
|
79 |
(s, p) -> { |
|
80 |
if (!DEB_BUNDLE_NAME_PATTERN.matcher(s).matches()) { |
|
81 |
throw new IllegalArgumentException(new ConfigException( |
|
82 |
MessageFormat.format(I18N.getString( |
|
83 |
"error.invalid-value-for-package-name"), s), |
|
84 |
I18N.getString( |
|
85 |
"error.invalid-value-for-package-name.advice"))); |
|
86 |
} |
|
87 |
||
88 |
return s; |
|
89 |
}); |
|
90 |
||
91 |
public static final BundlerParamInfo<String> FULL_PACKAGE_NAME = |
|
92 |
new StandardBundlerParam<> ( |
|
93 |
"linux.deb.fullPackageName", |
|
94 |
String.class, |
|
95 |
params -> BUNDLE_NAME.fetchFrom(params) + "-" |
|
96 |
+ VERSION.fetchFrom(params), |
|
97 |
(s, p) -> s); |
|
98 |
||
99 |
public static final BundlerParamInfo<File> DEB_IMAGE_DIR = |
|
100 |
new StandardBundlerParam<>( |
|
101 |
"linux.deb.imageDir", |
|
102 |
File.class, |
|
103 |
params -> { |
|
104 |
File imagesRoot = IMAGES_ROOT.fetchFrom(params); |
|
105 |
if (!imagesRoot.exists()) imagesRoot.mkdirs(); |
|
106 |
return new File(new File(imagesRoot, "linux-deb.image"), |
|
107 |
FULL_PACKAGE_NAME.fetchFrom(params)); |
|
108 |
}, |
|
109 |
(s, p) -> new File(s)); |
|
110 |
||
111 |
public static final BundlerParamInfo<File> APP_IMAGE_ROOT = |
|
112 |
new StandardBundlerParam<>( |
|
113 |
"linux.deb.imageRoot", |
|
114 |
File.class, |
|
115 |
params -> { |
|
116 |
File imageDir = DEB_IMAGE_DIR.fetchFrom(params); |
|
117 |
return new File(imageDir, LINUX_INSTALL_DIR.fetchFrom(params)); |
|
118 |
}, |
|
119 |
(s, p) -> new File(s)); |
|
120 |
||
121 |
public static final BundlerParamInfo<File> CONFIG_DIR = |
|
122 |
new StandardBundlerParam<>( |
|
123 |
"linux.deb.configDir", |
|
124 |
File.class, |
|
125 |
params -> new File(DEB_IMAGE_DIR.fetchFrom(params), "DEBIAN"), |
|
126 |
(s, p) -> new File(s)); |
|
127 |
||
128 |
public static final BundlerParamInfo<String> EMAIL = |
|
129 |
new StandardBundlerParam<> ( |
|
57255
f686bda3b831
8215574: Investigate and document usage of --category, --copyright, --vendor and --description
herrick
parents:
57150
diff
changeset
|
130 |
Arguments.CLIOptions.LINUX_DEB_MAINTAINER.getId(), |
57038 | 131 |
String.class, |
132 |
params -> "Unknown", |
|
133 |
(s, p) -> s); |
|
134 |
||
135 |
public static final BundlerParamInfo<String> MAINTAINER = |
|
136 |
new StandardBundlerParam<> ( |
|
57255
f686bda3b831
8215574: Investigate and document usage of --category, --copyright, --vendor and --description
herrick
parents:
57150
diff
changeset
|
137 |
BundleParams.PARAM_MAINTAINER, |
57038 | 138 |
String.class, |
139 |
params -> VENDOR.fetchFrom(params) + " <" |
|
140 |
+ EMAIL.fetchFrom(params) + ">", |
|
141 |
(s, p) -> s); |
|
142 |
||
143 |
public static final BundlerParamInfo<String> LICENSE_TEXT = |
|
144 |
new StandardBundlerParam<> ( |
|
145 |
"linux.deb.licenseText", |
|
146 |
String.class, |
|
147 |
params -> { |
|
148 |
try { |
|
57080 | 149 |
String licenseFile = LICENSE_FILE.fetchFrom(params); |
150 |
if (licenseFile != null) { |
|
151 |
return Files.readString(new File(licenseFile).toPath()); |
|
57038 | 152 |
} |
153 |
} catch (Exception e) { |
|
57140 | 154 |
Log.verbose(e); |
57038 | 155 |
} |
156 |
return "Unknown"; |
|
157 |
}, |
|
158 |
(s, p) -> s); |
|
159 |
||
160 |
public static final BundlerParamInfo<String> XDG_FILE_PREFIX = |
|
161 |
new StandardBundlerParam<> ( |
|
162 |
"linux.xdg-prefix", |
|
163 |
String.class, |
|
164 |
params -> { |
|
165 |
try { |
|
166 |
String vendor; |
|
167 |
if (params.containsKey(VENDOR.getID())) { |
|
168 |
vendor = VENDOR.fetchFrom(params); |
|
169 |
} else { |
|
57039 | 170 |
vendor = "jpackage"; |
57038 | 171 |
} |
57119 | 172 |
String appName = APP_NAME.fetchFrom(params); |
57038 | 173 |
|
174 |
return (appName + "-" + vendor).replaceAll("\\s", ""); |
|
175 |
} catch (Exception e) { |
|
57140 | 176 |
Log.verbose(e); |
57038 | 177 |
} |
178 |
return "unknown-MimeInfo.xml"; |
|
179 |
}, |
|
180 |
(s, p) -> s); |
|
181 |
||
57256 | 182 |
public static final BundlerParamInfo<String> MENU_GROUP = |
183 |
new StandardBundlerParam<>( |
|
184 |
Arguments.CLIOptions.LINUX_MENU_GROUP.getId(), |
|
185 |
String.class, |
|
186 |
params -> I18N.getString("param.menu-group.default"), |
|
187 |
(s, p) -> s |
|
188 |
); |
|
189 |
||
57038 | 190 |
private final static String DEFAULT_ICON = "javalogo_white_32.png"; |
191 |
private final static String DEFAULT_CONTROL_TEMPLATE = "template.control"; |
|
192 |
private final static String DEFAULT_PRERM_TEMPLATE = "template.prerm"; |
|
193 |
private final static String DEFAULT_PREINSTALL_TEMPLATE = |
|
194 |
"template.preinst"; |
|
195 |
private final static String DEFAULT_POSTRM_TEMPLATE = "template.postrm"; |
|
196 |
private final static String DEFAULT_POSTINSTALL_TEMPLATE = |
|
197 |
"template.postinst"; |
|
198 |
private final static String DEFAULT_COPYRIGHT_TEMPLATE = |
|
199 |
"template.copyright"; |
|
200 |
private final static String DEFAULT_DESKTOP_FILE_TEMPLATE = |
|
201 |
"template.desktop"; |
|
202 |
||
203 |
public final static String TOOL_DPKG = "dpkg-deb"; |
|
204 |
||
205 |
public static boolean testTool(String toolName, String minVersion) { |
|
206 |
try { |
|
207 |
ProcessBuilder pb = new ProcessBuilder( |
|
208 |
toolName, |
|
209 |
"--version"); |
|
210 |
// not interested in the output |
|
57391 | 211 |
IOUtils.exec(pb, true, null); |
57038 | 212 |
} catch (Exception e) { |
213 |
Log.verbose(MessageFormat.format(I18N.getString( |
|
214 |
"message.test-for-tool"), toolName, e.getMessage())); |
|
215 |
return false; |
|
216 |
} |
|
217 |
return true; |
|
218 |
} |
|
219 |
||
220 |
@Override |
|
221 |
public boolean validate(Map<String, ? super Object> p) |
|
222 |
throws UnsupportedPlatformException, ConfigException { |
|
223 |
try { |
|
224 |
if (p == null) throw new ConfigException( |
|
225 |
I18N.getString("error.parameters-null"), |
|
226 |
I18N.getString("error.parameters-null.advice")); |
|
227 |
||
228 |
//run basic validation to ensure requirements are met |
|
229 |
//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
|
230 |
APP_BUNDLER.fetchFrom(p).validate(p); |
57038 | 231 |
|
232 |
// NOTE: Can we validate that the required tools are available |
|
233 |
// before we start? |
|
234 |
if (!testTool(TOOL_DPKG, "1")){ |
|
235 |
throw new ConfigException(MessageFormat.format( |
|
236 |
I18N.getString("error.tool-not-found"), TOOL_DPKG), |
|
237 |
I18N.getString("error.tool-not-found.advice")); |
|
238 |
} |
|
239 |
||
240 |
||
57080 | 241 |
// Show warning is license file is missing |
242 |
String licenseFile = LICENSE_FILE.fetchFrom(p); |
|
243 |
if (licenseFile == null) { |
|
57038 | 244 |
Log.verbose(I18N.getString("message.debs-like-licenses")); |
245 |
} |
|
246 |
||
247 |
// only one mime type per association, at least one file extention |
|
248 |
List<Map<String, ? super Object>> associations = |
|
249 |
FILE_ASSOCIATIONS.fetchFrom(p); |
|
250 |
if (associations != null) { |
|
251 |
for (int i = 0; i < associations.size(); i++) { |
|
252 |
Map<String, ? super Object> assoc = associations.get(i); |
|
253 |
List<String> mimes = FA_CONTENT_TYPE.fetchFrom(assoc); |
|
254 |
if (mimes == null || mimes.isEmpty()) { |
|
255 |
String msgKey = |
|
256 |
"error.no-content-types-for-file-association"; |
|
257 |
throw new ConfigException( |
|
258 |
MessageFormat.format(I18N.getString(msgKey), i), |
|
259 |
I18N.getString(msgKey + ".advise")); |
|
260 |
||
261 |
} else if (mimes.size() > 1) { |
|
262 |
String msgKey = |
|
263 |
"error.too-many-content-types-for-file-association"; |
|
264 |
throw new ConfigException( |
|
265 |
MessageFormat.format(I18N.getString(msgKey), i), |
|
266 |
I18N.getString(msgKey + ".advise")); |
|
267 |
} |
|
268 |
} |
|
269 |
} |
|
270 |
||
271 |
// bundle name has some restrictions |
|
272 |
// the string converter will throw an exception if invalid |
|
273 |
BUNDLE_NAME.getStringConverter().apply(BUNDLE_NAME.fetchFrom(p), p); |
|
274 |
||
275 |
return true; |
|
276 |
} catch (RuntimeException re) { |
|
277 |
if (re.getCause() instanceof ConfigException) { |
|
278 |
throw (ConfigException) re.getCause(); |
|
279 |
} else { |
|
280 |
throw new ConfigException(re); |
|
281 |
} |
|
282 |
} |
|
283 |
} |
|
284 |
||
285 |
private boolean prepareProto(Map<String, ? super Object> p) |
|
57140 | 286 |
throws PackagerException, IOException { |
57038 | 287 |
File appImage = StandardBundlerParam.getPredefinedAppImage(p); |
288 |
File appDir = null; |
|
289 |
||
290 |
// we either have an application image or need to build one |
|
291 |
if (appImage != null) { |
|
292 |
appDir = new File(APP_IMAGE_ROOT.fetchFrom(p), |
|
293 |
APP_NAME.fetchFrom(p)); |
|
294 |
// copy everything from appImage dir into appDir/name |
|
295 |
IOUtils.copyRecursive(appImage.toPath(), appDir.toPath()); |
|
296 |
} else { |
|
297 |
appDir = APP_BUNDLER.fetchFrom(p).doBundle(p, |
|
298 |
APP_IMAGE_ROOT.fetchFrom(p), true); |
|
299 |
} |
|
300 |
return appDir != null; |
|
301 |
} |
|
302 |
||
303 |
//@Override |
|
57140 | 304 |
public File bundle(Map<String, ? super Object> p, |
305 |
File outdir) throws PackagerException { |
|
57038 | 306 |
if (!outdir.isDirectory() && !outdir.mkdirs()) { |
57140 | 307 |
throw new PackagerException ("error.cannot-create-output-dir", |
308 |
outdir.getAbsolutePath()); |
|
57038 | 309 |
} |
310 |
if (!outdir.canWrite()) { |
|
57140 | 311 |
throw new PackagerException("error.cannot-write-to-output-dir", |
312 |
outdir.getAbsolutePath()); |
|
57038 | 313 |
} |
314 |
||
315 |
// we want to create following structure |
|
316 |
// <package-name> |
|
317 |
// DEBIAN |
|
318 |
// control (file with main package details) |
|
319 |
// menu (request to create menu) |
|
320 |
// ... other control files if needed .... |
|
321 |
// opt (by default) |
|
322 |
// AppFolder (this is where app image goes) |
|
323 |
// launcher executable |
|
324 |
// app |
|
325 |
// runtime |
|
326 |
||
327 |
File imageDir = DEB_IMAGE_DIR.fetchFrom(p); |
|
328 |
File configDir = CONFIG_DIR.fetchFrom(p); |
|
329 |
||
330 |
try { |
|
331 |
||
332 |
imageDir.mkdirs(); |
|
333 |
configDir.mkdirs(); |
|
334 |
if (prepareProto(p) && prepareProjectConfig(p)) { |
|
335 |
return buildDeb(p, outdir); |
|
336 |
} |
|
337 |
return null; |
|
338 |
} catch (IOException ex) { |
|
57140 | 339 |
Log.verbose(ex); |
340 |
throw new PackagerException(ex); |
|
57038 | 341 |
} |
342 |
} |
|
343 |
||
344 |
/* |
|
345 |
* set permissions with a string like "rwxr-xr-x" |
|
346 |
* |
|
347 |
* This cannot be directly backport to 22u which is built with 1.6 |
|
348 |
*/ |
|
349 |
private void setPermissions(File file, String permissions) { |
|
350 |
Set<PosixFilePermission> filePermissions = |
|
351 |
PosixFilePermissions.fromString(permissions); |
|
352 |
try { |
|
353 |
if (file.exists()) { |
|
354 |
Files.setPosixFilePermissions(file.toPath(), filePermissions); |
|
355 |
} |
|
356 |
} catch (IOException ex) { |
|
57392 | 357 |
Log.error(ex.getMessage()); |
358 |
Log.verbose(ex); |
|
57038 | 359 |
} |
360 |
||
361 |
} |
|
362 |
||
363 |
private String getArch() { |
|
364 |
String arch = System.getProperty("os.arch"); |
|
365 |
if ("i386".equals(arch)) |
|
366 |
return "i386"; |
|
367 |
else |
|
368 |
return "amd64"; |
|
369 |
} |
|
370 |
||
371 |
private long getInstalledSizeKB(Map<String, ? super Object> params) { |
|
372 |
return getInstalledSizeKB(APP_IMAGE_ROOT.fetchFrom(params)) >> 10; |
|
373 |
} |
|
374 |
||
375 |
private long getInstalledSizeKB(File dir) { |
|
376 |
long count = 0; |
|
377 |
File[] children = dir.listFiles(); |
|
378 |
if (children != null) { |
|
379 |
for (File file : children) { |
|
380 |
if (file.isFile()) { |
|
381 |
count += file.length(); |
|
382 |
} |
|
383 |
else if (file.isDirectory()) { |
|
384 |
count += getInstalledSizeKB(file); |
|
385 |
} |
|
386 |
} |
|
387 |
} |
|
388 |
return count; |
|
389 |
} |
|
390 |
||
391 |
private boolean prepareProjectConfig(Map<String, ? super Object> params) |
|
392 |
throws IOException { |
|
393 |
Map<String, String> data = createReplacementData(params); |
|
394 |
File rootDir = LinuxAppBundler.getRootDir(APP_IMAGE_ROOT.fetchFrom( |
|
395 |
params), params); |
|
396 |
||
397 |
File iconTarget = getConfig_IconFile(rootDir, params); |
|
398 |
File icon = ICON_PNG.fetchFrom(params); |
|
57256 | 399 |
if (!StandardBundlerParam.isRuntimeInstaller(params)) { |
57038 | 400 |
// prepare installer icon |
401 |
if (icon == null || !icon.exists()) { |
|
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
402 |
fetchResource(iconTarget.getName(), |
57038 | 403 |
I18N.getString("resource.menu-icon"), |
404 |
DEFAULT_ICON, |
|
405 |
iconTarget, |
|
406 |
VERBOSE.fetchFrom(params), |
|
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
407 |
RESOURCE_DIR.fetchFrom(params)); |
57038 | 408 |
} else { |
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
409 |
fetchResource(iconTarget.getName(), |
57038 | 410 |
I18N.getString("resource.menu-icon"), |
411 |
icon, |
|
412 |
iconTarget, |
|
413 |
VERBOSE.fetchFrom(params), |
|
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
414 |
RESOURCE_DIR.fetchFrom(params)); |
57038 | 415 |
} |
416 |
} |
|
417 |
||
418 |
StringBuilder installScripts = new StringBuilder(); |
|
419 |
StringBuilder removeScripts = new StringBuilder(); |
|
57256 | 420 |
for (Map<String, ? super Object> addLauncher : |
421 |
ADD_LAUNCHERS.fetchFrom(params)) { |
|
422 |
Map<String, String> addLauncherData = |
|
423 |
createReplacementData(addLauncher); |
|
424 |
addLauncherData.put("APPLICATION_FS_NAME", |
|
57038 | 425 |
data.get("APPLICATION_FS_NAME")); |
57256 | 426 |
addLauncherData.put("DESKTOP_MIMES", ""); |
57038 | 427 |
|
57256 | 428 |
if (!StandardBundlerParam.isRuntimeInstaller(params)) { |
57038 | 429 |
// prepare desktop shortcut |
57390 | 430 |
try (Writer w = Files.newBufferedWriter( |
57038 | 431 |
getConfig_DesktopShortcutFile( |
57390 | 432 |
rootDir, addLauncher).toPath())) { |
433 |
String content = preprocessTextResource( |
|
434 |
getConfig_DesktopShortcutFile(rootDir, |
|
435 |
addLauncher).getName(), |
|
436 |
I18N.getString("resource.menu-shortcut-descriptor"), |
|
437 |
DEFAULT_DESKTOP_FILE_TEMPLATE, |
|
438 |
addLauncherData, |
|
439 |
VERBOSE.fetchFrom(params), |
|
440 |
RESOURCE_DIR.fetchFrom(params)); |
|
441 |
w.write(content); |
|
442 |
} |
|
57038 | 443 |
} |
444 |
||
445 |
// prepare installer icon |
|
57256 | 446 |
iconTarget = getConfig_IconFile(rootDir, addLauncher); |
447 |
icon = ICON_PNG.fetchFrom(addLauncher); |
|
57038 | 448 |
if (icon == null || !icon.exists()) { |
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
449 |
fetchResource(iconTarget.getName(), |
57038 | 450 |
I18N.getString("resource.menu-icon"), |
451 |
DEFAULT_ICON, |
|
452 |
iconTarget, |
|
453 |
VERBOSE.fetchFrom(params), |
|
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
454 |
RESOURCE_DIR.fetchFrom(params)); |
57038 | 455 |
} else { |
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
456 |
fetchResource(iconTarget.getName(), |
57038 | 457 |
I18N.getString("resource.menu-icon"), |
458 |
icon, |
|
459 |
iconTarget, |
|
460 |
VERBOSE.fetchFrom(params), |
|
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
461 |
RESOURCE_DIR.fetchFrom(params)); |
57038 | 462 |
} |
463 |
||
464 |
// postinst copying of desktop icon |
|
465 |
installScripts.append( |
|
466 |
" xdg-desktop-menu install --novendor "); |
|
467 |
installScripts.append(LINUX_INSTALL_DIR.fetchFrom(params)); |
|
468 |
installScripts.append("/"); |
|
469 |
installScripts.append(data.get("APPLICATION_FS_NAME")); |
|
470 |
installScripts.append("/"); |
|
471 |
installScripts.append( |
|
57256 | 472 |
addLauncherData.get("APPLICATION_LAUNCHER_FILENAME")); |
57038 | 473 |
installScripts.append(".desktop\n"); |
474 |
||
57096
d06bec27f8c9
8215903: modify behavior of retaining temporary output dir
herrick
parents:
57091
diff
changeset
|
475 |
// postrm cleanup of desktop icon |
57038 | 476 |
removeScripts.append( |
477 |
" xdg-desktop-menu uninstall --novendor "); |
|
478 |
removeScripts.append(LINUX_INSTALL_DIR.fetchFrom(params)); |
|
479 |
removeScripts.append("/"); |
|
480 |
removeScripts.append(data.get("APPLICATION_FS_NAME")); |
|
481 |
removeScripts.append("/"); |
|
482 |
removeScripts.append( |
|
57256 | 483 |
addLauncherData.get("APPLICATION_LAUNCHER_FILENAME")); |
57038 | 484 |
removeScripts.append(".desktop\n"); |
485 |
} |
|
57256 | 486 |
data.put("ADD_LAUNCHERS_INSTALL", installScripts.toString()); |
487 |
data.put("ADD_LAUNCHERS_REMOVE", removeScripts.toString()); |
|
57038 | 488 |
|
489 |
List<Map<String, ? super Object>> associations = |
|
490 |
FILE_ASSOCIATIONS.fetchFrom(params); |
|
491 |
data.put("FILE_ASSOCIATION_INSTALL", ""); |
|
492 |
data.put("FILE_ASSOCIATION_REMOVE", ""); |
|
493 |
data.put("DESKTOP_MIMES", ""); |
|
494 |
if (associations != null) { |
|
495 |
String mimeInfoFile = XDG_FILE_PREFIX.fetchFrom(params) |
|
496 |
+ "-MimeInfo.xml"; |
|
497 |
StringBuilder mimeInfo = new StringBuilder( |
|
498 |
"<?xml version=\"1.0\"?>\n<mime-info xmlns=" |
|
499 |
+ "'http://www.freedesktop.org/standards/shared-mime-info'>\n"); |
|
500 |
StringBuilder registrations = new StringBuilder(); |
|
501 |
StringBuilder deregistrations = new StringBuilder(); |
|
502 |
StringBuilder desktopMimes = new StringBuilder("MimeType="); |
|
503 |
boolean addedEntry = false; |
|
504 |
||
505 |
for (Map<String, ? super Object> assoc : associations) { |
|
506 |
// <mime-type type="application/x-vnd.awesome"> |
|
507 |
// <comment>Awesome document</comment> |
|
508 |
// <glob pattern="*.awesome"/> |
|
509 |
// <glob pattern="*.awe"/> |
|
510 |
// </mime-type> |
|
511 |
||
512 |
if (assoc == null) { |
|
513 |
continue; |
|
514 |
} |
|
515 |
||
516 |
String description = FA_DESCRIPTION.fetchFrom(assoc); |
|
517 |
File faIcon = FA_ICON.fetchFrom(assoc); |
|
518 |
List<String> extensions = FA_EXTENSIONS.fetchFrom(assoc); |
|
519 |
if (extensions == null) { |
|
520 |
Log.error(I18N.getString( |
|
521 |
"message.creating-association-with-null-extension")); |
|
522 |
} |
|
523 |
||
524 |
List<String> mimes = FA_CONTENT_TYPE.fetchFrom(assoc); |
|
525 |
if (mimes == null || mimes.isEmpty()) { |
|
526 |
continue; |
|
527 |
} |
|
528 |
String thisMime = mimes.get(0); |
|
529 |
String dashMime = thisMime.replace('/', '-'); |
|
530 |
||
531 |
mimeInfo.append(" <mime-type type='") |
|
532 |
.append(thisMime) |
|
533 |
.append("'>\n"); |
|
534 |
if (description != null && !description.isEmpty()) { |
|
535 |
mimeInfo.append(" <comment>") |
|
536 |
.append(description) |
|
537 |
.append("</comment>\n"); |
|
538 |
} |
|
539 |
||
540 |
if (extensions != null) { |
|
541 |
for (String ext : extensions) { |
|
542 |
mimeInfo.append(" <glob pattern='*.") |
|
543 |
.append(ext) |
|
544 |
.append("'/>\n"); |
|
545 |
} |
|
546 |
} |
|
547 |
||
548 |
mimeInfo.append(" </mime-type>\n"); |
|
549 |
if (!addedEntry) { |
|
550 |
registrations.append(" xdg-mime install ") |
|
551 |
.append(LINUX_INSTALL_DIR.fetchFrom(params)) |
|
552 |
.append("/") |
|
553 |
.append(data.get("APPLICATION_FS_NAME")) |
|
554 |
.append("/") |
|
555 |
.append(mimeInfoFile) |
|
556 |
.append("\n"); |
|
557 |
||
558 |
deregistrations.append(" xdg-mime uninstall ") |
|
559 |
.append(LINUX_INSTALL_DIR.fetchFrom(params)) |
|
560 |
.append("/") |
|
561 |
.append(data.get("APPLICATION_FS_NAME")) |
|
562 |
.append("/") |
|
563 |
.append(mimeInfoFile) |
|
564 |
.append("\n"); |
|
565 |
addedEntry = true; |
|
566 |
} else { |
|
567 |
desktopMimes.append(";"); |
|
568 |
} |
|
569 |
desktopMimes.append(thisMime); |
|
570 |
||
571 |
if (faIcon != null && faIcon.exists()) { |
|
572 |
int size = getSquareSizeOfImage(faIcon); |
|
573 |
||
574 |
if (size > 0) { |
|
575 |
File target = new File(rootDir, |
|
57119 | 576 |
APP_NAME.fetchFrom(params) |
57038 | 577 |
+ "_fa_" + faIcon.getName()); |
578 |
IOUtils.copyFile(faIcon, target); |
|
579 |
||
580 |
// xdg-icon-resource install --context mimetypes |
|
581 |
// --size 64 awesomeapp_fa_1.png |
|
582 |
// application-x.vnd-awesome |
|
583 |
registrations.append( |
|
584 |
" xdg-icon-resource install " |
|
585 |
+ "--context mimetypes --size ") |
|
586 |
.append(size) |
|
587 |
.append(" ") |
|
588 |
.append(LINUX_INSTALL_DIR.fetchFrom(params)) |
|
589 |
.append("/") |
|
590 |
.append(data.get("APPLICATION_FS_NAME")) |
|
591 |
.append("/") |
|
592 |
.append(target.getName()) |
|
593 |
.append(" ") |
|
594 |
.append(dashMime) |
|
595 |
.append("\n"); |
|
596 |
||
597 |
// x dg-icon-resource uninstall --context mimetypes |
|
598 |
// --size 64 awesomeapp_fa_1.png |
|
599 |
// application-x.vnd-awesome |
|
600 |
deregistrations.append( |
|
601 |
" xdg-icon-resource uninstall " |
|
602 |
+ "--context mimetypes --size ") |
|
603 |
.append(size) |
|
604 |
.append(" ") |
|
605 |
.append(LINUX_INSTALL_DIR.fetchFrom(params)) |
|
606 |
.append("/") |
|
607 |
.append(data.get("APPLICATION_FS_NAME")) |
|
608 |
.append("/") |
|
609 |
.append(target.getName()) |
|
610 |
.append(" ") |
|
611 |
.append(dashMime) |
|
612 |
.append("\n"); |
|
613 |
} |
|
614 |
} |
|
615 |
} |
|
616 |
mimeInfo.append("</mime-info>"); |
|
617 |
||
618 |
if (addedEntry) { |
|
57390 | 619 |
try (Writer w = Files.newBufferedWriter( |
620 |
new File(rootDir, mimeInfoFile).toPath())) { |
|
621 |
w.write(mimeInfo.toString()); |
|
622 |
} |
|
57038 | 623 |
data.put("FILE_ASSOCIATION_INSTALL", registrations.toString()); |
624 |
data.put("FILE_ASSOCIATION_REMOVE", deregistrations.toString()); |
|
625 |
data.put("DESKTOP_MIMES", desktopMimes.toString()); |
|
626 |
} |
|
627 |
} |
|
628 |
||
57256 | 629 |
if (!StandardBundlerParam.isRuntimeInstaller(params)) { |
57038 | 630 |
//prepare desktop shortcut |
57390 | 631 |
try (Writer w = Files.newBufferedWriter( |
632 |
getConfig_DesktopShortcutFile(rootDir, params).toPath())) { |
|
633 |
String content = preprocessTextResource( |
|
634 |
getConfig_DesktopShortcutFile( |
|
635 |
rootDir, params).getName(), |
|
636 |
I18N.getString("resource.menu-shortcut-descriptor"), |
|
637 |
DEFAULT_DESKTOP_FILE_TEMPLATE, |
|
638 |
data, |
|
639 |
VERBOSE.fetchFrom(params), |
|
640 |
RESOURCE_DIR.fetchFrom(params)); |
|
641 |
w.write(content); |
|
642 |
} |
|
643 |
} |
|
644 |
// prepare control file |
|
645 |
try (Writer w = Files.newBufferedWriter( |
|
646 |
getConfig_ControlFile(params).toPath())) { |
|
57038 | 647 |
String content = preprocessTextResource( |
57390 | 648 |
getConfig_ControlFile(params).getName(), |
649 |
I18N.getString("resource.deb-control-file"), |
|
650 |
DEFAULT_CONTROL_TEMPLATE, |
|
651 |
data, |
|
652 |
VERBOSE.fetchFrom(params), |
|
653 |
RESOURCE_DIR.fetchFrom(params)); |
|
654 |
w.write(content); |
|
655 |
} |
|
656 |
||
657 |
try (Writer w = Files.newBufferedWriter( |
|
658 |
getConfig_PreinstallFile(params).toPath())) { |
|
659 |
String content = preprocessTextResource( |
|
660 |
getConfig_PreinstallFile(params).getName(), |
|
661 |
I18N.getString("resource.deb-preinstall-script"), |
|
662 |
DEFAULT_PREINSTALL_TEMPLATE, |
|
57038 | 663 |
data, |
664 |
VERBOSE.fetchFrom(params), |
|
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57080
diff
changeset
|
665 |
RESOURCE_DIR.fetchFrom(params)); |
57038 | 666 |
w.write(content); |
667 |
} |
|
668 |
setPermissions(getConfig_PreinstallFile(params), "rwxr-xr-x"); |
|
669 |
||
57390 | 670 |
try (Writer w = Files.newBufferedWriter( |
671 |
getConfig_PrermFile(params).toPath())) { |
|
672 |
String content = preprocessTextResource( |
|
673 |
getConfig_PrermFile(params).getName(), |
|
674 |
I18N.getString("resource.deb-prerm-script"), |
|
675 |
DEFAULT_PRERM_TEMPLATE, |
|
676 |
data, |
|
677 |
VERBOSE.fetchFrom(params), |
|
678 |
RESOURCE_DIR.fetchFrom(params)); |
|
679 |
w.write(content); |
|
680 |
} |
|
57038 | 681 |
setPermissions(getConfig_PrermFile(params), "rwxr-xr-x"); |
682 |
||
57390 | 683 |
try (Writer w = Files.newBufferedWriter( |
684 |
getConfig_PostinstallFile(params).toPath())) { |
|
685 |
String content = preprocessTextResource( |
|
686 |
getConfig_PostinstallFile(params).getName(), |
|
687 |
I18N.getString("resource.deb-postinstall-script"), |
|
688 |
DEFAULT_POSTINSTALL_TEMPLATE, |
|
689 |
data, |
|
690 |
VERBOSE.fetchFrom(params), |
|
691 |
RESOURCE_DIR.fetchFrom(params)); |
|
692 |
w.write(content); |
|
693 |
} |
|
57038 | 694 |
setPermissions(getConfig_PostinstallFile(params), "rwxr-xr-x"); |
695 |
||
57390 | 696 |
try (Writer w = Files.newBufferedWriter( |
697 |
getConfig_PostrmFile(params).toPath())) { |
|
698 |
String content = preprocessTextResource( |
|
699 |
getConfig_PostrmFile(params).getName(), |
|
700 |
I18N.getString("resource.deb-postrm-script"), |
|
701 |
DEFAULT_POSTRM_TEMPLATE, |
|
702 |
data, |
|
703 |
VERBOSE.fetchFrom(params), |
|
704 |
RESOURCE_DIR.fetchFrom(params)); |
|
705 |
w.write(content); |
|
706 |
} |
|
57038 | 707 |
setPermissions(getConfig_PostrmFile(params), "rwxr-xr-x"); |
708 |
||
57390 | 709 |
try (Writer w = Files.newBufferedWriter( |
710 |
getConfig_CopyrightFile(params).toPath())) { |
|
711 |
String content = preprocessTextResource( |
|
712 |
getConfig_CopyrightFile(params).getName(), |
|
713 |
I18N.getString("resource.deb-copyright-file"), |
|
714 |
DEFAULT_COPYRIGHT_TEMPLATE, |
|
715 |
data, |
|
716 |
VERBOSE.fetchFrom(params), |
|
717 |
RESOURCE_DIR.fetchFrom(params)); |
|
718 |
w.write(content); |
|
719 |
} |
|
57038 | 720 |
|
721 |
return true; |
|
722 |
} |
|
723 |
||
724 |
private Map<String, String> createReplacementData( |
|
725 |
Map<String, ? super Object> params) { |
|
726 |
Map<String, String> data = new HashMap<>(); |
|
727 |
||
728 |
data.put("APPLICATION_NAME", APP_NAME.fetchFrom(params)); |
|
57119 | 729 |
data.put("APPLICATION_FS_NAME", APP_NAME.fetchFrom(params)); |
57038 | 730 |
data.put("APPLICATION_PACKAGE", BUNDLE_NAME.fetchFrom(params)); |
731 |
data.put("APPLICATION_VENDOR", VENDOR.fetchFrom(params)); |
|
732 |
data.put("APPLICATION_MAINTAINER", MAINTAINER.fetchFrom(params)); |
|
733 |
data.put("APPLICATION_VERSION", VERSION.fetchFrom(params)); |
|
57119 | 734 |
data.put("APPLICATION_LAUNCHER_FILENAME", APP_NAME.fetchFrom(params)); |
57038 | 735 |
data.put("INSTALLATION_DIRECTORY", LINUX_INSTALL_DIR.fetchFrom(params)); |
736 |
data.put("XDG_PREFIX", XDG_FILE_PREFIX.fetchFrom(params)); |
|
57256 | 737 |
data.put("DEPLOY_BUNDLE_CATEGORY", MENU_GROUP.fetchFrom(params)); |
57038 | 738 |
data.put("APPLICATION_DESCRIPTION", DESCRIPTION.fetchFrom(params)); |
739 |
data.put("APPLICATION_COPYRIGHT", COPYRIGHT.fetchFrom(params)); |
|
740 |
data.put("APPLICATION_LICENSE_TEXT", LICENSE_TEXT.fetchFrom(params)); |
|
741 |
data.put("APPLICATION_ARCH", getArch()); |
|
742 |
data.put("APPLICATION_INSTALLED_SIZE", |
|
743 |
Long.toString(getInstalledSizeKB(params))); |
|
744 |
String deps = LINUX_PACKAGE_DEPENDENCIES.fetchFrom(params); |
|
745 |
data.put("PACKAGE_DEPENDENCIES", |
|
746 |
deps.isEmpty() ? "" : "Depends: " + deps); |
|
57256 | 747 |
data.put("RUNTIME_INSTALLER", "" + |
748 |
StandardBundlerParam.isRuntimeInstaller(params)); |
|
57038 | 749 |
|
750 |
return data; |
|
751 |
} |
|
752 |
||
753 |
private File getConfig_DesktopShortcutFile(File rootDir, |
|
754 |
Map<String, ? super Object> params) { |
|
57119 | 755 |
return new File(rootDir, APP_NAME.fetchFrom(params) + ".desktop"); |
57038 | 756 |
} |
757 |
||
758 |
private File getConfig_IconFile(File rootDir, |
|
759 |
Map<String, ? super Object> params) { |
|
57119 | 760 |
return new File(rootDir, APP_NAME.fetchFrom(params) + ".png"); |
57038 | 761 |
} |
762 |
||
763 |
private File getConfig_InitScriptFile(Map<String, ? super Object> params) { |
|
764 |
return new File(LinuxAppBundler.getRootDir( |
|
765 |
APP_IMAGE_ROOT.fetchFrom(params), params), |
|
766 |
BUNDLE_NAME.fetchFrom(params) + ".init"); |
|
767 |
} |
|
768 |
||
769 |
private File getConfig_ControlFile(Map<String, ? super Object> params) { |
|
770 |
return new File(CONFIG_DIR.fetchFrom(params), "control"); |
|
771 |
} |
|
772 |
||
773 |
private File getConfig_PreinstallFile(Map<String, ? super Object> params) { |
|
774 |
return new File(CONFIG_DIR.fetchFrom(params), "preinst"); |
|
775 |
} |
|
776 |
||
777 |
private File getConfig_PrermFile(Map<String, ? super Object> params) { |
|
778 |
return new File(CONFIG_DIR.fetchFrom(params), "prerm"); |
|
779 |
} |
|
780 |
||
781 |
private File getConfig_PostinstallFile(Map<String, ? super Object> params) { |
|
782 |
return new File(CONFIG_DIR.fetchFrom(params), "postinst"); |
|
783 |
} |
|
784 |
||
785 |
private File getConfig_PostrmFile(Map<String, ? super Object> params) { |
|
786 |
return new File(CONFIG_DIR.fetchFrom(params), "postrm"); |
|
787 |
} |
|
788 |
||
789 |
private File getConfig_CopyrightFile(Map<String, ? super Object> params) { |
|
790 |
return new File(CONFIG_DIR.fetchFrom(params), "copyright"); |
|
791 |
} |
|
792 |
||
793 |
private File buildDeb(Map<String, ? super Object> params, |
|
794 |
File outdir) throws IOException { |
|
795 |
File outFile = new File(outdir, |
|
796 |
FULL_PACKAGE_NAME.fetchFrom(params)+".deb"); |
|
797 |
Log.verbose(MessageFormat.format(I18N.getString( |
|
798 |
"message.outputting-to-location"), outFile.getAbsolutePath())); |
|
799 |
||
800 |
outFile.getParentFile().mkdirs(); |
|
801 |
||
802 |
// run dpkg |
|
803 |
ProcessBuilder pb = new ProcessBuilder( |
|
804 |
"fakeroot", TOOL_DPKG, "-b", |
|
805 |
FULL_PACKAGE_NAME.fetchFrom(params), |
|
806 |
outFile.getAbsolutePath()); |
|
807 |
pb = pb.directory(DEB_IMAGE_DIR.fetchFrom(params).getParentFile()); |
|
57391 | 808 |
IOUtils.exec(pb); |
57038 | 809 |
|
810 |
Log.verbose(MessageFormat.format(I18N.getString( |
|
811 |
"message.output-to-location"), outFile.getAbsolutePath())); |
|
812 |
||
813 |
return outFile; |
|
814 |
} |
|
815 |
||
816 |
@Override |
|
817 |
public String getName() { |
|
57059 | 818 |
return I18N.getString("deb.bundler.name"); |
57038 | 819 |
} |
820 |
||
821 |
@Override |
|
822 |
public String getDescription() { |
|
57059 | 823 |
return I18N.getString("deb.bundler.description"); |
57038 | 824 |
} |
825 |
||
826 |
@Override |
|
827 |
public String getID() { |
|
828 |
return "deb"; |
|
829 |
} |
|
830 |
||
831 |
@Override |
|
832 |
public String getBundleType() { |
|
833 |
return "INSTALLER"; |
|
834 |
} |
|
835 |
||
836 |
@Override |
|
837 |
public Collection<BundlerParamInfo<?>> getBundleParameters() { |
|
838 |
Collection<BundlerParamInfo<?>> results = new LinkedHashSet<>(); |
|
839 |
results.addAll(LinuxAppBundler.getAppBundleParameters()); |
|
840 |
results.addAll(getDebBundleParameters()); |
|
841 |
return results; |
|
842 |
} |
|
843 |
||
844 |
public static Collection<BundlerParamInfo<?>> getDebBundleParameters() { |
|
845 |
return Arrays.asList( |
|
846 |
BUNDLE_NAME, |
|
847 |
COPYRIGHT, |
|
57256 | 848 |
MENU_GROUP, |
57038 | 849 |
DESCRIPTION, |
850 |
EMAIL, |
|
851 |
ICON_PNG, |
|
852 |
LICENSE_FILE, |
|
853 |
VENDOR |
|
854 |
); |
|
855 |
} |
|
856 |
||
857 |
@Override |
|
858 |
public File execute(Map<String, ? super Object> params, |
|
57140 | 859 |
File outputParentDir) throws PackagerException { |
57038 | 860 |
return bundle(params, outputParentDir); |
861 |
} |
|
862 |
||
863 |
@Override |
|
57150 | 864 |
public boolean supported(boolean runtimeInstaller) { |
57038 | 865 |
return (Platform.getPlatform() == Platform.LINUX); |
866 |
} |
|
867 |
||
868 |
public int getSquareSizeOfImage(File f) { |
|
869 |
try { |
|
870 |
BufferedImage bi = ImageIO.read(f); |
|
871 |
if (bi.getWidth() == bi.getHeight()) { |
|
872 |
return bi.getWidth(); |
|
873 |
} else { |
|
874 |
return 0; |
|
875 |
} |
|
876 |
} catch (Exception e) { |
|
57140 | 877 |
Log.verbose(e); |
57038 | 878 |
return 0; |
879 |
} |
|
880 |
} |
|
881 |
} |