package jdk.incubator.jpackage.internal;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.ResourceBundle;
import static jdk.incubator.jpackage.internal.MacAppImageBuilder.ICON_ICNS;
import static jdk.incubator.jpackage.internal.MacAppImageBuilder.MAC_CF_BUNDLE_IDENTIFIER;
import static jdk.incubator.jpackage.internal.OverridableResource.createResource;
import static jdk.incubator.jpackage.internal.StandardBundlerParam.APP_NAME;
import static jdk.incubator.jpackage.internal.StandardBundlerParam.CONFIG_ROOT;
import static jdk.incubator.jpackage.internal.StandardBundlerParam.LICENSE_FILE;
import static jdk.incubator.jpackage.internal.StandardBundlerParam.TEMP_ROOT;
import static jdk.incubator.jpackage.internal.StandardBundlerParam.VERBOSE;
public class MacDmgBundler extends MacBaseInstallerBundler {
private static final ResourceBundle I18N = ResourceBundle.getBundle(
"jdk.incubator.jpackage.internal.resources.MacResources");
static final String DEFAULT_BACKGROUND_IMAGE = "background_dmg.tiff";
static final String BACKGROUND_IMAGE_FOLDER =".background";
static final String BACKGROUND_IMAGE = "background.tiff";
static final String DEFAULT_DMG_SETUP_SCRIPT = "DMGsetup.scpt";
static final String TEMPLATE_BUNDLE_ICON = "java.icns";
static final String DEFAULT_LICENSE_PLIST="lic_template.plist";
public static final BundlerParamInfo<String> INSTALLER_SUFFIX =
new StandardBundlerParam<> (
"mac.dmg.installerName.suffix",
String.class,
params -> "",
(s, p) -> s);
public File bundle(Map<String, ? super Object> params,
File outdir) throws PackagerException {
Log.verbose(MessageFormat.format(I18N.getString("message.building-dmg"),
APP_NAME.fetchFrom(params)));
IOUtils.writableOutputDir(outdir.toPath());
try {
File appLocation = prepareAppBundle(params);
if (appLocation != null && prepareConfigFiles(params)) {
File configScript = getConfig_Script(params);
if (configScript.exists()) {
Log.verbose(MessageFormat.format(
I18N.getString("message.running-script"),
configScript.getAbsolutePath()));
IOUtils.run("bash", configScript);
}
return buildDMG(params, appLocation, outdir);
}
return null;
} catch (IOException | PackagerException ex) {
Log.verbose(ex);
throw new PackagerException(ex);
}
}
private static final String hdiutil = "/usr/bin/hdiutil";
private void prepareDMGSetupScript(Map<String, ? super Object> params)
throws IOException {
File dmgSetup = getConfig_VolumeScript(params);
Log.verbose(MessageFormat.format(
I18N.getString("message.preparing-dmg-setup"),
dmgSetup.getAbsolutePath()));
File imageDir = IMAGES_ROOT.fetchFrom(params);
if (!imageDir.exists()) imageDir.mkdirs();
Path rootPath = Path.of(imageDir.toString()).toRealPath();
Path volumePath = rootPath.resolve(APP_NAME.fetchFrom(params));
String volumeUrl = volumePath.toUri().toString() + File.separator;
Path bgFile = Path.of(rootPath.toString(), APP_NAME.fetchFrom(params),
BACKGROUND_IMAGE_FOLDER, BACKGROUND_IMAGE);
Map<String, String> data = new HashMap<>();
data.put("DEPLOY_VOLUME_URL", volumeUrl);
data.put("DEPLOY_BG_FILE", bgFile.toString());
data.put("DEPLOY_VOLUME_PATH", volumePath.toString());
data.put("DEPLOY_APPLICATION_NAME", APP_NAME.fetchFrom(params));
data.put("DEPLOY_INSTALL_LOCATION", getInstallDir(params));
createResource(DEFAULT_DMG_SETUP_SCRIPT, params)
.setCategory(I18N.getString("resource.dmg-setup-script"))
.setSubstitutionData(data)
.saveToFile(dmgSetup);
}
private File getConfig_VolumeScript(Map<String, ? super Object> params) {
return new File(CONFIG_ROOT.fetchFrom(params),
APP_NAME.fetchFrom(params) + "-dmg-setup.scpt");
}
private File getConfig_VolumeBackground(
Map<String, ? super Object> params) {
return new File(CONFIG_ROOT.fetchFrom(params),
APP_NAME.fetchFrom(params) + "-background.tiff");
}
private File getConfig_VolumeIcon(Map<String, ? super Object> params) {
return new File(CONFIG_ROOT.fetchFrom(params),
APP_NAME.fetchFrom(params) + "-volume.icns");
}
private File getConfig_LicenseFile(Map<String, ? super Object> params) {
return new File(CONFIG_ROOT.fetchFrom(params),
APP_NAME.fetchFrom(params) + "-license.plist");
}
private void prepareLicense(Map<String, ? super Object> params) {
try {
String licFileStr = LICENSE_FILE.fetchFrom(params);
if (licFileStr == null) {
return;
}
File licFile = new File(licFileStr);
byte[] licenseContentOriginal =
Files.readAllBytes(licFile.toPath());
String licenseInBase64 =
Base64.getEncoder().encodeToString(licenseContentOriginal);
Map<String, String> data = new HashMap<>();
data.put("APPLICATION_LICENSE_TEXT", licenseInBase64);
createResource(DEFAULT_LICENSE_PLIST, params)
.setCategory(I18N.getString("resource.license-setup"))
.setSubstitutionData(data)
.saveToFile(getConfig_LicenseFile(params));
} catch (IOException ex) {
Log.verbose(ex);
}
}
private boolean prepareConfigFiles(Map<String, ? super Object> params)
throws IOException {
createResource(DEFAULT_BACKGROUND_IMAGE, params)
.setCategory(I18N.getString("resource.dmg-background"))
.saveToFile(getConfig_VolumeBackground(params));
createResource(TEMPLATE_BUNDLE_ICON, params)
.setCategory(I18N.getString("resource.volume-icon"))
.setExternal(ICON_ICNS.fetchFrom(params))
.saveToFile(getConfig_VolumeIcon(params));
createResource(null, params)
.setCategory(I18N.getString("resource.post-install-script"))
.saveToFile(getConfig_Script(params));
prepareLicense(params);
prepareDMGSetupScript(params);
return true;
}
private File getConfig_Script(Map<String, ? super Object> params) {
return new File(CONFIG_ROOT.fetchFrom(params),
APP_NAME.fetchFrom(params) + "-post-image.sh");
}
private String findSetFileUtility() {
String typicalPaths[] = {"/Developer/Tools/SetFile",
"/usr/bin/SetFile", "/Developer/usr/bin/SetFile"};
String setFilePath = null;
for (String path: typicalPaths) {
File f = new File(path);
if (f.exists() && f.canExecute()) {
setFilePath = path;
break;
}
}
if (setFilePath != null) {
try {
ProcessBuilder pb = new ProcessBuilder(setFilePath, "-h");
Process p = pb.start();
int code = p.waitFor();
if (code == 0) {
return setFilePath;
}
} catch (Exception ignored) {}
return null;
}
try {
ProcessBuilder pb = new ProcessBuilder("xcrun", "-find", "SetFile");
Process p = pb.start();
InputStreamReader isr = new InputStreamReader(p.getInputStream());
BufferedReader br = new BufferedReader(isr);
String lineRead = br.readLine();
if (lineRead != null) {
File f = new File(lineRead);
if (f.exists() && f.canExecute()) {
return f.getAbsolutePath();
}
}
} catch (IOException ignored) {}
return null;
}
private File buildDMG( Map<String, ? super Object> params,
File appLocation, File outdir) throws IOException, PackagerException {
boolean copyAppImage = false;
File imagesRoot = IMAGES_ROOT.fetchFrom(params);
if (!imagesRoot.exists()) imagesRoot.mkdirs();
File protoDMG = new File(imagesRoot,
APP_NAME.fetchFrom(params) +"-tmp.dmg");
File finalDMG = new File(outdir, INSTALLER_NAME.fetchFrom(params)
+ INSTALLER_SUFFIX.fetchFrom(params) + ".dmg");
File srcFolder = APP_IMAGE_TEMP_ROOT.fetchFrom(params);
File predefinedImage =
StandardBundlerParam.getPredefinedAppImage(params);
if (predefinedImage != null) {
srcFolder = predefinedImage;
} else if (StandardBundlerParam.isRuntimeInstaller(params)) {
Path newRoot = Files.createTempDirectory(
TEMP_ROOT.fetchFrom(params).toPath(), "root-");
Path original = appLocation.toPath();
Path home = original.resolve("Contents/Home");
Path source = (Files.exists(home)) ? home : original;
Path root = newRoot.resolve(
MAC_CF_BUNDLE_IDENTIFIER.fetchFrom(params));
Path dest = root.resolve("Contents/Home");
IOUtils.copyRecursive(source, dest);
srcFolder = newRoot.toFile();
}
Log.verbose(MessageFormat.format(I18N.getString(
"message.creating-dmg-file"), finalDMG.getAbsolutePath()));
protoDMG.delete();
if (finalDMG.exists() && !finalDMG.delete()) {
throw new IOException(MessageFormat.format(I18N.getString(
"message.dmg-cannot-be-overwritten"),
finalDMG.getAbsolutePath()));
}
protoDMG.getParentFile().mkdirs();
finalDMG.getParentFile().mkdirs();
String hdiUtilVerbosityFlag = VERBOSE.fetchFrom(params) ?
"-verbose" : "-quiet";
ProcessBuilder pb = new ProcessBuilder(
hdiutil,
"create",
hdiUtilVerbosityFlag,
"-srcfolder", srcFolder.getAbsolutePath(),
"-volname", APP_NAME.fetchFrom(params),
"-ov", protoDMG.getAbsolutePath(),
"-fs", "HFS+",
"-format", "UDRW");
try {
IOUtils.exec(pb);
} catch (IOException ex) {
Log.verbose(ex);
copyAppImage = true;
long size = new PathGroup(Map.of(new Object(), srcFolder.toPath()))
.sizeInBytes();
size += 50 * 1024 * 1024;
pb = new ProcessBuilder(
hdiutil,
"create",
hdiUtilVerbosityFlag,
"-size", String.valueOf(size),
"-volname", APP_NAME.fetchFrom(params),
"-ov", protoDMG.getAbsolutePath(),
"-fs", "HFS+");
IOUtils.exec(pb);
}
pb = new ProcessBuilder(
hdiutil,
"attach",
protoDMG.getAbsolutePath(),
hdiUtilVerbosityFlag,
"-mountroot", imagesRoot.getAbsolutePath());
IOUtils.exec(pb, false, null, true);
File mountedRoot = new File(imagesRoot.getAbsolutePath(),
APP_NAME.fetchFrom(params));
if (copyAppImage) {
if (srcFolder.toPath().toString().toLowerCase().endsWith(".app")) {
Path destPath = mountedRoot.toPath()
.resolve(srcFolder.toPath().getFileName());
Files.createDirectory(destPath);
IOUtils.copyRecursive(srcFolder.toPath(), destPath);
} else {
IOUtils.copyRecursive(srcFolder.toPath(), mountedRoot.toPath());
}
}
try {
File bgdir = new File(mountedRoot, BACKGROUND_IMAGE_FOLDER);
bgdir.mkdirs();
IOUtils.copyFile(getConfig_VolumeBackground(params),
new File(bgdir, BACKGROUND_IMAGE));
try {
pb = new ProcessBuilder("osascript",
getConfig_VolumeScript(params).getAbsolutePath());
IOUtils.exec(pb);
} catch (IOException ex) {
Log.verbose(ex);
}
File volumeIconFile = new File(mountedRoot, ".VolumeIcon.icns");
IOUtils.copyFile(getConfig_VolumeIcon(params),
volumeIconFile);
String setFileUtility = findSetFileUtility();
if (setFileUtility != null) {
try {
volumeIconFile.setWritable(true);
pb = new ProcessBuilder(
setFileUtility,
"-c", "icnC",
volumeIconFile.getAbsolutePath());
IOUtils.exec(pb);
volumeIconFile.setReadOnly();
pb = new ProcessBuilder(
setFileUtility,
"-a", "C",
mountedRoot.getAbsolutePath());
IOUtils.exec(pb);
} catch (IOException ex) {
Log.error(ex.getMessage());
Log.verbose("Cannot enable custom icon using SetFile utility");
}
} else {
Log.verbose(I18N.getString("message.setfile.dmg"));
}
} finally {
pb = new ProcessBuilder(
hdiutil,
"detach",
"-force",
hdiUtilVerbosityFlag,
mountedRoot.getAbsolutePath());
IOUtils.exec(pb);
}
pb = new ProcessBuilder(
hdiutil,
"convert",
protoDMG.getAbsolutePath(),
hdiUtilVerbosityFlag,
"-format", "UDZO",
"-o", finalDMG.getAbsolutePath());
IOUtils.exec(pb);
if (getConfig_LicenseFile(params).exists()) {
pb = new ProcessBuilder(
hdiutil,
"unflatten",
finalDMG.getAbsolutePath()
);
IOUtils.exec(pb);
pb = new ProcessBuilder(
hdiutil,
"udifrez",
finalDMG.getAbsolutePath(),
"-xml",
getConfig_LicenseFile(params).getAbsolutePath()
);
IOUtils.exec(pb);
pb = new ProcessBuilder(
hdiutil,
"flatten",
finalDMG.getAbsolutePath()
);
IOUtils.exec(pb);
}
protoDMG.delete();
Log.verbose(MessageFormat.format(I18N.getString(
"message.output-to-location"),
APP_NAME.fetchFrom(params), finalDMG.getAbsolutePath()));
return finalDMG;
}
@Override
public String getName() {
return I18N.getString("dmg.bundler.name");
}
@Override
public String getID() {
return "dmg";
}
@Override
public boolean validate(Map<String, ? super Object> params)
throws ConfigException {
try {
Objects.requireNonNull(params);
validateAppImageAndBundeler(params);
return true;
} catch (RuntimeException re) {
if (re.getCause() instanceof ConfigException) {
throw (ConfigException) re.getCause();
} else {
throw new ConfigException(re);
}
}
}
@Override
public File execute(Map<String, ? super Object> params,
File outputParentDir) throws PackagerException {
return bundle(params, outputParentDir);
}
@Override
public boolean supported(boolean runtimeInstaller) {
return isSupported();
}
public final static String[] required =
{"/usr/bin/hdiutil", "/usr/bin/osascript"};
public static boolean isSupported() {
try {
for (String s : required) {
File f = new File(s);
if (!f.exists() || !f.canExecute()) {
return false;
}
}
return true;
} catch (Exception e) {
return false;
}
}
@Override
public boolean isDefault() {
return true;
}
}