Created
July 17, 2013 23:48
-
-
Save hightemp/6025594 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env rake | |
# encoding: utf-8 | |
require File.expand_path('../config/application', __FILE__) | |
Web::Application.load_tasks | |
desc "Push changes to server" | |
task :push_changes, :comment, :remote, :branch do |task, args| | |
Rake::Task['db:data:dump'].invoke | |
comment = args[:comment] || "Update #{Time.now}" | |
remote = args[:remote] || "origin" | |
branch = args[:branch] || "development" | |
sh "git add ." | |
sh "git commit -am '#{comment}'" | |
sh "git push #{remote} #{branch}" | |
end | |
namespace :db do | |
namespace :data do | |
def get_db_config() | |
Rails.application.config.database_configuration[Rails.env] | |
end | |
def get_sql_dump_file(filename=nil) | |
File.expand_path("../db/#{filename || "#{Rails.env}-dump.sql"}", __FILE__) | |
end | |
desc "Dump data" | |
task :dump, [:sql_dump_filename] => :environment do |task, args| | |
sql_dump_file = get_sql_dump_file(args[:sql_dump_filename]) | |
db_config = get_db_config() | |
tables = "" | |
ActiveRecord::Base.establish_connection | |
ActiveRecord::Base.connection.execute(" | |
select | |
group_concat(table_name separator ' ') | |
from | |
information_schema.tables | |
where | |
table_schema='#{db_config['database']}' | |
and | |
table_name not in ('schema_migrations') | |
").each do |line| | |
line.each do |table| | |
tables += "#{table} " | |
end | |
end | |
File.unlink sql_dump_file | |
cmd = "mysqldump5 " | |
cmd << "--no-create-db " | |
cmd << "--no-create-info " | |
cmd << "--lock-all-tables " | |
cmd << "--compact " | |
cmd << "-u#{db_config['username']} " | |
cmd << "-p#{db_config['password']} " | |
cmd << "--socket=#{db_config['socket']} " unless db_config['socket'].blank? | |
cmd << "--host=#{db_config['host']} " unless db_config['host'].blank? | |
cmd << "#{db_config['database']} #{tables} > #{sql_dump_file}" | |
sh cmd | |
lines = File.read(sql_dump_file).split("\n") | |
lines.each_with_index do |line, index| | |
fields = [] | |
table_name = line.match(/INSERT\s*(INTO\s*)?`(\w+)`/)[2] rescue nil | |
unless table_name.blank? | |
ActiveRecord::Base.connection.execute(" | |
select | |
column_name | |
from | |
information_schema.columns | |
where | |
table_name='#{table_name}' | |
and | |
table_schema='#{db_config['database']}' | |
group by | |
column_name | |
").each do |lines| | |
lines.each do |line| | |
fields << line | |
end | |
end | |
end | |
update = [] | |
fields.each { |field| update << "`#{field}`=VALUES(`#{field}`)" } | |
lines[index].gsub! /(INSERT.*);$/, "\\1 ON DUPLICATE KEY UPDATE #{update.join(',')};" | |
end | |
File.open(sql_dump_file, "w+") { |f| f.write(lines.join("\n")) } | |
end | |
desc "Restore data" | |
task :restore, [:sql_dump_filename, :drop] => :environment do |task, args| | |
drop = args[:drop] || false | |
sql_dump_file = get_sql_dump_file(args[:sql_dump_filename]) | |
db_config = get_db_config() | |
abort "Dump file doesn't exist: #{sql_dump_file}" unless File.exists? sql_dump_file | |
ActiveRecord::Base.establish_connection | |
Rake::Task['db:drop'].invoke if drop | |
Rake::Task['db:migrate'].invoke | |
begin | |
ActiveRecord::Base.connection.execute("START TRANSACTION") | |
lines = File.read(sql_dump_file) | |
lines.split("\n").each do |line| | |
puts "SQL: #{line}" | |
ActiveRecord::Base.connection.execute(line) | |
end | |
ActiveRecord::Base.connection.execute("COMMIT") | |
rescue Exception => e | |
ActiveRecord::Base.connection.execute("ROLLBACK") | |
puts e | |
end | |
end | |
end | |
end | |
desc "Generate translations" | |
task :generate_translations do | |
sh "rails g i18n_translation ru" | |
end | |
desc "Install gems" | |
task :bundle_install do | |
sh "bundle install --path .bundle/gems --binstubs .bundle/bin" | |
end | |
desc "Reload server" | |
task :reload_server do | |
sh "touch tmp/restart.txt" | |
end | |
desc "Stop server" | |
task :stop_server do | |
sh "passenger stop -p80" | |
=begin | |
if File.exists? "tmp/pids/server.pid" | |
pid = File.read "tmp/pids/server.pid" | |
sh "kill -9 #{pid}" | |
end | |
=end | |
end | |
desc "Start server" | |
task :start_server => ["stop_server"] do | |
#sh "rails s --port=30000 -d" | |
sh "passenger start -p80 -d" | |
end | |
desc "Clear all site folders" | |
task :clear_site do | |
require 'rake/clean' | |
["stylesheets","scripts","iamges","fonts"].each do |dir| | |
CLOBBER.include "app/#{dir}/site" | |
end | |
end | |
desc "Update images" | |
task :update_images => :environment do | |
ActiveRecord::Base.establish_connection | |
Picture.update_images | |
end | |
desc "Prepare stylesheets" | |
task :prepare do | |
assets_path = 'app/assets/stylesheets/site/' | |
FileList[assets_path + '**/*.css'].each do |f| | |
dirname = File.dirname(f) | |
filename = File.basename(f) | |
File.rename f, "#{dirname}#{filename}.scss" | |
puts "Renamed: #{f} => #{dirname}#{filename}.scss" | |
end | |
FileList[assets_path + '**/*.css.scss'].each do |f| | |
puts "Open: #{f}" | |
content = File.read f | |
font_matches = content.scan /@font-face\s*{([^}]*)}/ | |
puts "Found: #{font_matches.length} font blocks" | |
font_matches.each do |font_match| | |
urls_matches = font_match[0].scan /([,:\n\t\s]+)url\(["']?\.?\.?\/?fonts?([^"')]*)["']?\)/ | |
puts "Found: #{urls_matches.length} font urls" | |
if urls_matches.length>0 | |
font_new = font_match[0].gsub /([,:\n\t\s]+)url\(["']?\.?\.?\/?fonts?([^"')]*)["']?\)/, '\1font-url(\'site\2\')' | |
content.sub! font_match[0], font_new | |
end | |
end | |
urls_matches = content.scan /([,:\n\t\s]+)url\(["']?\.?\.?\/?ima?ge?s?([^"')]*)["']?\)/ | |
puts "Found: #{urls_matches.length} urls" | |
if urls_matches.length>0 | |
urls_matches.each do | |
content.sub! /([,:\n\t\s]+)url\(["']?\.?\.?\/?ima?ge?s?([^"')]*)["']?\)/, '\1image-url(\'site\2\')' | |
end | |
end | |
File.open(f, "w") { |h| h.write(content) } | |
puts "Fixed: #{f}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment