Equivalent for unix "read" command in rake tasks?

Discussion in 'Ruby' started by Rob Lucas, Nov 28, 2007.

  1. Rob Lucas

    Rob Lucas Guest

    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-forum.com/.
     
    Rob Lucas, Nov 28, 2007
    #1
    1. Advertising

  2. Rob Lucas

    Jano Svitok Guest

    On Nov 28, 2007 11:44 AM, Rob Lucas <> 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.
     
    Jano Svitok, Nov 28, 2007
    #2
    1. Advertising

  3. Rob Lucas

    Rob Lucas Guest

    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 <> 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-forum.com/.
     
    Rob Lucas, Nov 28, 2007
    #3
  4. Rob Lucas

    Thufir Guest

    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
     
    Thufir, Nov 29, 2007
    #4
  5. Rob Lucas

    Rob Lucas Guest

    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-forum.com/.
     
    Rob Lucas, Nov 29, 2007
    #5
  6. Rob Lucas

    Jano Svitok Guest

    On Nov 29, 2007 12:08 PM, Rob Lucas <> 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-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.
     
    Jano Svitok, Nov 29, 2007
    #6
  7. Rob Lucas

    Rob Lucas Guest

    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 <> 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-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.


    --
    Posted via http://www.ruby-forum.com/.
     
    Rob Lucas, Nov 29, 2007
    #7
  8. Rob Lucas

    Thufir Guest

    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
     
    Thufir, Dec 2, 2007
    #8
  9. Rob Lucas

    Jano Svitok Guest

    On Dec 2, 2007 11:21 AM, Thufir <> 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
     
    Jano Svitok, Dec 2, 2007
    #9
  10. Rob Lucas

    Thufir Guest

    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
     
    Thufir, Dec 9, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Francis Hwang
    Replies:
    0
    Views:
    246
    Francis Hwang
    May 26, 2006
  2. Esad Hajdarevic

    global rake tasks

    Esad Hajdarevic, Oct 9, 2006, in forum: Ruby
    Replies:
    1
    Views:
    117
    Kalman Noel
    Oct 9, 2006
  3. Esad Hajdarevic

    rakelibdir (was: Global rake tasks)

    Esad Hajdarevic, Oct 16, 2006, in forum: Ruby
    Replies:
    0
    Views:
    99
    Esad Hajdarevic
    Oct 16, 2006
  4. peppermonkey
    Replies:
    1
    Views:
    250
    Gregory Brown
    Feb 10, 2007
  5. John Feminella
    Replies:
    0
    Views:
    241
    John Feminella
    Apr 25, 2010
Loading...

Share This Page