Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix start-server to check ~/Applications and use bundled ruby #20

Closed
wants to merge 1 commit into from
Closed
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
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