isidore/isidore.sh

249 lines
5.1 KiB
Bash
Executable File

#!/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/>.
ISIDORE_VERSION="0.1.0"
ISIDORE_CONFIG_DIR=""
ISIDORE_CONFIG_DIR_OPTIONS=(
"$XDG_CONFIG_HOME/isidore"
"$HOME/.isidore")
ISIDORE_AUTHOR_NAME=""
PROG_NAME=$0
PROJECT_NAME="Isidore"
SOURCE_DIR="site/"
TMP_DIR="/tmp/isidore-workdesk/"
OUTPUT_DIR="output/"
TEMPLATE_DIR="templates/"
##
# 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" ]
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
}
##
# Print the usage of the Isidore command.
##
function print_usage()
{
echo "USAGE: $PROG_NAME <command> [options]"
}
function print_help()
{
print_usage
echo -e "\nCOMMANDS:
new [-i] <dir>
Create a new Isidore project (use interactive mode with '-i')
build
Build the project rooted in the current directory
serve | server
Serve a local HTTP server of the output directory on port 8080
clean
Clean the built files
version
Print the current version
help
Show this help information"
}
##
# Check to see if at the root of an Isidore project.
##
function check_in_project()
{
if ! [ -f "./config.php" ] || ! [ -f "./.isidore-version" ]
then
echo "ERROR: You are not currently in the root of an Isidore project."
exit 1
fi
}
##
# Create a new project.
#
# Params:
# $1 - The project directory
# $2 - Interactive mode (1 = true; 0 = false)
##
function new_project()
{
echo "$PROJECT_NAME v$ISIDORE_VERSION"
NEW_PROJECT_DIR=$1
NEW_PROJECT_NAME="New Project"
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
mkdir $NEW_PROJECT_DIR
mkdir -p $NEW_PROJECT_DIR/$SOURCE_DIR
mkdir -p $NEW_PROJECT_DIR/$TEMPLATE_DIR
echo \
"<?php
\$site_name = \"$NEW_PROJECT_NAME\";
\$site_author = \"$ISIDORE_AUTHOR_NAME\";
\$site_url = \"$NEW_PROJECT_URL\";
?>" >> $NEW_PROJECT_DIR/config.php
echo "$ISIDORE_VERSION" > $NEW_PROJECT_DIR/.isidore-version
echo "New project created."
[ $2 == 0 ] &&
echo "Consider taking a look at the $NEW_PROJECT_DIR/config.php file."
}
##
# Clean a project's build.
##
function clean_project()
{
check_in_project
if [ -d "$OUTPUT_DIR" ]
then
rm -r $OUTPUT_DIR
fi
echo "Project cleaned."
}
##
# Build the project.
##
function build_project()
{
check_in_project
CFG_HEADER_FILE="$TMP_DIR/config-header.php"
mkdir -p $OUTPUT_DIR
mkdir -p $TMP_DIR
echo "<?php include(getcwd() . '/config.php'); ?>" > $CFG_HEADER_FILE
for i in $(find $SOURCE_DIR -type d)
do
mkdir -p $OUTPUT_DIR/${i:${#SOURCE_DIR}}
mkdir -p $TMP_DIR/$i
done
for i in $(find $SOURCE_DIR -name '*.php' -not -name '*.cfg.php')
do
cat $CFG_HEADER_FILE $i > $TMP_DIR/$i
: ${i:${#SOURCE_DIR}}
OUT_FILE="$OUTPUT_DIR/${_: :-4}"
php $TMP_DIR/$i > $OUT_FILE
done
for i in $(find $SOURCE_DIR -type f -not -name '*.php')
do
cp $i $OUTPUT_DIR/${i:${#SOURCE_DIR}}
done
rm -r $TMP_DIR
}
set_config_vars
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
echo "Unknown argument for new '$i'. Use the 'help' command."
print_usage
exit 1
fi
esac
done
new_project $NEW_DIR $INTERACTIVE;;
build)
build_project;;
serve|server)
php -S localhost:8080 -t $OUTPUT_DIR/;;
clean)
clean_project;;
help)
print_help;;
version)
echo "$PROJECT_NAME v$ISIDORE_VERSION";;
*)
echo "Unknown command $1. Use the 'help' command."
print_usage
exit 1;;
esac