crimson/bin/setup_env

62 lines
2.3 KiB
Bash
Executable file

#!/bin/sh
### CRIMSON --- A simple PHP framework.
### Copyright © 2024 Freya Murphy <contact@freyacat.org>
###
### This file is part of CRIMSON.
###
### CRIMSON is free software; you can redistribute it and/or modify it
### under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 3 of the License, or (at
### your option) any later version.
###
### CRIMSON 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 General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with CRIMSON. If not, see <http://www.gnu.org/licenses/>.
# `setup_env`
# This script sets up the environment for crimson by loading all required .env
# files.
# Make sure errors fail to avoid nasal demons
set -e
# ================================================================ CONSTANTS ==
# CRIMSON_ROOT: This is the folder the crimson project is located in. This
# variable is needed since it is used to load any files located in crimson,
# including base.env, and docker compose files.
# PROJECT_ROOT: This is the folder that the user who called any crimson script
# is currently located in. For crimson to work this must be the folder that
# your project checkout is in. This is because crimson loads `.env` here to
# load any user specified environment.
# PROJECT_DATA: Where docker persistent data will be stored.
# PROEJCT_SOURCE: Where user project code is located.
# ENV_FILES: A colon seperated list of all .env files loaded.
export CRIMSON_ROOT="$(realpath "$(dirname "$(dirname "$0")")")"
export PROJECT_ROOT="$(realpath "$(pwd)")"
export PROJECT_DATA="${PROJECT_ROOT}/data"
export PROJECT_SOURCE="${PROJECT_ROOT}/src"
ENV_FILES=""
# ================================================================ BOOTSTRAP ==
# Load `base.env` and `.env`
function load_env {
local file seperator
file="$1"
seperator=":"
if [ -f "$file" ]; then
# dont add seperator at the start
if [ -z "$ENV_FILES" ]; then
seperator=""
fi
# load file
source "$file"
ENV_FILES="${ENV_FILES}${seperator}${file}"
fi
}
load_env "$CRIMSON_ROOT/base.env"
load_env "$PROJECT_ROOT/.env"