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: Commands:
- `new [-i] <dir>`: create a new project directory `<dir>`. Use `-i` for - `new [-i] <dir>`: create a new project.
interactive mode. - `<dir>`: new project directory.
- `build [-v]`: build the website (default). Use `-v` for verbose. - `-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. - `serve`, `server`: run an HTTP server of the output on port 8080.
- `clean`: cleans build files. - `clean`: cleans build files.
- `help`: show help information. - `help`: show help information.

View File

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