forked from collabbit/collabbit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
75 lines (64 loc) · 2.52 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
require 'find'
require 'active_record/fixtures'
require 'set'
require 'fileutils'
namespace :db do
desc "Turn development database into fixtures"
task :fixturize => :environment do
sql = "SELECT * FROM %s"
skip_tables = ["schema_info", "schema_migrations"]
ActiveRecord::Base.establish_connection
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
i = "000"
File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|
data = ActiveRecord::Base.connection.select_all(sql % table_name)
file.write data.inject({}) { |hash, record|
hash["#{table_name}_#{i.succ!}"] = record
hash
}.to_yaml
end
end
end
desc "Drop, create, migrate, populate the database"
task :redo => ['environment', 'db:drop', 'db:create', 'db:migrate', 'db:seed']
desc "Backup the database to a file. Options: DIR=base_dir RAILS_ENV=production MAX=20"
task :backup => [:environment] do
datestamp = Time.now.strftime("%Y%m%d%H%M%S")
base_path = ENV["DIR"] || "db"
backup_base = File.join(base_path, 'backup')
backup_folder = File.join(backup_base, datestamp)
backup_file = File.join(backup_folder, "#{RAILS_ENV}_dump.sql.gz")
FileUtils.mkdir_p(backup_folder)
db_config = ActiveRecord::Base.configurations[RAILS_ENV]
sh "mysqldump -u #{db_config['username']} -p#{db_config['password']} -Q -O add-locks=FALSE -O lock-tables=FALSE #{db_config['database']} | gzip -c > #{backup_file}"
dir = Dir.new(backup_base)
all_backups = dir.entries[2..-1].sort.reverse
puts "Created backup: #{backup_file}"
end
end
def flatten(hsh, prefix = '')
hsh.to_a.inject([]) do |memo, e|
k, v = e
memo + (v.is_a?(String) ? ["#{prefix}#{k}"] : flatten(v, "#{prefix}#{k}."))
end
end
namespace :i18n do
desc 'Find unimplemented strings'
task :missing => :environment do
strings = Set.new
Dir.glob("#{RAILS_ROOT}/app/**/*.{rb,erb}") do |path|
File.open(path) do |file|
strings.merge(file.read.scan(/\st\((\'|\")([^\)]+)(\'|\")\)/).map{|a|a[1]})
end
end
translations = flatten(YAML.load_file("#{RAILS_ROOT}/config/locales/en.yml")['en'])
puts (strings - translations).to_a.join("\n")
end
end