2023-11-04 09:05:08 +00:00
|
|
|
#!/usr/bin/env bb
|
|
|
|
|
;; vi: ft=clojure
|
|
|
|
|
;;
|
|
|
|
|
;; A Procfile runner for tmux. Each procfile will have its own tmux session and
|
|
|
|
|
;; each process will have its own window. This way you can use tmux to attach
|
|
|
|
|
;; view logs and also restart individual processes.
|
|
|
|
|
;;
|
|
|
|
|
(require '[babashka.process :refer [shell]]
|
|
|
|
|
'[babashka.cli :as cli]
|
|
|
|
|
'[babashka.fs :as fs]
|
|
|
|
|
'[clojure.string :as string])
|
|
|
|
|
|
|
|
|
|
(defn current-project-name
|
|
|
|
|
"Returns the current project name based on the current working directory.
|
|
|
|
|
This assumes you have our porjects organised in a GOPATH style. This will
|
|
|
|
|
return the last two components of the current working directory, that should
|
|
|
|
|
be {org}/{project}"
|
|
|
|
|
[]
|
|
|
|
|
(string/join "/" (map #(.toString %) (take 2 (reverse(fs/components (fs/cwd)))))))
|
|
|
|
|
|
2024-09-04 18:00:55 +00:00
|
|
|
(defn parse-procfile
|
2023-11-04 09:05:08 +00:00
|
|
|
"Parses a procfile file and returns a list of [name command] pairs."
|
2024-09-04 18:00:55 +00:00
|
|
|
[file]
|
|
|
|
|
(map #(string/split % #":\s+") (string/split (slurp file) #"\n" )))
|
2023-11-04 09:05:08 +00:00
|
|
|
|
2024-09-04 18:00:55 +00:00
|
|
|
(defn has-tmux-session?
|
2023-11-04 09:05:08 +00:00
|
|
|
"Tests to see if a session already exists with a given name."
|
|
|
|
|
[session-name]
|
|
|
|
|
(string/includes? (:out (shell {:out :string} "tmux list-sessions")) session-name))
|
|
|
|
|
|
2024-09-04 18:00:55 +00:00
|
|
|
(defn has-tmux-window?
|
2023-11-04 09:05:08 +00:00
|
|
|
"Tests to see if a window already exists within a tmux session."
|
|
|
|
|
[session-name window-name]
|
|
|
|
|
(string/includes? (:out (shell {:out :string} (format "tmux list-windows -t '%s'" session-name))) window-name))
|
|
|
|
|
|
2024-09-04 18:00:55 +00:00
|
|
|
(defn create-tmux-window
|
2023-11-04 09:05:08 +00:00
|
|
|
"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."
|
|
|
|
|
[project name & commands]
|
2024-09-04 18:00:55 +00:00
|
|
|
(when-not (has-tmux-window? project name)
|
2023-11-04 09:05:08 +00:00
|
|
|
(shell (format "tmux new-window -t '%s' -n '%s'" project name))
|
|
|
|
|
(doseq [command commands]
|
|
|
|
|
(shell (format "tmux send-keys -t %s:'%s' '%s' C-m" project name command)))))
|
|
|
|
|
|
2024-09-04 18:00:55 +00:00
|
|
|
(defn tmux-start [file]
|
|
|
|
|
(when (not (has-tmux-session? (current-project-name)))
|
2023-11-04 09:05:08 +00:00
|
|
|
(shell (format "tmux new-session -d -c %s -s '%s'" (fs/cwd) (current-project-name))))
|
|
|
|
|
|
2024-09-04 18:00:55 +00:00
|
|
|
(doseq [[name command] (parse-procfile file)]
|
|
|
|
|
(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)))
|
2023-11-04 09:05:08 +00:00
|
|
|
|
|
|
|
|
(defn command-stop [_]
|
2024-09-04 18:00:55 +00:00
|
|
|
(when (has-tmux-session? (current-project-name))
|
2023-11-04 09:05:08 +00:00
|
|
|
(shell (format "tmux kill-session -t '%s'" (current-project-name)))))
|
|
|
|
|
|
|
|
|
|
(defn command-restart [args]
|
|
|
|
|
(command-stop args)
|
|
|
|
|
(command-start args))
|
|
|
|
|
|
|
|
|
|
(defn help [_m]
|
|
|
|
|
(println "A Procfile runner for tmux")
|
|
|
|
|
(println "")
|
|
|
|
|
(println "\033[1mCommands:\033[0m")
|
|
|
|
|
(println " start Start all of the processes in your Procfile.dev")
|
|
|
|
|
(println " stop Stop all the currently running processes")
|
|
|
|
|
(println " restart Restart all of the processes")
|
|
|
|
|
(println ""))
|
|
|
|
|
|
|
|
|
|
(def command-table
|
2024-09-04 18:00:55 +00:00
|
|
|
[{:cmds ["start"] :fn command-start :opts {:file "Procfile.dev"}}
|
2023-11-04 09:05:08 +00:00
|
|
|
{:cmds ["stop"] :fn command-stop}
|
|
|
|
|
{:cmds ["restart"] :fn command-restart}
|
|
|
|
|
{:cmds [] :fn help}])
|
|
|
|
|
|
|
|
|
|
(cli/dispatch command-table *command-line-args*)
|
|
|
|
|
|