forked from mitijain123/dot_files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sake
executable file
·316 lines (267 loc) · 7.19 KB
/
sake
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env ruby
require "rubygems"
require "rake"
require "rake/tasklib"
require "colored"
require "fileutils"
require "mysql"
require "grit"
class Object
def blank?
respond_to?(:empty?) ? empty? : !self
end
# An object is present if it's not blank.
def present?
!blank?
end
def presence
self if present?
end
end
class NilClass #:nodoc:
def blank?
true
end
end
class FalseClass #:nodoc:
def blank?
true
end
end
class TrueClass #:nodoc:
def blank?
false
end
end
class Array #:nodoc:
alias_method :blank?, :empty?
end
class Hash #:nodoc:
alias_method :blank?, :empty?
end
class String #:nodoc:
def blank?
self !~ /\S/
end
end
class Numeric #:nodoc:
def blank?
false
end
end
include FileUtils
Rake.application.init("sake")
desc "Push current branch"
task :gp do
branch_name = @git_help.current_branch
puts "Pushing branch #{branch_name}"
system("git push origin #{branch_name}")
end
desc "Merge remote of current with current branch"
task :gm do
branch_name = @git_help.current_branch
puts "Merging origin/#{branch_name} with #{branch_name}"
system("git merge origin/#{branch_name}")
end
desc "Rebase remote of current with current branch"
task :gb do
branch_name = @git_help.current_branch
puts "Rebasing origin/#{branch_name} with #{branch_name}"
system("git rebase origin/#{branch_name}")
end
desc "Show diff"
task :gd do
system("git diff")
end
desc "Do hard reset"
task :grst do
system("git reset --hard HEAD")
end
desc "Clone code from one of the CRRC repos"
task :gcs do
project_name = ENV['project']
system("git clone git@code.crrc.ca:#{project_name}.git")
end
desc "Creating a tracking local branch"
task :gct do
branch_name = ENV['branch']
if(branch_name.blank?)
abort("Argument branch=".red+"<branch_name>".green)
else
system("git checkout --track -b #{branch_name} origin/#{branch_name}")
end
end
desc "Creating local branch for all branches"
task :create_branch do
repo = Grit::Repo.new(".")
local_branches = repo.branches.collect { |x| x.name }
puts local_branches.join(",")
remote_origin_branches = repo.remotes.collect { |x| x.name =~ /^origin\/(.+)/ ? $1 : nil }.compact
puts "********** Remote origin branches are **********"
puts remote_origin_branches.join(",")
remote_origin_branches.each do |branch_name|
if branch_name != 'HEAD' && !local_branches.include?(branch_name)
puts "********** Creating branch #{branch_name}"
system("git checkout --track -b #{branch_name} origin/#{branch_name}")
elsif(branch_name != 'HEAD' && local_branches.include?(branch_name))
puts "########## Rebasing branch #{branch_name} with origin/#{branch_name}"
system("git checkout #{branch_name}")
system("git rebase origin/#{branch_name}")
end
end
end
desc "drop a table from particular db"
task :drop_table do
db = ENV['db']
table_name = ENV['table']
username = ENV['user'] || 'root'
password = ENV['password'] || ''
host = ENV['host'] || 'localhost'
if !db.blank? && !table_name.blank?
puts "Going to drop #{table_name} from #{db}"
db_util = Sake::Database.new()
db_util.delete_table(db,table_name,username,password,host)
else
abort("Please supply db and table to delete")
end
end
desc "Update all Solaro related repos"
task :slu do
solaro_dir = File.join(ENV['HOME'],'checkout','solaro')
cd(solaro_dir) { system("git fetch") }
securo_admin_dir = File.join(ENV['HOME'],'checkout','securo_admin')
cd(securo_admin_dir) { system("git fetch") }
payment_dir = File.join(ENV['HOME'],'checkout','securo_payment_middleware')
cd(payment_dir) { system("git fetch") }
solaro_revision_dir = File.join(ENV['HOME'],'solaro_revision')
cd(solaro_revision_dir) { system("git fetch") }
gitosis_dir = File.join(ENV['HOME'],'gitosis-admin')
cd(gitosis_dir) { system("git fetch") }
end
desc "Globally available task list"
task :default do
Rake.application.tasks.each do |task|
puts sprintf("%-25s : %s",task.name.bold.red,task.comment.blue)
end
end
desc "Update solaro"
task :sl do
solaro_dir = File.join(ENV['HOME'],'checkout','solaro')
cd(solaro_dir) do
system("git fetch")
Rake.application.invoke_task("gm")
end
end
desc "Update rails and arel"
task :ru do
rails_dir = File.join(ENV['HOME'],'checkout','rails')
cd(rails_dir) do
system("git fetch")
system("git fetch upstream")
system("git rebase upstream/master")
end
arel_dir = File.join(ENV['HOME'],'checkout','arel')
cd(arel_dir) do
system("git fetch")
Rake.application.invoke_task('gb')
end
rack_dir = File.join(ENV['HOME'],'checkout','rack')
cd(rack_dir) do
system("git fetch")
Rake.application.invoke_task('gb')
end
end
desc "truncate db"
task :dbt do
db = ENV['db']
username = ENV['user'] || 'root'
password = ENV['password'] || ''
host = ENV['host'] || 'localhost'
if not db.blank?
db_util = Sake::Database.new()
db_util.truncate_db(db,username,password,host)
else
abort("Please supply db name to truncate")
end
end
desc "Run ruby"
task :run_ruby do
ruby "~/checkout/solaro/sandbox/benchmark_find_select.rb"
end
desc "sandbox db"
task :box_db do
db = ENV['db'] || 'sandbox'
username = ENV['user'] || 'root'
password = ENV['password'] || ''
host = ENV['host'] || 'localhost'
file =<<-EOD
common: &common
adapter: mysql
reconnect: false
pool: 5
host: #{host}
username: #{username}
password: #{password}
development:
<<: *common
database: #{db}_development
test:
<<: *common
database: #{db}_test
production:
<<: *common
database: #{db}_production
EOD
current_dir = Dir.pwd.split("/").last
if(current_dir == 'config')
File.open("database.yml","w") { |fl| fl.write(file)}
else
cd("config") do
File.open("database.yml","w") { |fl| fl.write(file)}
end
end
['production','development','test'].each do |db_env|
db_name = "#{db}_#{db_env}"
puts "Going to recreate #{db_name}"
system("mysqladmin -h #{host} -u #{username} -f drop #{db_name}")
system("mysqladmin -h #{host} -u #{username} -f create #{db_name}")
end
end
module Sake
class Git
def current_branch
cmd_output = `git symbolic-ref HEAD`
branch_name = cmd_output.strip.split("/")[-1]
branch_name
end
end
class Database
def truncate_db(db,username = 'root',password = '', host = 'localhost')
puts "Truncating Database : ".bold + db.red.bold
puts "** Press enter to continue **".green.bold
STDIN.getc
begin
connection = Mysql.connect(host,username,password,db)
result = connection.real_query("show tables")
tables = []
result.each_hash { |table| tables << table.values.first }
tables.each do |table|
puts "Truncating #{table}"
connection.real_query("truncate #{table}")
end
ensure
connection.close()
end
end
def delete_table(db,table_name,username = 'root',password = '',host = 'localhost')
begin
connection = Mysql.connect(host,username,password,db)
result = connection.real_query("drop table #{table_name}")
ensure
connection.close()
end
end
end
end
@git_help = Sake::Git.new()
Rake.application.top_level()