changeset 57757:c61cd64e09ac

8235955: [dmg] DMG creation fails without error message if previous DMG was not ejected Reviewed-by: herrick, asemenyuk
author almatvee
date Tue, 21 Jan 2020 08:46:06 -0500
parents 2dfbe080038c
children dff4792e3206
files src/jdk.incubator.jpackage/macosx/classes/jdk/incubator/jpackage/internal/MacDmgBundler.java src/jdk.incubator.jpackage/macosx/classes/jdk/incubator/jpackage/internal/resources/DMGsetup.scpt
diffstat 2 files changed, 62 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.incubator.jpackage/macosx/classes/jdk/incubator/jpackage/internal/MacDmgBundler.java	Tue Jan 21 14:08:43 2020 +0100
+++ b/src/jdk.incubator.jpackage/macosx/classes/jdk/incubator/jpackage/internal/MacDmgBundler.java	Tue Jan 21 08:46:06 2020 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2020, 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
@@ -27,6 +27,7 @@
 
 import java.io.*;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.text.MessageFormat;
 import java.util.*;
 import static jdk.incubator.jpackage.internal.MacAppImageBuilder.ICON_ICNS;
@@ -39,8 +40,12 @@
     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 DEFAULT_DMG_SETUP_SCRIPT="DMGsetup.scpt";
+    // Background image name in resources
+    static final String DEFAULT_BACKGROUND_IMAGE = "background_dmg.tiff";
+    // Backround image name and folder under which it will be stored in DMG
+    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";
@@ -84,16 +89,31 @@
 
     private static final String hdiutil = "/usr/bin/hdiutil";
 
-    private void prepareDMGSetupScript(String volumeName,
-            Map<String, ? super Object> params) throws IOException {
+    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()));
 
+        // We need to use URL for DMG to find it. We cannot use volume name, since
+        // user might have open DMG with same volume name already. Url should end with
+        // '/' and it should be real path (no symbolic links).
+        File imageDir = IMAGES_ROOT.fetchFrom(params);
+        if (!imageDir.exists()) imageDir.mkdirs(); // Create it, since it does not exist
+        Path rootPath = Path.of(imageDir.toString()).toRealPath();
+        Path volumePath = rootPath.resolve(APP_NAME.fetchFrom(params));
+        String volumeUrl = volumePath.toUri().toString() + File.separator;
+
+        // Provide full path to backround image, so we can find it.
+        Path bgFile = Path.of(rootPath.toString(), APP_NAME.fetchFrom(params),
+                              BACKGROUND_IMAGE_FOLDER, BACKGROUND_IMAGE);
+
         //prepare config for exe
         Map<String, String> data = new HashMap<>();
-        data.put("DEPLOY_ACTUAL_VOLUME_NAME", volumeName);
+        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", "(path to applications folder)");
@@ -170,12 +190,7 @@
 
         prepareLicense(params);
 
-        // In theory we need to extract name from results of attach command
-        // However, this will be a problem for customization as name will
-        // possibly change every time and developer will not be able to fix it
-        // As we are using tmp dir chance we get "different" name are low =>
-        // Use fixed name we used for bundle
-        prepareDMGSetupScript(APP_NAME.fetchFrom(params), params);
+        prepareDMGSetupScript(params);
 
         return true;
     }
@@ -302,10 +317,10 @@
                     volumeIconFile);
 
             // background image
-            File bgdir = new File(mountedRoot, ".background");
+            File bgdir = new File(mountedRoot, BACKGROUND_IMAGE_FOLDER);
             bgdir.mkdirs();
             IOUtils.copyFile(getConfig_VolumeBackground(params),
-                    new File(bgdir, "background.tiff"));
+                    new File(bgdir, BACKGROUND_IMAGE));
 
             // Indicate that we want a custom icon
             // NB: attributes of the root directory are ignored
--- a/src/jdk.incubator.jpackage/macosx/classes/jdk/incubator/jpackage/internal/resources/DMGsetup.scpt	Tue Jan 21 14:08:43 2020 +0100
+++ b/src/jdk.incubator.jpackage/macosx/classes/jdk/incubator/jpackage/internal/resources/DMGsetup.scpt	Tue Jan 21 08:46:06 2020 -0500
@@ -1,38 +1,40 @@
 tell application "Finder"
-  tell disk "DEPLOY_ACTUAL_VOLUME_NAME"
-    open
-    set current view of container window to icon view
-    set toolbar visible of container window to false
-    set statusbar visible of container window to false
+  set theDisk to a reference to (disks whose URL = "DEPLOY_VOLUME_URL")
+  open theDisk
+
+  set theWindow to a reference to (container window of disks whose URL = "DEPLOY_VOLUME_URL")
 
-    -- size of window should match size of background
-    set the bounds of container window to {400, 100, 917, 380}
+  set current view of theWindow to icon view
+  set toolbar visible of theWindow to false
+  set statusbar visible of theWindow to false
 
-    set theViewOptions to the icon view options of container window
-    set arrangement of theViewOptions to not arranged
-    set icon size of theViewOptions to 128
-    set background picture of theViewOptions to file ".background:background.tiff"
+  -- size of window should match size of background
+  set the bounds of theWindow to {400, 100, 917, 380}
 
-    -- Create alias for install location
-    make new alias file at container window to DEPLOY_INSTALL_LOCATION with properties {name:"DEPLOY_INSTALL_NAME"}
+  set theViewOptions to a reference to the icon view options of theWindow
+  set arrangement of theViewOptions to not arranged
+  set icon size of theViewOptions to 128
+  set background picture of theViewOptions to POSIX file "DEPLOY_BG_FILE"
 
-    set allTheFiles to the name of every item of container window
-    repeat with theFile in allTheFiles
-      set theFilePath to POSIX Path of theFile
-      if theFilePath is "/DEPLOY_APPLICATION_NAME.app"
-        -- Position application location
-        set position of item theFile of container window to {120, 130}
-      else if theFilePath is "/DEPLOY_INSTALL_NAME"
-        -- Position install location
-        set position of item theFile of container window to {390, 130}
-      else
-        -- Move all other files far enough to be not visible if user has "show hidden files" option set
-        set position of item theFile of container window to {1000, 130}
-      end
-    end repeat
+  -- Create alias for install location
+  make new alias file at POSIX file "DEPLOY_VOLUME_PATH" to DEPLOY_INSTALL_LOCATION with properties {name:"DEPLOY_INSTALL_NAME"}
 
-    update without registering applications
-    delay 5
-    close
-  end tell
+  set allTheFiles to the name of every item of theWindow
+  repeat with theFile in allTheFiles
+    set theFilePath to POSIX path of theFile
+    if theFilePath is "/DEPLOY_APPLICATION_NAME.app" then
+      -- Position application location
+      set position of item theFile of theWindow to {120, 130}
+    else if theFilePath is "/DEPLOY_INSTALL_NAME" then
+      -- Position install location
+      set position of item theFile of theWindow to {390, 130}
+    else
+      -- Move all other files far enough to be not visible if user has "show hidden files" option set
+      set position of item theFile of theWindow to {1000, 130}
+    end if
+  end repeat
+
+  update theDisk without registering applications
+  delay 5
+  close (get window of theDisk)
 end tell