Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make an interactive Rake task?

Tags:

rake

I want to run a Rake task that asks the user for input.

I know that I can supply input on the command line, but I want to ask the user if they are sure they want to proceed with a particular action in case they mistyped one of the values supplied to the Rake task.

like image 711
AKWF Avatar asked Mar 25 '11 02:03

AKWF


People also ask

How do I run a rake task?

Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.

Where to write Rake task in rails?

We can also write our custom Rake tasks in Rails environment by creating files with . rake extension in ./lib/tasks. It's a common practice after cloning a repository for the first time, to run ./bin/setup, in order to automatically fetch all the libraries, create db, seed data etc.

Where do I put Rake tasks?

rake extension and are placed in Rails. root/lib/tasks . You can create these custom rake tasks with the bin/rails generate task command. If your need to interact with your application models, perform database queries and so on, your task should depend on the environment task, which will load your application code.


1 Answers

Something like this might work

task :action do   STDOUT.puts "I'm acting!" end  task :check do   STDOUT.puts "Are you sure? (y/n)"   input = STDIN.gets.strip   if input == 'y'     Rake::Task["action"].reenable     Rake::Task["action"].invoke   else     STDOUT.puts "So sorry for the confusion"   end end 

Task reenabling and invoking from How to run Rake tasks from within Rake tasks?

like image 61
ricaurte Avatar answered Sep 19 '22 19:09

ricaurte