Skip to content

Instantly share code, notes, and snippets.

@Oshuma
Created February 12, 2009 06:30
Show Gist options
  • Select an option

  • Save Oshuma/62515 to your computer and use it in GitHub Desktop.

Select an option

Save Oshuma/62515 to your computer and use it in GitHub Desktop.
CakePHP Rakefile...wait, what?
# A Rakefile I use to ease the pain of working with CakePHP.
# Put this in your app/ directory.
APP_DIR = File.dirname(__FILE__)
CAKE_DIR = File.join(File.dirname(__FILE__), '..', 'cake')
CONSOLE_DIR = File.join(CAKE_DIR, 'console')
task :default => 'notes:todo'
desc 'Open the CakePHP console'
task :console do
cake('console')
end
namespace :db do
desc 'Open a MySQL shell using the default connection'
task :console do
File.open("#{APP_DIR}/config/database.php") do |f|
# Read the config file.
config = f.read
# Grab the database connection info.
host = fetch_from(config, 'host')
login = fetch_from(config, 'login')
password = fetch_from(config, 'password')
database = fetch_from(config, 'database')
# Now open a database shell.
sh "mysql -h #{host} -u #{login} --password='#{password}' -D #{database}"
end
end
desc 'Load the fixtures'
task :fixtures do
cake('fixtures')
end
desc 'Migrate the database schema'
task :migrate do
cake('schema run create')
end
desc 'Run the migrations and load the fixtures'
task :setup do
Rake::Task['db:migrate'].invoke
Rake::Task['db:fixtures'].invoke
end
end
desc 'Open the CakePHP bake console'
task :bake do
cake('bake')
end
desc 'List all FIXME and TODO notes'
task :notes do
Rake::Task['notes:fixme'].invoke
puts '-' * 40
Rake::Task['notes:todo'].invoke
end
namespace :notes do
desc 'List FIXME notes'
task :fixme do
grep_for('FIXME')
end
desc 'List TODO notes (default task)'
task :todo do
grep_for('TODO')
end
end
desc 'Run unit tests'
task :test do
cake('testsuite app all')
end
private
# Run a CakePHP console command.
def cake(command)
sh "#{CONSOLE_DIR}/cake #{command}"
end
# Grep for +tag+ in the current dir, excluding this Rakefile.
def grep_for(tag)
find_cmd = "find #{APP_DIR} \\! -name Rakefile -type f"
find_cmd << " -exec grep -Hn --color #{tag} \{\} \\;"
sh find_cmd
end
# Return the given key's value from the (PHP) config file.
# This will read through the config string looking for
# 'key' => 'value' pairs in the PHP array. Then, it
# will strip newlines, spaces, and substitute any quotes.
#
# Yes, completely hackish.
def fetch_from(config, key)
value = config.grep(/'#{key}' => (\w*)/).first.split('=>')[1]
value.gsub!(',', '').gsub!("'", '').chomp!.strip!
value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment