< prev index next >

src/jdk.jpackage/linux/classes/jdk/jpackage/internal/LinuxDebBundler.java

Print this page




  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 
  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;
  44 
  45 public class LinuxDebBundler extends AbstractBundler {
  46 
  47     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  48                     "jdk.jpackage.internal.resources.LinuxResources");
  49 
  50     public static final BundlerParamInfo<LinuxAppBundler> APP_BUNDLER =
  51             new StandardBundlerParam<>(
  52             I18N.getString("param.deb-app-bundler.name"),
  53             I18N.getString("param.deb-app-bundler.description"),
  54             "linux.app.bundler",
  55             LinuxAppBundler.class,
  56             params -> new LinuxAppBundler(),
  57             (s, p) -> null);
  58 
  59     // Debian rules for package naming are used here
  60     // https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Source
  61     //
  62     // Package names must consist only of lower case letters (a-z),
  63     // digits (0-9), plus (+) and minus (-) signs, and periods (.).
  64     // They must be at least two characters long and
  65     // must start with an alphanumeric character.
  66     //
  67     private static final Pattern DEB_BUNDLE_NAME_PATTERN =
  68             Pattern.compile("^[a-z][a-z\\d\\+\\-\\.]+");
  69 
  70     public static final BundlerParamInfo<String> BUNDLE_NAME =
  71             new StandardBundlerParam<> (
  72             I18N.getString("param.bundle-name.name"),
  73             I18N.getString("param.bundle-name.description"),
  74             Arguments.CLIOptions.LINUX_BUNDLE_NAME.getId(),
  75             String.class,
  76             params -> {
  77                 String nm = APP_NAME.fetchFrom(params);
  78 
  79                 if (nm == null) return null;
  80 
  81                 // make sure to lower case and spaces/underscores become dashes
  82                 nm = nm.toLowerCase().replaceAll("[ _]", "-");
  83                 return nm;
  84             },
  85             (s, p) -> {
  86                 if (!DEB_BUNDLE_NAME_PATTERN.matcher(s).matches()) {
  87                     throw new IllegalArgumentException(new ConfigException(
  88                             MessageFormat.format(I18N.getString(
  89                             "error.invalid-value-for-package-name"), s),
  90                             I18N.getString(
  91                             "error.invalid-value-for-package-name.advice")));
  92                 }
  93 
  94                 return s;
  95             });
  96 
  97     public static final BundlerParamInfo<String> FULL_PACKAGE_NAME =
  98             new StandardBundlerParam<> (
  99             I18N.getString("param.full-package-name.name"),
 100             I18N.getString("param.full-package-name.description"),
 101             "linux.deb.fullPackageName",
 102             String.class,
 103             params -> BUNDLE_NAME.fetchFrom(params) + "-"
 104                     + VERSION.fetchFrom(params),
 105             (s, p) -> s);
 106 
 107     public static final BundlerParamInfo<File> DEB_IMAGE_DIR =
 108             new StandardBundlerParam<>(
 109             I18N.getString("param.image-dir.name"),
 110             I18N.getString("param.image-dir.description"),
 111             "linux.deb.imageDir",
 112             File.class,
 113             params -> {
 114                 File imagesRoot = IMAGES_ROOT.fetchFrom(params);
 115                 if (!imagesRoot.exists()) imagesRoot.mkdirs();
 116                 return new File(new File(imagesRoot, "linux-deb.image"),
 117                         FULL_PACKAGE_NAME.fetchFrom(params));
 118             },
 119             (s, p) -> new File(s));
 120 
 121     public static final BundlerParamInfo<File> APP_IMAGE_ROOT =
 122             new StandardBundlerParam<>(
 123             I18N.getString("param.app-image-root.name"),
 124             I18N.getString("param.app-image-root.description"),
 125             "linux.deb.imageRoot",
 126             File.class,
 127             params -> {
 128                 File imageDir = DEB_IMAGE_DIR.fetchFrom(params);
 129                 return new File(imageDir, LINUX_INSTALL_DIR.fetchFrom(params));
 130             },
 131             (s, p) -> new File(s));
 132 
 133     public static final BundlerParamInfo<File> CONFIG_DIR =
 134             new StandardBundlerParam<>(
 135             I18N.getString("param.config-dir.name"),
 136             I18N.getString("param.config-dir.description"),
 137             "linux.deb.configDir",
 138             File.class,
 139             params ->  new File(DEB_IMAGE_DIR.fetchFrom(params), "DEBIAN"),
 140             (s, p) -> new File(s));
 141 
 142     public static final BundlerParamInfo<String> EMAIL =
 143             new StandardBundlerParam<> (
 144             I18N.getString("param.maintainer-email.name"),
 145             I18N.getString("param.maintainer-email.description"),
 146             BundleParams.PARAM_EMAIL,
 147             String.class,
 148             params -> "Unknown",
 149             (s, p) -> s);
 150 
 151     public static final BundlerParamInfo<String> MAINTAINER =
 152             new StandardBundlerParam<> (
 153             I18N.getString("param.maintainer-name.name"),
 154             I18N.getString("param.maintainer-name.description"),
 155             Arguments.CLIOptions.LINUX_DEB_MAINTAINER.getId(),
 156             String.class,
 157             params -> VENDOR.fetchFrom(params) + " <"
 158                     + EMAIL.fetchFrom(params) + ">",
 159             (s, p) -> s);
 160 
 161     public static final BundlerParamInfo<String> LICENSE_TEXT =
 162             new StandardBundlerParam<> (
 163             I18N.getString("param.license-text.name"),
 164             I18N.getString("param.license-text.description"),
 165             "linux.deb.licenseText",
 166             String.class,
 167             params -> {
 168                 try {
 169                     String licenseFile = LICENSE_FILE.fetchFrom(params);
 170                     if (licenseFile != null) {
 171                         return Files.readString(new File(licenseFile).toPath());
 172                     }
 173                 } catch (Exception e) {
 174                     Log.verbose(e);
 175                 }
 176                 return "Unknown";
 177             },
 178             (s, p) -> s);
 179 
 180     public static final BundlerParamInfo<String> XDG_FILE_PREFIX =
 181             new StandardBundlerParam<> (
 182             I18N.getString("param.xdg-prefix.name"),
 183             I18N.getString("param.xdg-prefix.description"),
 184             "linux.xdg-prefix",
 185             String.class,
 186             params -> {
 187                 try {
 188                     String vendor;
 189                     if (params.containsKey(VENDOR.getID())) {
 190                         vendor = VENDOR.fetchFrom(params);
 191                     } else {
 192                         vendor = "jpackage";
 193                     }
 194                     String appName = APP_NAME.fetchFrom(params);
 195 
 196                     return (appName + "-" + vendor).replaceAll("\\s", "");
 197                 } catch (Exception e) {
 198                     Log.verbose(e);
 199                 }
 200                 return "unknown-MimeInfo.xml";
 201             },
 202             (s, p) -> s);
 203 








 204     private final static String DEFAULT_ICON = "javalogo_white_32.png";
 205     private final static String DEFAULT_CONTROL_TEMPLATE = "template.control";
 206     private final static String DEFAULT_PRERM_TEMPLATE = "template.prerm";
 207     private final static String DEFAULT_PREINSTALL_TEMPLATE =
 208             "template.preinst";
 209     private final static String DEFAULT_POSTRM_TEMPLATE = "template.postrm";
 210     private final static String DEFAULT_POSTINSTALL_TEMPLATE =
 211             "template.postinst";
 212     private final static String DEFAULT_COPYRIGHT_TEMPLATE =
 213             "template.copyright";
 214     private final static String DEFAULT_DESKTOP_FILE_TEMPLATE =
 215             "template.desktop";
 216 
 217     public final static String TOOL_DPKG = "dpkg-deb";
 218 
 219     public static boolean testTool(String toolName, String minVersion) {
 220         try {
 221             ProcessBuilder pb = new ProcessBuilder(
 222                     toolName,
 223                     "--version");


 393             for (File file : children) {
 394                 if (file.isFile()) {
 395                     count += file.length();
 396                 }
 397                 else if (file.isDirectory()) {
 398                     count += getInstalledSizeKB(file);
 399                 }
 400             }
 401         }
 402         return count;
 403     }
 404 
 405     private boolean prepareProjectConfig(Map<String, ? super Object> params)
 406             throws IOException {
 407         Map<String, String> data = createReplacementData(params);
 408         File rootDir = LinuxAppBundler.getRootDir(APP_IMAGE_ROOT.fetchFrom(
 409                 params), params);
 410 
 411         File iconTarget = getConfig_IconFile(rootDir, params);
 412         File icon = ICON_PNG.fetchFrom(params);
 413         if (!RUNTIME_INSTALLER.fetchFrom(params)) {
 414             // prepare installer icon
 415             if (icon == null || !icon.exists()) {
 416                 fetchResource(iconTarget.getName(),
 417                         I18N.getString("resource.menu-icon"),
 418                         DEFAULT_ICON,
 419                         iconTarget,
 420                         VERBOSE.fetchFrom(params),
 421                         RESOURCE_DIR.fetchFrom(params));
 422             } else {
 423                 fetchResource(iconTarget.getName(),
 424                         I18N.getString("resource.menu-icon"),
 425                         icon,
 426                         iconTarget,
 427                         VERBOSE.fetchFrom(params),
 428                         RESOURCE_DIR.fetchFrom(params));
 429             }
 430         }
 431 
 432         StringBuilder installScripts = new StringBuilder();
 433         StringBuilder removeScripts = new StringBuilder();
 434         for (Map<String, ? super Object> secondaryLauncher :
 435                 SECONDARY_LAUNCHERS.fetchFrom(params)) {
 436             Map<String, String> secondaryLauncherData =
 437                     createReplacementData(secondaryLauncher);
 438             secondaryLauncherData.put("APPLICATION_FS_NAME",
 439                     data.get("APPLICATION_FS_NAME"));
 440             secondaryLauncherData.put("DESKTOP_MIMES", "");
 441 
 442             if (!RUNTIME_INSTALLER.fetchFrom(params)) {
 443                 // prepare desktop shortcut
 444                 Writer w = new BufferedWriter(new FileWriter(
 445                         getConfig_DesktopShortcutFile(
 446                                 rootDir, secondaryLauncher)));
 447                 String content = preprocessTextResource(
 448                         getConfig_DesktopShortcutFile(rootDir,
 449                         secondaryLauncher).getName(),
 450                         I18N.getString("resource.menu-shortcut-descriptor"),
 451                         DEFAULT_DESKTOP_FILE_TEMPLATE,
 452                         secondaryLauncherData,
 453                         VERBOSE.fetchFrom(params),
 454                         RESOURCE_DIR.fetchFrom(params));
 455                 w.write(content);
 456                 w.close();
 457             }
 458 
 459             // prepare installer icon
 460             iconTarget = getConfig_IconFile(rootDir, secondaryLauncher);
 461             icon = ICON_PNG.fetchFrom(secondaryLauncher);
 462             if (icon == null || !icon.exists()) {
 463                 fetchResource(iconTarget.getName(),
 464                         I18N.getString("resource.menu-icon"),
 465                         DEFAULT_ICON,
 466                         iconTarget,
 467                         VERBOSE.fetchFrom(params),
 468                         RESOURCE_DIR.fetchFrom(params));
 469             } else {
 470                 fetchResource(iconTarget.getName(),
 471                         I18N.getString("resource.menu-icon"),
 472                         icon,
 473                         iconTarget,
 474                         VERBOSE.fetchFrom(params),
 475                         RESOURCE_DIR.fetchFrom(params));
 476             }
 477 
 478             // postinst copying of desktop icon
 479             installScripts.append(
 480                     "        xdg-desktop-menu install --novendor ");
 481             installScripts.append(LINUX_INSTALL_DIR.fetchFrom(params));
 482             installScripts.append("/");
 483             installScripts.append(data.get("APPLICATION_FS_NAME"));
 484             installScripts.append("/");
 485             installScripts.append(
 486                     secondaryLauncherData.get("APPLICATION_LAUNCHER_FILENAME"));
 487             installScripts.append(".desktop\n");
 488 
 489             // postrm cleanup of desktop icon
 490             removeScripts.append(
 491                     "        xdg-desktop-menu uninstall --novendor ");
 492             removeScripts.append(LINUX_INSTALL_DIR.fetchFrom(params));
 493             removeScripts.append("/");
 494             removeScripts.append(data.get("APPLICATION_FS_NAME"));
 495             removeScripts.append("/");
 496             removeScripts.append(
 497                     secondaryLauncherData.get("APPLICATION_LAUNCHER_FILENAME"));
 498             removeScripts.append(".desktop\n");
 499         }
 500         data.put("SECONDARY_LAUNCHERS_INSTALL", installScripts.toString());
 501         data.put("SECONDARY_LAUNCHERS_REMOVE", removeScripts.toString());
 502 
 503         List<Map<String, ? super Object>> associations =
 504                 FILE_ASSOCIATIONS.fetchFrom(params);
 505         data.put("FILE_ASSOCIATION_INSTALL", "");
 506         data.put("FILE_ASSOCIATION_REMOVE", "");
 507         data.put("DESKTOP_MIMES", "");
 508         if (associations != null) {
 509             String mimeInfoFile = XDG_FILE_PREFIX.fetchFrom(params)
 510                     + "-MimeInfo.xml";
 511             StringBuilder mimeInfo = new StringBuilder(
 512                 "<?xml version=\"1.0\"?>\n<mime-info xmlns="
 513                 + "'http://www.freedesktop.org/standards/shared-mime-info'>\n");
 514             StringBuilder registrations = new StringBuilder();
 515             StringBuilder deregistrations = new StringBuilder();
 516             StringBuilder desktopMimes = new StringBuilder("MimeType=");
 517             boolean addedEntry = false;
 518 
 519             for (Map<String, ? super Object> assoc : associations) {
 520                 //  <mime-type type="application/x-vnd.awesome">
 521                 //    <comment>Awesome document</comment>


 623                                 .append(target.getName())
 624                                 .append(" ")
 625                                 .append(dashMime)
 626                                 .append("\n");
 627                     }
 628                 }
 629             }
 630             mimeInfo.append("</mime-info>");
 631 
 632             if (addedEntry) {
 633                 Writer w = new BufferedWriter(new FileWriter(
 634                         new File(rootDir, mimeInfoFile)));
 635                 w.write(mimeInfo.toString());
 636                 w.close();
 637                 data.put("FILE_ASSOCIATION_INSTALL", registrations.toString());
 638                 data.put("FILE_ASSOCIATION_REMOVE", deregistrations.toString());
 639                 data.put("DESKTOP_MIMES", desktopMimes.toString());
 640             }
 641         }
 642 
 643         if (!RUNTIME_INSTALLER.fetchFrom(params)) {
 644             //prepare desktop shortcut
 645             Writer w = new BufferedWriter(new FileWriter(
 646                     getConfig_DesktopShortcutFile(rootDir, params)));
 647             String content = preprocessTextResource(
 648                     getConfig_DesktopShortcutFile(
 649                     rootDir, params).getName(),
 650                     I18N.getString("resource.menu-shortcut-descriptor"),
 651                     DEFAULT_DESKTOP_FILE_TEMPLATE,
 652                     data,
 653                     VERBOSE.fetchFrom(params),
 654                     RESOURCE_DIR.fetchFrom(params));
 655             w.write(content);
 656             w.close();
 657         }
 658         // prepare control file
 659         Writer w = new BufferedWriter(new FileWriter(
 660                 getConfig_ControlFile(params)));
 661         String content = preprocessTextResource(
 662                 getConfig_ControlFile(params).getName(),
 663                 I18N.getString("resource.deb-control-file"),


 728                 RESOURCE_DIR.fetchFrom(params));
 729         w.write(content);
 730         w.close();
 731 
 732         return true;
 733     }
 734 
 735     private Map<String, String> createReplacementData(
 736             Map<String, ? super Object> params) {
 737         Map<String, String> data = new HashMap<>();
 738 
 739         data.put("APPLICATION_NAME", APP_NAME.fetchFrom(params));
 740         data.put("APPLICATION_FS_NAME", APP_NAME.fetchFrom(params));
 741         data.put("APPLICATION_PACKAGE", BUNDLE_NAME.fetchFrom(params));
 742         data.put("APPLICATION_VENDOR", VENDOR.fetchFrom(params));
 743         data.put("APPLICATION_MAINTAINER", MAINTAINER.fetchFrom(params));
 744         data.put("APPLICATION_VERSION", VERSION.fetchFrom(params));
 745         data.put("APPLICATION_LAUNCHER_FILENAME", APP_NAME.fetchFrom(params));
 746         data.put("INSTALLATION_DIRECTORY", LINUX_INSTALL_DIR.fetchFrom(params));
 747         data.put("XDG_PREFIX", XDG_FILE_PREFIX.fetchFrom(params));
 748         data.put("DEPLOY_BUNDLE_CATEGORY", CATEGORY.fetchFrom(params));
 749         data.put("APPLICATION_DESCRIPTION", DESCRIPTION.fetchFrom(params));
 750         data.put("APPLICATION_SUMMARY", TITLE.fetchFrom(params));
 751         data.put("APPLICATION_COPYRIGHT", COPYRIGHT.fetchFrom(params));
 752         data.put("APPLICATION_LICENSE_TEXT", LICENSE_TEXT.fetchFrom(params));
 753         data.put("APPLICATION_ARCH", getArch());
 754         data.put("APPLICATION_INSTALLED_SIZE",
 755                 Long.toString(getInstalledSizeKB(params)));
 756         String deps = LINUX_PACKAGE_DEPENDENCIES.fetchFrom(params);
 757         data.put("PACKAGE_DEPENDENCIES",
 758                 deps.isEmpty() ? "" : "Depends: " + deps);
 759         data.put("RUNTIME_INSTALLER",
 760                 RUNTIME_INSTALLER.fetchFrom(params).toString());
 761 
 762         return data;
 763     }
 764 
 765     private File getConfig_DesktopShortcutFile(File rootDir,
 766             Map<String, ? super Object> params) {
 767         return new File(rootDir, APP_NAME.fetchFrom(params) + ".desktop");
 768     }
 769 
 770     private File getConfig_IconFile(File rootDir,
 771             Map<String, ? super Object> params) {
 772         return new File(rootDir, APP_NAME.fetchFrom(params) + ".png");
 773     }
 774 
 775     private File getConfig_InitScriptFile(Map<String, ? super Object> params) {
 776         return new File(LinuxAppBundler.getRootDir(
 777                 APP_IMAGE_ROOT.fetchFrom(params), params),
 778                         BUNDLE_NAME.fetchFrom(params) + ".init");
 779     }
 780 


 840         return "deb";
 841     }
 842 
 843     @Override
 844     public String getBundleType() {
 845         return "INSTALLER";
 846     }
 847 
 848     @Override
 849     public Collection<BundlerParamInfo<?>> getBundleParameters() {
 850         Collection<BundlerParamInfo<?>> results = new LinkedHashSet<>();
 851         results.addAll(LinuxAppBundler.getAppBundleParameters());
 852         results.addAll(getDebBundleParameters());
 853         return results;
 854     }
 855 
 856     public static Collection<BundlerParamInfo<?>> getDebBundleParameters() {
 857         return Arrays.asList(
 858                 BUNDLE_NAME,
 859                 COPYRIGHT,
 860                 CATEGORY,
 861                 DESCRIPTION,
 862                 EMAIL,
 863                 ICON_PNG,
 864                 LICENSE_FILE,
 865                 TITLE,
 866                 VENDOR
 867         );
 868     }
 869 
 870     @Override
 871     public File execute(Map<String, ? super Object> params,
 872             File outputParentDir) throws PackagerException {
 873         return bundle(params, outputParentDir);
 874     }
 875 
 876     @Override
 877     public boolean supported(boolean runtimeInstaller) {
 878         return (Platform.getPlatform() == Platform.LINUX);
 879     }
 880 


  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 
  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;
  44 
  45 public class LinuxDebBundler extends AbstractBundler {
  46 
  47     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  48                     "jdk.jpackage.internal.resources.LinuxResources");
  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<> (


 132             BundleParams.PARAM_EMAIL,
 133             String.class,
 134             params -> "Unknown",
 135             (s, p) -> s);
 136 
 137     public static final BundlerParamInfo<String> MAINTAINER =
 138             new StandardBundlerParam<> (


 139             Arguments.CLIOptions.LINUX_DEB_MAINTAINER.getId(),
 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 {
 151                     String licenseFile = LICENSE_FILE.fetchFrom(params);
 152                     if (licenseFile != null) {
 153                         return Files.readString(new File(licenseFile).toPath());
 154                     }
 155                 } catch (Exception e) {
 156                     Log.verbose(e);
 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 {
 172                         vendor = "jpackage";
 173                     }
 174                     String appName = APP_NAME.fetchFrom(params);
 175 
 176                     return (appName + "-" + vendor).replaceAll("\\s", "");
 177                 } catch (Exception e) {
 178                     Log.verbose(e);
 179                 }
 180                 return "unknown-MimeInfo.xml";
 181             },
 182             (s, p) -> s);
 183 
 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 
 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");


 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);
 401         if (!StandardBundlerParam.isRuntimeInstaller(params)) {
 402             // prepare installer icon
 403             if (icon == null || !icon.exists()) {
 404                 fetchResource(iconTarget.getName(),
 405                         I18N.getString("resource.menu-icon"),
 406                         DEFAULT_ICON,
 407                         iconTarget,
 408                         VERBOSE.fetchFrom(params),
 409                         RESOURCE_DIR.fetchFrom(params));
 410             } else {
 411                 fetchResource(iconTarget.getName(),
 412                         I18N.getString("resource.menu-icon"),
 413                         icon,
 414                         iconTarget,
 415                         VERBOSE.fetchFrom(params),
 416                         RESOURCE_DIR.fetchFrom(params));
 417             }
 418         }
 419 
 420         StringBuilder installScripts = new StringBuilder();
 421         StringBuilder removeScripts = new StringBuilder();
 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",
 427                     data.get("APPLICATION_FS_NAME"));
 428             addLauncherData.put("DESKTOP_MIMES", "");
 429 
 430             if (!StandardBundlerParam.isRuntimeInstaller(params)) {
 431                 // prepare desktop shortcut
 432                 Writer w = new BufferedWriter(new FileWriter(
 433                         getConfig_DesktopShortcutFile(
 434                                 rootDir, addLauncher)));
 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                 w.close();
 445             }
 446 
 447             // prepare installer icon
 448             iconTarget = getConfig_IconFile(rootDir, addLauncher);
 449             icon = ICON_PNG.fetchFrom(addLauncher);
 450             if (icon == null || !icon.exists()) {
 451                 fetchResource(iconTarget.getName(),
 452                         I18N.getString("resource.menu-icon"),
 453                         DEFAULT_ICON,
 454                         iconTarget,
 455                         VERBOSE.fetchFrom(params),
 456                         RESOURCE_DIR.fetchFrom(params));
 457             } else {
 458                 fetchResource(iconTarget.getName(),
 459                         I18N.getString("resource.menu-icon"),
 460                         icon,
 461                         iconTarget,
 462                         VERBOSE.fetchFrom(params),
 463                         RESOURCE_DIR.fetchFrom(params));
 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(
 474                     addLauncherData.get("APPLICATION_LAUNCHER_FILENAME"));
 475             installScripts.append(".desktop\n");
 476 
 477             // postrm cleanup of desktop icon
 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(
 485                     addLauncherData.get("APPLICATION_LAUNCHER_FILENAME"));
 486             removeScripts.append(".desktop\n");
 487         }
 488         data.put("ADD_LAUNCHERS_INSTALL", installScripts.toString());
 489         data.put("ADD_LAUNCHERS_REMOVE", removeScripts.toString());
 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>


 611                                 .append(target.getName())
 612                                 .append(" ")
 613                                 .append(dashMime)
 614                                 .append("\n");
 615                     }
 616                 }
 617             }
 618             mimeInfo.append("</mime-info>");
 619 
 620             if (addedEntry) {
 621                 Writer w = new BufferedWriter(new FileWriter(
 622                         new File(rootDir, mimeInfoFile)));
 623                 w.write(mimeInfo.toString());
 624                 w.close();
 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 
 631         if (!StandardBundlerParam.isRuntimeInstaller(params)) {
 632             //prepare desktop shortcut
 633             Writer w = new BufferedWriter(new FileWriter(
 634                     getConfig_DesktopShortcutFile(rootDir, params)));
 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             w.close();
 645         }
 646         // prepare control file
 647         Writer w = new BufferedWriter(new FileWriter(
 648                 getConfig_ControlFile(params)));
 649         String content = preprocessTextResource(
 650                 getConfig_ControlFile(params).getName(),
 651                 I18N.getString("resource.deb-control-file"),


 716                 RESOURCE_DIR.fetchFrom(params));
 717         w.write(content);
 718         w.close();
 719 
 720         return true;
 721     }
 722 
 723     private Map<String, String> createReplacementData(
 724             Map<String, ? super Object> params) {
 725         Map<String, String> data = new HashMap<>();
 726 
 727         data.put("APPLICATION_NAME", APP_NAME.fetchFrom(params));
 728         data.put("APPLICATION_FS_NAME", APP_NAME.fetchFrom(params));
 729         data.put("APPLICATION_PACKAGE", BUNDLE_NAME.fetchFrom(params));
 730         data.put("APPLICATION_VENDOR", VENDOR.fetchFrom(params));
 731         data.put("APPLICATION_MAINTAINER", MAINTAINER.fetchFrom(params));
 732         data.put("APPLICATION_VERSION", VERSION.fetchFrom(params));
 733         data.put("APPLICATION_LAUNCHER_FILENAME", APP_NAME.fetchFrom(params));
 734         data.put("INSTALLATION_DIRECTORY", LINUX_INSTALL_DIR.fetchFrom(params));
 735         data.put("XDG_PREFIX", XDG_FILE_PREFIX.fetchFrom(params));
 736         data.put("DEPLOY_BUNDLE_CATEGORY", MENU_GROUP.fetchFrom(params));
 737         data.put("APPLICATION_DESCRIPTION", DESCRIPTION.fetchFrom(params));
 738         data.put("APPLICATION_SUMMARY", TITLE.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);
 747         data.put("RUNTIME_INSTALLER", "" +
 748                 StandardBundlerParam.isRuntimeInstaller(params));
 749 
 750         return data;
 751     }
 752 
 753     private File getConfig_DesktopShortcutFile(File rootDir,
 754             Map<String, ? super Object> params) {
 755         return new File(rootDir, APP_NAME.fetchFrom(params) + ".desktop");
 756     }
 757 
 758     private File getConfig_IconFile(File rootDir,
 759             Map<String, ? super Object> params) {
 760         return new File(rootDir, APP_NAME.fetchFrom(params) + ".png");
 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 


 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,
 848                 MENU_GROUP,
 849                 DESCRIPTION,
 850                 EMAIL,
 851                 ICON_PNG,
 852                 LICENSE_FILE,
 853                 TITLE,
 854                 VENDOR
 855         );
 856     }
 857 
 858     @Override
 859     public File execute(Map<String, ? super Object> params,
 860             File outputParentDir) throws PackagerException {
 861         return bundle(params, outputParentDir);
 862     }
 863 
 864     @Override
 865     public boolean supported(boolean runtimeInstaller) {
 866         return (Platform.getPlatform() == Platform.LINUX);
 867     }
 868 
< prev index next >