Implement concurrent page compilation.

This commit is contained in:
Nicolás A. Ortega Froysa 2022-11-10 19:10:41 +01:00
parent 60c6cb3bcd
commit fc50e669a5
2 changed files with 49 additions and 21 deletions

View File

@ -14,9 +14,13 @@ Synopsis: `isidore <command> [options]`
Commands:
- `new [-i] <dir>`: create a new project directory `<dir>`. Use `-i` for
interactive mode.
- `build [-v]`: build the website (default). Use `-v` for verbose.
- `new [-i] <dir>`: create a new project.
- `<dir>`: new project directory.
- `-i`: interactive mode.
- `build [-v] [-j<n>]`: build the website.
- `-j<n>`: number of jobs to run concurrently. With `0` it compiles all files
at once.
- `-v`: run with verbose output.
- `serve`, `server`: run an HTTP server of the output on port 8080.
- `clean`: cleans build files.
- `help`: show help information.

View File

@ -92,8 +92,8 @@ function print_help()
print_usage
echo -e "\nCOMMANDS:
new [-i] <dir>
Create a new Isidore project (use interactive mode with '-i')
build
Create a new Isidore project
build [-v] [-j<n>]
Build the project rooted in the current directory
serve | server
Serve a local HTTP server of the output directory on port 8080
@ -172,6 +172,7 @@ function clean_project()
#
# Params:
# $1 - Verbose flag
# $2 - Maximum number of jobs to run
##
function build_project()
{
@ -184,26 +185,40 @@ function build_project()
mkdir -p $OUTPUT_DIR/${i:${#SOURCE_DIR}}
done
# number of current jobs
local NUM_JOBS="\j"
for i in $(find site -type f -regextype posix-extended -not -regex '.*\.(cfg|CFG)\.(php|PHP)')
do
if [[ $i =~ .*\.(php|PHP) ]]
then
: ${i:${#SOURCE_DIR}}
local OUT_FILE="$OUTPUT_DIR/${_: :-4}"
if [ $1 == 1 ]
while (( $2 != 0 )) && (( ${NUM_JOBS@P} >= $2 ))
do
wait -n
done
# parallel block
(
if [[ $i =~ .*\.(php|PHP) ]]
then
echo "Building $OUT_FILE ..."
: ${i:${#SOURCE_DIR}}
local OUT_FILE="$OUTPUT_DIR/${_: :-4}"
if [ $1 == 1 ]
then
echo "Building $OUT_FILE ..."
fi
cat $PROJECT_CONFIG_FILE $i | php > $OUT_FILE
else
local OUT_FILE="$OUTPUT_DIR/${i:${#SOURCE_DIR}}"
if [ $1 == 1 ]
then
echo "Copying $OUT_FILE ..."
fi
cp $i $OUT_FILE
fi
cat $PROJECT_CONFIG_FILE $i | php > $OUT_FILE
else
local OUT_FILE="$OUTPUT_DIR/${i:${#SOURCE_DIR}}"
if [ $1 == 1 ]
then
echo "Copying $OUT_FILE ..."
fi
cp $i $OUT_FILE
fi
) &
done
# wait for jobs to finish
wait -n
}
set_config_vars
@ -244,6 +259,7 @@ case $1 in
new_project $NEW_DIR $INTERACTIVE;;
build)
VERBOSE=0
JOBS=1
if [ $# -gt 1 ]
then
for i in ${@:2}
@ -251,6 +267,14 @@ case $1 in
case $i in
-v)
VERBOSE=1;;
-j*)
if ! [[ "$i" =~ ^-j[0-9]+$ ]]
then
echo "Argument '$i' badly formatted. Should be '-j<n>' where <n> is a number."
print_usage
exit 1
fi
JOBS=${i:2};;
*)
echo "Unknown argument for build '$i'. Use the 'help' subcommand."
print_usage
@ -258,7 +282,7 @@ case $1 in
esac
done
fi
build_project $VERBOSE;;
build_project $VERBOSE $JOBS;;
serve|server)
php -S localhost:8080 -t $OUTPUT_DIR/;;
clean)