src/jdk.jpackage/linux/classes/jdk/jpackage/internal/LinuxPackageBundler.java
author herrick
Tue, 24 Sep 2019 13:41:16 -0400
branchJDK-8200758-branch
changeset 58301 e0efb29609bd
child 58302 718bd56695b3
permissions -rw-r--r--
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code Submitted-by: asemenyuk Reviewed-by: herrick, almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     1
/*
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     4
 *
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    10
 *
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    15
 * accompanied this code).
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    16
 *
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    20
 *
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    23
 * questions.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    24
 */
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    25
package jdk.jpackage.internal;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    26
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    27
import java.awt.image.BufferedImage;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    28
import java.io.BufferedReader;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    29
import java.io.BufferedWriter;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    30
import java.io.File;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    31
import java.io.FileWriter;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    32
import java.io.IOException;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    33
import java.io.InputStream;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    34
import java.io.InputStreamReader;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    35
import java.io.Writer;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    36
import java.nio.file.Files;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    37
import java.nio.file.Path;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    38
import java.text.MessageFormat;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    39
import java.util.ArrayList;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    40
import java.util.Arrays;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    41
import java.util.Collections;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    42
import java.util.HashMap;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    43
import java.util.List;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    44
import java.util.Map;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    45
import java.util.ResourceBundle;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    46
import java.util.stream.Collectors;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    47
import java.util.stream.Stream;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    48
import javax.imageio.ImageIO;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    49
import javax.xml.stream.XMLOutputFactory;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    50
import javax.xml.stream.XMLStreamException;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    51
import javax.xml.stream.XMLStreamWriter;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    52
import static jdk.jpackage.internal.LinuxAppBundler.LINUX_INSTALL_DIR;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    53
import static jdk.jpackage.internal.LinuxAppBundler.LINUX_PACKAGE_DEPENDENCIES;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    54
import static jdk.jpackage.internal.LinuxAppImageBuilder.DEFAULT_ICON;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    55
import static jdk.jpackage.internal.LinuxAppImageBuilder.ICON_PNG;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    56
import static jdk.jpackage.internal.StandardBundlerParam.*;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    57
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    58
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    59
abstract class LinuxPackageBundler extends AbstractBundler {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    60
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    61
    protected static final ResourceBundle I18N = ResourceBundle.getBundle(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    62
            "jdk.jpackage.internal.resources.LinuxResources");
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    63
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    64
    private static final String DESKTOP_COMMANDS_INSTALL = "DESKTOP_COMMANDS_INSTALL";
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    65
    private static final String DESKTOP_COMMANDS_UNINSTALL = "DESKTOP_COMMANDS_UNINSTALL";
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    66
    private static final String UTILITY_SCRIPTS = "UTILITY_SCRIPTS";
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    67
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    68
    private static final BundlerParamInfo<LinuxAppBundler> APP_BUNDLER =
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    69
        new StandardBundlerParam<>(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    70
                "linux.app.bundler",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    71
                LinuxAppBundler.class,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    72
                (params) -> new LinuxAppBundler(),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    73
                null
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    74
        );
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    75
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    76
    private static final BundlerParamInfo<String> MENU_GROUP =
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    77
        new StandardBundlerParam<>(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    78
                Arguments.CLIOptions.LINUX_MENU_GROUP.getId(),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    79
                String.class,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    80
                params -> I18N.getString("param.menu-group.default"),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    81
                (s, p) -> s
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    82
        );
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    83
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    84
    private static final StandardBundlerParam<Boolean> SHORTCUT_HINT =
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    85
        new StandardBundlerParam<>(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    86
                Arguments.CLIOptions.LINUX_SHORTCUT_HINT.getId(),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    87
                Boolean.class,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    88
                params -> false,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    89
                (s, p) -> (s == null || "null".equalsIgnoreCase(s))
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    90
                        ? false : Boolean.valueOf(s)
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    91
        );
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    92
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    93
    LinuxPackageBundler(BundlerParamInfo<String> packageName) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    94
        this.packageName = packageName;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    95
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    96
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    97
    private final BundlerParamInfo<String> packageName;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    98
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    99
    @Override
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   100
    final public boolean validate(Map<String, ? super Object> params)
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   101
            throws ConfigException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   102
        try {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   103
            if (params == null) throw new ConfigException(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   104
                    I18N.getString("error.parameters-null"),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   105
                    I18N.getString("error.parameters-null.advice"));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   106
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   107
            // run basic validation to ensure requirements are met
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   108
            // we are not interested in return code, only possible exception
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   109
            APP_BUNDLER.fetchFrom(params).validate(params);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   110
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   111
            validateFileAssociations(FILE_ASSOCIATIONS.fetchFrom(params));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   112
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   113
            // If package name has some restrictions, the string converter will
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   114
            // throw an exception if invalid
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   115
            packageName.getStringConverter().apply(packageName.fetchFrom(params),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   116
                params);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   117
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   118
            // Packaging specific validation
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   119
            doValidate(params);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   120
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   121
            return true;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   122
        } catch (RuntimeException re) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   123
            if (re.getCause() instanceof ConfigException) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   124
                throw (ConfigException) re.getCause();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   125
            } else {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   126
                throw new ConfigException(re);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   127
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   128
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   129
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   130
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   131
    @Override
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   132
    final public String getBundleType() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   133
        return "INSTALLER";
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   134
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   135
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   136
    @Override
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   137
    final public File execute(Map<String, ? super Object> params,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   138
            File outputParentDir) throws PackagerException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   139
        IOUtils.writableOutputDir(outputParentDir.toPath());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   140
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   141
        PlatformPackage thePackage = createMetaPackage(params);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   142
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   143
        try {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   144
            File appImage = StandardBundlerParam.getPredefinedAppImage(params);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   145
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   146
            // we either have an application image or need to build one
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   147
            if (appImage != null) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   148
                appImageLayout(params).resolveAt(appImage.toPath()).copy(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   149
                        thePackage.sourceApplicationLayout());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   150
            } else {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   151
                appImage = APP_BUNDLER.fetchFrom(params).doBundle(params,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   152
                        thePackage.sourceRoot().toFile(), true);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   153
                ApplicationLayout srcAppLayout = appImageLayout(params).resolveAt(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   154
                        appImage.toPath());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   155
                if (appImage.equals(PREDEFINED_RUNTIME_IMAGE.fetchFrom(params))) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   156
                    // Application image points to run-time image.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   157
                    // Copy it.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   158
                    srcAppLayout.copy(thePackage.sourceApplicationLayout());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   159
                } else {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   160
                    // Application image is a newly created directory tree.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   161
                    // Move it.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   162
                    srcAppLayout.move(thePackage.sourceApplicationLayout());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   163
                    if (appImage.exists()) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   164
                        // Empty app image directory might remain after all application
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   165
                        // directories have been moved.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   166
                        appImage.delete();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   167
                    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   168
                }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   169
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   170
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   171
            Map<String, String> data = createDefaultReplacementData(params);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   172
            if (StandardBundlerParam.isRuntimeInstaller(params)) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   173
                Stream.of(DESKTOP_COMMANDS_INSTALL, DESKTOP_COMMANDS_UNINSTALL,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   174
                        UTILITY_SCRIPTS).forEach(v -> data.put(v, ""));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   175
            } else {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   176
                data.putAll(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   177
                        new DesktopIntegration(thePackage, params).prepareForApplication());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   178
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   179
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   180
            data.putAll(createReplacementData(params));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   181
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   182
            return buildPackageBundle(Collections.unmodifiableMap(data), params,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   183
                    outputParentDir);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   184
        } catch (IOException ex) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   185
            Log.verbose(ex);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   186
            throw new PackagerException(ex);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   187
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   188
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   189
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   190
    private Map<String, String> createDefaultReplacementData(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   191
            Map<String, ? super Object> params) throws IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   192
        Map<String, String> data = new HashMap<>();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   193
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   194
        data.put("APPLICATION_PACKAGE", createMetaPackage(params).name());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   195
        data.put("APPLICATION_VENDOR", VENDOR.fetchFrom(params));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   196
        data.put("APPLICATION_VERSION", VERSION.fetchFrom(params));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   197
        data.put("APPLICATION_DESCRIPTION", DESCRIPTION.fetchFrom(params));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   198
        data.put("APPLICATION_RELEASE", RELEASE.fetchFrom(params));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   199
        data.put("PACKAGE_DEPENDENCIES", LINUX_PACKAGE_DEPENDENCIES.fetchFrom(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   200
                params));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   201
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   202
        return data;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   203
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   204
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   205
    abstract void doValidate(Map<String, ? super Object> params)
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   206
            throws ConfigException;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   207
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   208
    abstract protected Map<String, String> createReplacementData(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   209
            Map<String, ? super Object> params) throws IOException;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   210
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   211
    abstract protected File buildPackageBundle(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   212
            Map<String, String> replacementData,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   213
            Map<String, ? super Object> params, File outputParentDir) throws
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   214
            PackagerException, IOException;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   215
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   216
    final protected PlatformPackage createMetaPackage(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   217
            Map<String, ? super Object> params) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   218
        return new PlatformPackage() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   219
            @Override
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   220
            public String name() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   221
                return packageName.fetchFrom(params);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   222
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   223
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   224
            @Override
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   225
            public Path sourceRoot() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   226
                return IMAGES_ROOT.fetchFrom(params).toPath().toAbsolutePath();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   227
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   228
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   229
            @Override
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   230
            public ApplicationLayout sourceApplicationLayout() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   231
                return appImageLayout(params).resolveAt(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   232
                        applicationInstallDir(sourceRoot()));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   233
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   234
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   235
            @Override
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   236
            public ApplicationLayout installedApplicationLayout() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   237
                return appImageLayout(params).resolveAt(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   238
                        applicationInstallDir(Path.of("/")));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   239
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   240
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   241
            private Path applicationInstallDir(Path root) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   242
                Path installDir = Path.of(LINUX_INSTALL_DIR.fetchFrom(params),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   243
                        name());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   244
                if (installDir.isAbsolute()) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   245
                    installDir = Path.of("." + installDir.toString()).normalize();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   246
                }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   247
                return root.resolve(installDir);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   248
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   249
        };
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   250
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   251
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   252
    private ApplicationLayout appImageLayout(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   253
            Map<String, ? super Object> params) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   254
        if (StandardBundlerParam.isRuntimeInstaller(params)) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   255
            return ApplicationLayout.javaRuntime();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   256
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   257
        return ApplicationLayout.unixApp();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   258
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   259
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   260
    private static void validateFileAssociations(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   261
            List<Map<String, ? super Object>> associations) throws
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   262
            ConfigException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   263
        // only one mime type per association, at least one file extention
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   264
        int assocIdx = 0;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   265
        for (var assoc : associations) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   266
            ++assocIdx;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   267
            List<String> mimes = FA_CONTENT_TYPE.fetchFrom(assoc);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   268
            if (mimes == null || mimes.isEmpty()) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   269
                String msgKey = "error.no-content-types-for-file-association";
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   270
                throw new ConfigException(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   271
                        MessageFormat.format(I18N.getString(msgKey), assocIdx),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   272
                        I18N.getString(msgKey + ".advise"));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   273
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   274
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   275
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   276
            if (mimes.size() > 1) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   277
                String msgKey = "error.too-many-content-types-for-file-association";
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   278
                throw new ConfigException(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   279
                        MessageFormat.format(I18N.getString(msgKey), assocIdx),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   280
                        I18N.getString(msgKey + ".advise"));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   281
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   282
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   283
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   284
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   285
    /**
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   286
     * Helper to create files for desktop integration.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   287
     */
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   288
    private class DesktopIntegration {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   289
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   290
        DesktopIntegration(PlatformPackage thePackage,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   291
                Map<String, ? super Object> params) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   292
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   293
            associations = FILE_ASSOCIATIONS.fetchFrom(params).stream().filter(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   294
                    a -> {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   295
                        if (a == null) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   296
                            return false;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   297
                        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   298
                        List<String> mimes = FA_CONTENT_TYPE.fetchFrom(a);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   299
                        return (mimes != null && !mimes.isEmpty());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   300
                    }).collect(Collectors.toUnmodifiableList());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   302
            launchers = ADD_LAUNCHERS.fetchFrom(params);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   303
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   304
            this.thePackage = thePackage;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   305
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   306
            customIconFile = ICON_PNG.fetchFrom(params);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   307
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   308
            verbose = VERBOSE.fetchFrom(params);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   309
            resourceDir = RESOURCE_DIR.fetchFrom(params);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   310
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   311
            // XDG recommends to use vendor prefix in desktop file names as xdg
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   312
            // commands copy files to system directories.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   313
            // Package name should be a good prefix.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   314
            final String desktopFileName = String.format("%s-%s.desktop",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   315
                        thePackage.name(), APP_NAME.fetchFrom(params));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   316
            final String mimeInfoFileName = String.format("%s-%s-MimeInfo.xml",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   317
                        thePackage.name(), APP_NAME.fetchFrom(params));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   318
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   319
            mimeInfoFile = new DesktopFile(mimeInfoFileName);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   320
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   321
            if (!associations.isEmpty() || SHORTCUT_HINT.fetchFrom(params) || customIconFile != null) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   322
                //
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   323
                // Create primary .desktop file if one of conditions is met:
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   324
                // - there are file associations configured
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   325
                // - user explicitely requested to create a shortcut
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   326
                // - custom icon specified
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   327
                //
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   328
                desktopFile = new DesktopFile(desktopFileName);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   329
                iconFile = new DesktopFile(String.format("%s.png",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   330
                        APP_NAME.fetchFrom(params)));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   331
            } else {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   332
                desktopFile = null;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   333
                iconFile = null;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   334
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   335
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   336
            this.desktopFileData = Collections.unmodifiableMap(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   337
                    createDataForDesktopFile(params));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   338
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   339
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   340
        Map<String, String> prepareForApplication() throws IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   341
            if (iconFile != null) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   342
                // Create application icon file.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   343
                prepareSrcIconFile();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   344
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   345
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   346
            Map<String, String> data = new HashMap<>(desktopFileData);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   347
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   348
            final ShellCommands shellCommands;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   349
            if (desktopFile != null) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   350
                // Create application desktop description file.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   351
                createDesktopFile(data);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   352
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   353
                // Shell commands will be created only if desktop file
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   354
                // should be installed.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   355
                shellCommands = new ShellCommands();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   356
            } else {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   357
                shellCommands = null;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   358
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   359
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   360
            if (!associations.isEmpty()) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   361
                // Create XML file with mime types corresponding to file associations.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   362
                createFileAssociationsMimeInfoFile();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   363
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   364
                shellCommands.setFileAssociations();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   365
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   366
                // Create icon files corresponding to file associations
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   367
                Map<String, Path> mimeTypeWithIconFile = createFileAssociationIconFiles();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   368
                mimeTypeWithIconFile.forEach((k, v) -> {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   369
                    shellCommands.addIcon(k, v);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   370
                });
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   371
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   372
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   373
            // Create shell commands to install/uninstall integration with desktop of the app.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   374
            if (shellCommands != null) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   375
                shellCommands.applyTo(data);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   376
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   377
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   378
            boolean needCleanupScripts = !associations.isEmpty();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   379
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   380
            // Take care of additional launchers if there are any.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   381
            // Process every additional launcher as the main application launcher.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   382
            // Collect shell commands to install/uninstall integration with desktop
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   383
            // of the additional launchers and append them to the corresponding
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   384
            // commands of the main launcher.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   385
            List<String> installShellCmds = new ArrayList<>(Arrays.asList(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   386
                    data.get(DESKTOP_COMMANDS_INSTALL)));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   387
            List<String> uninstallShellCmds = new ArrayList<>(Arrays.asList(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   388
                    data.get(DESKTOP_COMMANDS_UNINSTALL)));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   389
            for (Map<String, ? super Object> params : launchers) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   390
                DesktopIntegration integration = new DesktopIntegration(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   391
                        thePackage, params);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   392
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   393
                if (!integration.associations.isEmpty()) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   394
                    needCleanupScripts = true;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   395
                }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   396
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   397
                Map<String, String> launcherData = integration.prepareForApplication();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   398
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   399
                installShellCmds.add(launcherData.get(DESKTOP_COMMANDS_INSTALL));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   400
                uninstallShellCmds.add(launcherData.get(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   401
                        DESKTOP_COMMANDS_UNINSTALL));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   402
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   403
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   404
            data.put(DESKTOP_COMMANDS_INSTALL, stringifyShellCommands(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   405
                    installShellCmds));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   406
            data.put(DESKTOP_COMMANDS_UNINSTALL, stringifyShellCommands(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   407
                    uninstallShellCmds));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   408
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   409
            if (needCleanupScripts) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   410
                // Pull in utils.sh scrips library.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   411
                try (InputStream is = getResourceAsStream("utils.sh");
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   412
                        InputStreamReader isr = new InputStreamReader(is);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   413
                        BufferedReader reader = new BufferedReader(isr)) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   414
                    data.put(UTILITY_SCRIPTS, reader.lines().collect(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   415
                            Collectors.joining(System.lineSeparator())));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   416
                }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   417
            } else {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   418
                data.put(UTILITY_SCRIPTS, "");
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   419
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   420
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   421
            return data;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   422
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   423
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   424
        private Map<String, String> createDataForDesktopFile(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   425
                Map<String, ? super Object> params) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   426
            Map<String, String> data = new HashMap<>();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   427
            data.put("APPLICATION_NAME", APP_NAME.fetchFrom(params));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   428
            data.put("APPLICATION_DESCRIPTION", DESCRIPTION.fetchFrom(params));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   429
            data.put("APPLICATION_ICON",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   430
                    iconFile != null ? iconFile.installPath().toString() : null);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   431
            data.put("DEPLOY_BUNDLE_CATEGORY", MENU_GROUP.fetchFrom(params));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   432
            data.put("APPLICATION_LAUNCHER",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   433
                    thePackage.installedApplicationLayout().launchersDirectory().resolve(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   434
                            LinuxAppImageBuilder.getLauncherName(params)).toString());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   435
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   436
            return data;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   437
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   438
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   439
        /**
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   440
         * Shell commands to integrate something with desktop.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   441
         */
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   442
        private class ShellCommands {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   443
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   444
            ShellCommands() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   445
                registerIconCmds = new ArrayList<>();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   446
                unregisterIconCmds = new ArrayList<>();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   447
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   448
                registerDesktopFileCmd = String.join(" ", "xdg-desktop-menu",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   449
                        "install", desktopFile.installPath().toString());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   450
                unregisterDesktopFileCmd = String.join(" ", "xdg-desktop-menu",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   451
                        "uninstall", desktopFile.installPath().toString());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   452
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   453
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   454
            void setFileAssociations() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   455
                registerFileAssociationsCmd = String.join(" ", "xdg-mime",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   456
                        "install",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   457
                        mimeInfoFile.installPath().toString());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   458
                unregisterFileAssociationsCmd = String.join(" ", "xdg-mime",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   459
                        "uninstall", mimeInfoFile.installPath().toString());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   460
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   461
                //
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   462
                // Add manual cleanup of system files to get rid of
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   463
                // the default mime type handlers.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   464
                //
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   465
                // Even after mime type is unregisterd with `xdg-mime uninstall`
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   466
                // command and desktop file deleted with `xdg-desktop-menu uninstall`
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   467
                // command, records in
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   468
                // `/usr/share/applications/defaults.list` (Ubuntu 16) or
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   469
                // `/usr/local/share/applications/defaults.list` (OracleLinux 7)
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   470
                // files remain referencing deleted mime time and deleted
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   471
                // desktop file which makes `xdg-mime query default` output name
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   472
                // of non-existing desktop file.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   473
                //
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   474
                String cleanUpCommand = String.join(" ",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   475
                        "uninstall_default_mime_handler",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   476
                        desktopFile.installPath().getFileName().toString(),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   477
                        String.join(" ", getMimeTypeNamesFromFileAssociations()));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   478
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   479
                unregisterFileAssociationsCmd = stringifyShellCommands(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   480
                        unregisterFileAssociationsCmd, cleanUpCommand);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   481
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   482
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   483
            void addIcon(String mimeType, Path iconFile) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   484
                final int imgSize = getSquareSizeOfImage(iconFile.toFile());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   485
                final String dashMime = mimeType.replace('/', '-');
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   486
                registerIconCmds.add(String.join(" ", "xdg-icon-resource",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   487
                        "install", "--context", "mimetypes", "--size ",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   488
                        Integer.toString(imgSize), iconFile.toString(), dashMime));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   489
                unregisterIconCmds.add(String.join(" ", "xdg-icon-resource",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   490
                        "uninstall", dashMime));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   491
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   492
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   493
            void applyTo(Map<String, String> data) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   494
                List<String> cmds = new ArrayList<>();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   495
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   496
                cmds.add(registerDesktopFileCmd);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   497
                cmds.add(registerFileAssociationsCmd);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   498
                cmds.addAll(registerIconCmds);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   499
                data.put(DESKTOP_COMMANDS_INSTALL, stringifyShellCommands(cmds));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   500
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   501
                cmds.clear();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   502
                cmds.add(unregisterDesktopFileCmd);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   503
                cmds.add(unregisterFileAssociationsCmd);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   504
                cmds.addAll(unregisterIconCmds);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   505
                data.put(DESKTOP_COMMANDS_UNINSTALL, stringifyShellCommands(cmds));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   506
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   507
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   508
            private String registerDesktopFileCmd;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   509
            private String unregisterDesktopFileCmd;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   510
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   511
            private String registerFileAssociationsCmd;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   512
            private String unregisterFileAssociationsCmd;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   513
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   514
            private List<String> registerIconCmds;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   515
            private List<String> unregisterIconCmds;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   516
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   517
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   518
        private final PlatformPackage thePackage;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   519
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   520
        private final List<Map<String, ? super Object>> associations;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   521
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   522
        private final List<Map<String, ? super Object>> launchers;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   523
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   524
        /**
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   525
         * Desktop integration file. xml, icon, etc.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   526
         * Resides somewhere in application installation tree.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   527
         * Has two paths:
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   528
         *  - path where it should be placed at package build time;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   529
         *  - path where it should be installed by package manager;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   530
         */
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   531
        private class DesktopFile {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   532
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   533
            DesktopFile(String fileName) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   534
                installPath = thePackage
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   535
                        .installedApplicationLayout()
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   536
                        .destktopIntegrationDirectory().resolve(fileName);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   537
                srcPath = thePackage
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   538
                        .sourceApplicationLayout()
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   539
                        .destktopIntegrationDirectory().resolve(fileName);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   540
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   541
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   542
            private final Path installPath;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   543
            private final Path srcPath;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   544
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   545
            Path installPath() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   546
                return installPath;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   547
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   548
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   549
            Path srcPath() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   550
                return srcPath;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   551
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   552
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   553
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   554
        private final boolean verbose;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   555
        private final File resourceDir;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   556
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   557
        private final DesktopFile mimeInfoFile;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   558
        private final DesktopFile desktopFile;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   559
        private final DesktopFile iconFile;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   560
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   561
        private final Map<String, String> desktopFileData;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   562
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   563
        /**
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   564
         * Path to icon file provided by user or null.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   565
         */
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   566
        private final File customIconFile;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   567
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   568
        private void appendFileAssociation(XMLStreamWriter xml,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   569
                Map<String, ? super Object> assoc) throws XMLStreamException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   570
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   571
            xml.writeStartElement("mime-type");
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   572
            final String thisMime = FA_CONTENT_TYPE.fetchFrom(assoc).get(0);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   573
            xml.writeAttribute("type", thisMime);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   574
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   575
            final String description = FA_DESCRIPTION.fetchFrom(assoc);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   576
            if (description != null && !description.isEmpty()) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   577
                xml.writeStartElement("comment");
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   578
                xml.writeCharacters(description);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   579
                xml.writeEndElement();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   580
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   581
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   582
            final List<String> extensions = FA_EXTENSIONS.fetchFrom(assoc);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   583
            if (extensions == null) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   584
                Log.error(I18N.getString(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   585
                        "message.creating-association-with-null-extension"));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   586
            } else {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   587
                for (String ext : extensions) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   588
                    xml.writeStartElement("glob");
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   589
                    xml.writeAttribute("pattern", "*." + ext);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   590
                    xml.writeEndElement();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   591
                }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   592
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   593
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   594
            xml.writeEndElement();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   595
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   596
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   597
        private void createFileAssociationsMimeInfoFile() throws IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   598
            XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   599
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   600
            try (Writer w = new BufferedWriter(new FileWriter(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   601
                    mimeInfoFile.srcPath().toFile()))) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   602
                XMLStreamWriter xml = xmlFactory.createXMLStreamWriter(w);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   603
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   604
                xml.writeStartDocument();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   605
                xml.writeStartElement("mime-info");
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   606
                xml.writeNamespace("xmlns",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   607
                        "http://www.freedesktop.org/standards/shared-mime-info");
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   608
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   609
                for (var assoc : associations) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   610
                    appendFileAssociation(xml, assoc);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   611
                }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   612
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   613
                xml.writeEndElement();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   614
                xml.writeEndDocument();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   615
                xml.flush();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   616
                xml.close();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   617
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   618
            } catch (XMLStreamException ex) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   619
                Log.verbose(ex);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   620
                throw new IOException(ex);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   621
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   622
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   623
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   624
        private Map<String, Path> createFileAssociationIconFiles() throws
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   625
                IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   626
            Map<String, Path> mimeTypeWithIconFile = new HashMap<>();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   627
            for (var assoc : associations) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   628
                File customFaIcon = FA_ICON.fetchFrom(assoc);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   629
                if (customFaIcon == null || !customFaIcon.exists() || getSquareSizeOfImage(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   630
                        customFaIcon) == 0) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   631
                    continue;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   632
                }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   633
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   634
                String fname = iconFile.srcPath().getFileName().toString();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   635
                if (fname.indexOf(".") > 0) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   636
                    fname = fname.substring(0, fname.lastIndexOf("."));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   637
                }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   638
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   639
                DesktopFile faIconFile = new DesktopFile(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   640
                        fname + "_fa_" + customFaIcon.getName());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   641
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   642
                IOUtils.copyFile(customFaIcon, faIconFile.srcPath().toFile());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   643
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   644
                mimeTypeWithIconFile.put(FA_CONTENT_TYPE.fetchFrom(assoc).get(0),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   645
                        faIconFile.installPath());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   646
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   647
            return mimeTypeWithIconFile;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   648
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   649
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   650
        private void createDesktopFile(Map<String, String> data) throws IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   651
            List<String> mimeTypes = getMimeTypeNamesFromFileAssociations();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   652
            data.put("DESKTOP_MIMES", "MimeType=" + String.join(";", mimeTypes));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   653
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   654
            // prepare desktop shortcut
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   655
            try (Writer w = Files.newBufferedWriter(desktopFile.srcPath())) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   656
                String content = preprocessTextResource(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   657
                        desktopFile.srcPath().getFileName().toString(),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   658
                        I18N.getString("resource.menu-shortcut-descriptor"),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   659
                        "template.desktop",
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   660
                        data,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   661
                        verbose,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   662
                        resourceDir);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   663
                w.write(content);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   664
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   665
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   666
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   667
        private void prepareSrcIconFile() throws IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   668
            if (customIconFile == null || !customIconFile.exists()) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   669
                fetchResource(iconFile.srcPath().getFileName().toString(),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   670
                        I18N.getString("resource.menu-icon"),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   671
                        DEFAULT_ICON,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   672
                        iconFile.srcPath().toFile(),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   673
                        verbose,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   674
                        resourceDir);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   675
            } else {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   676
                fetchResource(iconFile.srcPath().getFileName().toString(),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   677
                        I18N.getString("resource.menu-icon"),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   678
                        customIconFile,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   679
                        iconFile.srcPath().toFile(),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   680
                        verbose,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   681
                        resourceDir);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   682
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   683
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   684
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   685
        private List<String> getMimeTypeNamesFromFileAssociations() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   686
            return associations.stream().map(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   687
                    a -> FA_CONTENT_TYPE.fetchFrom(a).get(0)).collect(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   688
                            Collectors.toUnmodifiableList());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   689
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   690
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   691
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   692
    private static int getSquareSizeOfImage(File f) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   693
        try {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   694
            BufferedImage bi = ImageIO.read(f);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   695
            if (bi.getWidth() == bi.getHeight()) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   696
                return bi.getWidth();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   697
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   698
        } catch (IOException e) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   699
            Log.verbose(e);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   700
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   701
        return 0;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   702
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   703
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   704
    private static String stringifyShellCommands(String ... commands) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   705
        return stringifyShellCommands(Arrays.asList(commands));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   706
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   707
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   708
    private static String stringifyShellCommands(List<String> commands) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   709
        return String.join(System.lineSeparator(), commands.stream().filter(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   710
                s -> s != null && !s.isEmpty()).collect(Collectors.toList()));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   711
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   712
}