isidore/isidore.sh

296 lines
5.9 KiB
Bash
Raw Normal View History

2022-10-29 11:41:15 +00:00
#!/bin/bash
# Copyright (C) 2022 Ortega Froysa, Nicolás <nicolas@ortegas.org>
# Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2022-10-29 12:16:07 +00:00
ISIDORE_VERSION="0.1.0"
2022-10-29 11:41:15 +00:00
ISIDORE_CONFIG_DIR=""
ISIDORE_CONFIG_DIR_OPTIONS=(
"$XDG_CONFIG_HOME/isidore"
"$HOME/.isidore")
ISIDORE_AUTHOR_NAME=""
2022-10-29 12:23:42 +00:00
PROG_NAME=$0
2022-10-29 12:24:36 +00:00
PROJECT_NAME="Isidore"
2022-10-29 12:23:42 +00:00
SOURCE_DIR="site/"
OUTPUT_DIR="output/"
TEMPLATE_DIR="templates/"
2022-11-10 16:07:03 +00:00
PROJECT_CONFIG_FILE="config.php"
2022-10-29 11:41:15 +00:00
##
# Create the configuration file. This function is called if it does not already
# exist.
##
function create_config_file()
{
echo "Creating $ISIDORE_CONFIG_DIR/config file..."
echo "Please enter the following..."
echo -n "Author name: "
read ISIDORE_AUTHOR_NAME
echo "ISIDORE_AUTHOR_NAME=\"$ISIDORE_AUTHOR_NAME\"" > $ISIDORE_CONFIG_DIR/config
}
##
# Read the configuration file and set the basic config variables.
##
function set_config_vars()
{
for i in "${ISIDORE_CONFIG_DIR_OPTIONS[@]}"
do
if [ -d $i ]
then
ISIDORE_CONFIG_DIR=$i
break
fi
done
if [ -z "$ISIDORE_CONFIG_DIR" ]
then
if ! [ -z "$XDG_CONFIG_HOME" ]
2022-10-29 11:41:15 +00:00
then
ISIDORE_CONFIG_DIR="$XDG_CONFIG_HOME/isidore"
else
ISIDORE_CONFIG_DIR="$HOME/.isidore"
fi
echo "Creating new Jerome configuration directory in $ISIDORE_CONFIG_DIR"
mkdir -p $ISIDORE_CONFIG_DIR
create_config_file
fi
if ! [ -f "$ISIDORE_CONFIG_DIR/config" ]
then
create_config_file
fi
source $ISIDORE_CONFIG_DIR/config
}
2022-10-29 12:17:30 +00:00
##
# Print the usage of the Isidore command.
##
2022-10-29 12:03:47 +00:00
function print_usage()
{
2022-10-29 12:23:42 +00:00
echo "USAGE: $PROG_NAME <command> [options]"
}
function print_help()
{
print_usage
echo -e "\nCOMMANDS:
new [-i] <dir>
2022-11-10 18:10:41 +00:00
Create a new Isidore project
build [-v] [-j<n>]
2022-10-29 12:23:42 +00:00
Build the project rooted in the current directory
2022-11-07 19:26:23 +00:00
serve | server
Serve a local HTTP server of the output directory on port 8080
2022-10-29 12:23:42 +00:00
clean
Clean the built files
version
Print the current version
help
Show this help information"
2022-10-29 12:03:47 +00:00
}
2022-10-29 12:16:07 +00:00
##
# Check to see if at the root of an Isidore project.
##
function check_in_project()
{
2022-11-10 16:07:03 +00:00
if ! [ -f "$PROJECT_CONFIG_FILE" ] || ! [ -f "./.isidore-version" ]
2022-10-29 12:16:07 +00:00
then
echo "ERROR: You are not currently in the root of an Isidore project."
exit 1
fi
}
2022-10-29 11:41:15 +00:00
##
2022-10-29 12:03:47 +00:00
# Create a new project.
2022-10-29 11:41:15 +00:00
#
# Params:
# $1 - The project directory
# $2 - Interactive mode (1 = true; 0 = false)
2022-10-29 11:41:15 +00:00
##
function new_project()
{
2022-10-29 13:09:55 +00:00
echo "$PROJECT_NAME v$ISIDORE_VERSION"
2022-11-08 15:40:13 +00:00
local NEW_PROJECT_DIR=$1
local NEW_PROJECT_NAME="New Project"
local NEW_PROJECT_URL="http://example.com/"
if [ $2 == 1 ]
then
echo -n "Project name: "
read NEW_PROJECT_NAME
echo -n "URL: "
read NEW_PROJECT_URL
fi
2022-10-29 11:41:15 +00:00
mkdir $NEW_PROJECT_DIR
mkdir -p $NEW_PROJECT_DIR/$SOURCE_DIR
mkdir -p $NEW_PROJECT_DIR/$TEMPLATE_DIR
2022-10-29 12:03:47 +00:00
echo \
"<?php
\$site_name = \"$NEW_PROJECT_NAME\";
2022-10-29 13:09:55 +00:00
\$site_author = \"$ISIDORE_AUTHOR_NAME\";
\$site_url = \"$NEW_PROJECT_URL\";
2022-11-10 16:07:03 +00:00
?>" >> $NEW_PROJECT_DIR/$PROJECT_CONFIG_FILE
2022-10-29 12:16:07 +00:00
echo "$ISIDORE_VERSION" > $NEW_PROJECT_DIR/.isidore-version
2022-10-29 13:09:55 +00:00
echo "New project created."
[ $2 == 0 ] &&
2022-11-10 16:07:03 +00:00
echo "Consider taking a look at the $NEW_PROJECT_DIR/$PROJECT_CONFIG_FILE file."
2022-10-29 12:03:47 +00:00
}
##
# Clean a project's build.
##
function clean_project()
{
2022-10-29 12:16:07 +00:00
check_in_project
if [ -d "$OUTPUT_DIR" ]
2022-10-29 12:16:07 +00:00
then
rm -r $OUTPUT_DIR
2022-10-29 12:16:07 +00:00
fi
2022-10-29 12:03:47 +00:00
echo "Project cleaned."
2022-10-29 11:41:15 +00:00
}
2022-10-29 13:09:55 +00:00
##
# Build the project.
2022-11-08 15:08:56 +00:00
#
# Params:
# $1 - Verbose flag
2022-11-10 18:10:41 +00:00
# $2 - Maximum number of jobs to run
2022-10-29 13:09:55 +00:00
##
function build_project()
{
check_in_project
mkdir -p $OUTPUT_DIR
for i in $(find $SOURCE_DIR -type d)
do
mkdir -p $OUTPUT_DIR/${i:${#SOURCE_DIR}}
done
2022-11-10 18:10:41 +00:00
# number of current jobs
local NUM_JOBS="\j"
2022-11-10 16:45:00 +00:00
for i in $(find site -type f -regextype posix-extended -not -regex '.*\.(cfg|CFG)\.(php|PHP)')
2022-10-29 13:09:55 +00:00
do
2022-11-10 18:10:41 +00:00
while (( $2 != 0 )) && (( ${NUM_JOBS@P} >= $2 ))
do
wait -n
done
# parallel block
(
if [[ $i =~ .*\.(php|PHP) ]]
2022-11-10 16:45:00 +00:00
then
2022-11-10 18:10:41 +00:00
: ${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
2022-11-10 16:45:00 +00:00
fi
2022-11-10 18:10:41 +00:00
) &
2022-11-08 14:12:29 +00:00
done
2022-11-10 18:10:41 +00:00
# wait for jobs to finish
wait -n
2022-10-29 13:09:55 +00:00
}
2022-10-29 11:41:15 +00:00
set_config_vars
2022-10-29 12:03:47 +00:00
2022-11-08 15:02:38 +00:00
if [ $# -lt 1 ]
then
echo "No subcommand was specified. Use the 'help' subcommand."
2022-11-08 15:08:56 +00:00
print_usage
2022-11-08 15:02:38 +00:00
exit 1
fi
2022-10-29 12:03:47 +00:00
case $1 in
new)
if [ $# -lt 2 ]
then
echo "Insufficient number of arguments."
print_usage
exit 1
fi
INTERACTIVE=0
NEW_DIR=""
for i in ${@:2}
do
case $i in
-i)
INTERACTIVE=1;;
*)
if [ -z "$NEW_DIR" ]
then
NEW_DIR="$i"
else
2022-11-08 15:02:38 +00:00
echo "Unknown argument for new '$i'. Use the 'help' subcommand."
print_usage
exit 1
2022-11-08 15:08:56 +00:00
fi;;
esac
done
new_project $NEW_DIR $INTERACTIVE;;
2022-10-29 13:09:55 +00:00
build)
2022-11-08 15:08:56 +00:00
VERBOSE=0
2022-11-10 18:10:41 +00:00
JOBS=1
2022-11-10 18:22:24 +00:00
for i in ${@:2}
do
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."
2022-11-08 15:08:56 +00:00
print_usage
2022-11-10 18:22:24 +00:00
exit 1
fi
JOBS=${i:2};;
*)
echo "Unknown argument for build '$i'. Use the 'help' subcommand."
print_usage
exit 1;;
esac
done
2022-11-10 18:10:41 +00:00
build_project $VERBOSE $JOBS;;
2022-11-07 19:26:23 +00:00
serve|server)
php -S localhost:8080 -t $OUTPUT_DIR/;;
2022-10-29 12:03:47 +00:00
clean)
clean_project;;
2022-10-29 12:23:42 +00:00
help)
print_help;;
2022-10-29 12:24:36 +00:00
version)
echo "$PROJECT_NAME v$ISIDORE_VERSION";;
2022-10-29 12:03:47 +00:00
*)
2022-11-08 15:02:38 +00:00
echo "Unknown command $1. Use the 'help' subcommand."
print_usage
exit 1;;
2022-10-29 12:03:47 +00:00
esac