187 lines
5.0 KiB
Bash
Executable File
187 lines
5.0 KiB
Bash
Executable File
#!/bin/zsh
|
|
|
|
name_executable() {
|
|
echo "The current name of the executable is $1"
|
|
|
|
read "?Enter the name of the executable: " NAME
|
|
echo $NAME
|
|
}
|
|
|
|
help() {
|
|
echo "This script installs a project onto the user's system."
|
|
echo "Usage: install"
|
|
echo " install <path-to-project>"
|
|
echo " install -h | --help"
|
|
echo "\nIf no path is provided, the script will look for a dependencies.txt file in the current directory."
|
|
exit 0
|
|
}
|
|
|
|
error_missing_dependencies() {
|
|
echo "The dependencies.txt file is missing from the project directory."
|
|
echo "This file should contain the required packages for the project to run."
|
|
echo "The format should be as follows: <command>,<package>"
|
|
echo "For example: python,python3"
|
|
echo "Please create this file before continuing."
|
|
echo "You can find an example file in the root directory of this project."
|
|
exit 1
|
|
}
|
|
|
|
success() {
|
|
NAME=$1
|
|
|
|
echo "The $NAME project and it's dependencies have been successfully installed onto your system."
|
|
echo "You can now run the project by typing $NAME into the terminal."
|
|
exit 0
|
|
}
|
|
|
|
install_python() {
|
|
NAME=$(name_executable $1)
|
|
|
|
# Get the current working directory and the virtual environment
|
|
CWD=$(pwd)
|
|
ENV=$(pwd)/bin/activate
|
|
COMMAND="python $CWD/$NAME.py"
|
|
|
|
# Create the virtual environment
|
|
python -m venv .
|
|
source bin/activate
|
|
|
|
# Install Python packages in the virtual environment
|
|
if [[ -f requirements.txt ]]; then
|
|
pip install -r requirements.txt
|
|
fi
|
|
|
|
|
|
# Create the executable
|
|
sudo echo "#!/bin/zsh" > /usr/local/bin/$NAME
|
|
sudo echo "\nsource $ENV" >> /usr/local/bin/$NAME
|
|
sudo echo "$COMMAND" >> /usr/local/bin/$NAME
|
|
sudo echo "\n# Auto-generated by the install script" >> /usr/local/bin/$NAME
|
|
sudo chmod +x /usr/local/bin/$NAME
|
|
|
|
success $NAME
|
|
}
|
|
|
|
install() {
|
|
PROJECT_NAME=$1
|
|
|
|
echo "\nInstalling the $PROJECT_NAME project onto your system..."
|
|
|
|
echo "Please select the project language or build system:"
|
|
echo "1. C"
|
|
echo "2. Python"
|
|
echo "3. Java"
|
|
echo "4. Other"
|
|
|
|
read "?Enter the number of the project type: " PROJECT_TYPE
|
|
|
|
case $PROJECT_TYPE in
|
|
1)
|
|
echo "C"
|
|
echo "Need to implement"
|
|
;;
|
|
2)
|
|
install_python "$PROJECT_NAME"
|
|
;;
|
|
3)
|
|
echo "Java"
|
|
echo "Need to implement"
|
|
;;
|
|
4)
|
|
echo "Other"
|
|
echo "Need to implement: have user specify the command/commands to run the project"
|
|
;;
|
|
*)
|
|
echo "Invalid option"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
install_dependencies() {
|
|
echo "Checking for required packages..."
|
|
|
|
# Check if the check-dependency command is installed
|
|
if ! command -v check-dependency &> /dev/null
|
|
then
|
|
echo "The command check-dependency could not be found. Please install it before continuing."
|
|
echo "You can find it at https://github.com/quaxlyqueen/scripts."
|
|
exit 1
|
|
fi
|
|
|
|
# Iterate through each line of the dependencies.txt file
|
|
# and check if the package is installed with check-dependency <command> <package>
|
|
while read -r line; do
|
|
command=$(echo $line | awk -F ',' '{print $1}')
|
|
package=$(echo $line | awk -F ',' '{print $2}')
|
|
|
|
check-dependency $command $package
|
|
|
|
# If the package could not be installed, exit the script
|
|
if [[ $? -eq 1 ]]; then
|
|
echo "The command $command from package $package could not be installed. Please install it manually before continuing."
|
|
exit 1
|
|
fi
|
|
|
|
done < dependencies.txt
|
|
}
|
|
|
|
check_sudo() {
|
|
if [[ $EUID -ne 0 ]]; then
|
|
echo "This script must be run as root"
|
|
echo "Try 'sudo ./install'"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
check_args() {
|
|
check_sudo
|
|
|
|
if [[ $# -gt 1 ]]; then
|
|
echo "ERROR: There were too many arguments.\n"
|
|
echo "Usage: install"
|
|
echo " install <path-to-project>"
|
|
echo " install -h | --help"
|
|
exit 1
|
|
|
|
# No arguments provided
|
|
elif [[ $# -eq 0 ]]; then
|
|
|
|
# Check if the current directory contains the required dependencies.txt file.
|
|
if [[ ! -f dependencies.txt ]]; then
|
|
error_missing_dependencies
|
|
|
|
# Install the project
|
|
else
|
|
PROJECT_NAME=$(pwd | cut -d '/' -f5)
|
|
install_dependencies
|
|
install "$PROJECT_NAME"
|
|
fi
|
|
|
|
# User argument provided
|
|
else
|
|
|
|
# Display help message
|
|
if [[ $1 == "-h" || $1 == "--help" ]]; then
|
|
help
|
|
fi
|
|
|
|
# Check if the specified directory exists
|
|
if [[ ! -d $1 ]]; then
|
|
echo "The specified project directory does not exist"
|
|
exit 1
|
|
|
|
# Check if the specified directory contains the required dependencies.txt file.
|
|
elif [[ ! -f $1/dependencies.txt ]]; then
|
|
error_missing_dependencies
|
|
|
|
# Install the project
|
|
else
|
|
PROJECT_NAME=$1
|
|
install_dependencies
|
|
cd $PROJECT_NAME
|
|
fi
|
|
fi
|
|
}
|
|
|
|
check_args $1
|