| OLD | NEW |
| (Empty) |
| 1 #!/bin/bash | |
| 2 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 3 # for details. All rights reserved. Use of this source code is governed by | |
| 4 # BSD-style license that can be found in the LICENSE file. | |
| 5 | |
| 6 # This is partly based on | |
| 7 # https://bitbucket.org/rmacnak/nsvm/src/ | |
| 8 # b2de52432a2baff9c4ada099430fb16a771d34ef/vm/onebuild/installer-Darwin.gmk | |
| 9 | |
| 10 # Fail if a command failed | |
| 11 set -e | |
| 12 set -o errexit | |
| 13 set -o nounset | |
| 14 | |
| 15 if [ $# -ne 4 ]; then | |
| 16 echo "Usage $0 <output.dmg> <raw-editor-bundle> <folder-icon> <volume-name>" | |
| 17 exit 1 | |
| 18 fi | |
| 19 | |
| 20 OUTPUT_DMG_FILE=$1 | |
| 21 INPUT_FOLDER_PATH=$2 | |
| 22 FOLDER_ICON=$3 | |
| 23 INPUT_VOLUME_NAME=$4 | |
| 24 | |
| 25 FOLDER_NAME="Dart" | |
| 26 VOLUME_MOUNTPOINT="/Volumes/$INPUT_VOLUME_NAME" | |
| 27 SPARSEIMAGE="$OUTPUT_DMG_FILE.sparseimage" | |
| 28 | |
| 29 # Input validations | |
| 30 if [ ! -d "$INPUT_FOLDER_PATH" ]; then | |
| 31 echo "Editor bundle folder does not exist ($INPUT_FOLDER_PATH)" | |
| 32 exit 1 | |
| 33 fi | |
| 34 | |
| 35 # If an old image is still mounted, umount it | |
| 36 if [ -e "$VOLUME_MOUNTPOINT" ]; then | |
| 37 hdiutil eject "$VOLUME_MOUNTPOINT" | |
| 38 fi | |
| 39 | |
| 40 # Remove old output files | |
| 41 if [ -f "$SPARSEIMAGE" ]; then | |
| 42 rm "$SPARSEIMAGE" | |
| 43 fi | |
| 44 if [ -f "$OUTPUT_DMG_FILE" ]; then | |
| 45 rm "$OUTPUT_DMG_FILE" | |
| 46 fi | |
| 47 | |
| 48 # This function will set (or replace) the icon of a folder. | |
| 49 # Finder displays a default folder icon. Since the installer | |
| 50 # will consist of a folder and a link to "/Applications", we want | |
| 51 # the folder to have a nice icon. | |
| 52 # In order to make Finder display a custom icon, we need to | |
| 53 # - Have a "FOLDER/Icon\r" file which contains the icon resource | |
| 54 # (i.e. the metadata of this file will contain an icon) | |
| 55 # - Have the 'custom icon' attribute set on "FOLDER" | |
| 56 # Additionally we mark the "FOLDER/Icon\r" file as invisible, so it | |
| 57 # is not shown in Finder (although it's visible on the commandline). | |
| 58 replace_folder_icon() { | |
| 59 FOLDER="$1" | |
| 60 ICON="$2" | |
| 61 TEMP_ICON_RESOURCE='/tmp/icns.rsrc' | |
| 62 ICON_RESOURCE="$FOLDER"/$'Icon\r' | |
| 63 | |
| 64 # Add finder icon to the image file | |
| 65 sips -i "$ICON" > /dev/null | |
| 66 | |
| 67 # Extract the finder icon resource | |
| 68 DeRez -only icns "$ICON" > "$TEMP_ICON_RESOURCE" | |
| 69 | |
| 70 # Create the icon resource | |
| 71 rm -f "$ICON_RESOURCE" | |
| 72 Rez -append "$TEMP_ICON_RESOURCE" -o "$ICON_RESOURCE" | |
| 73 rm "$TEMP_ICON_RESOURCE" | |
| 74 | |
| 75 # Set the 'custom icon' attribute on $FOLDER | |
| 76 SetFile -a C "$FOLDER" | |
| 77 | |
| 78 # Make the $ICON_RESOURCE invisible for finder | |
| 79 SetFile -a V "$ICON_RESOURCE" | |
| 80 } | |
| 81 | |
| 82 | |
| 83 # Create a new image and attach it | |
| 84 hdiutil create -size 400m -type SPARSE -volname "$INPUT_VOLUME_NAME" -fs \ | |
| 85 'Journaled HFS+' "$SPARSEIMAGE" | |
| 86 hdiutil attach "$SPARSEIMAGE" | |
| 87 | |
| 88 # Add link to /Applications (so the user can drag-and-drop into it) | |
| 89 ln -s /Applications "$VOLUME_MOUNTPOINT/" | |
| 90 # Copy our application | |
| 91 ditto "$INPUT_FOLDER_PATH" "$VOLUME_MOUNTPOINT/$FOLDER_NAME" | |
| 92 # Set custom icon on this folder | |
| 93 replace_folder_icon "$VOLUME_MOUNTPOINT/$FOLDER_NAME" "$FOLDER_ICON" | |
| 94 # Make sure that the dmg gets opened when mounting the image | |
| 95 bless --folder "$VOLUME_MOUNTPOINT" --openfolder "$VOLUME_MOUNTPOINT" | |
| 96 | |
| 97 # Use an applescript to setup the layout of the folder. | |
| 98 osascript << EOF | |
| 99 tell application "Finder" | |
| 100 tell disk "$INPUT_VOLUME_NAME" | |
| 101 open | |
| 102 tell container window | |
| 103 set current view to icon view | |
| 104 set toolbar visible to false | |
| 105 set statusbar visible to false | |
| 106 set position to {100, 100} | |
| 107 set bounds to {100, 100, 512, 256} | |
| 108 end tell | |
| 109 tell icon view options of container window | |
| 110 set arrangement to not arranged | |
| 111 set icon size to 128 | |
| 112 end tell | |
| 113 set position of item "$FOLDER_NAME" to {64, 64} | |
| 114 set position of item "Applications" to {320, 64} | |
| 115 eject | |
| 116 end tell | |
| 117 end tell | |
| 118 EOF | |
| 119 | |
| 120 # Wait until the script above has umounted the image | |
| 121 while [ -e "$VOLUME_MOUNTPOINT" ]; do | |
| 122 echo "Waiting for Finder to eject $VOLUME_MOUNTPOINT" | |
| 123 sleep 2 | |
| 124 done | |
| 125 | |
| 126 # Compress the sparse image | |
| 127 hdiutil convert "$SPARSEIMAGE" -format UDBZ -o "$OUTPUT_DMG_FILE" | |
| 128 | |
| 129 # Remove sparse image | |
| 130 rm "$SPARSEIMAGE" | |
| 131 | |
| OLD | NEW |