Sorcerer's IsleCode Lucee on Jetty / files

     1#!/bin/bash
     2
     3function main ()
     4{
     5	[ "$*" == "" ] && { showHelp ; exit ; }
     6
     7	setupDefaults
     8
     9	parseOptions "$@"
    10
    11	validateOptions || { printInfo "Aborting..." ; exit 1 ; }
    12
    13	printInfo "Building Lucee on Jetty"
    14
    15	createBundle
    16
    17	[ -f "$OutputFile" ] || printFatal "No output file found [$OutputFile]"
    18	printInfo "Build Complete [$OutputFile]"
    19}
    20
    21
    22function showHelp ()
    23{
    24	cat <<- endhelp
    25		Lucee on Jetty build script - configures and bundles Lucee with Jetty.
    26
    27		Usage:
    28		  $0 OPTION...
    29
    30		General Options:
    31		  --help    Shows this help text and exits.
    32		  --quiet   Only print warnings and errors.
    33		  --strict  Treat warnings as errors (i.e. stop and emit exit status).
    34
    35		Build Options:
    36		  --jetty=PATH      Path of a jetty-home or jetty-distribution archive.
    37		  --lucee=PATH      Path of a Lucee jar file.
    38		  --module=PATH     Path of a Jetty module.mod file. (+)
    39		  --extension=PATH  Path of a Lucee extension.lex file. (+)
    40		  --output=PATH     Path of bundle, default build/luje-TIMESTAMP.tar
    41
    42		  Options marked (+) can be specified multiple times for additional files.
    43
    44		Examples:
    45		  You must specify at least jetty-home and lucee jar:
    46		    $0 --jetty=jetty-home-9.4.44.v20210927.zip --lucee=lucee-5.3.8.206.jar
    47
    48		  When numerous options are present, escaping newlines can assist readability:
    49		    $0 \\
    50		     --jetty=jetty-home-9.4.44.v20210927.zip \\
    51		     --lucee=lucee-light-5.3.8.206.jar \\
    52		     --extension=../ext/lucee.admin.extension-1.0.0.3.lex \\
    53		     --extension=../ext/lucee.doc.extension-1.0.0.2.lex \\
    54		     --output=../out/luje_9444_538.zip
    55
    56		View "readme.md" in parent directory for more information,
    57		or see online help at: https://docs.sorcerersisle.com/luje
    58	endhelp
    59
    60}
    61
    62function printDebug   { echo -e "\e[1;97mDEBUG: \e[0;34;47m$*\e[0m" ;}
    63function printVerbose { [ "$Verbosity" -gt 1 ] && echo -e "\e[32m$*\e[0m" ;}
    64function printInfo    { [ "$Verbosity" -gt 0 ] && echo "$*" ;}
    65function printWarn    { echo -e "\e[93mWarning: $*\e[0m" 1>&2 ; [ "$Strictness" -gt 0 ] && exit 1 ;}
    66function printError   { echo -e "\e[91mError: $*\e[0m" 1>&2 ;}
    67function printFatal   { printError "$*" ; exit 1 ; }
    68
    69
    70function setupDefaults ()
    71{
    72	BuildId="luje-$(date '+%s')"
    73	ScriptPath="$(realpath "$0")"
    74	BuildDir="$(dirname "$ScriptPath")"
    75	MainDir="$(dirname "$BuildDir")"
    76	SourceDir="${MainDir}/src"
    77	WorkingDir="${BuildDir}/${BuildId}"
    78
    79	Verbosity=1
    80	Strictness=0
    81	Debug=0
    82
    83	JettyFile=
    84	LuceeFile=
    85	OutputFile="${BuildDir}/${BuildId}.tar"
    86	ModuleFiles=("$MainDir/lucee.jetty-module/lucee.mod")
    87	declare -ga ExtensionFiles
    88}
    89
    90
    91function parseOptions ()
    92{
    93	while [[ -n "$*" ]]
    94	do
    95		case "$1" in
    96			--        ) break ;;
    97			--help    ) showHelp ; exit ;;
    98			--quiet   ) Verbosity=0  ;;
    99			--verbose ) Verbosity=2  ;;
   100			--strict  ) Strictness=1 ;;
   101			--debug   ) Debug=1 ; Verbosity=2 ; Strictness=1  ;;
   102
   103			--jetty=*     ) JettyFile="$(realpath -m "${1#*=}")"      ;;
   104			--lucee=*     ) LuceeFile="$(realpath -m "${1#*=}")"      ;;
   105			--output=*    ) OutputFile="$(realpath -m "${1#*=}")"     ;;
   106			--module=*    ) ModuleFiles+=("$(realpath -m "${1#*=}")")    ;;
   107			--extension=* ) ExtensionFiles+=("$(realpath -m "${1#*=}")") ;;
   108
   109			'' ) printWarn "Empty parameter $# from end." ;;
   110
   111			* )
   112				printError "Unknown option \"$1\""
   113				echo -e "\nSee \"$0 --help\" for valid options."
   114				exit 1
   115		esac
   116
   117		shift
   118	done
   119
   120	if [ "$Debug" -gt 0 ]
   121	then
   122		printDebug "Verbosity=$Verbosity"
   123		printDebug "Strictness=$Strictness"
   124		printDebug "JettyFile=$JettyFile"
   125		printDebug "LuceeFile=$LuceeFile"
   126		printDebug "OutputFile=$OutputFile"
   127		printDebug "ModuleFiles=${ModuleFiles[*]}"
   128		printDebug "ExtensionFiles=${ExtensionFiles[*]}"
   129	fi
   130}
   131
   132
   133function validateOptions ()
   134{
   135	Errors=0
   136
   137	if [ -z "$JettyFile" ]
   138	then
   139		printError "No Jetty file specified. (The --jetty option is required.)"
   140		Errors=1
   141	elif [ ! -f "$JettyFile" ]
   142	then
   143		printError "Cannot see Jetty file [$JettyFile] - check file exists and has correct permissions."
   144		Errors=1
   145	fi
   146
   147	if [ -z "$LuceeFile" ]
   148	then
   149		printError "No Lucee file specified. (The --lucee option is required.)"
   150		Errors=1
   151	elif [ ! -f "$LuceeFile" ]
   152	then
   153		printError "Cannot see Lucee jar [$LuceeFile] - check file exists and has correct permissions."
   154		Errors=1
   155	fi
   156
   157	if [ ! -d "$SourceDir" ]
   158	then
   159		printError "Invalid Source directory [$SourceDir] - check directory exists and has correct permissions."
   160		Errors=1
   161	fi
   162
   163	if [ -f "$OutputFile" ]
   164	then
   165		printError "Invalid Output file [$OutputFile] - file already exists."
   166		Errors=1
   167	fi
   168
   169	for CurModule in "${ModuleFiles[@]}"
   170	do
   171		if [ ! -f "$CurModule" ]
   172		then
   173			printError "Specified module [$CurModule] is not a file, or has incorrect permissions."
   174			Errors=1
   175		fi
   176	done
   177
   178	for CurExtension in "${ExtensionFiles[@]}"
   179	do
   180		if [ ! -f "$CurExtension" ]
   181		then
   182			printError "Specified extension [$CurExtension] is not a file, or has incorrect permissions."
   183			Errors=1
   184		fi
   185	done
   186
   187	return "$Errors"
   188}
   189
   190
   191function createBundle ()
   192{
   193	mkdir "$WorkingDir" && printInfo "Created working directory [$WorkingDir]"
   194
   195	cd "$WorkingDir" || printFatal "failed to cd $WorkingDir"
   196	printVerbose "In directory [$PWD]"
   197
   198
   199	## 1 - template (jetty-base) ##
   200
   201	printInfo "1/7 Copy template files "
   202	cp -r "$SourceDir"/* ./ && printVerbose "    Copied [$SourceDir]"
   203
   204	mkdir -p "lucee-base/logs" && printVerbose "    Created [lucee-base/logs]"
   205
   206
   207	## 2 - jetty-home ##
   208
   209	printInfo "2/7 Extract Jetty"
   210	if [ "${JettyFile##*.}" = "zip" ]
   211	then
   212		unzip -q "$JettyFile" && printVerbose "    Extracted [$JettyFile]"
   213	else
   214		tar -xaf "$JettyFile" && printVerbose "    Extracted [$JettyFile]"
   215	fi
   216	shopt -s nullglob
   217	mv jetty-{home,distribution}-* jetty-home
   218	shopt -u nullglob
   219	if [ ! -d jetty-home ]; then
   220		printError "Jetty archive [$JettyFile] did not provide expected jetty-home-{version} or jetty-distribution-{version} directory."
   221		printInfo "Aborting..."
   222		exit 1
   223	fi
   224
   225	chmod -R +w jetty-home
   226
   227
   228	## 3 - jetty modules ##
   229
   230	if [ ${#ModuleFiles[@]} -gt 0 ]
   231	then
   232		printInfo "3/7 Copy ${#ModuleFiles[@]} Jetty module(s)"
   233		ModuleDir=lucee-base/modules/
   234		mkdir -p "$ModuleDir"
   235		for CurModule in "${ModuleFiles[@]}"
   236		do
   237			if [ -d "$CurModule" ]
   238			then
   239				cp -r "$CurModule"/* "$ModuleDir" && printVerbose "    Copied contents of directory [$CurModule]"
   240			elif [ -f "$CurModule" ]
   241			then
   242				cp "$CurModule" "$ModuleDir" && printVerbose "    Copied module [$CurModule]"
   243				CurModuleDir="${CurModule%.mod}"
   244				[ -d "$CurModuleDir" ] && cp -r "$CurModuleDir" "$ModuleDir" && printVerbose "    Copied module directory [$CurModuleDir]"
   245			else
   246				printWarn "Unknown Module $CurModule"
   247			fi
   248		done
   249	else
   250		printInfo "3/7 No Jetty modules specified"
   251	fi
   252
   253
   254	## 4 - lucee ##
   255
   256	printInfo "4/7 Copy Lucee jar"
   257	JarLocation=$(find ./ -name 'lucee_jar_goes_here')
   258	[ -z "$JarLocation" ] && printFatal "Unable to find target location for Lucee jar"
   259	cp "$LuceeFile" "$(dirname "$JarLocation")" && printVerbose "    Copied [$LuceeFile]"
   260	rm "$JarLocation"
   261
   262
   263	## 5 - lucee extensions ##
   264
   265	if [ ${#ExtensionFiles[@]} -gt 0 ]
   266	then
   267		printInfo "5/7 Copy ${#ExtensionFiles[@]} Lucee extension(s)"
   268		ExtensionDir=lucee-base/lucee-server/deploy/
   269		mkdir -p "$ExtensionDir"
   270		for CurExtension in "${ExtensionFiles[@]}"
   271		do
   272			if [ -d "$CurExtension" ]
   273			then
   274				cp -r "$CurExtension"/*.lex "$ExtensionDir" && printVerbose "    Copied *.lex from directory [$CurExtension]"
   275			elif [ -f "$CurExtension" ]
   276			then
   277				cp "$CurExtension" "$ExtensionDir" && printVerbose "    Copied extension [$CurExtension]"
   278			else
   279				printWarn "Unknown extension [$CurExtension]"
   280			fi
   281		done
   282	else
   283		printInfo "3/7 No Lucee extensions specified"
   284	fi
   285
   286
   287	## 6 - readme ##
   288
   289	printInfo "6/7 Update readme versions"
   290	JettyVersion=$(echo "$JettyFile" | grep -oP 'jetty-(distribution|home)-\K[\d.]*(?=(\.M\d|\.v\d+|)\.(zip|tgz|tar(\.gz)?)$)')
   291	LuceeVersion=$(echo "$LuceeFile" | grep -oP 'lucee-(light-)?\K[\d.]*(?=\.jar$)')
   292	printVerbose "    Jetty Version [$JettyVersion]"
   293	printVerbose "    Lucee Version [$LuceeVersion]"
   294	[ -z "$JettyVersion" ] && printWarn "Failed to read version from filename [$JettyFile]"
   295	[ -z "$LuceeVersion" ] && printWarn "Failed to read version from filename [$LuceeFile]"
   296	sed "s/{JETTY_VERSION}/$JettyVersion/g" < README.TXT > README.TMP
   297	sed "s/{LUCEE_VERSION}/$LuceeVersion/g" < README.TMP > README.TXT
   298	rm README.TMP
   299
   300
   301	## 7 - archive ##
   302
   303	printInfo "7/7 Create bundle & cleanup"
   304	mkdir -p "$(dirname "$OutputFile")"
   305
   306	if [ "${OutputFile##*.}" = "zip" ]
   307	then
   308		if [ -n "$(type -t zip)" ]
   309		then
   310			zip "$OutputFile" -- * && printVerbose "    Compressed [$OutputFile] with zip"
   311		elif  [ -n "$(type -t 7z)" ]
   312		then
   313			7z a "$OutputFile" -- * > /dev/null && printVerbose "    Compressed [$OutputFile] with 7z"
   314		else
   315			printFatal "No 'zip' or '7z' command; cannot create zip file"
   316		fi
   317	else
   318		tar -caf "$OutputFile" -- * && printVerbose "    Compressed [$OutputFile] with tar"
   319	fi
   320
   321	cd .. || printFatal "failed to cd .."
   322
   323	printVerbose "    Removing working directory [$WorkingDir]"
   324	rm -r "$WorkingDir" || printWarn "Failed to remove [$WorkingDir]"
   325}
   326
   327main "$@"