3

I've been stuck on this one for a while. I'm trying to run a rails shell command from my cocoa application to create a news rails app. When I run

~/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/bin/rails new projectname

I'm able to create a new project. But if I run something like this

NSString *path = @"~/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/bin/rails";
NSString *script = [NSString stringWithFormat:@"%@ new ~/Desktop/testapp", path];
system([script UTF8String]);

or this

- (IBAction)buildProject:(NSButton *)sender 
{
NSString *path = @"~/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/bin/rails";
NSArray *args = [NSArray arrayWithObjects:@"new", @"~/Desktop/testapp", nil];

NSTask *task = [NSTask new];
[task setLaunchPath:path];
[task setArguments:args];
[task launch];
}

I get the following error

/Users/dylanross/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/bin/rails:7:in `require': no such file to load -- rails/cli (LoadError)
from /Users/dylanross/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/bin/rails:7
Justin Boo
  • 10,132
  • 8
  • 50
  • 71
Dylan Ross
  • 31
  • 2
  • RVM performs magic on your shell that is missing when you do your call. I am not 100% sure, but you might get it by executing `/usr/bin/env ~/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/bin/rails new projectname` instead. – Amadan May 14 '12 at 07:53
  • As @Amadan says you probably need to initialize your ruby environment somehow and by the fact that `system` (which probably uses a non-interactive shell) did not work i guess the environment is setup in your `~/.bashrc` file. You could try `sh -i -c ".../rails new bla"`. – Mattias Wadman May 14 '12 at 08:41
  • this answer run OK on 10.8.2 http://stackoverflow.com/questions/412562/execute-a-terminal-command-from-a-cocoa-app – amoblin Jan 24 '13 at 06:06

1 Answers1

0

I had a similar problem because I was using /bin/sh as launchPath.

Try this swift code.

let task = NSTask()
task.launchPath = "/bin/bash"
let fileToRun = "/Users/johndoe/Desktop/testapp/app.rb"
task.arguments = ["-l", "rvm-auto-ruby", fileToRun]
let pipe = NSPipe()
task.standardOutput = pipe
task.launch()
neoneye
  • 50,398
  • 25
  • 166
  • 151