build.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/bin/sh
  2. # Part of Zombie Navigator
  3. # Copyright © 2016 Zombie Navigator Developers
  4. addonExt=".xpi"
  5. # TODO: Colors not good
  6. TERM_OKBLUE='\033[94m'
  7. #TERM_OKGREEN='\033[92m'
  8. TERM_OKGREEN='\033[32m'
  9. #TERM_WARNING='\033[93m'
  10. TERM_WARNING='\033[33m'
  11. TERM_FAIL='\033[91m'
  12. TERM_UNIMPORTANT='\033[95m'
  13. TERM_TEST='\033[96m'
  14. #TERM_TEST='\033[37m'
  15. TERM_BOLD='\033[1m'
  16. TERM_UNDERLINE='\033[4m'
  17. TERM_ENDC='\033[0m'
  18. mylog () {
  19. [ -t 2 ] && [ -z "$ZOMBIE_NOCOLOR" ] && {
  20. printf "${TERM_UNIMPORTANT}[%s]${TERM_ENDC} ${TERM_OKBLUE}%s${TERM_ENDC}\n" "`date`" "$@" >&2
  21. :;} || {
  22. echo "[`date`] $@" >&2
  23. }
  24. }
  25. mynotice () {
  26. [ -t 2 ] && [ -z "$ZOMBIE_NOCOLOR" ] && {
  27. printf "${TERM_UNIMPORTANT}[%s]${TERM_ENDC} ${TERM_OKGREEN}%s${TERM_ENDC}\n" "`date`" "$@" >&2
  28. :;} || {
  29. echo "[`date`] $@" >&2
  30. }
  31. }
  32. mywarn () {
  33. [ -t 2 ] && [ -z "$ZOMBIE_NOCOLOR" ] && {
  34. printf "${TERM_BOLD}[%s]${TERM_ENDC} ${TERM_UNDERLINE}Warning${TERM_ENDC}: ${TERM_WARNING}%s${TERM_ENDC}\n" "`date`" "$@" >&2
  35. :;} || {
  36. echo "[`date`] Warning: $@" >&2
  37. }
  38. }
  39. unset LANGUAGE
  40. LANG=C
  41. LC_ALL=C
  42. dir="`dirname "$0"`"
  43. dir="`cd "$dir"; cd .. ; pwd`"
  44. cd "$dir/src"
  45. . ../version_info
  46. # Create .xpi
  47. while [ "$#" -gt 0 ] ; do
  48. case "$1" in
  49. --minify-js)
  50. minify_js=1
  51. ;;
  52. --help)
  53. echo "Usage: $0 [--minify-js]"
  54. exit
  55. ;;
  56. --version)
  57. echo "Will build: '$addon_version'"
  58. exit
  59. ;;
  60. esac
  61. shift
  62. done
  63. mylog "$addon_id version $addon_version build script"
  64. buildsDir="$dir/builds"
  65. buildTmpDir="$dir/.build"
  66. mkdir -p "$buildsDir"
  67. rm -rf "$buildTmpDir"
  68. cd ..
  69. cp -rf src "$buildTmpDir"
  70. cd "$buildTmpDir"
  71. mylog "Excluding development files..."
  72. rm -fv ./debug.js
  73. rm -frv ./data/debug/
  74. mylog "Optimizing files..."
  75. find . -type f \( -name '*.html' -or -name '*.js' \) -execdir sh -c 'egrep -v "^\s*$|vim:" "$1" > "$1.tmp" && mv -f "$1.tmp" "$1" || rm -f "$1.tmp"' -- {} \;
  76. find . -type f -name '*.css' -execdir sh -c 'egrep -v "^\s*$|vim:" "$1" | tr -d "\t\n" | perl -pe "s/\s*([{:>+,])\s*/\1/g ; s/;}/}/g" > "$1.tmp" && mv -f "$1.tmp" "$1" || rm -f "$1.tmp"' -- {} \;
  77. find . -type f -name '*.json' -execdir sh -c 'egrep -v "^\s*$" "$1" | sed "s/^ *//" | tr -d "\n" > "$1.tmp" && mv -f "$1.tmp" "$1" || rm -f "$1.tmp"' -- {} \;
  78. #find . -type f -name '*.js' -exec sh -c 'egrep -v "^\s*@([^l]([^i]([^c]([^e]([^n]([^s]([^e].*)?)?)?)?)?)?)?($|[^a-z]+)" "$1" > "$1.tmp" && mv -f "$1.tmp" "$1" || rm -f "$1.tmp"' -- {} \;
  79. jsmin_sed_path="$dir/tools/optimization/js-min.sed"
  80. jsmin_egrep='^\s*(/\*\*?)?\s*@(file|module|public|method|private|protected)($|[^a-z])|^\s*/\*\*\s*[Ee]xports\s\*/\s*$|^\s*/\*!_DEBUGONLY_!\*/'
  81. find . -type f -name '*.js' -exec sh -c 'egrep -v "$3" "$1" | sed -f "$2" > "$1.tmp" && mv -f "$1.tmp" "$1" || rm -f "$1.tmp"' -- {} "$jsmin_sed_path" "$jsmin_egrep" \;
  82. #grep -v '^\s*$' < './install.rdf' > './install.rdf~'
  83. #sed 's/^\s*//' < './install.rdf~' > './install.rdf'
  84. #rm -fv './install.rdf~'
  85. [ "$minify_js" ] && {
  86. mynotice "EXPERIMENTAL: Doing JS minification..."
  87. mynotice "This may result in problems!"
  88. find . -type f -name '*.js' -exec sh -c 'echo "Compressing: $1" ; ../bin/estidy < "$1" > "$1.tmp" && mv -f "$1.tmp" "$1" || rm -f "$1.tmp"' -- {} \;
  89. } || {
  90. mylog "Skipping JS minification!"
  91. }
  92. cat ./build.meta >> ./index.js
  93. du -a -h --apparent-size --max-depth 1 || du -ahd1
  94. mylog "Building the addon..."
  95. mynotice "Please ignore JPM warnings about the 'semver' (if any) (NOT relevant for stable release versions)"
  96. mylog "Because we use '~' for prereleases (for consistence with GNU/Linux conventions)"
  97. ../bin/build | egrep -v 'semver|^\s*$|semantic|versioning rules|values indicating major,|^"1\.4\.56"|^"0\.0\.1"$|^"1\.0\.0rc4"$' || {
  98. mylog "[ERROR] JPM: Failed"
  99. exit 1
  100. }
  101. if which unzip >/dev/null 2>&1 ; then
  102. mkdir .built
  103. cd .built
  104. unzip ../*"${addonExt}" | tr '\n' '\r'
  105. s10=' '
  106. printf "$s10$s10$s10$s10$s10$s10$s10$s10\r"
  107. grep -v '^\s*$' < './install.rdf' > './install.rdf~'
  108. sed 's/^\s*//' < './install.rdf~' > './install.rdf'
  109. rm -fv './install.rdf~'
  110. # This is safe
  111. buildName=`printf '%s' ../*"${addonExt}"`
  112. rm "$buildName"
  113. # Skipping directory entries
  114. # Assuming no space characters in file names
  115. zip ../.rebuild.zip `find . -type f` | tr '\n' '\r'
  116. printf "$s10$s10$s10$s10$s10$s10$s10$s10\r"
  117. mv -f ../.rebuild.zip "$buildName"
  118. cd ..
  119. else
  120. mywarn "NOTE: Skipping post-build optimization (unzip needed)..."
  121. fi
  122. if which advzip >/dev/null 2>&1 ; then
  123. # AdvanceCOMP available
  124. mylog "Recompressing..."
  125. advzip -k -z -4 -i 200 ./*"${addonExt}"
  126. else
  127. mywarn "NOTE: Skipping recompression (AdvanceCOMP needed)..."
  128. fi
  129. if which strip-nondeterminism >/dev/null 2>&1 ; then
  130. # strip-nondeterminism available
  131. mylog "Trying to strip nondeterminism (not yet tested)..."
  132. strip-nondeterminism -t zip ./*"${addonExt}"
  133. else
  134. mywarn "NOTE: Deterministic build disabled (strip-nondeterminism needed)..."
  135. fi
  136. sha256sum ./*"${addonExt}"
  137. mylog "Saving the archive..."
  138. targetDir="$buildsDir/`date +%s`"
  139. [ "$minify_js" ] && targetDir="$targetDir.min-js"
  140. mkdir -p "$targetDir"
  141. mv -b -v ./*"${addonExt}" "$targetDir"/ || mv -n -v ./*"${addonExt}" "$targetDir"/
  142. chmod a-wx "$targetDir"/*
  143. (
  144. cd "$buildsDir"
  145. rm -f latest
  146. ln -sv "./`basename "$targetDir"`" latest
  147. )
  148. cd ..
  149. rm -rf "$buildTmpDir"
  150. mynotice "Done? ($addon_id $addon_version)"
  151. # vim: ts=4 noet ai