From a308ccbc2908e49472bd9b9364639c9bbc02c5f7 Mon Sep 17 00:00:00 2001 From: Armin Briegel <1933192+scriptingosx@users.noreply.github.com> Date: Tue, 2 Aug 2022 14:51:18 +0200 Subject: [PATCH] code cleanup --- ...tdialog_example.sh => swiftdialog_list.sh} | 4 +- fragments/functions.sh | 79 +++++++++---------- fragments/header.sh | 26 ++++-- fragments/main.sh | 5 +- 4 files changed, 60 insertions(+), 54 deletions(-) rename MDM/{swiftdialog_example.sh => swiftdialog_list.sh} (98%) diff --git a/MDM/swiftdialog_example.sh b/MDM/swiftdialog_list.sh similarity index 98% rename from MDM/swiftdialog_example.sh rename to MDM/swiftdialog_list.sh index e962785..1cd7f21 100755 --- a/MDM/swiftdialog_example.sh +++ b/MDM/swiftdialog_list.sh @@ -100,8 +100,8 @@ installomator() { startItem $description $installomator $label \ - DIALOG_PROGRESS="list" \ - DIALOG_CMD_FILE=$dialog_command_file \ + DIALOG_CMD_FILE=${(q)dialog_command_file} \ + DIALOG_LIST_ITEM_NAME=${(q)description} \ DEBUG=$DEBUG \ LOGGING=DEBUG diff --git a/fragments/functions.sh b/fragments/functions.sh index ffddba3..2c4b35a 100644 --- a/fragments/functions.sh +++ b/fragments/functions.sh @@ -117,12 +117,8 @@ printlog(){ while IFS= read -r logmessage; do if [[ "$(whoami)" == "root" ]]; then echo "$timestamp" : "${log_priority}${space_char} : $label : ${logmessage}" | tee -a $log_location - updateDialogProgressText "${logmessage}" -# updateDialogProgress "increment" else echo "$timestamp" : "${log_priority}${space_char} : $label : ${logmessage}" - updateDialogProgressText "${logmessage}" -# updateDialogProgress "increment" fi done <<< "$log_message" fi @@ -649,7 +645,7 @@ installFromPKG() { # install pkg printlog "Installing $archiveName to $targetDir" - if [[ $DIALOG_PROGRESS == "main" || $DIALOG_PROGRESS == "list" ]]; then + if [[ $DIALOG_CMD_FILE != "" ]]; then # pipe pipe="$tmpDir/installpipe" # initialise named pipe for installer output @@ -659,14 +655,11 @@ installFromPKG() { readPKGInstallPipe $pipe "$DIALOG_CMD_FILE" & installPipePID=$! printlog "listening to output of installer with pipe $pipe and command file $DIALOG_CMD_FILE on PID $installPipePID" DEBUG - # curl (extract - line in "# MARK: download the archive" of Installomator.sh) pkgInstall=$(installer -verboseR -pkg "$archiveName" -tgt "$targetDir" 2>&1 | tee $pipe) pkgInstallStatus=$pipestatus[1] # because we are tee-ing the output, we want the pipe status of the first command in the chain, not the most recent one killProcess $installPipePID - #enableDialogButtonAndSetToDone "$DIALOGCMDFILE" - #quitDialog $DIALOGCMDFILE else pkgInstall=$(installer -verbose -dumplog -pkg "$archiveName" -tgt "$targetDir" 2>&1) pkgInstallStatus=$(echo $?) @@ -853,11 +846,9 @@ runUpdateTool() { finishing() { printlog "Finishing..." - if [[ $DIALOG_PROGRESS == "main" || $DIALOG_PROGRESS == "list" ]]; then - updateDialogProgress "complete" - fi + updateDialog "wait" "Finishing..." - sleep 5 # wait a moment to let spotlight catch up + sleep 3 # wait a moment to let spotlight catch up getAppVersion if [[ -z $appversion ]]; then @@ -940,12 +931,7 @@ readDownloadPipe() { fi if [[ $char == % ]]; then - if [[ $DIALOG_PROGRESS == "main" ]]; then - updateDialogProgressText "Downloading $name - $progress%" - updateDialogProgress "$progress" - elif [[ $DIALOG_PROGRESS == "list" ]]; then - updateDialogListItem $progress $name "Downloading..." $log - fi + updateDialog $progress "Downloading $name - $progress%" progress="" keep=0 fi @@ -971,12 +957,7 @@ readPKGInstallPipe() { progress="$progress$char" fi if [[ $char == . && $keep == 1 ]]; then - if [[ $DIALOG_PROGRESS == "main" ]]; then - updateDialogProgressText "Installing $appname $progress%" $log - updateDialogProgress "$progress" $log - elif [[ $DIALOG_PROGRESS == "list" ]]; then - updateDialogListItem $progress $name "Installing..." $log - fi + updateDialog $progress "Installing $appname" progress="" keep=0 fi @@ -988,22 +969,40 @@ killProcess() { builtin kill $1 2>/dev/null } -updateDialogProgress() { - local progress=$1 - local log=${2:-$DIALOG_CMD_FILE} - echo "progress: $progress" >> $log -} +updateDialog() { + local state=$1 + local message=$2 + local listitem=${3:-$DIALOG_LIST_ITEM_NAME} + local cmd_file=${4:-$DIALOG_CMD_FILE} + local progress="" -updateDialogProgressText() { - local message=$1 - local log=${2:-$DIALOG_CMD_FILE} - echo "progresstext: $message" >> $log -} + if [[ state =~ [0-9]* \ + || state == "reset" \ + || state == "increment" \ + || state == "complete" \ + || state == "indeterminate" ]]; then + progress=$state + fi -updateDialogListItem() { - local progress=$1 - local listitem=${2:-$name} - local action=${3:-"Processing..."} - local log=${4:-$DIALOG_CMD_FILE} - echo "listitem: title: $listitem, statustext: $action, progress: $progress" >> $log + # when to cmdfile is set, do nothing + if [[ $$cmd_file == "" ]]; then + return + fi + + if [[ $listitem == "" ]]; then + # no listitem set, update main progress bar and progress text + if [[ $progress != "" ]]; then + echo "progress: $progress" >> $cmd_file + fi + if [[ $message != "" ]]; then + echo "progresstext: $message" >> $cmd_file + fi + else + # list item has a value, so we update the progress and text in the list + if [[ $progress != "" ]]; then + echo "listitem: title: $listitem, statustext: $message, progress: $progress" >> $cmd_file + else + echo "listitem: title: $listitem, statustext: $message, status: $state" >> $cmd_file + fi + fi } diff --git a/fragments/header.sh b/fragments/header.sh index e2ba549..b8baf3d 100644 --- a/fragments/header.sh +++ b/fragments/header.sh @@ -137,14 +137,24 @@ IGNORE_DND_APPS="" # example that will ignore browsers when evaluating DND: # IGNORE_DND_APPS="firefox,Google Chrome,Safari,Microsoft Edge,Opera,Amphetamine,caffeinate" -# show swiftDialog window with download and install progress -DIALOG_PROGRESS="no" -# options: -# - no do not show download or install progress (default) -# - main show download or install progress in the main progress bar -# - list show download or install progress with each list item -# the `-list` option requires the DIALOG_LIST_INDEX to be set -DIALOG_CMD_FILE="/private/var/tmp/dialog.log" + +# Swift Dialog integration + +# These variables will allow Installomator to communicate progress with Swift Dialog +# https://github.com/bartreardon/swiftDialog + +# This requires Swift Dialog 2.11.2 or higher. + +DIALOG_CMD_FILE="" +# When this variable is set, Installomator will write Swift Dialog commands to this path. +# Installomator will not launch Swift Dialog. The process calling Installomator will have +# launch and configure Swift Dialog to listen to this file. +# See `MDM/swiftdialog_example.sh` for an example. + +DIALOG_LIST_ITEM_NAME="" +# When this variable is set, progress for downloads and installs will be sent to this +# listitem. +# When the variable is unset, progress will be sent to Swift Dialog's main progress bar. diff --git a/fragments/main.sh b/fragments/main.sh index 864f671..6cf93b9 100644 --- a/fragments/main.sh +++ b/fragments/main.sh @@ -195,7 +195,7 @@ else fi fi - if [[ $DIALOG_PROGRESS == "main" || $DIALOG_PROGRESS == "list" ]]; then + if [[ $DIALOG_CMD_FILE != "" ]]; then # pipe pipe="$tmpDir/downloadpipe" # initialise named pipe for curl output @@ -205,14 +205,11 @@ else readDownloadPipe $pipe "$DIALOG_CMD_FILE" & downloadPipePID=$! printlog "listening to output of curl with pipe $pipe and command file $DIALOG_CMD_FILE on PID $downloadPipePID" DEBUG - # curl (extract - line in "# MARK: download the archive" of Installomator.sh) curlDownload=$(curl -fL -# --show-error ${curlOptions} "$downloadURL" -o "$archiveName" 2>&1 | tee $pipe) # because we are tee-ing the output, we want the pipe status of the first command in the chain, not the most recent one curlDownloadStatus=$(echo $pipestatus[1]) killProcess $downloadPipePID - #enableDialogButtonAndSetToDone "$DIALOGCMDFILE" - #quitDialog $DIALOGCMDFILE else printlog "No Dialog connection, just download" DEBUG curlDownload=$(curl -v -fsL --show-error ${curlOptions} "$downloadURL" -o "$archiveName" 2>&1)