Equivalent for unix "read" command in rake tasks?

R

Rob Lucas

Hi,

Perhaps I'm missing something, but I can't find information anywhere
about adding any level of interaction with the user into rake tasks.
What I want really is something equivalent to the unix/ linux command
"read" so that the task can vary conditionally depending on responses
from the user. The point would be to replace various shell scripts that
I have with a single, more elegant Rakefile.

Simply inserting the shell command...

sh "read whatever_variable_name"

... into the Rakefile doesn't work- I get the following error:

Command failed with status (127): [read whatever_variable_name...]

I guess this is because Rake doesn't respond to the results of shell
commands, but only executes them "blind".

Any suggestions would be much appreciated.

Thanks,

Rob.
 
J

Jano Svitok

Hi,

Perhaps I'm missing something, but I can't find information anywhere
about adding any level of interaction with the user into rake tasks.
What I want really is something equivalent to the unix/ linux command
"read" so that the task can vary conditionally depending on responses
from the user. The point would be to replace various shell scripts that
I have with a single, more elegant Rakefile.

File.read ? Kernel#gets ? Readline ?

Rakefile is a normal ruby file, so you can do anything you can do in ruby.

You can event call IRB to get more interactivity, although I'm not sure
if that's the intent of rake ;-)

Other than that, you can parametrize rake tasks through environment variables:

either by really setting the env, or passing on command line:

rake task_name var1=abcd var2=efgh

then in your rakefile you can reference them as ENV['var1'] and ENV['var2']

J.
 
R

Rob Lucas

Great. Thanks for the help- I now have something working. Part of what
confused me here was that I could write a very simple script like:

puts "Input something: "
first_input = gets.chomp
puts "Input something else: "
second_input = gets.chomp
puts "You entered: " + first_input + " and " + second_input

... and this would automatically default to looking for input from the
user at the command line. However, doing the same thing in a Rakefile,
it was by default looking for input from an object with the name of the
rake task instead, and I would thus get an error message like:

No such file or directory - rake_task_name

I thus had to overtly specify to do a "gets" on the standard input:

$stdin.gets

... and this worked fine.

Perhaps this is obvious to more experienced ruby programmers, but not to
me!

Thanks again,

Rob.


Jano said:
Hi,

Perhaps I'm missing something, but I can't find information anywhere
about adding any level of interaction with the user into rake tasks.
What I want really is something equivalent to the unix/ linux command
"read" so that the task can vary conditionally depending on responses
from the user. The point would be to replace various shell scripts that
I have with a single, more elegant Rakefile.

File.read ? Kernel#gets ? Readline ?

Rakefile is a normal ruby file, so you can do anything you can do in
ruby.

You can event call IRB to get more interactivity, although I'm not sure
if that's the intent of rake ;-)

Other than that, you can parametrize rake tasks through environment
variables:

either by really setting the env, or passing on command line:

rake task_name var1=abcd var2=efgh

then in your rakefile you can reference them as ENV['var1'] and
ENV['var2']

J.
 
T

Thufir

Perhaps I'm missing something, but I can't find information anywhere
about adding any level of interaction with the user into rake tasks.
What I want really is something equivalent to the unix/ linux command
"read" so that the task can vary conditionally depending on responses
from the user. The point would be to replace various shell scripts that
I have with a single, more elegant Rakefile.


I'm new to rails and have only touched rake a bit. Would expand on what
you're currently doing, please?



thanks,

Thufir
 
R

Rob Lucas

Hi Thufir,

I'm new to Rake as well, so I don't know if I can tell you anything much
more than what is in the previous posts. The project I was concerned
with here was to use Rake to do deployments of my Rails sites- moving
and backing up the previously released version, unzipping the new one,
setting up the mongrel server stuff etc. I needed my Rake task to stop
and ask the user for input at various points, and that was what this
question was about.

This is an example of the solution I ended up with for asking the user
for input :

puts "\nDeploying #{new_release_name}\n\nPlease enter the name of the
last release:\n"
last_release_name = $stdin.gets.chomp

This waits for the user to enter a name for the last release at the
command line, then stores that string in the variable
"last_release_name", and carries on with the task.

For this to work, I needed to specify in the Rake task that "gets"
should look to standard input - $stdin - for input from the user, as by
default in Rake, "gets" apparently looks for input to an object with the
name of the task itself.

I originally framed the question in terms of the unix command "read",
which does something similar to "gets" here, because I was familiar with
this from writing shell scripts.

I hope this is helpful.

Rob.
 
J

Jano Svitok

Hi Thufir,

I'm new to Rake as well, so I don't know if I can tell you anything much
more than what is in the previous posts. The project I was concerned
with here was to use Rake to do deployments of my Rails sites- moving
and backing up the previously released version, unzipping the new one,
setting up the mongrel server stuff etc. I needed my Rake task to stop
and ask the user for input at various points, and that was what this
question was about.

This is an example of the solution I ended up with for asking the user
for input :

puts "\nDeploying #{new_release_name}\n\nPlease enter the name of the
last release:\n"
last_release_name = $stdin.gets.chomp

This waits for the user to enter a name for the last release at the
command line, then stores that string in the variable
"last_release_name", and carries on with the task.

For this to work, I needed to specify in the Rake task that "gets"
should look to standard input - $stdin - for input from the user, as by
default in Rake, "gets" apparently looks for input to an object with the
name of the task itself.

I originally framed the question in terms of the unix command "read",
which does something similar to "gets" here, because I was familiar with
this from writing shell scripts.

I hope this is helpful.

Rob.

You may find subversion (instead of zipping the old sources) and
capistrano (for the overall deployment) useful
- see for example
http://topfunky.com/clients/peepcode/free-episodes/peepcode-free-deprec.mov

Other than that, you can
- store the release name in some file for later use (you wouldn't have
to ask the user, just read from the file)
- provide the name on the command line
rake something LAST_RELEASE=asdfghjkl

J.
 
R

Rob Lucas

Yes. These are useful ideas. In particular, it looks like Capistrano
could be useful here.

Thanks again,

Rob.
 
T

Thufir

You may find subversion (instead of zipping the old sources) and
capistrano (for the overall deployment) useful


I don't understand how subversion works in conjunction with rails.
What's being "uploaded"? One file at a time? the whole directory?



thanks,

-Thufir
 
J

Jano Svitok

I don't understand how subversion works in conjunction with rails.
What's being "uploaded"? One file at a time? the whole directory?

From what I understand, you don't upload, you do a svn export/checkout
to a directory on the server.
It obviously requires shell access to the webhost, although there are
surely ways how one can manage
without one. The export is usually followed bu running the tests, and
in case of success, replacing
the actual version with the new one, and restarting mongrels.

This is more or less my idea about what capistrano might do. For an
exact description see their page.

Jano
 
T

Thufir

This is more or less my idea about what capistrano might do. For an
exact description see their page.


I'm reading about svn, subversion, but am using google code and it seems
to work fine. The sqlite db, and a few inconsequentials, are also being
uploaded, but that's configurable (I imagine).



-Thufir
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top