Skip to content

Commit

Permalink
- another fix of update mechanism (hdiutil block dmg mounting when ca…
Browse files Browse the repository at this point in the history
…ll it from bash, moved a mostly all logic to swift)
  • Loading branch information
exelban committed Jul 9, 2020
1 parent 8296acb commit a0100c9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 51 deletions.
4 changes: 2 additions & 2 deletions Stats.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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 = "";
Expand Down
14 changes: 10 additions & 4 deletions Stats/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Stats/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
16 changes: 16 additions & 0 deletions StatsKit/extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
49 changes: 7 additions & 42 deletions StatsKit/updater.sh
Original file line number Diff line number Diff line change
@@ -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"

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

/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"
rm -rf $APPLICATION_PATH/Stats.app
cp -rf $MOUNT_PATH/Stats.app $APPLICATION_PATH/Stats.app

echo "New version started"
elif [[ "$STEP" == "3" ]]; then
/usr/bin/hdiutil detach "$MOUNT_PATH"
/bin/rm -rf "$MOUNT_PATH"
/bin/rm -rf "$DMG_PATH"
$APPLICATION_PATH/Stats.app/Contents/MacOS/Stats --dmg-path "$DMG_PATH" --mount-path "$MOUNT_PATH"

echo "Done"
else
echo "Step not defined"
fi
echo "New version started"
14 changes: 12 additions & 2 deletions StatsKit/updater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down

0 comments on commit a0100c9

Please sign in to comment.