3 # Build extension crx file automatically
6 # Author: kodango <dangoakachan@foxmail.com>
7 # Homepage: http://kodango.com/buildcrx
13 Usage: `basename $0` [OPTIONS]
16 -d specify the extension directory, required
17 -p specify the pem file, optional
18 -o specify the output directory, optional
19 -n specify the build extension name, optional
20 -e specify the exclude pattern like grep, optional
21 -h show this help message
30 while getopts ":d:p:o:n:e:h" opt
; do
41 exclude_pattern
=$OPTARG;;
45 echo "[ERROR] Invalid option: -$OPTARG" >&2
48 echo "[ERROR] Option -$OPTARG requires an argument." >&2
53 if [ ! -d "$ext_dir" ]; then
54 echo "[ERROR] Extension directory '$ext_dir' doesn't exist!" >&2
58 if [ ! -f "$pem_file" ]; then
59 echo "[WARNING] Pem file doesn't exist, do you miss it?"
62 if [ -z "$output_dir" ]; then
66 if [ ! -d "$output_dir" ]; then
70 if [ -z "$ext_name" ]; then
71 ext_name
=`basename $ext_dir`
76 build_ext
="$build/$ext_name"
78 echo "[INFO] Crete build directory"
82 echo "[INFO] Copy extension files to build directory" && (
85 if [ -n "$exclude_pattern" ]; then
86 find . |
grep -vE "$exclude_pattern" |
cpio -pdm $build_ext 2>/dev
/null
88 find . |
cpio -pdm $build_ext
92 echo -n "[INFO] Search the current build version in manifest.json: "
93 version
=`awk -F'"' '/"version"/{print $4}' $build_ext/manifest.json`
96 echo -n "[INFO] Search the update url in manifest.json: "
97 update_url
=`awk -F'"' '/"update_url"/{print $4}' $build_ext/manifest.json`
100 if [ -n "$update_url" ]; then
101 update_file
="$output_dir/${update_url##*/}"
102 curl
--silent $update_url -o $update_file
104 sed -i -r "s/(<updatecheck.*version=)[^ /]+/\1'$version'/" $update_file
105 echo "[INFO] Update the version number in $update_file to $version"
108 echo "[DEBUG] Package crx file with crxmake.sh script"
110 # Codes below are taken from crxmake.sh
111 # http://code.google.com/p/chromium/issues/attachmentText?id=15059&aid=-2305436989939443553&name=crxmake.sh
113 crx
="$output_dir/$ext_name.crx"
114 pub
="$build/$ext_name.pub"
115 sig
="$build/$ext_name.sig"
116 zip="$build/$ext_name.zip"
119 (cd "$build_ext" && zip -qr -9 -X "$zip" .
)
122 openssl sha1
-sha1 -binary -sign "$pem_file" < "$zip" > "$sig"
125 openssl rsa
-pubout -outform DER
< "$pem_file" > "$pub" 2>/dev
/null
127 # Take "abcdefgh" and return it as "ghefcdab"
128 function byte_swap
()
130 echo "${1:6:2}${1:4:2}${1:2:2}${1:0:2}"
133 crmagic_hex
="4372 3234" # Cr24
134 version_hex
="0200 0000" # 2
135 pub_len_hex
=$
(byte_swap $
(printf '%08x\n' $
(ls -l "$pub" |
awk '{print $5}')))
136 sig_len_hex
=$
(byte_swap $
(printf '%08x\n' $
(ls -l "$sig" |
awk '{print $5}')))
139 echo "$crmagic_hex $version_hex $pub_len_hex $sig_len_hex" | xxd
-r -p
140 cat "$pub" "$sig" "$zip"
143 echo "[INFO] $crx is packaged successfully"