你如何从cocoa应用程序运行rails命令?
我已经坚持了一段时间。 我正在尝试从我的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
我遇到了类似的问题,因为我使用/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()