summaryrefslogtreecommitdiff
path: root/bin/setup_env
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-12-23 22:14:44 -0500
committerFreya Murphy <freya@freyacat.org>2024-12-23 22:14:44 -0500
commit45b58da72187a7b1ed4d75fc25bf5dd59a86a9c4 (patch)
tree721d43e663d0b9d7c15ebc4b180ba709e9e0f9c3 /bin/setup_env
parentlang null check (diff)
downloadcrimson-45b58da72187a7b1ed4d75fc25bf5dd59a86a9c4.tar.gz
crimson-45b58da72187a7b1ed4d75fc25bf5dd59a86a9c4.tar.bz2
crimson-45b58da72187a7b1ed4d75fc25bf5dd59a86a9c4.zip
rework tooling to make environement loading easier
Diffstat (limited to 'bin/setup_env')
-rwxr-xr-xbin/setup_env62
1 files changed, 62 insertions, 0 deletions
diff --git a/bin/setup_env b/bin/setup_env
new file mode 100755
index 0000000..4a3049c
--- /dev/null
+++ b/bin/setup_env
@@ -0,0 +1,62 @@
+#!/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"