Skip to content

Instantly share code, notes, and snippets.

@phdoerfler
Created July 30, 2019 13:48
Show Gist options
  • Save phdoerfler/1a3db3bfa80718797a6473878bd17398 to your computer and use it in GitHub Desktop.
Save phdoerfler/1a3db3bfa80718797a6473878bd17398 to your computer and use it in GitHub Desktop.
update-all
use std::io::{self, Write};
use std::process::{exit, Command, Stdio};
fn main() {
main_guarded().unwrap_or_else(|e| {
eprintln!("{}", e);
exit(1);
})
}
fn main_guarded() -> io::Result<()> {
let cmds = [
"echo Brew… && brew upgrade && brew cu --all --cleanup --no-brew-update && echo brew cleanup… && brew cleanup -v -d && exit",
"echo Node… && npm-update && exit",
"echo Gems… && gem update && exit",
"echo Rust… && rustup update && exit",
"echo Conda… && update-conda && exit",
"echo zplug… && zplug update && exit"
];
let code = js_for_horizontal_splits(&cmds);
osascript(&code);
Ok(())
}
fn osascript(code: &str) {
//println!("Running the following code with osascript:
//{}", code);
let mut child = Command::new("osascript")
.arg("-l")
.arg("JavaScript")
.arg("-i")
.stdin(Stdio::piped())
.stderr(Stdio::null())
.stdout(Stdio::null())
.spawn()
.expect("failed to execute process");
let child_stdin = child.stdin.as_mut().unwrap();
child_stdin.write_all(code.as_bytes()).expect("sending input to osascript failed");
child.wait().expect("osascript failed");
}
// This might eventually become a bit more generic but for now let's keep it simple
fn js_for_horizontal_splits(cmds: &[&str]) -> String {
let head_cmd = cmds[0];
let mut js = format!(r#"
var iTerm = Application('iTerm');
iTerm.includeStandardAdditions = true
iTerm.createWindowWithProfile("automation")
"#);
js.push_str(&format!(r#"
iTerm.currentWindow().currentSession().write({{ text: '{}', newline: true }})
"#, head_cmd));
// loop thing
for cmd in cmds.iter().skip(1) {
js.push_str(&format!(r#"
iTerm.currentWindow().currentSession().splitHorizontallyWithSameProfile().write({{ text: '{}', newline: true }})
"#, cmd));
}
js
}
fn spawn_in_tab(cmd: &str) {
Command::new("tab")
.arg(cmd)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.expect("Failed to launch tab")
.wait()
.expect("tab failed");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment