Bug 1828523 [wpt PR 39579] - Only restore dialog focus if focus is in the dialog...
[gecko.git] / tools / update-packaging / common.sh
blob4b994f30169c9ed2b28d92dbfe7a168ad8abd514
1 #!/bin/bash
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 # Code shared by update packaging scripts.
8 # Author: Darin Fisher
11 # -----------------------------------------------------------------------------
12 QUIET=0
14 # By default just assume that these tools exist on our path
15 MAR=${MAR:-mar}
16 MBSDIFF=${MBSDIFF:-mbsdiff}
17 XZ=${XZ:-xz}
18 $XZ --version > /dev/null 2>&1
19 if [ $? -ne 0 ]; then
20 # If $XZ is not set and not found on the path then this is probably
21 # running on a windows buildbot. Some of the Windows build systems have
22 # xz.exe in topsrcdir/xz/. Look in the places this would be in both a
23 # mozilla-central and comm-central build.
24 XZ="$(dirname "$(dirname "$(dirname "$0")")")/xz/xz.exe"
25 $XZ --version > /dev/null 2>&1
26 if [ $? -ne 0 ]; then
27 XZ="$(dirname "$(dirname "$(dirname "$(dirname "$0")")")")/xz/xz.exe"
28 $XZ --version > /dev/null 2>&1
29 if [ $? -ne 0 ]; then
30 echo "xz was not found on this system!"
31 echo "exiting"
32 exit 1
36 # Ensure that we're always using the right compression settings
37 export XZ_OPT="-T1 -7e"
39 # -----------------------------------------------------------------------------
40 # Helper routines
42 notice() {
43 echo "$*" 1>&2
46 verbose_notice() {
47 if [ $QUIET -eq 0 ]; then
48 notice "$*"
52 get_file_size() {
53 info=($(ls -ln "$1"))
54 echo ${info[4]}
57 copy_perm() {
58 reference="$1"
59 target="$2"
61 if [ -x "$reference" ]; then
62 chmod 0755 "$target"
63 else
64 chmod 0644 "$target"
68 make_add_instruction() {
69 f="$1"
70 filev3="$2"
72 # Used to log to the console
73 if [ $4 ]; then
74 forced=" (forced)"
75 else
76 forced=
79 is_extension=$(echo "$f" | grep -c 'distribution/extensions/.*/')
80 if [ $is_extension = "1" ]; then
81 # Use the subdirectory of the extensions folder as the file to test
82 # before performing this add instruction.
83 testdir=$(echo "$f" | sed 's/\(.*distribution\/extensions\/[^\/]*\)\/.*/\1/')
84 verbose_notice " add-if \"$testdir\" \"$f\""
85 echo "add-if \"$testdir\" \"$f\"" >> "$filev3"
86 else
87 verbose_notice " add \"$f\"$forced"
88 echo "add \"$f\"" >> "$filev3"
92 check_for_add_if_not_update() {
93 add_if_not_file_chk="$1"
95 if [ `basename $add_if_not_file_chk` = "channel-prefs.js" -o \
96 `basename $add_if_not_file_chk` = "update-settings.ini" ]; then
97 ## "true" *giggle*
98 return 0;
100 ## 'false'... because this is bash. Oh yay!
101 return 1;
104 make_add_if_not_instruction() {
105 f="$1"
106 filev3="$2"
108 verbose_notice " add-if-not \"$f\" \"$f\""
109 echo "add-if-not \"$f\" \"$f\"" >> "$filev3"
112 make_patch_instruction() {
113 f="$1"
114 filev3="$2"
116 is_extension=$(echo "$f" | grep -c 'distribution/extensions/.*/')
117 if [ $is_extension = "1" ]; then
118 # Use the subdirectory of the extensions folder as the file to test
119 # before performing this add instruction.
120 testdir=$(echo "$f" | sed 's/\(.*distribution\/extensions\/[^\/]*\)\/.*/\1/')
121 verbose_notice " patch-if \"$testdir\" \"$f.patch\" \"$f\""
122 echo "patch-if \"$testdir\" \"$f.patch\" \"$f\"" >> "$filev3"
123 else
124 verbose_notice " patch \"$f.patch\" \"$f\""
125 echo "patch \"$f.patch\" \"$f\"" >> "$filev3"
129 append_remove_instructions() {
130 dir="$1"
131 filev3="$2"
133 if [ -f "$dir/removed-files" ]; then
134 listfile="$dir/removed-files"
135 elif [ -f "$dir/Contents/Resources/removed-files" ]; then
136 listfile="$dir/Contents/Resources/removed-files"
138 if [ -n "$listfile" ]; then
139 # Map spaces to pipes so that we correctly handle filenames with spaces.
140 files=($(cat "$listfile" | tr " " "|" | sort -r))
141 num_files=${#files[*]}
142 for ((i=0; $i<$num_files; i=$i+1)); do
143 # Map pipes back to whitespace and remove carriage returns
144 f=$(echo ${files[$i]} | tr "|" " " | tr -d '\r')
145 # Trim whitespace
146 f=$(echo $f)
147 # Exclude blank lines.
148 if [ -n "$f" ]; then
149 # Exclude comments
150 if [ ! $(echo "$f" | grep -c '^#') = 1 ]; then
151 if [ $(echo "$f" | grep -c '\/$') = 1 ]; then
152 verbose_notice " rmdir \"$f\""
153 echo "rmdir \"$f\"" >> "$filev3"
154 elif [ $(echo "$f" | grep -c '\/\*$') = 1 ]; then
155 # Remove the *
156 f=$(echo "$f" | sed -e 's:\*$::')
157 verbose_notice " rmrfdir \"$f\""
158 echo "rmrfdir \"$f\"" >> "$filev3"
159 else
160 verbose_notice " remove \"$f\""
161 echo "remove \"$f\"" >> "$filev3"
165 done
169 # List all files in the current directory, stripping leading "./"
170 # Pass a variable name and it will be filled as an array.
171 list_files() {
172 count=0
173 temp_filelist=$(mktemp)
174 find . -type f \
175 ! -name "update.manifest" \
176 ! -name "updatev2.manifest" \
177 ! -name "updatev3.manifest" \
178 | sed 's/\.\/\(.*\)/\1/' \
179 | sort -r > "${temp_filelist}"
180 while read file; do
181 eval "${1}[$count]=\"$file\""
182 (( count++ ))
183 done < "${temp_filelist}"
184 rm "${temp_filelist}"
187 # List all directories in the current directory, stripping leading "./"
188 list_dirs() {
189 count=0
190 temp_dirlist=$(mktemp)
191 find . -type d \
192 ! -name "." \
193 ! -name ".." \
194 | sed 's/\.\/\(.*\)/\1/' \
195 | sort -r > "${temp_dirlist}"
196 while read dir; do
197 eval "${1}[$count]=\"$dir\""
198 (( count++ ))
199 done < "${temp_dirlist}"
200 rm "${temp_dirlist}"