Bash build script

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 23:27, 10 June 2006
Asqueella (Talk | contribs)
(Script 1 - exclude SVN files from packaging)
<-- Previous diff
Revision as of 15:45, 28 June 2006
Asqueella (Talk | contribs)
(Script 1 - zip doesn't like when it's handed a lot of files on the command line, make it read from stdin instead)
Next diff -->
Line 81: Line 81:
done done
-zip -0 -r $JAR_FILE `cat files`+zip -0 -r $JAR_FILE -@ < files
# The following statement should be used instead if you don't wish to use the JAR file # The following statement should be used instead if you don't wish to use the JAR file
#cp --verbose --parents `cat files` $TMP_DIR/chrome #cp --verbose --parents `cat files` $TMP_DIR/chrome

Revision as of 15:45, 28 June 2006

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

Two bash scripts that automate packaging extensions.


Script 1

Here is a bash script I'm using to package my extensions (under Cygwin). See also Windows build script.

# -- builds JAR and XPI files for mozilla extensions
#   by Nickolay Ponomarev <>
#   (original version based on Nathan Yergler's build script)
# Most recent version is at <>

# This script assumes the following directory structure:
# ./
#   chrome.manifest (optional - for newer extensions)
#   install.rdf
#   (other files listed in $ROOT_FILES)
#   content/    |
#   locale/     |} these can be named arbitrary and listed in $CHROME_PROVIDERS
#   skin/       |
#   defaults/   |
#   components/ |} these must be listed in $ROOT_DIRS in order to be packaged
#   ...         |
# It uses a temporary directory ./build when building; don't use that!
# Script's output is:
# ./$APP_NAME.xpi
# ./$APP_NAME.jar  (only if $KEEP_JAR=1)
# ./files -- the list of packaged files
# Note: It modifies chrome.manifest when packaging so that it points to 
#       chrome/$APP_NAME.jar!/*

# default configuration file is ./, unless another file is 
# specified in command-line. Available config variables:
APP_NAME=          # short-name, jar and xpi files name. Must be lowercase with no spaces
CHROME_PROVIDERS=  # which chrome providers we have (space-separated list)
CLEAN_UP=          # delete the jar / "files" when done?       (1/0)
ROOT_FILES=        # put these files in root of xpi (space separated list of leaf filenames)
ROOT_DIRS=         # ...and these directories       (space separated list)
BEFORE_BUILD=      # run this before building       (bash command)
AFTER_BUILD=       # ...and this after the build    (bash command)

if [ -z $1 ]; then
  . ./
  . $1

if [ -z $APP_NAME ]; then
  echo "You need to create build config file first!"
  echo "Read comments at the beginning of this script for more info."


#uncomment to debug
#set -x

# remove any left-over files from previous build
rm -f $APP_NAME.jar $APP_NAME.xpi files
rm -rf $TMP_DIR


mkdir --parents --verbose $TMP_DIR/chrome

# generate the JAR file, excluding CVS, SVN, and temporary files
echo "Generating $JAR_FILE..."
  find $CHROME_SUBDIR \( -path '*CVS*' -o -path '*.svn*' \) -prune -o -type f -print | grep -v \~ >> files

zip -0 -r $JAR_FILE -@ < files
# The following statement should be used instead if you don't wish to use the JAR file
#cp --verbose --parents `cat files` $TMP_DIR/chrome

# prepare components and defaults
echo "Copying various files to $TMP_DIR folder..."
for DIR in $ROOT_DIRS; do
  mkdir $TMP_DIR/$DIR
  FILES="`find $DIR \( -path '*CVS*' -o -path '*.svn*' \) -prune -o -type f -print | grep -v \~`"
  echo $FILES >> files
  cp --verbose --parents $FILES $TMP_DIR

# Copy other files to the root of future XPI.
for ROOT_FILE in $ROOT_FILES install.rdf chrome.manifest; do
  cp --verbose $ROOT_FILE $TMP_DIR
  if [ -f $ROOT_FILE ]; then
    echo $ROOT_FILE >> files


if [ -f "chrome.manifest" ]; then
  echo "Preprocessing chrome.manifest..."
  # You think this is scary?
  # Then try this! (Same, but with characters escaped for bash :)
  sed -i -r s/^\(content\\s+\\S*\\s+\)\(\\S*\\/\)$/\\1jar:chrome\\/$APP_NAME\\.jar!\\/\\2/ chrome.manifest
  sed -i -r s/^\(skin\|locale\)\(\\s+\\S*\\s+\\S*\\s+\)\(.*\\/\)$/\\1\\2jar:chrome\\/$APP_NAME\\.jar!\\/\\3/ chrome.manifest

  # (it simply adds jar:chrome/whatever.jar!/ at appropriate positions of chrome.manifest)

# generate the XPI file
echo "Generating $APP_NAME.xpi..."
zip -r ../$APP_NAME.xpi *

cd "$ROOT_DIR"

echo "Cleanup..."
if [ $CLEAN_UP = 0 ]; then
  # save the jar file
  mv $TMP_DIR/chrome/$APP_NAME.jar .
  rm ./files

# remove the working files
rm -rf $TMP_DIR
echo "Done!"


Example file:


# Build config for the build script, Look there for more info.

CHROME_PROVIDERS="content locale skin"

Script 2

The extension has to be packed for distribution as .xpi file. To automate this, you can use a small shell script which does the zipping for you:

[[ "$buildid" == "" ]] && buildid=cvs-`date +%Y%m%d`
cd src
zip -r ../messenger-dispmua.jar . -x "CVS"
cd ..
mkdir chrome
mv messenger-dispmua.jar chrome/
rm "$xpifile"
zip "$xpifile" chrome/messenger-dispmua.jar install.rdf
rm -r chrome/
echo -n Created file:
ls "$xpifile"

Of course you have to change the jar file name from messenger-dispmua to one matching your extension, but you see how to do it. One more thing: If you pass a parameter to the script, it is taken as the version and appended to the name of the xpi file