-
Notifications
You must be signed in to change notification settings - Fork 20
/
pack.sh
executable file
·146 lines (118 loc) · 3.08 KB
/
pack.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
#!/usr/bin/env sh
# ======================================================================
# Pack a chrome extension into a zip archive
# ======================================================================
set -e
# ======================================================================
# Function definitions
# ======================================================================
usage () {
cat 1>&2 <<EOF
COMMON USAGE
Simply run \`pack.sh .' in the extension directory which contains
\`exclude.lst' (see below.)
SYNOPSIS
pack.sh [-h] [-x <file>] <extension-directory>
DESCRIPTION
Pack.sh packs a chrome extension into a zip archive, which can be
uploaded to the Chrome Web Store.
OPTIONS
-x <file>
Specify a file which contains file patterns.
Files that match a pattern are excluded from the archive.
Default to \`exclude.lst'.
-h
Display help (this text)
EOF
}
test_dir () {
# a target directory
local dir
# do not show a message ('yes') or do ('no')
# default to 'no'
local quiet
dir="$1"
quiet="$2"
if [ ! -e "$dir" ]
then
[ -z "$quiet" ] && echo "\`$dir' does not exist." 1>&2
return 1
fi
if [ ! -d "$dir" ]
then
[ -z "$quiet" ] && echo "\`$dir' is not a directory." 1>&2
return 1
fi
if [ ! -x "$dir" ]
then
[ -z "$quiet" ] && echo "\`$dir' cannot be searched." 1>&2
return 1
fi
}
test_file () {
# a target filename
local filename
# do not show a message ('yes') or do ('no')
# default to 'no'
local quiet
filename="$1"
quiet="$2"
if [ ! -e "$filename" ]
then
[ -z "$quiet" ] && echo "\`$filename' does not exist." 1>&2
return 1
fi
if [ ! -f "$filename" ]
then
[ -z "$quiet" ] && echo "\`$filename' is not a regular file." 1>&2
return 1
fi
}
# derive an archive name from a directory name
archive_name () {
# the extension directory
local ext_dir
basename $(pwd)
}
main () {
# the extension directory
local ext_dir
# a file which contains the list of patterns that excludes the
# matching files from the zip archive
local exclude_lst
ext_dir="$1"
exclude_lst="$2"
# check if the extension directory exists
test_dir "$ext_dir"
# check if the exclusion list exists
[ ! -z "$exclude_lst" ] && test_file "$exclude_lst"
# compress it!
if [ -z "$exclude_lst" ]
then
zip -r $(archive_name "$ext_dir") "$ext_dir"
else
zip -r $(archive_name "$ext_dir") "$ext_dir" --exclude @"$exclude_lst"
fi
}
# ======================================================================
# Execution starts here
# ======================================================================
# process options
while getopts x:h opt
do
case $opt in
x) exclude_lst="$OPTARG";;
h) usage; exit 0;;
\?) usage; exit 1;;
esac
done
shift `expr $OPTIND - 1`
if [ $# -ne 1 ]
then
usage
exit 1
fi
# configure
ext_dir="$1"
# get the work done
main "$ext_dir" "$exclude_lst"