-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathlaunch.sh
executable file
·93 lines (78 loc) · 2.57 KB
/
launch.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
88
89
90
91
92
93
#!/bin/bash
# Function to activate the virtual environment
activate_venv() {
echo "Activating virtual environment..."
source venv/bin/activate
}
# Function to deactivate the virtual environment
deactivate_venv() {
echo "Deactivating virtual environment..."
deactivate
}
# Function to run the Python script with additional arguments
run_script() {
local script=$1
shift # Shift past the script name to get any additional parameters
echo "Running script: $script with arguments: $@"
python3 "$script" "$@"
}
# Function to list and choose a script
list_and_choose_script() {
echo "Listing available scripts in the current directory and ./games folder:"
# Find all Python scripts, sort them alphabetically, and read into an array
IFS=$'\n' read -d '' -r -a scripts < <(find . ./games -maxdepth 1 -name "*.py" | sort && printf '\0')
if [ ${#scripts[@]} -eq 0 ]; then
echo "No Python scripts found in the current directory or ./games directory."
exit 1
fi
# Enumerate all found scripts
for i in "${!scripts[@]}"; do
# Remove leading './' for cleaner display
display_name="${scripts[$i]#./}"
echo "$((i+1))) $display_name"
done
# Prompt user to select a script
echo "Please select a script by number:"
read -r choice
# Validate the choice
if ! [[ "$choice" =~ ^[0-9]+$ ]]; then
echo "Invalid input. Please enter a number."
exit 1
fi
if (( choice < 1 || choice > ${#scripts[@]} )); then
echo "Choice out of range. Exiting..."
exit 1
fi
selected_script="${scripts[$((choice-1))]}"
if [ -n "$selected_script" ]; then
activate_venv
run_script "$selected_script" "${@:2}"
deactivate_venv
else
echo "Invalid selection. Exiting..."
exit 1
fi
}
# Check if the script argument is provided
if [ -z "$1" ]; then
list_and_choose_script
else
# Append '.py' if it is not present in the script name
script_name="$1"
if [[ ! "$script_name" == *".py" ]]; then
script_name="${script_name}.py"
fi
# Check if the script exists in the ./games directory or current directory
if [ -f "./games/$script_name" ]; then
script_path="./games/$script_name"
elif [ -f "$script_name" ]; then
script_path="$script_name"
else
echo "Specified script not found in the ./games directory or current directory."
list_and_choose_script
exit 1
fi
activate_venv
run_script "$script_path" "${@:2}"
deactivate_venv
fi