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

纵饮孤独 提交于 2020-01-23 11:20:06

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/10579364/how-do-you-run-the-rails-command-from-a-cocoa-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!