[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Equivalent for unix "read" command in rake tasks?

Rob Lucas

11/28/2007 10:45:00 AM

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.
--
Posted via http://www.ruby-....

9 Answers

Jano Svitok

11/28/2007 11:06:00 AM

0

On Nov 28, 2007 11:44 AM, Rob Lucas <roblucas@lucasandblench.com> wrote:
> 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.

Rob Lucas

11/28/2007 3:25:00 PM

0

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 Svitok wrote:
> On Nov 28, 2007 11:44 AM, Rob Lucas <roblucas@lucasandblench.com> wrote:
>> 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.

--
Posted via http://www.ruby-....

Thufir Hawat

11/29/2007 2:25:00 AM

0

On Wed, 28 Nov 2007 19:44:34 +0900, Rob Lucas wrote:

> 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


Rob Lucas

11/29/2007 11:09:00 AM

0

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.


Thufir wrote:
> On Wed, 28 Nov 2007 19:44:34 +0900, Rob Lucas wrote:
>
>> 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
--
Posted via http://www.ruby-....

Jano Svitok

11/29/2007 11:36:00 AM

0

On Nov 29, 2007 12:08 PM, Rob Lucas <roblucas@lucasandblench.com> wrote:
> 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-...

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.

Rob Lucas

11/29/2007 12:02:00 PM

0

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

Thanks again,

Rob.

Jano Svitok wrote:
> On Nov 29, 2007 12:08 PM, Rob Lucas <roblucas@lucasandblench.com> wrote:
>> This is an example of the solution I ended up with for asking the user
>> For this to work, I needed to specify in the Rake task that "gets"
>> 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-...
>
> 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.

--
Posted via http://www.ruby-....

Thufir Hawat

12/2/2007 10:22:00 AM

0

On Thu, 29 Nov 2007 20:36:11 +0900, Jano Svitok wrote:


> 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


Jano Svitok

12/2/2007 10:39:00 AM

0

On Dec 2, 2007 11:21 AM, Thufir <hawat.thufir@gmail.com> wrote:
> On Thu, 29 Nov 2007 20:36:11 +0900, Jano Svitok wrote:
>
>
> > 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?

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

Thufir Hawat

12/9/2007 7:10:00 AM

0

On Sun, 02 Dec 2007 19:38:58 +0900, Jano Svitok wrote:


> 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