$ cat Rakefile
task 'open' do
puts "opening the users.html in Chrome"
end
$ rake open
opening the users.html in Chrome
Notice that Rake will print the shell command it is running.
task 'open' do
sh "open -a 'Google Chrome.app' users.html"
end
Delcare a task named :default
.
It has no body, so it will do nothing.
This hash syntax means that it depends on the task open
,
which will cause it to run that task.
task default: 'open'
# ...
Run the default task:
$ rake
open -a 'Google Chrome.app' users.html
# ...
desc 'Open the users html page'
task 'open' do
sh 'say "opening the users.html in Chrome"'
end
All tasks that have descriptions:
$ rake -T
rake open # Open the users html page
All tasks:
$ rake -AT
rake default #
rake open # Open the users html page
Our open task will fail if users.html
hasn't been built.
desc 'Open the users html page'
task 'open' do
sh "open -a 'Google Chrome.app' users.html"
end
So we can a hash again to say that open
depends on users.html
This is the same thing we did with default, except the key is a string (Rake accepts strings and symbols), and we have a block to execute for this task.
desc 'Open the users html page'
task 'open' => 'users.html' do
sh "open -a 'Google Chrome.app' users.html"
end
Here, we use the file
method instead of the task
method.
This will cause Rake to consider it satisfied (not need to be run),
if the file exists. If the file doesn't exist, it will run our block
and generate it. We can give this a description, like any other task,
even though we don't.
file 'users.html' do
sh 'ruby build_html.rb'
end
users.html
was built from the data in users.json
.
So if that data ever updates, we want to rebuild it.
We can say that users.html
depends on users.json
like this:
file 'users.html' => 'users.json' do
sh 'ruby build_html.rb'
end
Because users.html
was declared with file
,
and it depends on users.json
, which is a file,
Rake will decide that it needs to run the block to build users.html
if either:
users.html
doesn't existusers.json
was modified more recently thanusers.html
Otherwise it will consider the task satisfied and do nothing:
# Doesn't build users.html, because it's up to date
$ rake
open -a 'Google Chrome.app' users.html
# Now edit users.json, add another user
# Now, users.json is more recent than users.html
# So it rebuilds users.html before opening it
$ rake
ruby build_html.rb
open -a 'Google Chrome.app' users.html
# Now that it's rebuilt, it's up to date
# So it just opens the file without building it again
$ rake
open -a 'Google Chrome.app' users.html