Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rmux-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ serde_json = { version = "1", optional = true }
sha1 = { version = "0.10", optional = true }
sha2 = { version = "0.10", optional = true }
subtle = { version = "2.6", optional = true }
tempfile = "3"
tokio = { version = "1.48.0", features = ["io-util", "macros", "net", "process", "rt", "sync", "time"] }
toml = { version = "0.8", optional = true }
tracing = "0.1"
Expand Down
73 changes: 73 additions & 0 deletions crates/rmux-server/src/handler_scripting/source_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::sync::atomic::Ordering;

Expand Down Expand Up @@ -101,6 +103,60 @@ impl RequestHandler {
self.config_loading_depth.fetch_sub(1, Ordering::Relaxed);
}

fn extract_plugin_paths(command: &ParsedSourceFileCommand) -> Vec<&str> {
command
.paths
.iter()
.filter(|p| p.ends_with(".tmux"))
.map(String::as_str)
.collect()
}

async fn run_plugin_scripts(&self, plugin_paths: Vec<&str>) -> Result<(), RmuxError> {
let socket_path = self.server_socket_path.lock().unwrap().clone();
let session_name = {
let state = self.state.lock().await;
let name = state.sessions.iter().next().map(|(name, _)| name.clone());
name
};
let Some(session_name) = session_name else {
return Err(RmuxError::Server("no active session".to_owned()));
};
let rmux_binary = std::env::current_exe().unwrap();
let tmp = tempfile::tempdir().unwrap();
let shim_path = tmp.path().join("tmux");
std::fs::write(
&shim_path,
format!(
"#!/bin/sh\nexec {} -S \"$RMUX_SOCKET\" \"$@\"",
rmux_binary.display()
),
)
.unwrap();
#[cfg(unix)]
std::fs::set_permissions(&shim_path, std::fs::Permissions::from_mode(0o755)).unwrap();
let path_env = format!(
"{}:{}",
tmp.path().display(),
std::env::var("PATH").unwrap_or_default()
);
for path in plugin_paths {
let status = tokio::process::Command::new("bash")
.arg(path)
.env("PATH", &path_env)
.env("RMUX_SOCKET", socket_path.to_string_lossy().as_ref())
.env("RMUX_SESSION", session_name.as_str())
.status()
.await;
if let Err(e) = status {
return Err(RmuxError::Server(format!(
"failed to run plugin {path}: {e}"
)));
}
}
Ok(())
}

pub(in crate::handler) async fn handle_source_file(
&self,
requester_pid: u32,
Expand All @@ -110,6 +166,15 @@ impl RequestHandler {
if command.target.is_none() {
command.target = self.implicit_source_file_target(requester_pid).await;
}

let plugin_paths = Self::extract_plugin_paths(&command);
if !plugin_paths.is_empty() {
return match self.run_plugin_scripts(plugin_paths).await {
Ok(()) => Response::SourceFile(SourceFileResponse::no_output()),
Err(error) => Response::Error(ErrorResponse { error }),
};
}

let mut loaded = match self.load_source_file_command(&command, 1).await {
Ok(loaded) => loaded,
Err(error) => return Response::Error(ErrorResponse { error }),
Expand Down Expand Up @@ -167,6 +232,14 @@ impl RequestHandler {
mut command: ParsedSourceFileCommand,
context: &QueueExecutionContext,
) -> Result<QueueCommandAction, RmuxError> {
let plugin_paths = Self::extract_plugin_paths(&command);
if !plugin_paths.is_empty() {
self.run_plugin_scripts(plugin_paths).await?;
return Ok(QueueCommandAction::Normal {
output: None,
error: None,
});
}
let depth = context.source_file_depth.saturating_add(1);
command.current_file = context.current_file.clone();
let mut loaded = self.load_source_file_command(&command, depth).await?;
Expand Down
Loading