Skip to content

Commit

Permalink
Pack bash initialization into an installed script
Browse files Browse the repository at this point in the history
Issue: #17
  • Loading branch information
dspinellis committed Apr 4, 2024
1 parent 37dff63 commit d47deeb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 40 deletions.
46 changes: 6 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,48 +170,14 @@ if you want to use the capabilities of the _ai-cli_ library, configure your syst
to use the Homebrew commands in preference to the ones supplied with macOS.

## Shell startup configuration
You can configure the _ai-cli_ library to be always available in your _bash_ shell by
adding the following lines in your `.bashrc` file.
Adjust the `AI_CLI_LIB` setting to match the _ai-cli_ library installation path;
You can configure the _ai-cli_ library to be always available in your _bash_
shell by adding the following lines in your `.bashrc` file
(ideally near its beginning for performance reasons).
Adjust the provided path match the _ai-cli_ library installation path;
it is currently set for a local installation in your home directory.
```bash
# >>> initialize the ai-cli library >>>

# Location of the ai-cli shared library; adjust as needed.
AI_CLI_LIB=("$HOME/lib/ai_cli."*)

# Execute only if configured, installed, and not initialized.
if [[ -r ~/.aicliconfig && -r $AI_CLI_LIB && "$LD_PRELOAD" != *$AI_CLI_LIB* ]]
then
# Set Linux and Cygwin environment variable.
if [ -z "$LD_PRELOAD" ] ; then
export LD_PRELOAD="$AI_CLI_LIB"
else
LD_PRELOAD="$LD_PRELOAD:$AI_CLI_LIB"
fi

# Set macOS environment variables.
if [ -z "$DYLD_LIBRARY_PATH" ] ; then
export DYLD_LIBRARY_PATH=/opt/homebrew/lib
else
DYLD_LIBRARY_PATH="/opt/homebrew/lib:$DYLD_LIBRARY_PATH"
fi
if [ -z "$DYLD_INSERT_LIBRARIES" ] ; then
export DYLD_INSERT_LIBRARIES="$AI_CLI_LIB"
else
DYLD_INSERT_LIBRARIES="$LD_PRELOAD:$AI_CLI_LIB"
fi

# Overlay current bash with a new instance, which will include the required
# environment variables.
if shopt -q login_shell ; then
exec -l bash
else
exec bash
fi

fi
# <<< initialize the ai-cli library <<<
# Initialize the ai-cli library
source $HOME/share/ai-cli/ai-cli-activate-bash.sh
```

## Reference documentation
Expand Down
4 changes: 4 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ MANPREFIX ?= "$(PREFIX)/share/man/"
SHAREPREFIX ?= "$(PREFIX)/share/ai-cli"

PROGS=rl_driver $(SHARED_LIB)
ACTIVATION_SCRIPTS=$(wildcard ai-cli-activate-*)
RL_SRC=ai_cli.c config.c ini.c fetch_anthropic.c fetch_hal.c fetch_openai.c \
fetch_llamacpp.c support.c
TEST_SRC=$(wildcard *_test.c)
Expand Down Expand Up @@ -113,6 +114,9 @@ install: ai_cli.$(DLL_EXTENSION) # Help: Install library and manual pages
install -m 644 ai_cli.5 $(DESTDIR)$(MANPREFIX)/man5
install -m 644 ai_cli.7 $(DESTDIR)$(MANPREFIX)/man7
install -m 644 ai-cli-config $(DESTDIR)$(SHAREPREFIX)/config
for s in $(ACTIVATION_SCRIPTS) ; do \
sed -e "s|__LIBPREFIX__|$(LIBPREFIX)|" $$s >$(DESTDIR)$(SHAREPREFIX)/$$s ; \
done

help: # Help: Show this help message
@echo 'The following make targets are available.'
Expand Down
56 changes: 56 additions & 0 deletions src/ai-cli-activate-bash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#
# ai_cli - readline wrapper to obtain a generative AI suggestion
#
# Bash commands to activate ai-cli
#
# Copyright 2023-2024 Diomidis Spinellis
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

if [ "$0" != bash -a "$0" != -bash ] ; then
echo 'The ai-cli-lib activation script must be sourced, not executed.' 1>&2
exit 1
fi

AI_CLI_LIB=("__LIBPREFIX__/ai_cli."*)
if [[ $- == *i* # Shell is interactive
&& -r ~/.aicliconfig # User has configured script
&& -r $AI_CLI_LIB # Library installed
&& "$LD_PRELOAD" != *$AI_CLI_LIB* ]] # Variable not set
then
# Linux and Cygwin (is also used for checking)
if [ -z "$LD_PRELOAD" ] ; then
export LD_PRELOAD="$AI_CLI_LIB"
else
LD_PRELOAD="$LD_PRELOAD:$AI_CLI_LIB"
fi

# macOS
if [ -z "$DYLD_LIBRARY_PATH" ] ; then
export DYLD_LIBRARY_PATH=/opt/homebrew/lib
else
DYLD_LIBRARY_PATH="/opt/homebrew/lib:$DYLD_LIBRARY_PATH"
fi
if [ -z "$DYLD_INSERT_LIBRARIES" ] ; then
export DYLD_INSERT_LIBRARIES="$AI_CLI_LIB"
else
DYLD_INSERT_LIBRARIES="$LD_PRELOAD:$AI_CLI_LIB"
fi

if shopt -q login_shell ; then
exec -l bash
else
exec bash
fi
fi

0 comments on commit d47deeb

Please sign in to comment.