-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathprecommit-setup.sh
87 lines (79 loc) · 2.43 KB
/
precommit-setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
# Function to add the installation path to PATH
add_to_path() {
if [[ ":$PATH:" != *":$1:"* ]]; then
export PATH="$PATH:$1"
echo "Added $1 to PATH"
else
echo "$1 is already in PATH"
fi
}
# Function to check if Python and pip are installed
check_python_and_pip() {
if ! command -v python3 &> /dev/null; then
echo "Python3 is not installed. Please install Python3 and try again."
exit 1
fi
if ! command -v pip &> /dev/null; then
echo "pip is not installed. Please install pip and try again."
exit 1
fi
}
# Function to install pre-commit
install_precommit() {
if ! command -v pre-commit &> /dev/null; then
echo "pre-commit not found, installing..."
pip install --user pre-commit
else
echo "pre-commit is already installed"
fi
}
# Check if Python and pip are installed
check_python_and_pip
# Detect the operating system
OS="$(uname -s)"
case "$OS" in
Linux*)
echo "Detected Linux"
INSTALL_PATH="$HOME/.local/bin"
install_precommit
add_to_path "$INSTALL_PATH"
;;
Darwin*)
echo "Detected MacOS"
INSTALL_PATH="$HOME/.local/bin"
install_precommit
add_to_path "$INSTALL_PATH"
;;
CYGWIN*|MINGW32*|MSYS*|MINGW*)
echo "Detected Windows"
INSTALL_PATH="$HOME/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0/LocalCache/local-packages/Python312/Scripts"
install_precommit
add_to_path "$INSTALL_PATH"
;;
*)
echo "Unknown OS"
exit 1
;;
esac
# Add the installation path to the shell profile for persistence
if [[ "$OS" == "Linux" || "$OS" == "Darwin" ]]; then
SHELL_PROFILE="$HOME/.bashrc"
if [[ -f "$HOME/.zshrc" ]]; then
SHELL_PROFILE="$HOME/.zshrc"
fi
echo "export PATH=\$PATH:$INSTALL_PATH" >> "$SHELL_PROFILE"
source "$SHELL_PROFILE"
elif [[ "$OS" == "CYGWIN"* || "$OS" == "MINGW"* || "$OS" == "MSYS"* ]]; then
SHELL_PROFILE="$HOME/.bash_profile"
echo "export PATH=\$PATH:$INSTALL_PATH" >> "$SHELL_PROFILE"
source "$SHELL_PROFILE"
fi
# Verify the installation
if command -v pre-commit &> /dev/null; then
echo "pre-commit installation successful"
pre-commit --version
else
echo "pre-commit installation failed"
exit 1
fi