From 9fabd36b1c4568b394928af57991df5c7720b99e Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Fri, 12 May 2017 15:43:13 -0400 Subject: [PATCH] Fix start-server to check ~/Applications and use bundled ruby (#17) (#18) --- src/installation.rs | 25 +++++++++++++++++++++++++ src/lib.rs | 31 +++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 src/installation.rs diff --git a/src/installation.rs b/src/installation.rs new file mode 100644 index 0000000..a6d00b7 --- /dev/null +++ b/src/installation.rs @@ -0,0 +1,25 @@ +use std::path::Path; + +struct Installation { + base: Path +} + +impl Installation { + fn new(base: &str) -> Installation { + Installation { + base: Path::new(&String::from(base)) + } + } + + fn ruby_path(&self) -> &Path { + self.base.join("app/server/native/osx/ruby/bin/ruby").as_path() + } + + fn server_path(&self) -> &Path { + self.base.join("app/server/bin/sonic-pi-server.rb").as_path() + } + + fn exists(&self) -> bool { + self.ruby_path().exists() && self.server_path().exists() + } +} diff --git a/src/lib.rs b/src/lib.rs index 732c9f7..390e242 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,16 +2,18 @@ extern crate rosc; extern crate nix; extern crate ansi_term; -use std::process; +use std::{env, process}; use std::path::Path; use std::ffi::CString; +use std::io::{self, Read}; use nix::unistd::execv; mod server; mod file; mod log_packet; +mod installation; -use std::io::{self, Read}; +use installation::Installation; /// Read code from STDIN and send to Sonic Pi Server. /// @@ -92,13 +94,26 @@ pub fn logs() { /// Find the Sonic Pi server executable and run it. If it can be found. /// pub fn start_server() { - let paths = ["/Applications/Sonic Pi.app/server/bin/sonic-pi-server.rb", - "./app/server/bin/sonic-pi-server.rb", - "/usr/lib/sonic-pi/server/bin/sonic-pi-server.rb"]; + let paths = vec![Installation::new("/Applications/Sonic Pi.app"), + Installation::new("."), + Installation::new("/usr/lib/sonic-pi")]; + + // Look in ~/Applications + match env::home_dir() { + Some(path) => { + let home = format!("{}/Applications/Sonic Pi.app", path.to_str().unwrap()); + + paths.insert(0, Installation::new(home)); + } + None => {} + }; + + match paths.iter().find(|&&i| i.exists()) { + Some(i) => { + let ruby = i.ruby_path(); + let script = i.server_path(); - match paths.iter().find(|&&p| Path::new(p).exists()) { - Some(p) => { - execv(&CString::new(*p).unwrap(), &[]).expect(&format!("Unable to start {}", *p)) + execv(&CString::new(*ruby).unwrap(), &[script]).expect(&format!("Unable to start {}", *script)); } None => { println!("I couldn't find the Sonic Pi server executable :(");