-
Notifications
You must be signed in to change notification settings - Fork 8
/
Rakefile
85 lines (60 loc) · 1.93 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
76
77
78
79
80
81
82
83
84
85
require 'bundler/gem_tasks'
require 'sevak'
require 'highline'
# run tests by running command `rake test`
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << 'test'
t.verbose = true
t.test_files = FileList['test/**/*_spec.rb']
t.warning = false
end
# Load all rake tasks in the tasks folder
tasks = FileList["tasks/*.rake"]
tasks.each { |task| load(task) }
namespace :consumer do
desc 'Interactively create a new consumer'
task :new do
cli = HighLine.new
consumer_name = cli.ask('Input consumer name(eg. push_alert) :') { |q| q.validate = /\A[a-z]+[a-z_]+[a-z]\z/}
queue_name = cli.ask('Input queue name(eg. myqueue)') { |q| q.validate = /\A[a-z]+[a-z.]+[a-z]\z/}
to_class_name = Proc.new do |name|
name.split('_').map(&:capitalize).join
end
text1 = <<-CODE
module Sevak
class #{to_class_name.call(consumer_name)}Consumer < Consumer
queue_name "#{queue_name}"
def run(payload)
puts "Consumer running"
end
end
end
CODE
if File.exists?("app/consumers/#{consumer_name}_consumer.rb")
puts "Already exists file: app/consumers/#{consumer_name}_consumer.rb"
ans = cli.ask('Do you want to overwrite it ? (y/n)') { |q| q.validate = /Y|N|y|n/}
exit(-1) if ans.downcase == 'n'
end
file = File.open("app/consumers/#{consumer_name}_consumer.rb", 'w+')
file.write(text1)
file.close
text2 = <<-CODE
namespace :sevak do
desc "Run the #{consumer_name} worker"
task :#{consumer_name} do
consumer = Sevak::#{to_class_name.call(consumer_name)}Consumer.new
consumer.start
end
end
CODE
if File.exists?("lib/tasks/#{consumer_name}.rake")
puts "Already exists file: lib/tasks/#{consumer_name}.rake"
ans = cli.ask('Do you want to overwrite it ? (y/n)') { |q| q.validate = /Y|N|y|n/}
exit(-1) if ans.downcase == 'n'
end
file = File.open("lib/tasks/#{consumer_name}.rake", 'w+')
file.write(text2)
file.close
end
end