/*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.jpackage.internal;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.*;
import java.util.regex.Pattern;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import static jdk.jpackage.internal.WindowsBundlerParam.*;
/**
* WinMsiBundler
*
* Produces .msi installer from application image. Uses WiX Toolkit to build
* .msi installer.
*
* {@link #execute} method creates a number of source files with the description
* of installer to be processed by WiX tools. Generated source files are stored
* in "config" subdirectory next to "app" subdirectory in the root work
* directory. The following WiX source files are generated:
*
* - main.wxs. Main source file with the installer description
*
- bundle.wxi. Source file with application and Java run-time directory tree
* description. This source file is included from main.wxs
*
- icons.wxi. Source file with the list of icons used by the application.
* This source file is included from main.wxs
*
*
* main.wxs file is a copy of main.wxs resource from
* jdk.jpackage.internal.resources package. It is parametrized with the
* following WiX variables:
*
* - JpAppName. Name of the application. Set to the value of --name command
* line option
*
- JpAppVersion. Version of the application. Set to the value of
* --app-version command line option
*
- JpAppVendor. Vendor of the application. Set to the value of --vendor
* command line option
*
- JpAppDescription. Description of the application. Set to the value of
* --description command line option
*
- JpProductCode. Set to product code UUID of the application. Random value
* generated by jpackage every time {@link #execute} method is called
*
- JpProductUpgradeCode. Set to upgrade code UUID of the application. Random
* value generated by jpackage every time {@link #execute} method is called if
* --win-upgrade-uuid command line option is not specified. Otherwise this
* variable is set to the value of --win-upgrade-uuid command line option
*
- JpAllowDowngrades. Set to "yes" if --win-upgrade-uuid command line option
* was specified. Undefined otherwise
*
- JpLicenseRtf. Set to the value of --license-file command line option.
* Undefined is --license-file command line option was not specified
*
- JpInstallDirChooser. Set to "yes" if --win-dir-chooser command line
* option was specified. Undefined otherwise
*
- JpConfigDir. Absolute path to the directory with generated WiX source
* files.
*
- JpIsSystemWide. Set to "yes" if --win-per-user-install command line
* option was not specified. Undefined otherwise
*
- JpWixVersion36OrNewer. Set to "yes" if WiX Toolkit v3.6 or newer is used.
* Undefined otherwise
*
*/
public class WinMsiBundler extends AbstractBundler {
private static final ResourceBundle I18N = ResourceBundle.getBundle(
"jdk.jpackage.internal.resources.WinResources");
public static final BundlerParamInfo APP_BUNDLER =
new WindowsBundlerParam<>(
"win.app.bundler",
WinAppBundler.class,
params -> new WinAppBundler(),
null);
public static final BundlerParamInfo CAN_USE_WIX36 =
new WindowsBundlerParam<>(
"win.msi.canUseWix36",
Boolean.class,
params -> false,
(s, p) -> Boolean.valueOf(s));
public static final BundlerParamInfo MSI_IMAGE_DIR =
new WindowsBundlerParam<>(
"win.msi.imageDir",
File.class,
params -> {
File imagesRoot = IMAGES_ROOT.fetchFrom(params);
if (!imagesRoot.exists()) imagesRoot.mkdirs();
return new File(imagesRoot, "win-msi.image");
},
(s, p) -> null);
public static final BundlerParamInfo WIN_APP_IMAGE =
new WindowsBundlerParam<>(
"win.app.image",
File.class,
null,
(s, p) -> null);
public static final StandardBundlerParam MSI_SYSTEM_WIDE =
new StandardBundlerParam<>(
Arguments.CLIOptions.WIN_PER_USER_INSTALLATION.getId(),
Boolean.class,
params -> true, // MSIs default to system wide
// valueOf(null) is false,
// and we actually do want null
(s, p) -> (s == null || "null".equalsIgnoreCase(s))? null
: Boolean.valueOf(s)
);
public static final StandardBundlerParam PRODUCT_VERSION =
new StandardBundlerParam<>(
"win.msi.productVersion",
String.class,
VERSION::fetchFrom,
(s, p) -> s
);
public static final BundlerParamInfo UPGRADE_UUID =
new WindowsBundlerParam<>(
Arguments.CLIOptions.WIN_UPGRADE_UUID.getId(),
UUID.class,
params -> UUID.randomUUID(),
(s, p) -> UUID.fromString(s));
private static final String TOOL_CANDLE = "candle.exe";
private static final String TOOL_LIGHT = "light.exe";
// autodetect just v3.7, v3.8, 3.9, 3.10 and 3.11
private static final String AUTODETECT_DIRS =
";C:\\Program Files (x86)\\WiX Toolset v3.11\\bin;"
+ "C:\\Program Files\\WiX Toolset v3.11\\bin;"
+ "C:\\Program Files (x86)\\WiX Toolset v3.10\\bin;"
+ "C:\\Program Files\\WiX Toolset v3.10\\bin;"
+ "C:\\Program Files (x86)\\WiX Toolset v3.9\\bin;"
+ "C:\\Program Files\\WiX Toolset v3.9\\bin;"
+ "C:\\Program Files (x86)\\WiX Toolset v3.8\\bin;"
+ "C:\\Program Files\\WiX Toolset v3.8\\bin;"
+ "C:\\Program Files (x86)\\WiX Toolset v3.7\\bin;"
+ "C:\\Program Files\\WiX Toolset v3.7\\bin";
private static String getCandlePath() {
for (String dirString : (System.getenv("PATH")
+ AUTODETECT_DIRS).split(";")) {
File f = new File(dirString.replace("\"", ""), TOOL_CANDLE);
if (f.isFile()) {
return f.toString();
}
}
return null;
}
private static String getLightPath() {
for (String dirString : (System.getenv("PATH")
+ AUTODETECT_DIRS).split(";")) {
File f = new File(dirString.replace("\"", ""), TOOL_LIGHT);
if (f.isFile()) {
return f.toString();
}
}
return null;
}
public static final StandardBundlerParam MENU_HINT =
new WindowsBundlerParam<>(
Arguments.CLIOptions.WIN_MENU_HINT.getId(),
Boolean.class,
params -> false,
// valueOf(null) is false,
// and we actually do want null in some cases
(s, p) -> (s == null ||
"null".equalsIgnoreCase(s))? true : Boolean.valueOf(s)
);
public static final StandardBundlerParam SHORTCUT_HINT =
new WindowsBundlerParam<>(
Arguments.CLIOptions.WIN_SHORTCUT_HINT.getId(),
Boolean.class,
params -> false,
// valueOf(null) is false,
// and we actually do want null in some cases
(s, p) -> (s == null ||
"null".equalsIgnoreCase(s))? false : Boolean.valueOf(s)
);
@Override
public String getName() {
return I18N.getString("msi.bundler.name");
}
@Override
public String getID() {
return "msi";
}
@Override
public String getBundleType() {
return "INSTALLER";
}
@Override
public File execute(Map params,
File outputParentDir) throws PackagerException {
return bundle(params, outputParentDir);
}
@Override
public boolean supported(boolean platformInstaller) {
return isSupported();
}
public static boolean isSupported() {
try {
return validateWixTools();
} catch (Exception e) {
return false;
}
}
private static String findToolVersion(String toolName) {
try {
if (toolName == null || "".equals(toolName)) return null;
ProcessBuilder pb = new ProcessBuilder(
toolName,
"/?");
VersionExtractor ve = new VersionExtractor("version (\\d+.\\d+)");
// not interested in the output
IOUtils.exec(pb, true, ve);
String version = ve.getVersion();
Log.verbose(MessageFormat.format(
I18N.getString("message.tool-version"),
toolName, version));
return version;
} catch (Exception e) {
Log.verbose(e);
return null;
}
}
public static boolean validateWixTools() {
String candleVersion = findToolVersion(getCandlePath());
String lightVersion = findToolVersion(getLightPath());
// WiX 3.0+ is required
String minVersion = "3.0";
if (VersionExtractor.isLessThan(candleVersion, minVersion)) {
Log.verbose(MessageFormat.format(
I18N.getString("message.wrong-tool-version"),
TOOL_CANDLE, candleVersion, minVersion));
return false;
}
if (VersionExtractor.isLessThan(lightVersion, minVersion)) {
Log.verbose(MessageFormat.format(
I18N.getString("message.wrong-tool-version"),
TOOL_LIGHT, lightVersion, minVersion));
return false;
}
return true;
}
@Override
public boolean validate(Map params)
throws ConfigException {
try {
if (params == null) throw new ConfigException(
I18N.getString("error.parameters-null"),
I18N.getString("error.parameters-null.advice"));
// run basic validation to ensure requirements are met
// we are not interested in return code, only possible exception
if (!validateWixTools()){
throw new ConfigException(
I18N.getString("error.no-wix-tools"),
I18N.getString("error.no-wix-tools.advice"));
}
String lightVersion = findToolVersion(getLightPath());
if (!VersionExtractor.isLessThan(lightVersion, "3.6")) {
Log.verbose(I18N.getString("message.use-wix36-features"));
params.put(CAN_USE_WIX36.getID(), Boolean.TRUE);
}
/********* validate bundle parameters *************/
String version = PRODUCT_VERSION.fetchFrom(params);
if (!isVersionStringValid(version)) {
throw new ConfigException(
MessageFormat.format(I18N.getString(
"error.version-string-wrong-format"), version),
MessageFormat.format(I18N.getString(
"error.version-string-wrong-format.advice"),
PRODUCT_VERSION.getID()));
}
// only one mime type per association, at least one file extension
List