modified: tasks/fgi.wdl
[GalaxyCodeBases.git] / crx / buildcrx.sh
blob9b13cdf9798fd8d44dc9fd6dd5fa5235b8c3559c
1 #!/bin/bash
3 # Build extension crx file automatically
4 #
5 # Version: 0.0.1
6 # Author: kodango <dangoakachan@foxmail.com>
7 # Homepage: http://kodango.com/buildcrx
10 function usage()
12 cat << EOF
13 Usage: `basename $0` [OPTIONS]
15 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
22 EOF
25 if [ $# -eq 0 ]; then
26 usage
27 exit
30 while getopts ":d:p:o:n:e:h" opt; do
31 case $opt in
33 ext_dir=$OPTARG;;
35 pem_file=$OPTARG;;
37 output_dir=$OPTARG;;
39 ext_name=$OPTARG;;
41 exclude_pattern=$OPTARG;;
43 usage;;
44 \?)
45 echo "[ERROR] Invalid option: -$OPTARG" >&2
46 exit 1;;
48 echo "[ERROR] Option -$OPTARG requires an argument." >&2
49 exit 1;;
50 esac
51 done
53 if [ ! -d "$ext_dir" ]; then
54 echo "[ERROR] Extension directory '$ext_dir' doesn't exist!" >&2
55 exit 1
58 if [ ! -f "$pem_file" ]; then
59 echo "[WARNING] Pem file doesn't exist, do you miss it?"
62 if [ -z "$output_dir" ]; then
63 output_dir="./output"
66 if [ ! -d "$output_dir" ]; then
67 mkdir -p $output_dir
70 if [ -z "$ext_name" ]; then
71 ext_name=`basename $ext_dir`
74 cwd=`pwd`
75 build="$cwd/build"
76 build_ext="$build/$ext_name"
78 echo "[INFO] Crete build directory"
79 rm -rf $build
80 mkdir -p $build_ext
82 echo "[INFO] Copy extension files to build directory" && (
83 cd $ext_dir
85 if [ -n "$exclude_pattern" ]; then
86 find . | grep -vE "$exclude_pattern" | cpio -pdm $build_ext 2>/dev/null
87 else
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`
94 echo $version
96 echo -n "[INFO] Search the update url in manifest.json: "
97 update_url=`awk -F'"' '/"update_url"/{print $4}' $build_ext/manifest.json`
98 echo $update_url
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"
118 # zip up the crx dir
119 (cd "$build_ext" && zip -qr -9 -X "$zip" .)
121 # signature
122 openssl sha1 -sha1 -binary -sign "$pem_file" < "$zip" > "$sig"
124 # public key
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"
141 ) > "$crx"
143 echo "[INFO] $crx is packaged successfully"