-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkode.sh
More file actions
executable file
·187 lines (165 loc) · 6.04 KB
/
kode.sh
File metadata and controls
executable file
·187 lines (165 loc) · 6.04 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env bash
kode() (
# Determine the config directory
config_dir
if [[ -n "${XDG_CONFIG_HOME}" ]]; then
config_dir="${XDG_CONFIG_HOME}/kodesh"
else
config_dir="${HOME}/.config/kodesh"
fi
config="${config_dir}/config.json"
ide_prefs="${config_dir}/ide_preferences.json"
log_dir="${config_dir}/logs"
# Function to print help message
print_help() {
echo "Usage: kode [OPTIONS] [PROJECT] [IDE]"
echo
echo "Options:"
echo " --help Show this help message"
echo " --set-default-dir DIR Set the default projects directory"
echo " --add-project DIR Add a project directory manually"
echo " cat Display the contents of the config file"
echo " ls List projects in the default directory and manual projects"
echo
echo "Arguments:"
echo " PROJECT Name of the project directory"
echo " IDE (Optional) IDE to use (z: Zed, vs: VS Code, id: IntelliJ IDEA)"
echo " If not specified, the last used IDE for the project will be used"
echo
echo "Examples:"
echo " kode myproject vs Open 'myproject' with VS Code and set it as the preferred IDE"
echo " kode myproject Open 'myproject' with the last used IDE"
echo " kode --set-default-dir ~/work"
echo " kode --add-project ~/documents/project"
}
# Function to set default projects directory
set_default_dir() {
if [ -d "$1" ]; then
jq --arg dir "$1" '.default_projects_dir = $dir' "$config" > "${config}.tmp" && mv "${config}.tmp" "$config"
echo "Default projects directory set to $1"
else
echo "Error: Directory $1 does not exist"
fi
}
# Function to add a project manually
add_project() {
if [ -d "$1" ]; then
project_name=$(basename "$1")
jq --arg name "$project_name" --arg path "$1" '.manual_projects[$name] = $path' "$config" > "${config}.tmp" && mv "${config}.tmp" "$config"
echo "Project $project_name added with path $1"
else
echo "Error: Directory $1 does not exist"
fi
}
# Ensure config directory exists
mkdir -p "${config_dir}"
# Create config files if they don't exist
[ ! -f "$config" ] && echo '{}' > "$config"
[ ! -f "$ide_prefs" ] && echo '{}' > "$ide_prefs"
# Parse options
case "$1" in
--help)
print_help
return 0
;;
--set-default-dir)
set_default_dir "$2"
return 0
;;
--add-project)
add_project "$2"
return 0
;;
cat)
echo "Config:"
cat "$config"
echo "IDE Preferences:"
cat "$ide_prefs"
return 0
;;
ls)
default_dir=$(jq -r '.default_projects_dir // empty' "$config")
if [ -d "$default_dir" ]; then
echo "Projects in $default_dir:"
ls "$default_dir"
else
echo "Default projects directory not set or doesn't exist."
fi
echo "Manually added projects:"
jq -r '.manual_projects | keys[]' "$config"
return 0
;;
esac
if [ -z "$1" ]; then
echo "Error: Project name cannot be empty. Use 'kode --help' for usage information."
return 1
fi
# Get project directory
default_dir=$(jq -r '.default_projects_dir // empty' "$config")
manual_project_path=$(jq -r --arg name "$1" '.manual_projects[$name] // empty' "$config")
if [ -n "$manual_project_path" ]; then
project_dir="$manual_project_path"
elif [ -d "$default_dir/$1" ]; then
project_dir="$default_dir/$1"
else
echo "Error: Project '$1' not found. Use 'kode --add-project' to add it manually."
return 1
fi
if [ -z "$2" ]; then
ide=$(jq -r --arg name "$1" '.[$name] // empty' "$ide_prefs")
if [ -z "$ide" ]; then
echo "Error: No IDE preference set for this project. Please specify an IDE."
return 1
fi
else
ide="$2"
fi
# Get IDE paths from config
zed_path=$(jq -r '.ide_paths.zed // "/usr/bin/zeditor"' "$config")
vscode_path=$(jq -r '.ide_paths.vscode // "/usr/bin/code"' "$config")
intellij_path=$(jq -r '.ide_paths.intellij // "/usr/bin/idea"' "$config")
case "$ide" in
'z') command="$zed_path $project_dir" ;;
'vs') command="$vscode_path $project_dir" ;;
'id') command="$intellij_path $project_dir" ;;
*) echo "Error: Invalid IDE option. Please use either 'z', 'vs' or 'id'."
return 1 ;;
esac
# Update IDE preference
jq --arg key "$1" --arg value "$ide" '.[$key] = $value' "$ide_prefs" > "${ide_prefs}.tmp" && mv "${ide_prefs}.tmp" "$ide_prefs"
mkdir -p "$log_dir"
timestamp=$(date +"%Y%m%d_%H%M%S")
log_file="$log_dir/${timestamp}.log"
case "$ide" in
'z') ide_name="Zed" ;;
'vs') ide_name="VS Code" ;;
'id') ide_name="IntelliJ IDEA" ;;
esac
# Disabling shellcheck suggestion for printf command
# shellcheck disable=SC2059
printf "\e[32mOpening \e[33m\e[1m$1\e[0m\e[32m with \e[33m\e[1m$ide_name\e[0m\e[32m"
for _ in {1..3}; do
sleep 0.2
printf "."
done
printf "\e[0m\n"
sleep 0.2
# shellcheck disable=SC2086
nohup $command > "$log_file" 2>&1 &
cd "$project_dir" || return 1
)
# Initialize config with default values if it's empty
if [ ! -s "${XDG_CONFIG_HOME:-$HOME/.config}/kodesh/config.json" ]; then
mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/kodesh"
cat > "${XDG_CONFIG_HOME:-$HOME/.config}/kodesh/config.json" << EOF
{
"default_projects_dir": "$HOME/Projects",
"manual_projects": {},
"ide_paths": {
"zed": "/usr/bin/zeditor",
"vscode": "/usr/bin/code",
"intellij": "/usr/bin/idea"
}
}
EOF
fi