Specifying alternate prerequisites in rake

1 view
Skip to first unread message

Oivvio Polite

unread,
Nov 23, 2009, 3:40:20 PM11/23/09
to capis...@googlegroups.com
Is there anyway of specifying alternate prerequisites for a task, ie at
least one of the prerequisites must be run before the task itself is
run.

The way I'm doing it now is by using a "proxy task" that picks one of
the prerequisite tasks based on a global settings variable.

It looks something like this:

===========================================================================
class Settings
def initialize
@supports_rsync = false
@supports_ssh = false
@supports_ftp = false
end

attr_accessor :supports_rsync, :supports_ssh, :supports_ftp

end

settings = Settings.new()

settings.supports_ftp = true


task :backup => [:backup_database, :backup_files] do
#do some stuff
end

task :backup_files do
#do some stuff
if settings.supports_rsync
Rake::Task[:backup_files_with_rsync].invoke
elsif settings.supports_ssh
Rake::Task[:backup_files_with_ssh].invoke
elsif settings.supports_ftp
Rake::Task[:backup_files_with_ftp].invoke
else
puts "raise hell"
end
end

task :backup_database do
puts "backing up the database"
#do some stuff
end

task :backup_files_with_ftp do
puts "backing up the the files with ftp"
#do some stuff
end

task :backup_files_with_ssh do
puts "backing up the the files with ssh"
#do some stuff
end

task :backup_files_with_rsync do
puts "backing up the the files with rsync"
#do some stuff
end

task :default => [:backup] do
end

===========================================================================

This might be OK for a rake file where only one or two tasks has
alternate prerequisites, but for ten or twenty tasks it gets a bit
verbose.

Is there a cleaner way of doing it?

oivvio

--
http://pipedreams.polite.se/about/


Lee Hambley

unread,
Nov 23, 2009, 6:29:34 PM11/23/09
to capis...@googlegroups.com
These are per-server settings?

-- Lee Hambley

Twitter: @leehambley | @capistranorb

Alex Polite

unread,
Nov 24, 2009, 4:24:28 PM11/24/09
to capis...@googlegroups.com
On Tue, Nov 24, 2009 at 12:29:34AM +0100, Lee Hambley wrote:
> These are per-server settings?
>

Yes. I'm using rake to deploy wordpress-installations to different
servers, do backups, transfer the database from a test deployment to
a production deployment and other tedious stuff. Depending on the
hosting-provider different protocols are available.

Ideally I'd like to be able to do something like:



task :backup => [:backup_database, :backup_files] do
#do some stuff
end

task :backup_database => [:backup_db_via_ssh|:backup_db_via_mysql|:backup_db_via_ftp]
#do some stuff
end

Running :backup_database, rake would first try to run
:backup_db_via_ssh, if that failed it would run the next alternate
prerequisite.

But I guess that's not possible?


--
http://pipedreams.polite.se/about/


Lee Hambley

unread,
Nov 24, 2009, 4:30:24 PM11/24/09
to capis...@googlegroups.com
You can add arbirary properties to the server definitions, I will make a note to mail you in the morning; I need to take a little time with my answer. (you'll be pleased to know I have two things to make your life easier, and fix your problem)
Reply all
Reply to author
Forward
0 new messages