1+ #! /bin/bash
2+ 
3+ #  Script to start devcontainer with Neovim configuration mounted
4+ #  Usage: ./start-devcontainer-nvim.sh [command]
5+ 
6+ set  -e
7+ 
8+ #  Colors for output
9+ GREEN=' \033[0;32m' 
10+ YELLOW=' \033[1;33m' 
11+ RED=' \033[0;31m' 
12+ NC=' \033[0m' #  No Color
13+ 
14+ #  Get the directory of this script
15+ SCRIPT_DIR=" $(  cd  " $(  dirname " ${BASH_SOURCE[0]} " ) " &&  pwd  ) " 
16+ 
17+ #  Function to print colored messages
18+ print_status () {
19+     echo  -e " ${GREEN} [*]${NC}  $1 " 
20+ }
21+ 
22+ print_warning () {
23+     echo  -e " ${YELLOW} [!]${NC}  $1 " 
24+ }
25+ 
26+ print_error () {
27+     echo  -e " ${RED} [✗]${NC}  $1 " 
28+ }
29+ 
30+ #  Check if devcontainer CLI is installed
31+ if  !  command  -v devcontainer & >  /dev/null;  then 
32+     print_error " devcontainer CLI is not installed" 
33+     echo  " Install it with: npm install -g @devcontainers/cli" 
34+     exit  1
35+ fi 
36+ 
37+ #  Check if Docker is running
38+ if  !  docker info & >  /dev/null;  then 
39+     print_error " Docker is not running" 
40+     echo  " Please start Docker and try again" 
41+     exit  1
42+ fi 
43+ 
44+ #  Function to build the container
45+ build_container () {
46+     print_status " Building devcontainer..." 
47+     devcontainer build --workspace-folder " $SCRIPT_DIR " 
48+ }
49+ 
50+ #  Function to start the container with mounted Neovim config
51+ start_container () {
52+     print_status " Starting devcontainer with Neovim config mounted..." 
53+ 
54+     #  Check if local Neovim config exists
55+     if  [ -d  " $HOME /.config/nvim" ;  then 
56+         print_status " Mounting local Neovim config from $HOME /.config/nvim" 
57+         MOUNT_ARGS=" --mount type=bind,source=$HOME /.config/nvim,target=/home/vscode/.config/nvim" 
58+     else 
59+         print_warning " No Neovim config found at $HOME /.config/nvim" 
60+         MOUNT_ARGS=" " 
61+     fi 
62+ 
63+     #  Also mount local Neovim data directory if it exists (for plugins, etc.)
64+     if  [ -d  " $HOME /.local/share/nvim" ;  then 
65+         print_status " Mounting local Neovim data from $HOME /.local/share/nvim" 
66+         MOUNT_ARGS=" $MOUNT_ARGS  --mount type=bind,source=$HOME /.local/share/nvim,target=/home/vscode/.local/share/nvim" 
67+     fi 
68+ 
69+     #  Mount Neovim cache directory if it exists
70+     if  [ -d  " $HOME /.cache/nvim" ;  then 
71+         print_status " Mounting local Neovim cache from $HOME /.cache/nvim" 
72+         MOUNT_ARGS=" $MOUNT_ARGS  --mount type=bind,source=$HOME /.cache/nvim,target=/home/vscode/.cache/nvim" 
73+     fi 
74+ 
75+     devcontainer up $MOUNT_ARGS  --workspace-folder " $SCRIPT_DIR " 
76+ }
77+ 
78+ #  Function to install Neovim in the container
79+ install_nvim () {
80+     print_status " Installing Neovim in the container..." 
81+ 
82+     #  Check if nvim is already installed
83+     if  devcontainer exec  --workspace-folder " $SCRIPT_DIR " & > /dev/null;  then 
84+         print_status " Neovim is already installed" 
85+         return  0
86+     fi 
87+ 
88+     print_status " Installing Neovim via apt..." 
89+     devcontainer exec  --workspace-folder " $SCRIPT_DIR " 
90+     devcontainer exec  --workspace-folder " $SCRIPT_DIR " 
91+ 
92+     #  Install common dependencies for Neovim plugins
93+     print_status " Installing common dependencies (ripgrep, fd-find, etc.)..." 
94+     devcontainer exec  --workspace-folder " $SCRIPT_DIR " 
95+ 
96+     print_status " Neovim installation complete" 
97+ }
98+ 
99+ #  Function to run Neovim in the container
100+ run_nvim () {
101+     print_status " Starting Neovim in devcontainer..." 
102+ 
103+     #  Ensure Neovim is installed
104+     install_nvim
105+ 
106+     #  Pass any additional arguments to nvim
107+     shift  #  Remove the 'nvim' command from arguments
108+     devcontainer exec  --workspace-folder " $SCRIPT_DIR " " $@ " 
109+ }
110+ 
111+ #  Function to execute arbitrary commands in the container
112+ exec_command () {
113+     shift  #  Remove the 'exec' command from arguments
114+     devcontainer exec  --workspace-folder " $SCRIPT_DIR " " $@ " 
115+ }
116+ 
117+ #  Function to open a shell in the container
118+ open_shell () {
119+     print_status " Opening shell in devcontainer..." 
120+     devcontainer exec  --workspace-folder " $SCRIPT_DIR " 
121+ }
122+ 
123+ #  Function to stop the container
124+ stop_container () {
125+     print_status " Stopping devcontainer..." 
126+     docker stop python-3.10-ag2 2> /dev/null ||  true 
127+ }
128+ 
129+ #  Function to rebuild the container
130+ rebuild_container () {
131+     print_status " Rebuilding devcontainer..." 
132+     stop_container
133+     build_container
134+     start_container
135+ }
136+ 
137+ #  Function to check if container is running
138+ is_container_running () {
139+     docker ps --format ' {{.Names}}' |  grep -q ' ^python-3.10-ag2$' 
140+ }
141+ 
142+ #  Main script logic
143+ case  " ${1:- } " in 
144+     build)
145+         build_container
146+         ;;
147+     start)
148+         if  is_container_running;  then 
149+             print_warning " Container is already running" 
150+         else 
151+             start_container
152+         fi 
153+         ;;
154+     nvim)
155+         if  !  is_container_running;  then 
156+             start_container
157+         fi 
158+         run_nvim " $@ " 
159+         ;;
160+     exec)
161+         if  !  is_container_running;  then 
162+             print_error " Container is not running. Start it first with: $0  start" 
163+             exit  1
164+         fi 
165+         exec_command " $@ " 
166+         ;;
167+     shell)
168+         if  !  is_container_running;  then 
169+             start_container
170+         fi 
171+         open_shell
172+         ;;
173+     stop)
174+         stop_container
175+         ;;
176+     rebuild)
177+         rebuild_container
178+         ;;
179+     status)
180+         if  is_container_running;  then 
181+             print_status " Container is running" 
182+         else 
183+             print_warning " Container is not running" 
184+         fi 
185+         ;;
186+     * )
187+         echo  " Usage: $0  {build|start|nvim|exec|shell|stop|rebuild|status}" 
188+         echo  " " 
189+         echo  " Commands:" 
190+         echo  "   build    - Build the devcontainer image" 
191+         echo  "   start    - Start the devcontainer with mounted Neovim config" 
192+         echo  "   nvim     - Run Neovim in the devcontainer (installs if needed)" 
193+         echo  "   exec     - Execute a command in the running container" 
194+         echo  "   shell    - Open a shell in the devcontainer" 
195+         echo  "   stop     - Stop the running devcontainer" 
196+         echo  "   rebuild  - Rebuild and restart the devcontainer" 
197+         echo  "   status   - Check if the container is running" 
198+         echo  " " 
199+         echo  " Examples:" 
200+         echo  "   $0  start           # Start the devcontainer" 
201+         echo  "   $0  nvim            # Open Neovim" 
202+         echo  "   $0  nvim file.py    # Open a specific file in Neovim" 
203+         echo  "   $0  exec python     # Run Python in the container" 
204+         echo  "   $0  shell           # Open a shell in the container" 
205+         exit  1
206+         ;;
207+ esac 
0 commit comments