-
Notifications
You must be signed in to change notification settings - Fork 0
/
bourbon.sh
140 lines (118 loc) · 3.83 KB
/
bourbon.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
# File to store project-directory pairs
PROJECTS_FILE="$HOME/.bourbon_projects"
# Function to extract project name from pyproject.toml
get_project_name() {
while IFS='=' read -r key value; do
key=$(echo "$key" | tr -d '[:space:]')
value=$(echo "$value" | sed -e 's/^[[:space:]]*//; s/[[:space:]]*$//')
if [[ $key == "name" ]]; then
project_name="${value//[\"\']}"
break
fi
done < pyproject.toml
if [[ -z $project_name ]]; then
echo "Project name not found in pyproject.toml"
return 1
fi
}
# Function to add current project to PROJECTS_FILE
add_project() {
if [ ! -f "pyproject.toml" ]; then
echo "Error: No pyproject.toml file found in the current directory."
return 1
fi
get_project_name
if [ $? -ne 0 ]; then
return 1
fi
local project_name=$project_name
if grep -q "^$project_name|" $PROJECTS_FILE 2>/dev/null; then
echo "Error: Project '$project_name' is already added."
return 1
fi
local project_dir=$(pwd)
echo "$project_name|$project_dir" >> $PROJECTS_FILE
echo "Added $project_name to bourbon projects."
}
# Function to change directory to a project's directory and activate the virtual environment
cd_project() {
if [ ! -f $PROJECTS_FILE ]; then
echo "Error: No projects found. Add a project using 'bourbon add'."
return 1
fi
local project_name="$1"
local project_dir=$(grep "^$project_name|" $PROJECTS_FILE | cut -d '|' -f 2)
if [ -z "$project_dir" ]; then
echo "Error: Project '$project_name' not found."
return 1
fi
cd "$project_dir" || { echo "Error: Could not change directory to '$project_dir'."; return 1; }
if [ -f ".venv/bin/activate" ]; then
source .venv/bin/activate
else
echo "Warning: Virtual environment not found in the project directory."
fi
}
# Function to list all added projects
list_projects() {
if [ ! -f $PROJECTS_FILE ]; then
echo "No projects added."
return 0
fi
cut -d '|' -f 1 $PROJECTS_FILE
}
# Function to remove a project and its directory from PROJECTS_FILE
remove_project() {
if [ ! -f $PROJECTS_FILE ]; then
echo "Error: No projects found. Add a project using 'bourbon add'."
return 1
fi
local project_name="$1"
if [ -z "$project_name" ]; then
if [ ! -f "pyproject.toml" ]; then
echo "Error: No pyproject.toml file found in the current directory."
return 1
fi
get_project_name
project_name=$project_name
fi
if ! grep -q "^$project_name|" $PROJECTS_FILE; then
echo "Error: Project '$project_name' not found."
return 1
fi
local temp_file=$(mktemp)
grep -v "^$project_name|" $PROJECTS_FILE > $temp_file
mv $temp_file $PROJECTS_FILE
echo "Removed $project_name from bourbon projects."
}
# Function to process arguments and invoke the appropriate function
bourbon() {
if [ "$#" -eq 0 ]; then
echo "Usage: bourbon <command> [<args>]"
echo ""
echo "Commands:"
echo " add Add the current project to bourbon projects."
echo " <project> Change directory to the specified project and activate the virtual environment."
echo " list List all added bourbon projects."
echo " remove Remove a project from bourbon projects."
echo " Use 'remove <project>' to specify a project, or 'remove' to remove the current project."
return 0
fi
local command="$1"
shift
case $command in
add)
add_project
;;
list)
list_projects
;;
remove)
remove_project "$@"
;;
*)
cd_project "$command"
;;
esac
}