feat(bin): update pf script to use wezterm tabs
When the move from tmux I can no longer use pf to start processes from a Procfile in a new session. This will do basically the same if we are not in a tmux session and start all the processes in wezterm tabs in the same workspace. This currently does not support closing the tabs, if this needs to be done you can kill the hole terminal and start it again. This also updates the script so you can pass in a file and it will use that file. Previously it was hardcoded to `Procfile.dev`, now this is the default however, can be overridden
This commit is contained in:
parent
6b939f0486
commit
7603040373
1 changed files with 57 additions and 17 deletions
|
|
@ -18,43 +18,83 @@
|
||||||
[]
|
[]
|
||||||
(string/join "/" (map #(.toString %) (take 2 (reverse(fs/components (fs/cwd)))))))
|
(string/join "/" (map #(.toString %) (take 2 (reverse(fs/components (fs/cwd)))))))
|
||||||
|
|
||||||
(defn parse-procfile
|
(defn parse-procfile
|
||||||
"Parses a procfile file and returns a list of [name command] pairs."
|
"Parses a procfile file and returns a list of [name command] pairs."
|
||||||
[]
|
[file]
|
||||||
(map #(string/split % #":\s+") (string/split (slurp "Procfile.dev") #"\n" )))
|
(map #(string/split % #":\s+") (string/split (slurp file) #"\n" )))
|
||||||
|
|
||||||
(defn has-session?
|
(defn has-tmux-session?
|
||||||
"Tests to see if a session already exists with a given name."
|
"Tests to see if a session already exists with a given name."
|
||||||
[session-name]
|
[session-name]
|
||||||
(string/includes? (:out (shell {:out :string} "tmux list-sessions")) session-name))
|
(string/includes? (:out (shell {:out :string} "tmux list-sessions")) session-name))
|
||||||
|
|
||||||
(defn has-window?
|
(defn has-tmux-window?
|
||||||
"Tests to see if a window already exists within a tmux session."
|
"Tests to see if a window already exists within a tmux session."
|
||||||
[session-name window-name]
|
[session-name window-name]
|
||||||
(string/includes? (:out (shell {:out :string} (format "tmux list-windows -t '%s'" session-name))) window-name))
|
(string/includes? (:out (shell {:out :string} (format "tmux list-windows -t '%s'" session-name))) window-name))
|
||||||
|
|
||||||
(defn create-window
|
(defn create-tmux-window
|
||||||
"Creates a new tmux window in a session if it dose not already exists. Then
|
"Creates a new tmux window in a session if it dose not already exists. Then
|
||||||
it will run any commands that are passed inside the new window."
|
it will run any commands that are passed inside the new window."
|
||||||
[project name & commands]
|
[project name & commands]
|
||||||
(when-not (has-window? project name)
|
(when-not (has-tmux-window? project name)
|
||||||
(shell (format "tmux new-window -t '%s' -n '%s'" project name))
|
(shell (format "tmux new-window -t '%s' -n '%s'" project name))
|
||||||
(doseq [command commands]
|
(doseq [command commands]
|
||||||
(shell (format "tmux send-keys -t %s:'%s' '%s' C-m" project name command)))))
|
(shell (format "tmux send-keys -t %s:'%s' '%s' C-m" project name command)))))
|
||||||
|
|
||||||
(defn command-start [_]
|
(defn tmux-start [file]
|
||||||
(when (not (fs/exists? "Procfile.dev"))
|
(when (not (has-tmux-session? (current-project-name)))
|
||||||
(println "No Procfile.dev found in the current directory")
|
|
||||||
(System/exit 0))
|
|
||||||
|
|
||||||
(when (not (has-session? (current-project-name)))
|
|
||||||
(shell (format "tmux new-session -d -c %s -s '%s'" (fs/cwd) (current-project-name))))
|
(shell (format "tmux new-session -d -c %s -s '%s'" (fs/cwd) (current-project-name))))
|
||||||
|
|
||||||
(doseq [[name command] (parse-procfile)]
|
(doseq [[name command] (parse-procfile file)]
|
||||||
(create-window (current-project-name) name command)))
|
(create-tmux-window (current-project-name) name command)))
|
||||||
|
|
||||||
|
(defn is-in-tmux?
|
||||||
|
"Tests to see if we are in a tmux session or not."
|
||||||
|
[]
|
||||||
|
(not (nil? (System/getenv "TMUX"))))
|
||||||
|
|
||||||
|
(defn has-wezterm-pane?
|
||||||
|
"Tests to see if a wezterm pane already exists."
|
||||||
|
[name]
|
||||||
|
(string/includes? (:out (shell {:out :string} "wezterm cli list")) (format "pf: %s" name)))
|
||||||
|
|
||||||
|
(defn create-wezterm-pane
|
||||||
|
"Create a new wezterm pane and spawn a command in it."
|
||||||
|
[name command]
|
||||||
|
(let [pane-id (:out (shell {:out :string} (format "wezterm cli spawn --cwd %s" (fs/cwd))))]
|
||||||
|
(shell (format "wezterm cli set-tab-title --pane-id %s 'pf: %s'" pane-id name))
|
||||||
|
(shell {:in command} (format "wezterm cli send-text --pane-id %s" pane-id))
|
||||||
|
(shell {:in "\n"} (format "wezterm cli send-text --pane-id %s" pane-id))))
|
||||||
|
|
||||||
|
(defn wezterm-start [file]
|
||||||
|
(let [pane-id (System/getenv "WEZTERM_PANE")]
|
||||||
|
(doseq [[name command] (parse-procfile file)]
|
||||||
|
(when (not (has-wezterm-pane? name))
|
||||||
|
(create-wezterm-pane name command)))
|
||||||
|
|
||||||
|
(shell (format "wezterm cli activate-pane --pane-id %s" pane-id))))
|
||||||
|
|
||||||
|
(defn is-in-wezterm?
|
||||||
|
"Tests to see if we are in a wezterm session or not."
|
||||||
|
[]
|
||||||
|
(not (nil? (System/getenv "WEZTERM_PANE"))))
|
||||||
|
|
||||||
|
(defn command-start [m]
|
||||||
|
(let [file (get-in m [:opts :file] "Procfile.dev")]
|
||||||
|
(when (not (fs/exists? file))
|
||||||
|
(println "No Procfile.dev found in the current directory")
|
||||||
|
(System/exit 0))
|
||||||
|
|
||||||
|
(cond
|
||||||
|
(is-in-tmux?) (tmux-start file)
|
||||||
|
(is-in-wezterm?) (wezterm-start file)
|
||||||
|
:else (println "Unable to spawn processes in the current environment"))
|
||||||
|
|
||||||
|
(System/exit 0)))
|
||||||
|
|
||||||
(defn command-stop [_]
|
(defn command-stop [_]
|
||||||
(when (has-session? (current-project-name))
|
(when (has-tmux-session? (current-project-name))
|
||||||
(shell (format "tmux kill-session -t '%s'" (current-project-name)))))
|
(shell (format "tmux kill-session -t '%s'" (current-project-name)))))
|
||||||
|
|
||||||
(defn command-restart [args]
|
(defn command-restart [args]
|
||||||
|
|
@ -71,7 +111,7 @@
|
||||||
(println ""))
|
(println ""))
|
||||||
|
|
||||||
(def command-table
|
(def command-table
|
||||||
[{:cmds ["start"] :fn command-start}
|
[{:cmds ["start"] :fn command-start :opts {:file "Procfile.dev"}}
|
||||||
{:cmds ["stop"] :fn command-stop}
|
{:cmds ["stop"] :fn command-stop}
|
||||||
{:cmds ["restart"] :fn command-restart}
|
{:cmds ["restart"] :fn command-restart}
|
||||||
{:cmds [] :fn help}])
|
{:cmds [] :fn help}])
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue