问题
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