#!/bin/bash # # Copyright (C) 2010 Mike Gerwitz # # This file is part of ease.js. # # ease.js is free software: you can redistribute it and/or modify it under the # terms of the GNU Lesser General Public License as published by the Free # Software Foundation, either version 3 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License # for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see . # # PATH_TOOLS=$( dirname "$0" ) PATH_LIB="$PATH_TOOLS/../lib" MODULE_EXT='js' TPL_PATH="$PATH_TOOLS/combine.tpl" TPL_VAR='/**{CONTENT}**/' # order matters CAT_MODULES="util class interface" ## # Output template header ## tpl_header() { # cut out the top of the template (before the content location) cat "$TPL_PATH" \ | awk "{ if ( \$0 == \"$TPL_VAR\" ) exit; else print \$0; }" } ## # Output template footer ## tpl_footer() { # cut out the bottom of the template (after where we need to place the # content) cat "$TPL_PATH" \ | awk "BEGIN { go = 0 } { if ( \$0 == \"$TPL_VAR\" ) go = 1 else if ( go == 1 ) print \$0; }" } ## # Removes trailing commas from array and object declarations (certain browsers, # such as earlier versions of IE, do not parse trailing commas correctly) # # This is a very simple (dumb) system. It does not check to ensure we're not # replacing text inside a string, nor is it 100% certain we're in an array or # object declaration. However, until such an implementation is needed, I'd like # to keep it as simple (and fast) as possible. The below implementation is # suitable for our needs. ## rmtrail() { cat - \ | sed -n ' # copy first line to hold buffer 1h # if not the first line, append to hold buffer 1!H # if last line, process $ { # pull from hold and perform replacement g s/,\(\s*[]}]\)/\1/g # print result p } ' } # ensure we can locate our template (should be in the /tools dir) if [ ! -f "$TPL_PATH" ]; then echo "Error: combine.tpl not found ($TPL_PATH)" exit 1 fi # output combined file header tpl_header # output each of the modules for module in $CAT_MODULES; do filename="$PATH_LIB/$module.$MODULE_EXT" if [ ! -f "$filename" ]; then echo "Error: module $module not found ($filename)" exit 2 fi # each module must be enclosed in a closure to emulate a module echo "/** $module **/" echo "( function( exports )" echo "{" # add the module, removing trailing commas cat $filename | rmtrail echo "} )( exports['$module'] = {} );" done # output combined file footer tpl_footer exit 0