Help with system call

Lemme start out by begging. I really need help with this one. Any suggestions at all. Now that that’s out of the way…

I’m trying to call script/generate from within Rails and having no luck. I’ve tried a couple of different ways.

system “script/generate site_contents X”

  which returns false when it fails but I can't get any information other than the false return.

script/generate site_contents X [in backticks]

  this just returns nil from within Rails even though from irb it returns the STDOUT from the command.

I’m thanking you in advance because you’re really gonna save me on this one. Thank you thank you thank you. Please thank you.

RSL

Did you try adding "RAILS_ROOT +" to the path to script/generate?

b

Russell Norris wrote:

I actually did. :confused: I tried various File.expand_path versions too. I even tried moving to the proper directory via Dir.chdir. :confused: Any other ideas? I’m just stumped and stymied.

RSL

Russell Norris wrote:

Lemme start out by begging. I really need help with this one. Any
suggestions at all. Now that that's out of the way...

I'm trying to call script/generate from within Rails and having no luck.
I've tried a couple of different ways.

# system "script/generate site_contents X"
      which returns false when it fails but I can't get any information
other than the false return.
# `script/generate site_contents X` [in backticks]
      this just returns nil from within Rails even though from irb it
returns the STDOUT from the command.

I'm thanking you in advance because you're really gonna save me on this one.
Thank you thank you thank you. Please thank you.

This is the long way to do it just to show you the steps but this should work, at least under Unix/Linux:

cwd = `pwd`
cwd.chomp! # pwd returns a LF at the end
logger.debug("cwd: #{cwd}")
generate = cwd + "/script/generate controller test > #{cwd}/gen_test.out"
logger.debug("generate: #{generate}")
system(generate)

Well, took a peek into the under the rails hood and here's what's in
script/generate:

#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/boot'
require 'commands/generate'

So, I found generate.rb in rails/lib/commands and here's what it contains:

require "#{RAILS_ROOT}/config/environment"
require 'rails_generator'
require 'rails_generator/scripts/generate'

ARGV.shift if ['--help', '-h'].include?(ARGV[0])
Rails::Generator::Scripts::Generate.new.run(ARGV)

Running this from within a rails app, you obviously already have the
environment. Do the other two requires and you should be in business.

.....

Yep... I just did the two requires in script/console, and then did:

Rails::Generator::Scripts::Generate.new.run(['model','foobar'])

And it generated the foobar.rb model, the test, the yaml, etc.

Neat.

b

Russell Norris wrote:

I tried what you suggested, running those two rails_generator requires and got a new error:

couldn’t find HOME environment – expanding `~’

which is, of course a common Windows error. Except that I’m running Unix. Lovely, right? Whoa! I just checked ENV.inspect and got an empty hash! I’m gonna have to look into this. Uh, this is not right, is it kittens?

RSL

Okay. I’ve done some snooping and discovered that when I run ENV from the console I get something approximating all my shell variables but when I run it from an apache instance, I get nothing. I can set it [via SetEnv HOME /whatever] in the apache files but it never shows up in the ENV variable! I understand that the shell ENV and the Apache ENV are different but I’m not sure how to correctly set this now.

Help? I mean… Thank you for your future help! Please?

RSL

Thankfully, I remembered the whole ENV[“RAILS_ENV”] ||= “production” hack and have used this to set ENV[“HOME”]. Thank you so much to Michael and especially Ben for all your help. I’ve got a new problem now [with permissions on something in the generator process] but this one is solved and I’ll probably be able to hack around and find what’s up with this one.

Again, thanks! You really saved me and my sanity.

RSL