Skip to content

Commit

Permalink
Fix start-server to check ~/Applications and use bundled ruby (lpil#17)…
Browse files Browse the repository at this point in the history
… (lpil#18)
  • Loading branch information
yaychris committed May 12, 2017
1 parent 4907494 commit 9fabd36
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
25 changes: 25 additions & 0 deletions src/installation.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
31 changes: 23 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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 :(");
Expand Down

0 comments on commit 9fabd36

Please sign in to comment.