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