简体   繁体   中英

How do you run the rails command from a cocoa application?

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM