Google Chrome Extension build script

If you build Chrome extensions, you probably understand it’s a bit of a pain in the bum having to move all the files into the right place (especially if you have shared assets elsewhere), keep track of versions of your package, update your update.xml, and packaging it all up into a .crx and a .zip for publishing on the Chrome webstore. I found a it a pain in the bum anyway, so I wrote a bash script to simplify the build process a bit.

It pumps out a .crx (signed with your .pem, ready for hosting on your site) and a .zip (ready for uploading to the Chrome Store).

Here it is – it should work under linux and cygwin, let me know if you have any issues in the comments 🙂 – instructions at the top of the file.

Click here to download a copy of it.

There’s an example of how to use it at the end of this post.

crx_build.sh Chrome extension build script

#!/bin/bash
# Author Seb Maynard, 2012
# https://seb.so
#
# Script to generate and package CRX chrome extensions and zip files ready for upload
# requires updates.xml, manifest.json in current folder - these shouldn't reference a version number,
# but instead have xxxxxxx where the version number will be replaced in.
#
# This should work on cygwin and *nix
#
# required variables are
# $version (appended to the built filenames - like 0.0.1)
# $files (should be an array of files to include - like (updates.xml somefile.txt ../someotherfile.txt) )
# $modulename
#
# when this script has finished running, 2 variables will be availabe for use:
#   $filename_crx
#   $filename_zip
# which can then be used (for example) to automatically update your server with the new files

# dependency checks
if [ "$version" == "" ] ; then
	echo "Need a version variable (version=...)"
	exit
fi

if [ "$files" == "" ] ; then
	echo "Need a list of files (files=...)"
	exit
fi

if [ "$modulename" == "" ] ; then
	echo "Need a modulename (modulename=...)"
	exit
fi

# Copy in the files we need
rm -r dist_tmp
mkdir dist_tmp
for i in "${files[@]}"
do
	cp "$i" dist_tmp/
done

# Sort out the version numbers
echo Building dist_tmp folder
cd dist_tmp
sed -i "s/xxxxxxx/$version/g" updates.xml
sed -i "s/xxxxxxx/$version/g" manifest.json
cd ..

# build the Chrome crx for distribution
echo Building Chrome crx
chrome_run=`which google-chrome 2>/dev/null`
cygwin=
if [ ! -f "$chrome_run" ] ; then
	cygwin=yes
	chrome_run=`cygpath -u "$APPDATA/../Local/Google/Chrome/Application/chrome.exe"`
fi
if [ -f "$chrome_run" ] ; then
	path_dist_tmp=`pwd`/dist_tmp
	path_key=`pwd`/$modulename.pem
	if [ "$cygwin" == "yes" ] ; then
		path_dist_tmp=`cygpath -w $path_dist_tmp`
		path_key=`cygpath -w $path_key`
	fi
	echo Building dist_tmp.crx in $path_dist_tmp
	$chrome_run --no-message-box --pack-extension=$path_dist_tmp --pack-extension-key=$path_key
	filename_crx=$modulename-$version.crx
	if [ -f "$filename_crx" ] ; then
		echo $filename_crx already exists\! Skipping rename
	else
		mv dist_tmp.crx $filename_crx
		echo Built $filename_crx
	fi
else
	echo "Couldn't find Chrome!"
fi

# Build the zip for uploading
cd dist_tmp
filename_zip=$modulename-$version.zip
if [ -f "../$filename_zip" ] ; then
	echo $filename_zip already exists\! Skipping zip
else
	echo Removing update site line from packaged zip
	sed -i "s/\"update_url\"\:.*$//g" manifest.json
	zip -r ../$filename_zip *
fi
cd ../

Example usage

This is my build.sh for the raytracer:

#!/bin/bash

# version
version=0.0.3

# file list
files=(
	updates.xml
	manifest.json
	sebtracer-16.png
	sebtracer-48.png
	sebtracer-128.png
	../sebtracer2.html
	../sebtracer2.nmf
	../sebtracer2_x86_32.nexe
	../sebtracer2_x86_64.nexe
	)

modulename=SebTracer

source /s/git/Code/chromeextensions/crx_build.sh

Posted

in

, ,

by

Tags:

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.