你如何从cocoa应用程序运行rails命令?

时间:2012-05-14 07:50:49

标签: objective-c ruby-on-rails cocoa rvm

我已经被困在这个上一段时间了。我试图从我的cocoa应用程序运行rails shell命令来创建一个新闻rails应用程序。我跑的时候

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

我能够创建一个新项目。但如果我运行这样的东西

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]);

或者

- (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];
}

我收到以下错误

/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 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,因为我使用/bin/sh作为launchPath。

试试这个快速代码。

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