- another fix of update mechanism (hdiutil block dmg mounting when call it from bash, moved a mostly all logic to swift)

This commit is contained in:
Serhiy Mytrovtsiy
2020-07-09 22:34:19 +02:00
parent 8296acb5e9
commit a0100c994f
6 changed files with 48 additions and 51 deletions

View File

@@ -1695,7 +1695,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.14;
MARKETING_VERSION = 2.1.9;
MARKETING_VERSION = 2.1.10;
PRODUCT_BUNDLE_IDENTIFIER = eu.exelban.Stats;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
@@ -1727,7 +1727,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.14;
MARKETING_VERSION = 2.1.9;
MARKETING_VERSION = 2.1.10;
PRODUCT_BUNDLE_IDENTIFIER = eu.exelban.Stats;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";

View File

@@ -137,11 +137,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
}
}
if let dmgIndex = args.firstIndex(of: "--dmg") {
if let mountIndex = args.firstIndex(of: "--mount-path") {
if args.indices.contains(mountIndex+1) {
let mountPath = args[mountIndex+1]
asyncShell("/usr/bin/hdiutil detach \(mountPath)")
asyncShell("/bin/rm -rf \(mountPath)")
}
}
if let dmgIndex = args.firstIndex(of: "--dmg-path") {
if args.indices.contains(dmgIndex+1) {
let dmgPath = args[dmgIndex+1]
let pwd = Bundle.main.bundleURL.absoluteString.replacingOccurrences(of: "file://", with: "").replacingOccurrences(of: "Stats.app/", with: "")
asyncShell("sh \(pwd)/Stats.app/Contents/Resources/Scripts/updater.sh --step 3 --dmg \(dmgPath)")
asyncShell("/bin/rm -rf \(args[dmgIndex+1])")
}
}
}

View File

@@ -17,7 +17,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>14</string>
<string>16</string>
<key>Description</key>
<string>Simple macOS system monitor in your menu bar</string>
<key>LSApplicationCategoryType</key>

View File

@@ -651,6 +651,22 @@ public func asyncShell(_ args: String) {
task.launch()
}
public func syncShell(_ args: String) -> String {
let task = Process()
task.launchPath = "/bin/sh"
task.arguments = ["-c", args]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)!
return output
}
public func colorFromString(_ colorString: String) -> NSColor {
switch colorString {
case "black":

View File

@@ -1,54 +1,19 @@
#!/bin/bash
STEP=1
CURRENT_PATH=""
DMG_PATH="$HOME/Download/Stats.dmg"
MOUNT_PATH="/tmp/Stats"
APP_PATH=""
PID=""
APPLICATION_PATH="/Applications/"
while [[ "$#" > 0 ]]; do case $1 in
-d|--dmg) DMG_PATH="$2"; shift;;
-s|--step) STEP="$2"; shift;;
-a|--app) APP_PATH="$2"; shift;;
-p|--pid) PID="$2"; shift;;
-c|--current) CURRENT_PATH="$2"; shift;;
-a|--app) APPLICATION_PATH="$2"; shift;;
-m|--mount) MOUNT_PATH="$2"; shift;;
*) echo "Unknown parameter passed: $1"; exit 1;;
esac; shift; done
echo "Starting step #$STEP"
rm -rf $APPLICATION_PATH/Stats.app
cp -rf $MOUNT_PATH/Stats.app $APPLICATION_PATH/Stats.app
if [[ "$STEP" == "1" ]]; then
if [ -d "$MOUNT_PATH" ]; then
/usr/bin/hdiutil detach "$MOUNT_PATH"
if [ -d "$MOUNT_PATH" ]; then
/bin/rm -rf "$MOUNT_PATH"
fi
fi
$APPLICATION_PATH/Stats.app/Contents/MacOS/Stats --dmg-path "$DMG_PATH" --mount-path "$MOUNT_PATH"
/usr/bin/mktemp -d $MOUNT_PATH
/usr/bin/hdiutil attach "$DMG_PATH" -mountpoint "$MOUNT_PATH" -noverify -nobrowse -noautoopen
cp $MOUNT_PATH/Stats.app/Contents/Resources/Scripts/updater.sh $TMPDIR/updater.sh
sh $TMPDIR/updater.sh --step 2 --app "$CURRENT_PATH" --dmg "$DMG_PATH" >/dev/null &
kill -9 $PID
echo "DMG is mounted under $MOUNT_PATH. Starting step #2"
elif [[ "$STEP" == "2" ]]; then
rm -rf $APP_PATH/Stats.app
cp -rf $MOUNT_PATH/Stats.app $APP_PATH/Stats.app
$APP_PATH/Stats.app/Contents/MacOS/Stats --dmg "$DMG_PATH"
echo "New version started"
elif [[ "$STEP" == "3" ]]; then
/usr/bin/hdiutil detach "$MOUNT_PATH"
/bin/rm -rf "$MOUNT_PATH"
/bin/rm -rf "$DMG_PATH"
echo "Done"
else
echo "Step not defined"
fi
echo "New version started"

View File

@@ -111,9 +111,19 @@ public class macAppUpdater {
return
}
_ = syncShell("mkdir /tmp/Stats") // make sure that directory exist
let res = syncShell("/usr/bin/hdiutil attach \(path) -mountpoint /tmp/Stats -noverify -nobrowse -noautoopen") // mount the dmg
if res.contains("is busy") { // dmg can be busy, if yes, unmount it and mount again
_ = syncShell("/usr/bin/hdiutil detach $TMPDIR/Stats")
_ = syncShell("/usr/bin/hdiutil attach \(path) -mountpoint /tmp/Stats -noverify -nobrowse -noautoopen")
}
_ = syncShell("cp $TMPDIR/Stats/app/Stats.app/Contents/Resources/Scripts/updater.sh $TMPDIR/Stats/updater.sh") // copy updater script to tmp folder
let pwd = Bundle.main.bundleURL.absoluteString.replacingOccurrences(of: "file://", with: "").replacingOccurrences(of: "Stats.app/", with: "")
let pid: Int32 = ProcessInfo.processInfo.processIdentifier
asyncShell("sh \(pwd)/Stats.app/Contents/Resources/Scripts/updater.sh --current \(pwd) --dmg \(url) --pid \(pid)")
asyncShell("sh $TMPDIR/updater.sh --step 2 --app \(pwd) --dmg \(path) >/dev/null &") // run updater script in in background
exit(0)
}
} catch {
print ("file error: \(error)")