Skip to content

Commit

Permalink
external script for saving environment
Browse files Browse the repository at this point in the history
  • Loading branch information
phyver committed Jun 7, 2021
1 parent 5af0ce9 commit 5348684
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 40 deletions.
13 changes: 13 additions & 0 deletions bin/save_environment.sh
@@ -0,0 +1,13 @@
#!/bin/bash

# this script should be **sourced**
# it will output the current environment: names of variables, functions,
# aliases and processes

{
compgen -v | sed 's/^/Variable:/'
compgen -A function | sed 's/^/Function:/'
compgen -a | sed 's/^/Alias:/'
ps -o pid,comm | sed '1d;s/^/Process:/'
} | grep -vE "grep|ps|sed|sort|bash" | sort

25 changes: 15 additions & 10 deletions lib/gsh.sh
Expand Up @@ -207,15 +207,19 @@ _gsh_start() {

# re-source static.sh, in case some important directory was removed by accident
[ -f "$MISSION_DIR/static.sh" ] && mission_source "$MISSION_DIR/static.sh"

if [ -f "$MISSION_DIR/init.sh" ]
then
# attention, si l'initialisation a lieu dans un sous-shell et qu'elle
# définit des variables d'environnement, elles ne seront pas définies dans
# la session bash.
# je sauvegarde l'environnement avant / après l'initialisation pour
# afficher un message dans ce cas
# If init.sh is sourced in a subshell, variable definitions, changes of
# PWD, functions or aliases defined in init.sh will disappear.
# I save the environment before / after and display a warning when that's the case.
_PWD=$(pwd)
[ "$BASHPID" = $$ ] || compgen -v | sort > "$GSH_VAR"/env-before
if [ "$BASHPID" != $$ ]
then
local env_before=$(mktemp)
. save_environment.sh > "$env_before"
fi

mission_source "$MISSION_DIR/init.sh"
local exit_status=$?

Expand All @@ -227,17 +231,18 @@ _gsh_start() {
return
fi

[ "$BASHPID" = $$ ] || compgen -v | sort > "$GSH_VAR"/env-after

if [ "$BASHPID" != $$ ]
then
if [ "$_PWD" != "$(pwd)" ] || ! cmp -s "$GSH_VAR"/env-before "$GSH_VAR"/env-after
local env_after=$(mktemp)
. save_environment.sh > "$env_after"

if [ "$_PWD" != "$(pwd)" ] || ! cmp -s "$env_before" "$env_after"
then
echo "$(gettext "Error: this mission was initialized in a subshell.
You should run the command
\$ gsh reset
to make sure the mission is initialized properly.")" >&2
rm -f "$GSH_VAR"/env-{before,after}
rm -f "$/env_before" "$env_after"
fi
fi
fi
Expand Down
39 changes: 9 additions & 30 deletions lib/mission_source.sh
Expand Up @@ -52,20 +52,19 @@ mission_source() {
fi

echo " GSH: sourcing \$GSH_ROOT/${FILENAME#$GSH_ROOT/}" >&2
local TEMP=$(mktemp -d "$GSH_VAR/env-XXXXXX")
local source_ret_value="" # otherwise, it appears in the environment!
local _MISSION_DIR=""
local _TEXTDOMAIN=""
local _MISSION_NAME=""
local MISSION_NAME=""
local _PATH=""
local exit_status=""
local env_before=$(mktemp)
local env_after=$(mktemp)

# otherwise, record the environment (variables, functions and aliases)
# before and after to echo a message when there are differences
compgen -v | sort > "$TEMP"/before-V
compgen -A function | sort > "$TEMP"/before-F
compgen -a | sort > "$TEMP"/before-A
. save_environment.sh >"$env_before"
local _MISSION_DIR=$MISSION_DIR
export MISSION_DIR=$(dirname "$(realpath "$FILENAME")")
_TEXTDOMAIN=$TEXTDOMAIN
Expand All @@ -80,36 +79,16 @@ mission_source() {
export MISSION_NAME=$_MISSION_NAME
export MISSION_DIR=$_MISSION_DIR
export PATH=$_PATH
compgen -v | sort > "$TEMP"/after-V
compgen -A function | sed "/$MISSION_FN/d" | sort > "$TEMP"/after-F
compgen -a | sort > "$TEMP"/after-A
. save_environment.sh | grep -v "$MISSION_FN" > "$env_after"

local msg="GSH: environment modifications while sourcing \$GSH_ROOT/${FILENAME#$GSH_ROOT/}" >&2
if ! cmp -s "$TEMP"/{before,after}-V
if ! cmp -s "$env_before" "$env_after"
then
[ -n "$msg" ] && echo "$msg"
msg=""
comm -23 "$TEMP"/{before,after}-V | sed "s/^/-Var:/"
comm -13 "$TEMP"/{before,after}-V | sed "s/^/+Var:/"
echo "GSH: environment modifications while sourcing \$GSH_ROOT/${FILENAME#$GSH_ROOT/}" >&2
comm -23 "$env_before" "$env_after" | sed "s/^/-/"
comm -13 "$env_before" "$env_after" | sed "s/^/+/"
fi

if ! cmp -s "$TEMP"/{before,after}-F
then
[ -n "$msg" ] && echo "$msg"
msg=""
comm -23 "$TEMP"/{before,after}-F | sed "s/^/-Fun:/"
comm -13 "$TEMP"/{before,after}-F | sed "s/^/+Fun:/"
fi

if ! cmp -s "$TEMP"/{before,after}-A
then
[ -n "$msg" ] && echo "$msg"
msg=""
comm -23 "$TEMP"/{before,after}-A | sed "s/^/-Aliase:/"
comm -13 "$TEMP"/{before,after}-A | sed "s/^/+Aliase:/"
fi

rm -rf "$TEMP"
rm -f "$env_before" "$env_after"
unset -f "$MISSION_FN"
return $exit_status
}
Expand Down

0 comments on commit 5348684

Please sign in to comment.