Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated the project setup with a single command for both Windows and Unix-based systems using Batch and Shell scripts with pacman support and comments #162

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions setup.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@echo off
REM Ensure script is run as administrator
net session >nul 2>&1
if not %errorLevel% == 0 (
echo This script must be run as administrator.
pause
exit /b 1
)

REM Install Scoop if not already installed
where scoop >nul 2>&1
if %errorLevel% neq 0 (
echo Installing Scoop...
powershell -Command "Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force; iex (new-object net.webclient).downloadstring('https://get.scoop.sh')"
set PATH=%PATH%;%USERPROFILE%\scoop\shims
)

REM Install necessary tools using Scoop
echo Installing dependencies...
scoop install git python llvm cmake curl

REM Install Python dependencies
echo Installing Python dependencies...
pip install mako

REM Build and run the project using Cargo
echo Building the project...
cargo run

echo Setup complete. Press any key to exit.
pause
38 changes: 38 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works only on systems that follow the FHS 3.0 standard and e.g. on NixOS it's broken with:

$ /bin/bash
bash: /bin/bash: No such file or directory

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#!/usr/bin/env bash would be "universally POSIX way" :-) Should work also in NixOS.


# Ensure script is run as root
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root (use sudo)."
exit 1
fi

# Install necessary packages based on the package manager available
if [ -x "$(command -v apt-get)" ]; then
echo "Detected Debian-based system. Installing dependencies using apt-get."
sudo apt-get update
sudo apt-get install -y git python3-pip llvm cmake curl

elif [ -x "$(command -v yum)" ]; then
echo "Detected Red Hat-based system. Installing dependencies using yum."
sudo yum install -y git python3-pip llvm cmake curl

elif [ -x "$(command -v pacman)" ]; then
echo "Detected Arch-based system. Installing dependencies using pacman."
sudo pacman -Sy --needed git python-pip llvm cmake curl

elif [ -x "$(command -v brew)" ]; then
echo "Detected macOS. Installing dependencies using Homebrew."
brew install git python3 llvm cmake curl

else
echo "Unsupported OS or package manager. Please install dependencies manually."
exit 1
fi
Comment on lines +9 to +30
Copy link

@Kreyren Kreyren Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stacking if-elif-elif-...-else unnecesarily increases the maintanance burden and it's common bad programming practice to the point that it's a popular programming meme named after YandereDev who popularized it as it makes the code look bad and with complexity very difficult to maintain. In general if you have to do more than 3 stacked conditions then you probably want to use a switch statement or it's implementation in shell case.

On Linux it's standard to use Linux Standard Base (latest LSB 5.0) to identify the linux distribution and it's release as e.g. there are scenarios where pacman is used on multiple distributions that might not have the same packages or their naming e.g. Parabola GNU/Linux

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both bash and zsh have OSTYPE environment variable, which can be used along the lines of:

# Grep for 'darwin':
if [[ $OSTYPE =~ "darwin" ]]; then
    echo "macOS"
else
    echo "not macOS"
fi

That said, upstream projects never should modify system state as part of any script. At most it could suggest packages but not perform any action. It's a security border IMHO that should not be crossed.
 


# Install Python dependencies
echo "Installing Python dependencies..."
pip3 install mako

# Build and run the project using Cargo
echo "Building the project..."
cargo run
Comment on lines +1 to +38
Copy link

@Kreyren Kreyren Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#!/bin/bash
# Ensure script is run as root
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root (use sudo)."
exit 1
fi
# Install necessary packages based on the package manager available
if [ -x "$(command -v apt-get)" ]; then
echo "Detected Debian-based system. Installing dependencies using apt-get."
sudo apt-get update
sudo apt-get install -y git python3-pip llvm cmake curl
elif [ -x "$(command -v yum)" ]; then
echo "Detected Red Hat-based system. Installing dependencies using yum."
sudo yum install -y git python3-pip llvm cmake curl
elif [ -x "$(command -v pacman)" ]; then
echo "Detected Arch-based system. Installing dependencies using pacman."
sudo pacman -Sy --needed git python-pip llvm cmake curl
elif [ -x "$(command -v brew)" ]; then
echo "Detected macOS. Installing dependencies using Homebrew."
brew install git python3 llvm cmake curl
else
echo "Unsupported OS or package manager. Please install dependencies manually."
exit 1
fi
# Install Python dependencies
echo "Installing Python dependencies..."
pip3 install mako
# Build and run the project using Cargo
echo "Building the project..."
cargo run
#!/usr/bin/env sh
# shellcheck shell=sh # POSIX
set -e # Exit on false return
die() { printf "FATAL: %s/n" "$2"; exit "${1:-1}"; }
[ "$(id -u)" = 0 ] || die 13 "This script '$0' must be run as root" # Root Check
kernelId="$(uname --kernel-name)"
kernelRel="$(uname --kernel-release)"
kernel="$kernelId:$kernelRel"
case "$kernel" in
Linux:*)
command -v lsb_release 1>/dev/null || die 127 "Required command 'lsb_release', needed to identify the host's distribution was not found in the current environment. Please install it"
distroId="$(lsb_release -s -i | sed 's/"//g')"
distroRel="$(lsb_release -s -r | sed 's/"//g')"
distro="$distroId:$distroRel"
case "$distro" in
arch:*)
echo "Detected Red Hat-based system. Installing dependencies using yum."
sudo yum install -y git python3-pip llvm cmake curl
;;
debian:*)
echo "Detected Debian-based system. Installing dependencies using apt-get."
sudo apt-get update
sudo apt-get install -y git python3-pip llvm cmake curl
;;
fedora:*)
echo "Detected Red Hat-based system. Installing dependencies using yum."
sudo yum install -y git python3-pip llvm cmake curl
;;
NixOS:*)
die 1 "This script does not support NixOS as a method for installing dependencies, use the provided shell environment instead"
;;
*)
die 1 "Distribution '$distro' is not implemented, please contribute a support for it in script: $0"
esac
;;
Darwin:*)
echo "Detected macOS. Installing dependencies using Homebrew."
brew install git python3 llvm cmake curl
;;
*) die 1 "Kernel '$kernel' is not implemented, please contribute a support for it in script: $0"
esac
# Install Python dependencies
echo "Installing Python dependencies..."
pip3 install mako
# Build and run the project using Cargo
echo "Building the project..."
cargo run