-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-workflow.sh
177 lines (160 loc) · 4.73 KB
/
install-workflow.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/bash
# This script is expected to be run from within the directory that contains the info.plist.
# Every argument is a path that is copied into the final workflow, along with info.plist.
# Arguments may be directories, which are copied as directories into the result.
#
# Note: at least one file to copy must be given, because a workflow that is comprised solely
# of info.plist/icon.png does not need an install script, and so it is assumed to be an
# error.
set -e
shopt -s nullglob
function usage() {
echo "Usage:"
echo " install-workflow.sh [-hvd] [--] FILE [FILE...]"
echo " install-workflow.sh --update-plist"
echo
echo "Options:"
echo " -h --help Displays this help"
echo " -v --verbose Shows more verbose information"
echo " -d --debug Shows debug information"
echo " --update-plist Update ./info.plist from the installed workflow"
[[ $1 != verbose ]] && return
echo
echo "When invoked with a list of files, the workflow is installed using those files"
echo "and ./info.plist (and optionally ./icon.png)."
echo
echo "When invoked with --update-plist, the installed workflow is located and the"
echo "info.plist is copied back into the development directory."
}
verbose=
debug=
while (( $# > 0 )); do
case "$1" in
-[^-]?*)
flag=${1:0:2}
rest=${1:2}
shift
set -- "$flag" -"$rest" "$@"
continue
;;
-h|--help)
usage verbose
exit
;;
-v|--verbose)
verbose=yes
;;
-d|--debug)
debug=yes
verbose=yes
;;
--update-plist)
update_plist=yes
;;
--)
shift
break
;;
-*)
echo "error: unknown flag $1" >&2
usage >&2
exit 2
;;
*)
break
;;
esac
shift
done
if [[ -n $update_plist ]]; then
if (( $# > 0 )); then
echo "error: unexpected parameter $1" >&2
usage >&2
exit 2
fi
else
if (( $# == 0 )); then
usage >&2
exit 2
fi
fi
function getBundleID() {
local path=$1
if [[ ! -f "$path" ]]; then
echo "no such file: $path" >&2
return 1
fi
/usr/libexec/PlistBuddy -c 'Print :bundleid' "$path"
}
bundleid=$(getBundleID info.plist)
syncfolder=$(defaults read com.runningwithcrayons.Alfred-Preferences syncfolder)
syncfolder=${syncfolder/#~\//"$HOME"/}
prefs=${syncfolder%/}/Alfred.alfredpreferences
if [[ ! -d "$prefs" ]]; then
echo "error: Preferences path '$prefs' does not exist or is not a folder" >&2
exit 1
fi
dest=
for workflow in "$prefs"/workflows/user.workflow.*; do
[[ $debug = yes ]] && echo "debug: checking $workflow"
workflowid=$(getBundleID "$workflow"/info.plist)
if [[ "$bundleid" = "$workflowid" ]]; then
[[ $debug = yes ]] && echo "debug: found match"
dest=$workflow
break
fi
done
if [[ -z $dest ]]; then
if [[ -n $update_plist ]]; then
echo "error: No existing installed workflow found" >&2
exit 1
fi
[[ $verbose = yes ]] && echo "No existing installed workflow found; installing new workflow"
dest=$prefs/workflows/user.workflow.$(uuidgen)
mkdir "$dest"
fi
dest_pretty=${dest//"$HOME"\//\~/}
if [[ -n $update_plist ]]; then
# copy the info.plist from the installed workflow to the local folder
plist_path=$dest/info.plist
if [[ ! -f "$plist_path" ]]; then
plist_path=$dest/Info.plist
if [[ ! -f "$plist_path" ]]; then
echo "error: can't find info.plist in $dest_pretty" >&2
exit 1
fi
fi
echo "Copying info.plist from $dest_pretty"
cp "$plist_path" ./info.plist
exit
fi
echo "Installing to $dest_pretty"
install_queue=(info.plist)
if [[ -f icon.png ]]; then
install_queue+=(icon.png)
fi
install_queue+=("$@")
[[ $debug = yes ]] && echo "debug: install queue: [${install_queue[*]}]"
declare -a to_delete
for f in "$dest"/{*,.[!.]*,.??*}; do
for g in "${install_queue[@]}"; do
if [[ "$(basename "$g")" = "$(basename "$f")" ]]; then
# found a match
continue 2
fi
done
# no match
to_delete+=("$f")
done
[[ $debug = yes ]] && echo "debug: delete queue: [${to_delete[*]}]"
[[ -d "$dest" ]] || mkdir "$dest"
for f in "${install_queue[@]}"; do
[[ $verbose = yes ]] && echo "Copying $f"
# shellcheck disable=SC2046
cp -a $([[ $debug = yes ]] && echo "-v") "${f%/}" "$dest"
done
for f in "${to_delete[@]}"; do
[[ $verbose = yes ]] && echo "Deleting $f"
# shellcheck disable=SC2046
rm -rf $([[ $debug = yes ]] && echo "-v") "$f"
done