2018-12-31 13:26:17 +02:00
|
|
|
use crate::executor::RunType;
|
2018-12-15 21:52:21 +02:00
|
|
|
use crate::terminal::print_separator;
|
2020-08-22 14:46:17 +03:00
|
|
|
use crate::{
|
|
|
|
|
execution_context::ExecutionContext,
|
|
|
|
|
utils::{which, Check, PathExt},
|
|
|
|
|
};
|
2019-12-11 23:05:38 +02:00
|
|
|
use anyhow::Result;
|
2018-09-04 11:05:54 +03:00
|
|
|
use directories::BaseDirs;
|
|
|
|
|
use std::env;
|
2018-09-04 11:21:32 +03:00
|
|
|
use std::io;
|
2018-09-04 11:05:54 +03:00
|
|
|
use std::os::unix::process::CommandExt;
|
2020-08-22 14:46:17 +03:00
|
|
|
use std::path::PathBuf;
|
2019-06-13 09:21:39 +03:00
|
|
|
use std::process::{exit, Command};
|
2018-09-04 11:05:54 +03:00
|
|
|
|
2019-12-11 23:05:38 +02:00
|
|
|
pub fn run_tpm(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
|
2019-03-10 21:48:49 +02:00
|
|
|
let tpm = base_dirs
|
2018-09-04 11:05:54 +03:00
|
|
|
.home_dir()
|
|
|
|
|
.join(".tmux/plugins/tpm/bin/update_plugins")
|
2019-03-10 21:48:49 +02:00
|
|
|
.require()?;
|
2018-09-04 11:05:54 +03:00
|
|
|
|
2019-03-10 21:48:49 +02:00
|
|
|
print_separator("tmux plugins");
|
2018-09-04 11:05:54 +03:00
|
|
|
|
2022-11-03 04:26:20 +08:00
|
|
|
run_type.execute(tpm).arg("all").check_run()
|
2018-09-04 11:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
2019-11-06 07:27:01 +02:00
|
|
|
struct Tmux {
|
|
|
|
|
tmux: PathBuf,
|
|
|
|
|
args: Option<Vec<String>>,
|
2019-06-13 09:21:39 +03:00
|
|
|
}
|
|
|
|
|
|
2019-11-06 07:27:01 +02:00
|
|
|
impl Tmux {
|
2022-11-03 12:46:43 -04:00
|
|
|
fn new(args: Vec<String>) -> Self {
|
2019-11-06 07:27:01 +02:00
|
|
|
Self {
|
|
|
|
|
tmux: which("tmux").expect("Could not find tmux"),
|
2022-11-03 12:46:43 -04:00
|
|
|
args: if args.is_empty() { None } else { Some(args) },
|
2019-11-06 07:27:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
2018-09-04 11:21:32 +03:00
|
|
|
|
2019-11-06 07:27:01 +02:00
|
|
|
fn build(&self) -> Command {
|
|
|
|
|
let mut command = Command::new(&self.tmux);
|
|
|
|
|
if let Some(args) = self.args.as_ref() {
|
|
|
|
|
command.args(args).env_remove("TMUX");
|
|
|
|
|
}
|
|
|
|
|
command
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn has_session(&self, session_name: &str) -> Result<bool, io::Error> {
|
|
|
|
|
Ok(self
|
|
|
|
|
.build()
|
2022-10-23 11:34:30 +00:00
|
|
|
.args(["has-session", "-t", session_name])
|
2019-11-06 07:27:01 +02:00
|
|
|
.output()?
|
|
|
|
|
.status
|
|
|
|
|
.success())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn new_session(&self, session_name: &str) -> Result<bool, io::Error> {
|
|
|
|
|
Ok(self
|
|
|
|
|
.build()
|
2022-10-23 11:34:30 +00:00
|
|
|
.args(["new-session", "-d", "-s", session_name, "-n", "dummy"])
|
2019-11-06 07:27:01 +02:00
|
|
|
.spawn()?
|
|
|
|
|
.wait()?
|
|
|
|
|
.success())
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 23:05:38 +02:00
|
|
|
fn run_in_session(&self, command: &str) -> Result<()> {
|
2019-11-06 07:27:01 +02:00
|
|
|
self.build()
|
2022-10-23 11:34:30 +00:00
|
|
|
.args(["new-window", "-t", "topgrade", command])
|
2019-12-11 23:05:38 +02:00
|
|
|
.spawn()?
|
|
|
|
|
.wait()?
|
2019-11-06 07:27:01 +02:00
|
|
|
.check()?;
|
2018-09-04 11:21:32 +03:00
|
|
|
|
2019-11-06 07:27:01 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2018-09-04 11:21:32 +03:00
|
|
|
}
|
|
|
|
|
|
2022-11-03 12:46:43 -04:00
|
|
|
pub fn run_in_tmux(args: Vec<String>) -> ! {
|
2019-06-13 09:21:39 +03:00
|
|
|
let command = {
|
|
|
|
|
let mut command = vec![
|
|
|
|
|
String::from("env"),
|
|
|
|
|
String::from("TOPGRADE_KEEP_END=1"),
|
|
|
|
|
String::from("TOPGRADE_INSIDE_TMUX=1"),
|
|
|
|
|
];
|
|
|
|
|
command.extend(env::args());
|
2022-11-03 12:46:43 -04:00
|
|
|
shell_words::join(command)
|
2019-06-13 09:21:39 +03:00
|
|
|
};
|
2018-09-04 11:05:54 +03:00
|
|
|
|
2022-11-03 18:29:22 +00:00
|
|
|
let tmux = Tmux::new(args);
|
2019-11-06 07:27:01 +02:00
|
|
|
|
2019-12-08 20:59:42 +02:00
|
|
|
if !tmux.has_session("topgrade").expect("Error detecting a tmux session") {
|
|
|
|
|
tmux.new_session("topgrade").expect("Error creating a tmux session");
|
2019-06-13 09:21:39 +03:00
|
|
|
}
|
2018-09-04 11:21:32 +03:00
|
|
|
|
2022-11-01 23:21:30 +01:00
|
|
|
tmux.run_in_session(&command).expect("Error running Topgrade in tmux");
|
2019-11-06 07:27:01 +02:00
|
|
|
tmux.build()
|
2022-10-23 11:34:30 +00:00
|
|
|
.args(["kill-window", "-t", "topgrade:dummy"])
|
2019-06-13 09:21:39 +03:00
|
|
|
.output()
|
2019-12-08 20:59:42 +02:00
|
|
|
.expect("Error killing the dummy tmux window");
|
2018-09-04 11:21:32 +03:00
|
|
|
|
2019-06-13 09:21:39 +03:00
|
|
|
if env::var("TMUX").is_err() {
|
2022-10-23 11:34:30 +00:00
|
|
|
let err = tmux.build().args(["attach", "-t", "topgrade"]).exec();
|
2018-09-04 11:21:32 +03:00
|
|
|
panic!("{:?}", err);
|
|
|
|
|
} else {
|
2019-06-13 09:21:39 +03:00
|
|
|
println!("Topgrade launched in a new tmux session");
|
|
|
|
|
exit(0);
|
2018-09-04 11:21:32 +03:00
|
|
|
}
|
2018-09-04 11:05:54 +03:00
|
|
|
}
|
2019-06-13 16:43:23 +03:00
|
|
|
|
2020-08-22 14:46:17 +03:00
|
|
|
pub fn run_command(ctx: &ExecutionContext, command: &str) -> Result<()> {
|
2022-11-03 12:46:43 -04:00
|
|
|
Tmux::new(ctx.config().tmux_arguments()?)
|
2019-11-06 07:27:01 +02:00
|
|
|
.build()
|
2022-10-23 11:34:30 +00:00
|
|
|
.args(["new-window", "-a", "-t", "topgrade:1", command])
|
2019-06-13 16:43:23 +03:00
|
|
|
.env_remove("TMUX")
|
2019-12-11 23:05:38 +02:00
|
|
|
.spawn()?
|
|
|
|
|
.wait()?
|
2019-06-13 16:43:23 +03:00
|
|
|
.check()
|
|
|
|
|
}
|