diff --git a/bin/connect_to_devcontainer.sh b/bin/connect_to_devcontainer.sh index ee8cc48..cd8d837 100755 --- a/bin/connect_to_devcontainer.sh +++ b/bin/connect_to_devcontainer.sh @@ -7,36 +7,31 @@ set -e # TODO: Give the possibility of using multiple terminals, not only gnome-terminal # TODO: Give the possibility of opening a new tmux page instead of opening a new container -# Get the folder of the current file -function get_script_dir { - # SOURCE: https://stackoverflow.com/a/246128/10491337 - local SOURCE="${BASH_SOURCE[0]}" - local DIR="" - while [ -h "${SOURCE}" ]; do - DIR="$(cd -P "$(dirname "${SOURCE}")" >/dev/null 2>&1 && pwd)" - SOURCE="$(readlink "${SOURCE}")" - [[ "${SOURCE}" != /* ]] && SOURCE="${DIR}/${SOURCE}" - done - cd -P "$(dirname "${SOURCE}")" >/dev/null 2>&1 && pwd -} +if [ -n "$TMUX" ]; then + tmux_open="tmux split-window -h -t \"$TMUX_PANE\" " +else + tmux_open="" +fi -SCRIPT_DIR=$(get_script_dir) # Execute the command for opening the devcontainer in the following terminal: if [ -x "$(command -v alacritty)" ]; then # ALACRITTY TERMINAL EMULATOR REPOSTORY_NAME=$(basename "$(pwd)") - TERMINAL_TITLE="Devcontainer [${REPOSTORY_NAME}] - You are inside a Docker Container now...!" - alacritty --working-directory . --title "${TERMINAL_TITLE}" -e "${SCRIPT_DIR}"/open_shell_in_devcontainer.sh & + TERMINAL_TITLE="Devcontainer [${REPOSTORY_NAME}]" + command="alacritty --working-directory . --title "${TERMINAL_TITLE}" -e ${tmux_open}$@ &" elif [ -x "$(command -v gnome-terminal)" ]; then # GNOME TERMINAL - gnome-terminal -- bash -c "${SCRIPT_DIR}"/open_shell_in_devcontainer.sh + command="gnome-terminal -- ${tmux_open}$@" elif [ "$(uname)" == "Darwin" ] && [ -x "$(command -v iTerm)" ]; then # MAC ITERM2 TERMINAL EMULATOR - open -a iTerm.app "${SCRIPT_DIR}"/open_shell_in_devcontainer.sh + command="open -a iTerm.app ${tmux_open}$@" elif [ "$(uname)" == "Darwin" ] && [ -x "$(command -v Terminal)" ]; then # MAC TERMINAL - open -a Terminal.app "${SCRIPT_DIR}"/open_shell_in_devcontainer.sh + command="open -a Terminal.app ${tmux_open}$@" else # TERMINAL NO DEFINED echo "ERROR: No compatible emulators found!" + exit 1 fi + +eval "${command}" diff --git a/bin/open_shell_in_devcontainer.sh b/bin/open_shell_in_devcontainer.sh deleted file mode 100755 index 99304ea..0000000 --- a/bin/open_shell_in_devcontainer.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash -# This executable file has been isolated from ./bin/connect_to_devcontainer.sh so it -# can be executed in MAC Terminals (Termina.app/iTerm.app) - -tmux-split-cmd() (tmux split-window -h -t "$TMUX_PANE" "bash --rcfile <(echo '. ~/.bashrc;$*')") -tmux-new-cmd() (tmux new-window -n Devcontainer "bash --rcfile <(echo '. ~/.bashrc;$*')") - -set -e - -WORKSPACE="$(pwd)" -SHELL="zsh" - -workspace_folder=$(devcontainer read-configuration --include-merged-configuration --log-format json --workspace-folder . 2>/dev/null | jq .workspace.workspaceFolder | sed 's/"//g') -docker_id=$(docker ps -q -a --filter label=devcontainer.local_folder="${WORKSPACE}" --filter label=devcontainer.config_file="${WORKSPACE}"/.devcontainer/devcontainer.json) - -open_shell_in_devcontainer_command="docker exec -it ${docker_id} bash -c \"cd ${workspace_folder} && ${SHELL}\"" - -# Check if we are inside a tmux session -# If so, create a new pane and execute the open_shell_in_devcontainer_command -# If not, just execute the open_shell_in_devcontainer_command - -if [ -n "$TMUX" ]; then - # Replace by tmux-split-cmd if you want to split the current pane horizontally - tmux-new-cmd "${open_shell_in_devcontainer_command}" -else - eval "${open_shell_in_devcontainer_command}" -fi -# docker exec -it "${docker_id}" ${SHELL} - -# TODO: It would be great to use th devcontainer exec command as long as we know how to fix the visualization issue inside the docker container! -# If you want to give a try, just comment the docker exec -it ... above and uncommend the line below. Then execute the :DevcontainerConnect and open vim. -# There you will see visualzation issues which do not occur when connectint to docker via docker exec - -# devcontainer exec --workspace-folder ${WORKSPACE} ${SHELL} diff --git a/lua/devcontainer-cli/config/init.lua b/lua/devcontainer-cli/config/init.lua index f9381fe..269121b 100644 --- a/lua/devcontainer-cli/config/init.lua +++ b/lua/devcontainer-cli/config/init.lua @@ -35,7 +35,7 @@ local default_config = { dotfiles_repository = "git@github.com:erichlf/dotfiles", -- branch to checkout for repositories (this is a feature not supported by -- devcontainers in general, but we do) - dotfiles_repository = "devcontainer", + dotfiles_branch = "main", -- directory for the setup environment dotfiles_targetPath = "~/dotfiles", -- command that's executed for installed the dependencies from the @@ -43,9 +43,14 @@ local default_config = { dotfiles_installCommand = "install.sh", -- The number of columns to wrap text at terminal_columns = 80, + -- The particular binary to use for connecting to in the devcontainer + -- Most likely this should remain nvim + nvim_binary = "nvim", -- The shell to use for executing command. Available sh, bash, zsh or any -- other that uses '-c' to signify a command is to follow shell = 'bash', + -- The name of the socket file to use + port = "7777", } local options diff --git a/lua/devcontainer-cli/devcontainer_cli.lua b/lua/devcontainer-cli/devcontainer_cli.lua index bf0ecd9..ea3343f 100644 --- a/lua/devcontainer-cli/devcontainer_cli.lua +++ b/lua/devcontainer-cli/devcontainer_cli.lua @@ -1,15 +1,15 @@ -- Copyright (c) 2024 Erich L Foster --- +-- -- Permission is hereby granted, free of charge, to any person obtaining a copy of -- this software and associated documentation files (the "Software"), to deal in -- the Software without restriction, including without limitation the rights to -- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -- of the Software, and to permit persons to whom the Software is furnished to do -- so, subject to the following conditions: --- +-- -- The above copyright notice and this permission notice shall be included in all -- copies or substantial portions of the Software. --- +-- -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -17,30 +17,10 @@ -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -- SOFTWARE. - -local config = require("devcontainer-cli.config") local utils = require("devcontainer-cli.devcontainer_utils") local M = {} -local function define_autocommands() - local au_id = vim.api.nvim_create_augroup("devcontainer.docker.terminal", {}) - vim.api.nvim_create_autocmd("UILeave", { - group = au_id, - callback = function() - -- It connects with the Devcontainer just after quiting neovim. - -- TODO: checks that the devcontainer is not already connected - -- TODO: checks that there is a devcontainer running - vim.schedule( - function() - local command = config.nvim_plugin_folder .. "/bin/connect_to_devcontainer.sh" - vim.fn.jobstart(command, { detach = true }) - end - ) - end, - }) -end - -- executes a given command in the devcontainer of the current project directory ---@param opts (table) options for executing the command function M.exec(opts) @@ -82,7 +62,11 @@ end -- Thanks to the autocommand executed after leaving the UI, after closing the -- neovim window the devcontainer will be automatically open in a new terminal function M.connect() - define_autocommands() + if not utils.create_connect_cmd() then + vim.notify("Failed to create autocommand", vim.log.levels.ERROR) + return + end + vim.cmd("wqa") end diff --git a/lua/devcontainer-cli/devcontainer_utils.lua b/lua/devcontainer-cli/devcontainer_utils.lua index 15c74c0..daa133b 100644 --- a/lua/devcontainer-cli/devcontainer_utils.lua +++ b/lua/devcontainer-cli/devcontainer_utils.lua @@ -333,4 +333,34 @@ function M.toggle() _terminal:toggle() end +-- create the necessary functions needed to connect to nvim in a devcontainer +function M.create_connect_cmd() + local au_id = vim.api.nvim_create_augroup("devcontainer-cli.connect", {}) + local dev_command = _devcontainer_command("exec") + if dev_command == nil then + return false + end + dev_command = dev_command .. " " .. config.nvim_binary + + vim.api.nvim_create_autocmd( + "UILeave", + { + group = au_id, + callback = + function() + local connect_command = {config.nvim_plugin_folder .. "/bin/connect_to_devcontainer.sh"} + table.insert(connect_command, dev_command) + local command = table.concat(connect_command, " ") + vim.schedule( + function() + vim.fn.jobstart(command, { detach = true }) + end + ) + end + } + ) + + return true +end + return M