package jdk.incubator.jpackage.internal;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.ResourceBundle;
import static jdk.incubator.jpackage.internal.OverridableResource.createResource;
import static jdk.incubator.jpackage.internal.StandardBundlerParam.*;
public class WindowsAppImageBuilder extends AbstractAppImageBuilder {
private static final ResourceBundle I18N = ResourceBundle.getBundle(
"jdk.incubator.jpackage.internal.resources.WinResources");
private static final String TEMPLATE_APP_ICON ="java48.ico";
public static final BundlerParamInfo<File> ICON_ICO =
new StandardBundlerParam<>(
"icon.ico",
File.class,
params -> {
File f = ICON.fetchFrom(params);
if (f != null && !f.getName().toLowerCase().endsWith(".ico")) {
Log.error(MessageFormat.format(
I18N.getString("message.icon-not-ico"), f));
return null;
}
return f;
},
(s, p) -> new File(s));
public static final StandardBundlerParam<Boolean> CONSOLE_HINT =
new StandardBundlerParam<>(
Arguments.CLIOptions.WIN_CONSOLE_HINT.getId(),
Boolean.class,
params -> false,
(s, p) -> (s == null
|| "null".equalsIgnoreCase(s)) ? true : Boolean.valueOf(s));
WindowsAppImageBuilder(Path imageOutDir) {
super(imageOutDir);
}
private void writeEntry(InputStream in, Path dstFile) throws IOException {
Files.createDirectories(dstFile.getParent());
Files.copy(in, dstFile);
}
private static String getLauncherName(Map<String, ? super Object> params) {
return APP_NAME.fetchFrom(params) + ".exe";
}
public static String getLauncherResourceName(
Map<String, ? super Object> params) {
if (CONSOLE_HINT.fetchFrom(params)) {
return "jpackageapplauncher.exe";
} else {
return "jpackageapplauncherw.exe";
}
}
@Override
public void prepareApplicationFiles(Map<String, ? super Object> params)
throws IOException {
createLauncherForEntryPoint(params, null);
copyApplication(params);
List<Map<String, ? super Object>> entryPoints =
StandardBundlerParam.ADD_LAUNCHERS.fetchFrom(params);
for (Map<String, ? super Object> entryPoint : entryPoints) {
createLauncherForEntryPoint(AddLauncherArguments.merge(params,
entryPoint, ICON.getID(), ICON_ICO.getID()), params);
}
}
private void createLauncherForEntryPoint(Map<String, ? super Object> params,
Map<String, ? super Object> mainParams) throws IOException {
var iconResource = createIconResource(TEMPLATE_APP_ICON, ICON_ICO, params,
mainParams);
Path iconTarget = null;
if (iconResource != null) {
iconTarget = appLayout.destktopIntegrationDirectory().resolve(
APP_NAME.fetchFrom(params) + ".ico");
if (null == iconResource.saveToFile(iconTarget)) {
iconTarget = null;
}
}
writeCfgFile(params);
Path executableFile = appLayout.launchersDirectory().resolve(
getLauncherName(params));
try (InputStream is_launcher =
getResourceAsStream(getLauncherResourceName(params))) {
writeEntry(is_launcher, executableFile);
}
new ExecutableRebrander().rebrandLauncher(params, iconTarget, executableFile);
executableFile.toFile().setExecutable(true);
executableFile.toFile().setReadOnly();
}
}