<commit_before>#!/bin/bash
#---
# Zula Framework building tools
#
# @author <NAME>
# @author <NAME>
# @author <NAME>
# @copyright Copyright (c) 2010, <NAME>
# @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL 2
# @package Zula_Utils
#---

## All options and tasks with their default values
OPT_PATH_CLOSURE=/usr/share/java/closure-compiler/closure-compiler.jar
OPT_VERBOSE=false
OPT_PACKAGE_MODE=production
OPT_IS_MSWAG=false

TASK_JS_COMPRESS=false
TASK_PACKAGE=false
TASK_CHECK_EXCEPTIONS=false

while [[ $1 == -* ]]; do
	case "$1" in
		-j)
			TASK_JS_COMPRESS=true
			if [ $# -gt 1 -a "${2:0:1}" != "-" ]; then
				OPT_PATH_CLOSURE="$2"
				shift 2;
			else
				shift
			fi
			;;
		-m)
			OPT_IS_MSWAG=true
			shift
			;;
		-p)
			TASK_PACKAGE=true
			if [ $# -gt 1 -a "${2:0:1}" != "-" ]; then
				OPT_PACKAGE_MODE="$2"
				shift 2;
			else
				shift
			fi
			;;
		-v)
			OPT_VERBOSE=true
			shift
			;;
		-x)
			TASK_CHECK_EXCEPTIONS=true
			shift
			;;
		-h)
			echo -e "Zula Framework building tools.\nUsage: build.sh [-j [jar-path]] [-p [mode] [-m]] [-xvh]"
			echo "Options:"
			echo -e "\t-j\tCompress source JavaScript files using Google Closure Compiler (requires Java)"
			echo -e "\t-m\tCreates Microsoft Web App Gallery package (used only with '-p')."
			echo -e "\t-p\tCreates .tar.gz, .tar.bz2 and .zip archives. This implies '-j' always. Zula" \
					"\n\t\tapplication mode argument optional, 'development' or 'production' which" \
					"\n\t\tdefaults to 'production'."
			echo -e "\t-x\tCheck all thrown PHP exceptions are defined, and list those not used."
			echo -e "\t-v\tBe more verbose with output, providing more detail."
			echo -e "\t-h\tDisplays this help text.\n"
			echo "Report bugs to <<EMAIL>>"
			exit 0
			;;
		-*)
			echo "Invalid argument '$1'. See '-h' for help text."
			exit 1
			;;
	esac
done

##
# Returns if Verbose mode is enabled
##
verbose() {
	[ $OPT_VERBOSE == "true" ] && return 1 || return 0
}

##
## Begin processing of the tasks
##
if [ $TASK_JS_COMPRESS == "true" -o $TASK_PACKAGE == "true" ]; then
	verbose || echo -ne ":: Compressing source JavaScript files (*.src.js) "
	if [ -n "$JAVA_HOME" ]; then
		javaBin="${JAVA_HOME}/bin/java"
	else
		javaBin=`which java`
		if [ $? == "1" ]; then
			echo -e "\n---- 'java' bin not found. Please set JAVA_HOME variable or install Java."
			exit 1
		fi
	fi
	if [ ! -f $OPT_PATH_CLOSURE ]; then
		echo -e "\n---- jar file '$OPT_PATH_CLOSURE' does not exist."
		exit 1
	fi
	for sourceFile in `find . -name "*.src.js"`; do
		fileName=`basename $sourceFile .src.js`.js
		$javaBin -jar $OPT_PATH_CLOSURE --js $sourceFile --charset utf-8 --js_output_file `dirname $sourceFile`/$fileName \
					  --warning_level QUIET
		verbose || echo -ne "."
	done
	verbose || echo " done!"
fi

if [ $TASK_CHECK_EXCEPTIONS == "true" ]; then
	##
	## List all exceptions that a) Are defined but not thrown B) Thrown but not defined
	##
	builtinExceptions="Exception PDOException"

	expThrown=$(find . -name '*.php' -not -path "*/3rd_party/*" -print0 | xargs -0 grep 'throw new' |
				perl -pi -e 's/.*throw new ([^\(; ]+).*/\1/' |
				sort | uniq | grep -viE "^(${builtinExceptions// /|})$" |
				tr '\n' ' ' | sed 's/ $//')
	expDefined=$(find . -name '*.php' -not -path "*/3rd_party/*" -print0 | xargs -0 grep -iE "extends (${builtinExceptions// /|})" |
				 perl -pi -e 's/.* ([^\(; ]+) extends.*/\1/' |
				 sort | uniq | tr '\n' ' ' | sed 's/ $//')
	## Classes which extend previously discovered exceptions
	toCheck="$expDefined"
	while [ ! -z "$toCheck" ]; do
		expDefinedTmp=$(find . -name '*.php' -not -path "*/3rd_party/*" -print0 | xargs -0 grep -iE "extends (${toCheck// /|})" |
						perl -pi -e 's/.* ([^\(; ]+) extends.*/\1/' |
						sort | uniq | tr '\n' ' ' | sed 's/ $//')
		expDefined="$expDefined ${expDefinedTmp}"
		toCheck="${expDefinedTmp}"
	done
	expDefined=$(echo $expDefined | tr ' ' '\n' | sort | tr '\n' ' ' | sed 's/ $//')

	## Display the totals and exception names
	echo ":: Exceptions that need to be defined"
	for thrownException in $expThrown; do
		for definedException in $expDefined; do
			if [ ${thrownException} == ${definedException} ]; then
				continue 2
			fi
		done
		echo -e "\t${thrownException}"
	done

	echo ":: Exceptions that are unused"
	for definedException in $expDefined; do
		for thrownException in $expThrown; do
			if [ ${definedException} == ${thrownException} ]; then
				continue 2
			fi
		done
		echo -e "\t${definedException}"
	done
fi

if [ $TASK_PACKAGE == "true" ]; then
	##
	## Create the tar.gz, tar.bz2 and .zip archive of this project
	##
	verbose || echo -ne ":: Creating package archives "
	if [ ! -f "index.php" ]; then
		echo -e "\n---- unable to find 'index.php', unable to create project packages"
		exit 1
	fi

	projectId=`grep -oP "(?<=_PROJECT_ID', ')([^']*)" index.php`
	projectVersion=`grep -oP "(?<=version\s=\s).*$" config/default.dist/config.ini.php`
	packageName="${projectId}-${projectVersion}"

	tmpDir=`mktemp -d`"/${packageName}"
	curPwd=$PWD

	if [ -d ".git" ]; then
		git checkout-index -a -f --prefix="${tmpDir}/"
	elif [ -d ".svn" ]; then
		svn export . "${tmpDir}/"
	else
		cp -ar . $tmpDir
	fi

	## Move some files around and do certain edits depending on application mode
	rm -rf "${tmpDir}/config/default" "${tmpDir}/tmp/*" "${tmpDir}/application/logs/*.log" \
		   "${tmpDir}/assets/uploads/*" "${tmpDir}/.gitignore" "${tmpDir}/*~"
	touch "${tmDir}/tmp/index.html" "${tmpDir}/assets/uploads/index.html" 2> /dev/null
	mv "${tmpDir}/config/default.dist" "${tmpDir}/config/default"

	## Edit and remove some files depending on required application mode
	if [ "$OPT_PACKAGE_MODE" == "production" ]; then<commit_msg>Added lint check for *.php, *.html and *.txt to build.sh via new '-l' flag<commit_after>#!/bin/bash
#---
# Zula Framework building tools
#
# @author <NAME>
# @author <NAME>
# @author <NAME>
# @copyright Copyright (c) 2010, <NAME>
# @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL 2
# @package Zula_Utils
#---

## All options and tasks with their default values
OPT_PATH_CLOSURE=/usr/share/java/closure-compiler/closure-compiler.jar
OPT_VERBOSE=false
OPT_PACKAGE_MODE=production
OPT_IS_MSWAG=false

TASK_JS_COMPRESS=false
TASK_PACKAGE=false
TASK_CHECK_EXCEPTIONS=false
TASK_LINT=false

while [[ $1 == -* ]]; do
	case "$1" in
		-j)
			TASK_JS_COMPRESS=true
			if [ $# -gt 1 -a "${2:0:1}" != "-" ]; then
				OPT_PATH_CLOSURE="$2"
				shift 2;
			else
				shift
			fi
			;;
		-l)
			TASK_LINT=true
			shift
			;;
		-m)
			OPT_IS_MSWAG=true
			shift
			;;
		-p)
			TASK_PACKAGE=true
			if [ $# -gt 1 -a "${2:0:1}" != "-" ]; then
				OPT_PACKAGE_MODE="$2"
				shift 2;
			else
				shift
			fi
			;;
		-v)
			OPT_VERBOSE=true
			shift
			;;
		-x)
			TASK_CHECK_EXCEPTIONS=true
			shift
			;;
		-h)
			echo -e "Zula Framework building tools.\nUsage: build.sh [-j [jar-path]] [-p [mode] [-m]] [-vxlh]"
			echo "Options:"
			echo -e "\t-j\tCompress source JavaScript files using Google Closure Compiler (requires Java)"
			echo -e "\t-l\tCheck PHP syntax on *.php, *.html and *.txt files (lint check)."
			echo -e "\t-m\tCreates Microsoft Web App Gallery package (used only with '-p')."
			echo -e "\t-p\tCreates .tar.gz, .tar.bz2 and .zip archives. This implies '-l' and '-j'. Zula" \
					"\n\t\tapplication mode argument optional, 'development' or 'production' which" \
					"\n\t\tdefaults to 'production'."
			echo -e "\t-x\tCheck all thrown PHP exceptions are defined, and list those not used."
			echo -e "\t-v\tBe more verbose with output, providing more detail."
			echo -e "\t-h\tDisplays this help text.\n"
			echo "Report bugs to <<EMAIL>>"
			exit 0
			;;
		-*)
			echo "Invalid argument '$1'. See '-h' for help text."
			exit 1
			;;
	esac
done

##
# Returns if Verbose mode is enabled
##
verbose() {
	[ $OPT_VERBOSE == "true" ] && return 1 || return 0
}

##
## Begin processing of the tasks
##
if [ $TASK_LINT == "true" ]; then
	verbose || echo -n ":: PHP syntax check on *.php, *.html and *.txt files (this may take a while) ....."
	failedCount=0
	for file in `find . -name "*.php" -or -name "*.html" -or -name "*.txt"`; do
		lintResult=$(php -l $file 2>&1 > /dev/null)
		if [ $? -gt 0 ]; then
			if [ $failedCount -eq 0 -a $OPT_VERBOSE == "true" ]; then
				echo "" # Add a new line
			fi
			echo $lintResult
			let failedCount++
		fi
	done
	if [ $failedCount -gt 0 -a "$TASK_PACKAGE" == "true" ]; then
		## See if the user wants to continue if there are syntax errors
		echo
		read -p "---- PHP syntax errors detected, do you wish to continue? (y/n) [n]: "
		if [ "$REPLY" != "y" ]; then
			exit 0
		fi
	elif [ $failedCount -eq 0 -a $OPT_VERBOSE == "true" ]; then
		echo  " done!"
	fi
fi

if [ $TASK_JS_COMPRESS == "true" -o $TASK_PACKAGE == "true" ]; then
	verbose || echo -ne ":: Compressing source JavaScript files (*.src.js) "
	if [ -n "$JAVA_HOME" ]; then
		javaBin="${JAVA_HOME}/bin/java"
	else
		javaBin=`which java`
		if [ $? -eq 1 ]; then
			echo -e "\n---- 'java' bin not found. Please set JAVA_HOME variable or install Java."
			exit 2
		fi
	fi
	if [ ! -f $OPT_PATH_CLOSURE ]; then
		echo -e "\n---- jar file '$OPT_PATH_CLOSURE' does not exist."
		exit 2
	fi
	for sourceFile in `find . -name "*.src.js"`; do
		fileName=`basename $sourceFile .src.js`.js
		$javaBin -jar $OPT_PATH_CLOSURE --js $sourceFile --charset utf-8 --js_output_file `dirname $sourceFile`/$fileName \
					  --warning_level QUIET
		verbose || echo -ne "."
	done
	verbose || echo " done!"
fi

if [ $TASK_CHECK_EXCEPTIONS == "true" ]; then
	##
	## List all exceptions that a) Are defined but not thrown B) Thrown but not defined
	##
	builtinExceptions="Exception PDOException"

	expThrown=$(find . -name '*.php' -not -path "*/3rd_party/*" -print0 | xargs -0 grep 'throw new' |
				perl -pi -e 's/.*throw new ([^\(; ]+).*/\1/' |
				sort | uniq | grep -viE "^(${builtinExceptions// /|})$" |
				tr '\n' ' ' | sed 's/ $//')
	expDefined=$(find . -name '*.php' -not -path "*/3rd_party/*" -print0 | xargs -0 grep -iE "extends (${builtinExceptions// /|})" |
				 perl -pi -e 's/.* ([^\(; ]+) extends.*/\1/' |
				 sort | uniq | tr '\n' ' ' | sed 's/ $//')
	## Classes which extend previously discovered exceptions
	toCheck="$expDefined"
	while [ ! -z "$toCheck" ]; do
		expDefinedTmp=$(find . -name '*.php' -not -path "*/3rd_party/*" -print0 | xargs -0 grep -iE "extends (${toCheck// /|})" |
						perl -pi -e 's/.* ([^\(; ]+) extends.*/\1/' |
						sort | uniq | tr '\n' ' ' | sed 's/ $//')
		expDefined="$expDefined ${expDefinedTmp}"
		toCheck="${expDefinedTmp}"
	done
	expDefined=$(echo $expDefined | tr ' ' '\n' | sort | tr '\n' ' ' | sed 's/ $//')

	## Display the totals and exception names
	echo ":: Exceptions that need to be defined"
	for thrownException in $expThrown; do
		for definedException in $expDefined; do
			if [ ${thrownException} == ${definedException} ]; then
				continue 2
			fi
		done
		echo -e "\t${thrownException}"
	done

	echo ":: Exceptions that are unused"
	for definedException in $expDefined; do
		for thrownException in $expThrown; do
			if [ ${definedException} == ${thrownException} ]; then
				continue 2
			fi
		done
		echo -e "\t${definedException}"
	done
fi

if [ $TASK_PACKAGE == "true" ]; then
	##
	## Create the tar.gz, tar.bz2 and .zip archive of this project
	##
	verbose || echo -ne ":: Creating package archives "
	if [ ! -f "index.php" ]; then
		echo -e "\n---- unable to find 'index.php', unable to create project packages"
		exit 2
	fi

	projectId=`grep -oP "(?<=_PROJECT_ID', ')([^']*)" index.php`
	projectVersion=`grep -oP "(?<=version\s=\s).*$" config/default.dist/config.ini.php`
	packageName="${projectId}-${projectVersion}"

	tmpDir=`mktemp -d`"/${packageName}"
	curPwd=$<PASSWORD>

	if [ -d ".git" ]; then
		git checkout-index -a -f --prefix="${tmpDir}/"
	elif [ -d ".svn" ]; then
		svn export . "${tmpDir}/"
	else
		cp -ar . $tmpDir
	fi

	## Move some files around and do certain edits depending on application mode
	rm -rf "${tmpDir}/config/default" "${tmpDir}/tmp/*" "${tmpDir}/application/logs/*.log" \
		   "${tmpDir}/assets/uploads/*" "${tmpDir}/.gitignore" "${tmpDir}/*~"
	touch "${tmDir}/tmp/index.html" "${tmpDir}/assets/uploads/index.html" 2> /dev/null
	mv "${tmpDir}/config/default.dist" "${tmpDir}/config/default"

	## Edit and remove some files depending on required application mode
	if [ "$OPT_PACKAGE_MODE" == "production" ]; then
{'fraction_non_alphanumeric': 0.18036750483558994, 'fraction_numerical': 0.008381689232753063, 'mean_word_length': 3.030204611886976, 'pattern_counts': {'":': 9, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5345799', 'n_tokens_mistral': 5085, 'n_tokens_neox': 4539, 'n_words': 1346}