Skip to content

Commit

Permalink
feat(setup): Add setup scripts for easier installation
Browse files Browse the repository at this point in the history
- Manual Installation section updated to include step-by-step instructions
- Add setup script "setup_sc.sh" for automated setup process
- Add one-liner setup script "setup_quick_wget.sh" for swift setup
- Updated README.md to include instructions on using setup scripts
- Minor chores
  • Loading branch information
5n00py committed Dec 13, 2023
1 parent b9534bc commit 1f68dcb
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ possible options, run `gc-smart --help`.

## Installation

### Manual Installation

1. Clone this repository to your local machine using git:

```bash
Expand Down Expand Up @@ -119,6 +121,64 @@ possible options, run `gc-smart --help`.
```
(Don't forget to replace `/path/to/gc-smart` with the actual path to the script.
6. After the setup you will either have to restart your terminal or source your
shell configuration for the changes to take efferc. If you use Bash, run:
```bash
source ~/.bashrc
```
If you use Zsh, run:
```bash
source ~/.zshrc
```
### Using the Setup Script
For a more automated setup process, you can use the
[setup_sc.sh](setup/setup_sc.sh) script located in the `setup` directory. This
will set up the Python environment, install necessary dependencies, configure
the OpenAI API key and update the shell's configuration.

**Note:** If the `OPENAI_API_KEY` environment variable is not already set, the
script will prompt you to enter an OpenAI API key. You will need to paste the
key at the prompt, so make sure you have it ready, see also
[Where do I find my API key?](https://help.openai.com/en/articles/4936850-where-do-i-find-my-api-key).

1. Clone the Repository as described in the first step of the Manual
Installation.

2. Navigate to the setup directory, make the script executable and run it:

```bash
cd SmartCommit/setup && chmod +x setup_sc.sh && ./setup_sc.sh
```

The script will perform several actions:
- Create and set up a virtual environment for Python
- Check if Python version is 3.7 or higher
- Install requiret python libraries in the venv
- Prompt for adding an `OPENAI_API_KEY` and adding it to `.bashrc` or
`.zshrc` if they exist.
- Add `gc-smart` script to the shell's PATH environment variable.
- Make the necessary scripts executable.
3. Restart or source your shell configuration as descripted under 6. in the
Manual Installation above.
### Quick Setup with One-Liner
For an even swifter setup you can use a one-liner that clones the repository
into `~/.local/share/SmartCommit` and then runs the setup script:
```bash
bash <(curl -sL https://raw.githubusercontent.com/5n00py/SmartCommit/main/setup/setup_quick_wget.sh)
```
Upon completion don't forget to restart or source your shell configuration as
described under 6. in the Manuall Installation section above.

## Configuration

The `gpt-commit-prompter` used in `gc-smart` allows configuration through the
Expand Down
25 changes: 25 additions & 0 deletions setup/setup_quick_wget.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# Variables
REPO_URL="https://github.com/5n00py/SmartCommit.git"
INSTALL_DIR="$HOME/.local/share/SmartCommit"

# Create the installation directory if it doesn't exist
mkdir -p "$INSTALL_DIR"

# Clone the repository
echo "Cloning SmartCommit repository into $INSTALL_DIR"
git clone "$REPO_URL" "$INSTALL_DIR"

# Navigate to the SmartCommit setup directory
cd "$INSTALL_DIR/setup" || exit

# Make the setup script executable
chmod +x setup_sc.sh

# Run the setup script
echo "Running setup script..."
./setup_sc.sh

# End of script
echo "Quick setup of SmartCommit is complete."
77 changes: 77 additions & 0 deletions setup/setup_sc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

# Navigate to the script's directory
cd "$(dirname "$0")" || exit
# Set GCS_ROOT to the parent of the current directory (SmartCommit root)
GCS_ROOT=$(cd .. && pwd)
echo "Setting GCS_ROOT to $GCS_ROOT"

# Set up Python environment
echo "Setting up the Python environment..."
python3 -m venv "$GCS_ROOT/python/venv"
# shellcheck disable=SC1091
source "$GCS_ROOT/python/venv/bin/activate"

# Check Python version
python_version=$(python3 --version | cut -d ' ' -f 2)
minimum_version="3.7"

if printf '%s\n' "$python_version" "$minimum_version" | sort -V | head -n 1 | grep -q "$minimum_version"; then
echo "Python version is $python_version, which is acceptable."
else
echo "Python 3.7 or higher is required. Please upgrade your Python."
exit 1
fi

# Install required libraries
echo "Installing required Python libraries..."
pip install -r "$GCS_ROOT/python/requirements.txt"

# Deactivate the virtual environment
deactivate

# Make scripts executable
chmod +x "$GCS_ROOT/gc-smart"
chmod +x "$GCS_ROOT/python/gpt-commit-prompter"

# Check for OpenAI API key
if [ -z "$OPENAI_API_KEY" ]; then
echo "Settin up the OpenAI API Key:"
echo "See also: https://help.openai.com/en/articles/4936850-where-do-i-find-my-api-key"
echo "Please enter your OpenAI API key:"
read -r OPENAI_API_KEY

# Check if .bashrc exists and write the key export
if [ -f "$HOME/.bashrc" ]; then
echo "export OPENAI_API_KEY='$OPENAI_API_KEY'" >> "$HOME/.bashrc"
echo "Your OpenAI API key has been added to your .bashrc file."
fi

# Check if .zshrc exists and write the key export
if [ -f "$HOME/.zshrc" ]; then
echo "export OPENAI_API_KEY='$OPENAI_API_KEY'" >> "$HOME/.zshrc"
echo "Your OpenAI API key has been added to your .zshrc file."
fi

# If neither file exists, return an error
if [ ! -f "$HOME/.bashrc" ] && [ ! -f "$HOME/.zshrc" ]; then
echo "ERROR: Neither .bashrc nor .zshrc was found."
echo "Please manually add the OPENAI_API_KEY to your shell's configuration."
exit 1
fi

echo "WARNING: Keep your API key secure and do not share it."
fi

# Add script to PATH
echo "Adding gc-smart script to PATH..."
script_path="$GCS_ROOT/gc-smart"
if [ -f "$HOME/.bashrc" ]; then
echo "export PATH=\"\$PATH:$script_path\"" >> "$HOME/.bashrc"
fi
if [ -f "$HOME/.zshrc" ]; then
echo "export PATH=\"\$PATH:$script_path\"" >> "$HOME/.zshrc"
fi


echo "Setup complete. Please restart your terminal or source your .bashrc/.zshrc file."

0 comments on commit 1f68dcb

Please sign in to comment.