eventhub-command 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,91 +1,91 @@
1
- desc 'Generates a template for a processor'
2
- arg_name 'module_name processor_name'
3
- command :generate do |c|
4
- c.command :processor do |c|
5
- c.action do |global_options, options, args|
6
- require 'active_support/core_ext/string/inflections'
7
- require 'fileutils'
8
- require 'erb'
9
-
10
- if args.size != 2
11
- puts "Needs exactly 2 arguments: eh generate_processor ModuleName ProcessorName"
12
- exit -1
13
- end
14
-
15
- processor_module_name = args[0].camelcase
16
- processor_class_name = args[1].camelcase
17
- underscored_processor_module_name = processor_module_name.underscore
18
- underscored_processor_class_name = processor_class_name.underscore
19
-
20
- destination_dir = Eh::Settings.current.processors_src_dir
21
- destination_dir = File.join(destination_dir, "#{underscored_processor_module_name}.#{underscored_processor_class_name}")
22
-
23
- if Dir.exists? destination_dir
24
- puts "#{destination_dir} already exists!"
25
- exit -1
26
- end
27
-
28
- template_tmp_dir = Eh::Settings.current.template_tmp_dir
29
- checkout_git_repo(template_tmp_dir)
30
-
31
- FileUtils.cp_r template_tmp_dir, destination_dir
32
- FileUtils.rm_rf File.join(destination_dir, ".git")
33
- FileUtils.rm File.join(destination_dir, 'README.md')
34
-
35
- puts "Generating processor #{processor_module_name}::#{processor_class_name} in #{destination_dir}"
36
- Dir.glob(destination_dir + "/**/*.erb") do |file|
37
- template = ERB.new(File.read(file))
38
-
39
- File.open(file, "w") do |writeable_file|
40
- writeable_file.puts template.result(binding)
41
- end
42
-
43
- FileUtils.mv file, File.join(File.dirname(file), File.basename(file, ".erb"))
44
- end
45
-
46
- replacements = [
47
- ["underscored_processor_module_name", underscored_processor_module_name],
48
- ["underscored_processor_class_name", underscored_processor_class_name],
49
- ["processor_module_name", processor_module_name],
50
- ["processor_class_name", processor_class_name]
51
- ]
52
-
53
- rename_files_with_replacements(destination_dir, replacements)
54
-
55
- FileUtils.rm_rf template_tmp_dir
56
-
57
- puts "Done."
58
- end
59
- end
60
-
61
- def shallow_clone_git_repository(source_url, destination_dir)
62
- system("git clone --depth 1 #{source_url} #{destination_dir}")
63
- end
64
-
65
- def rename_files_with_replacements(destination_dir, replacements)
66
- Dir.glob(destination_dir + "/**/*") do |src_file_path|
67
- if File.file? src_file_path
68
- dir = File.dirname src_file_path
69
- file_with_replacements = File.basename src_file_path
70
-
71
- replacements.each do |find_string, replace_string|
72
- file_with_replacements.sub!(find_string, replace_string)
73
- end
74
-
75
- dest_file_path = File.join(dir, file_with_replacements)
76
-
77
- if src_file_path != dest_file_path
78
- FileUtils.mv src_file_path, dest_file_path
79
- end
80
- end
81
- end
82
- end
83
-
84
- def checkout_git_repo(destination_dir)
85
- template_repository_url = Eh::Settings.current.processor_template_repository_url
86
- puts "Checking out latest template from #{template_repository_url}"
87
- FileUtils.rm_rf(destination_dir)
88
- FileUtils.mkdir(destination_dir)
89
- shallow_clone_git_repository(template_repository_url, destination_dir)
90
- end
91
- end
1
+ desc 'Generates a template for a processor'
2
+ arg_name 'module_name processor_name'
3
+ command :generate do |c|
4
+ c.command :processor do |c|
5
+ c.action do |global_options, options, args|
6
+ require 'active_support/core_ext/string/inflections'
7
+ require 'fileutils'
8
+ require 'erb'
9
+
10
+ if args.size != 2
11
+ puts "Needs exactly 2 arguments: eh generate_processor ModuleName ProcessorName"
12
+ exit -1
13
+ end
14
+
15
+ processor_module_name = args[0].camelcase
16
+ processor_class_name = args[1].camelcase
17
+ underscored_processor_module_name = processor_module_name.underscore
18
+ underscored_processor_class_name = processor_class_name.underscore
19
+
20
+ destination_dir = Eh::Settings.current.processors_src_dir
21
+ destination_dir = File.join(destination_dir, "#{underscored_processor_module_name}.#{underscored_processor_class_name}")
22
+
23
+ if Dir.exists? destination_dir
24
+ puts "#{destination_dir} already exists!"
25
+ exit -1
26
+ end
27
+
28
+ template_tmp_dir = Eh::Settings.current.template_tmp_dir
29
+ checkout_git_repo(template_tmp_dir)
30
+
31
+ FileUtils.cp_r template_tmp_dir, destination_dir
32
+ FileUtils.rm_rf File.join(destination_dir, ".git")
33
+ FileUtils.rm File.join(destination_dir, 'README.md')
34
+
35
+ puts "Generating processor #{processor_module_name}::#{processor_class_name} in #{destination_dir}"
36
+ Dir.glob(destination_dir + "/**/*.erb") do |file|
37
+ template = ERB.new(File.read(file))
38
+
39
+ File.open(file, "w") do |writeable_file|
40
+ writeable_file.puts template.result(binding)
41
+ end
42
+
43
+ FileUtils.mv file, File.join(File.dirname(file), File.basename(file, ".erb"))
44
+ end
45
+
46
+ replacements = [
47
+ ["underscored_processor_module_name", underscored_processor_module_name],
48
+ ["underscored_processor_class_name", underscored_processor_class_name],
49
+ ["processor_module_name", processor_module_name],
50
+ ["processor_class_name", processor_class_name]
51
+ ]
52
+
53
+ rename_files_with_replacements(destination_dir, replacements)
54
+
55
+ FileUtils.rm_rf template_tmp_dir
56
+
57
+ puts "Done."
58
+ end
59
+ end
60
+
61
+ def shallow_clone_git_repository(source_url, destination_dir)
62
+ system("git clone --depth 1 #{source_url} #{destination_dir}")
63
+ end
64
+
65
+ def rename_files_with_replacements(destination_dir, replacements)
66
+ Dir.glob(destination_dir + "/**/*") do |src_file_path|
67
+ if File.file? src_file_path
68
+ dir = File.dirname src_file_path
69
+ file_with_replacements = File.basename src_file_path
70
+
71
+ replacements.each do |find_string, replace_string|
72
+ file_with_replacements.sub!(find_string, replace_string)
73
+ end
74
+
75
+ dest_file_path = File.join(dir, file_with_replacements)
76
+
77
+ if src_file_path != dest_file_path
78
+ FileUtils.mv src_file_path, dest_file_path
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ def checkout_git_repo(destination_dir)
85
+ template_repository_url = Eh::Settings.current.processor_template_repository_url
86
+ puts "Checking out latest template from #{template_repository_url}"
87
+ FileUtils.rm_rf(destination_dir)
88
+ FileUtils.mkdir(destination_dir)
89
+ shallow_clone_git_repository(template_repository_url, destination_dir)
90
+ end
91
+ end
@@ -3,14 +3,14 @@ command :package do |c|
3
3
  c.flag([:s, :source], :desc => "Source directory to read processors from.")
4
4
  c.flag([:d, :destination], :desc => "Destination directory to place created zip files.")
5
5
 
6
- c.desc 'Packages processors to zip files. '
6
+ c.desc 'Packages ruby processors to zip files'
7
7
  c.arg_name '[processor_name,[other_processor_name,pattern*]]'
8
- c.command :ruby do |c|
9
- c.flag([:i, :include], :desc => "Include processors by name format: [processor_name,[other_processor_name,pattern*]]", :type => String, :long_desc => "You can specify multiple processors by providing a comma-separated list as well as pattern using '*'")
10
- c.flag([:x, :exclude], :desc => "Exclude processors by name format: [processor_name,[other_processor_name,pattern*]]", :type => String, :long_desc => "You can specify multiple processors by providing a comma-separated list as well as pattern using '*'")
8
+ c.command :ruby do |ruby|
9
+ ruby.flag([:i, :include], :desc => "Include processors by name format: [processor_name,[other_processor_name,pattern*]]", :type => String, :long_desc => "You can specify multiple processors by providing a comma-separated list as well as pattern using '*'")
10
+ ruby.flag([:x, :exclude], :desc => "Exclude processors by name format: [processor_name,[other_processor_name,pattern*]]", :type => String, :long_desc => "You can specify multiple processors by providing a comma-separated list as well as pattern using '*'")
11
11
 
12
- c.action do |global_options, options, args|
13
- options['s'] ||= Eh::Settings.current.processors_src_dir
12
+ ruby.action do |global_options, options, args|
13
+ options['s'] ||= Eh::Settings.current.ruby_processors_src_dir
14
14
  options['d'] ||= Eh::Settings.current.ruby_release_dir
15
15
  source_dir = options.fetch('s')
16
16
  destination_dir = options.fetch('d')
@@ -21,9 +21,28 @@ command :package do |c|
21
21
  end
22
22
  end
23
23
 
24
+ c.desc 'Packages go processors to zip files'
25
+ c.command :go do |go|
24
26
 
25
- c.command :rails do |c|
26
- c.action do |global_options, options, args|
27
+ go.flag([:p, :platform], :desc => "Define target platform [linux, osx, window]")
28
+
29
+ go.action do |global_options, options, args|
30
+ options['s'] ||= Eh::Settings.current.go_processors_src_dir
31
+ options['d'] ||= Eh::Settings.current.go_release_dir
32
+ source_dir = options.fetch('s')
33
+ destination_dir = options.fetch('d')
34
+ include_pattern = options['i'] || args[0]
35
+ exclude_pattern = options['x']
36
+ platform = options['p'] || 'linux'
37
+ packager = Packager::Go.new(source_dir, destination_dir, include_pattern, exclude_pattern, platform)
38
+ packager.package
39
+ end
40
+
41
+ end
42
+
43
+ c.desc 'Packages rails console app to zip file'
44
+ c.command :rails do |rails|
45
+ rails.action do |global_options, options, args|
27
46
  options['s'] ||= Eh::Settings.current.rails_src_dir
28
47
  options['d'] ||= Eh::Settings.current.rails_release_dir
29
48
  source_dir = options.fetch('s')
@@ -1,81 +1,81 @@
1
- desc "manage repositories"
2
-
3
- command :repository do |command|
4
- command.desc "Lists all avaiable repositories"
5
- command.command :list do |command|
6
- command.action do |global_options,options,args|
7
-
8
- Eh::Settings.current.repositories.each_with_index do |repository, index|
9
- if repository.current?
10
- puts "#{index + 1}: #{repository.url} (current)"
11
- else
12
- puts "#{index + 1}: #{repository.url}"
13
- end
14
- end
15
- end
16
- end
17
-
18
- command.desc "selects a repository: eh repository select INDEX"
19
- command.command :select do |command|
20
- command.action do |global_options,options,args|
21
- if Eh::Settings.current.repositories.length == 0
22
- raise "No repository configured so far"
23
- end
24
- if args.length != 1
25
- raise "Need exactly 1 arguments: index"
26
- end
27
- selected = args[0].to_i
28
- Eh::Settings.current.data['repositories'].each_with_index do |repository, index|
29
- repository['current'] = (index + 1) == selected
30
- end
31
- puts "Selected #{Eh::Settings.current.repository.url}"
32
- Eh::Settings.current.write
33
- end
34
- end
35
-
36
- command.desc 'add a repository to the config: eh repository add URL DIR USERNAME PASSWORD'
37
- command.command :add do |command|
38
- command.action do |global_options, options, args|
39
- if args.length != 4
40
- raise "Need exactly 4 arguments: URL, DIR, USERNAME, PASSWORD"
41
- end
42
- Eh::Settings.current.data['repositories'] ||= []
43
-
44
- # check if same repo already exists
45
- exists = Eh::Settings.current.data['repositories'].any? do |repository|
46
- repository['url'] == args[0]
47
- end
48
- if exists
49
- raise "Already configured repository for '#{args[0]}'"
50
- end
51
-
52
- Eh::Settings.current.data['repositories'] << {
53
- 'url' => args[0],
54
- 'dir' => args[1],
55
- 'deploy_username' => args[2],
56
- 'deploy_password' => args[3],
57
- 'current' => (Eh::Settings.current.data['repositories'].length == 0)
58
- }
59
- Eh::Settings.current.write
60
- end
61
- end
62
-
63
-
64
- command.desc 'remove a repository from the config: eh repository remove INDEX'
65
- command.command :remove do |command|
66
- command.action do |global_options, options, args|
67
-
68
- if args.length != 1
69
- raise "Need exactly 1 arguments: index"
70
- end
71
- selected = args[0].to_i
72
-
73
- if Eh::Settings.current.repositories[selected - 1].nil?
74
- raise "No repository with index #{selected}"
75
- end
76
-
77
- Eh::Settings.current.data['repositories'].delete_at(selected - 1)
78
- Eh::Settings.current.write
79
- end
80
- end
81
- end
1
+ desc "manage repositories"
2
+
3
+ command :repository do |command|
4
+ command.desc "Lists all avaiable repositories"
5
+ command.command :list do |command|
6
+ command.action do |global_options,options,args|
7
+
8
+ Eh::Settings.current.repositories.each_with_index do |repository, index|
9
+ if repository.current?
10
+ puts "#{index + 1}: #{repository.url} (current)"
11
+ else
12
+ puts "#{index + 1}: #{repository.url}"
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ command.desc "selects a repository: eh repository select INDEX"
19
+ command.command :select do |command|
20
+ command.action do |global_options,options,args|
21
+ if Eh::Settings.current.repositories.length == 0
22
+ raise "No repository configured so far"
23
+ end
24
+ if args.length != 1
25
+ raise "Need exactly 1 arguments: index"
26
+ end
27
+ selected = args[0].to_i
28
+ Eh::Settings.current.data['repositories'].each_with_index do |repository, index|
29
+ repository['current'] = (index + 1) == selected
30
+ end
31
+ puts "Selected #{Eh::Settings.current.repository.url}"
32
+ Eh::Settings.current.write
33
+ end
34
+ end
35
+
36
+ command.desc 'add a repository to the config: eh repository add URL DIR USERNAME PASSWORD'
37
+ command.command :add do |command|
38
+ command.action do |global_options, options, args|
39
+ if args.length != 4
40
+ raise "Need exactly 4 arguments: URL, DIR, USERNAME, PASSWORD"
41
+ end
42
+ Eh::Settings.current.data['repositories'] ||= []
43
+
44
+ # check if same repo already exists
45
+ exists = Eh::Settings.current.data['repositories'].any? do |repository|
46
+ repository['url'] == args[0]
47
+ end
48
+ if exists
49
+ raise "Already configured repository for '#{args[0]}'"
50
+ end
51
+
52
+ Eh::Settings.current.data['repositories'] << {
53
+ 'url' => args[0],
54
+ 'dir' => args[1],
55
+ 'deploy_username' => args[2],
56
+ 'deploy_password' => args[3],
57
+ 'current' => (Eh::Settings.current.data['repositories'].length == 0)
58
+ }
59
+ Eh::Settings.current.write
60
+ end
61
+ end
62
+
63
+
64
+ command.desc 'remove a repository from the config: eh repository remove INDEX'
65
+ command.command :remove do |command|
66
+ command.action do |global_options, options, args|
67
+
68
+ if args.length != 1
69
+ raise "Need exactly 1 arguments: index"
70
+ end
71
+ selected = args[0].to_i
72
+
73
+ if Eh::Settings.current.repositories[selected - 1].nil?
74
+ raise "No repository with index #{selected}"
75
+ end
76
+
77
+ Eh::Settings.current.data['repositories'].delete_at(selected - 1)
78
+ Eh::Settings.current.write
79
+ end
80
+ end
81
+ end
data/lib/eh/settings.rb CHANGED
@@ -109,10 +109,18 @@ class Eh::Settings
109
109
  releases_dir('ruby')
110
110
  end
111
111
 
112
- def processors_src_dir
112
+ def go_release_dir
113
+ releases_dir('go')
114
+ end
115
+
116
+ def ruby_processors_src_dir
113
117
  File.join(repository.dir, 'src', 'ruby')
114
118
  end
115
119
 
120
+ def go_processors_src_dir
121
+ File.join(repository.dir, 'src', 'go', 'src', 'github.com', 'cme-eventhub')
122
+ end
123
+
116
124
  def rails_src_dir
117
125
  File.join(repository.dir, 'src', 'rails')
118
126
  end
data/lib/eh/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eh
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
data/lib/packager.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module Packager
2
2
  end
3
3
  require_relative 'packager/ruby'
4
+ require_relative 'packager/go'
4
5
  require_relative 'packager/rails'
@@ -0,0 +1,159 @@
1
+ class Packager::Go
2
+
3
+ PLATFORMS = ['linux','windows','darwin']
4
+
5
+ def initialize(source_dir, destination_dir, include_pattern_string, exclude_pattern_string, platform)
6
+ @source_dir = Pathname.new(source_dir)
7
+ @destination_dir = Pathname.new(destination_dir)
8
+ @include_pattern_string = include_pattern_string
9
+ @exclude_pattern_string = exclude_pattern_string
10
+ @platform = platform
11
+ end
12
+
13
+ def package
14
+ assert_at_least_one_processor!
15
+ validate_platforms!
16
+ create_destination_dir
17
+ #copy_deployment_management_files
18
+ build_processors
19
+ package_processors
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :source_dir, :destination_dir, :include_pattern_string, :exclude_pattern_string
25
+
26
+ def build_processors
27
+ puts "Build processors"
28
+ processor_names.each do |processor_name|
29
+ build_processor(processor_name)
30
+ end
31
+ end
32
+
33
+ def build_processor(processor_name)
34
+ print "Build: #{processor_name.light_blue}"
35
+ system "cd #{processor_source_dir(processor_name)} && GOOS=#{@platform} GOARCH=amd64 go build"
36
+ puts " done".green
37
+ end
38
+
39
+ def package_processors
40
+ puts "Start packaging"
41
+ processor_names.each do |processor_name|
42
+ package_processor(processor_name)
43
+ end
44
+ puts "Done packaging #{processor_names.size} processors"
45
+ end
46
+
47
+ def package_processor(processor_name)
48
+ print "Package: #{processor_name.light_blue} "
49
+ remove_destination_file(processor_name)
50
+
51
+ Zip::File.open(destination_file_name(processor_name), Zip::File::CREATE) do |zipfile|
52
+ files = files_to_zip(processor_name)
53
+ files.each do |file|
54
+ relative_filename = file.relative_path_from(source_dir)
55
+ zipfile.add(relative_filename, file)
56
+ end
57
+ end
58
+ puts " done".green
59
+ end
60
+
61
+
62
+ def files_to_zip(processor_name)
63
+ # currently take only the binary and the config folder
64
+ files = Dir.glob(File.join(processor_source_dir(processor_name), 'config', '**', '*'))
65
+ files << File.join(processor_source_dir(processor_name),processor_name)
66
+
67
+ files.map do |file|
68
+ Pathname.new(file)
69
+ end
70
+ end
71
+
72
+ def processor_source_dir(processor_name)
73
+ File.join(source_dir, processor_name)
74
+ end
75
+
76
+ def create_destination_dir
77
+ FileUtils.mkdir_p(destination_dir)
78
+ end
79
+
80
+ def remove_destination_file(processor_name)
81
+ FileUtils.rm destination_file_name(processor_name), force: true
82
+ end
83
+
84
+ def destination_file_name(processor_name)
85
+ File.join(destination_dir, "#{processor_name}.zip")
86
+ end
87
+
88
+ def copy_deployment_management_files
89
+ Eh::Settings.current.deployment_management_files.each do |file|
90
+ FileUtils.cp(file, destination_dir)
91
+ end
92
+ end
93
+
94
+ def assert_at_least_one_processor!
95
+ if processor_names.empty?
96
+ raise "There are no processor names. Either your -s directory is empty or you specified a strange combination of include and exclude pattern."
97
+ end
98
+ end
99
+
100
+ def validate_platforms!
101
+ unless PLATFORMS.include?(@platform)
102
+ raise "Given platform [#{@platform}] is not allowed out of [#{PLATFORMS.join(', ')}]"
103
+ end
104
+ end
105
+
106
+ def processor_names
107
+ included_names = existing_processor_names
108
+ included_names = included_processor_names(included_names)
109
+ excluded_names = excluded_processor_names(included_names)
110
+ included_names - excluded_names
111
+ end
112
+
113
+ def existing_processor_names
114
+ Dir["#{source_dir}/*"].map do |dir|
115
+ File.basename(dir)
116
+ end.delete_if do |item|
117
+ !File.directory?("#{source_dir}/#{item}")
118
+ end.sort
119
+ end
120
+
121
+ def included_processor_names(names)
122
+ # if processor names are given as arguments then use them.
123
+ # can contain wildcards like "console.*" to include all processors
124
+ # starting with "console.".
125
+ names.select do |name|
126
+ include_patterns.empty? || include_patterns.any? do |pattern|
127
+ wildcard_pattern_match?(pattern, name) || pattern_match?(pattern, name)
128
+ end
129
+ end
130
+ end
131
+
132
+ def excluded_processor_names(names)
133
+ names.select do |name|
134
+ exclude_patterns.any? && exclude_patterns.any? do |pattern|
135
+ wildcard_pattern_match?(pattern, name) || pattern_match?(pattern, name)
136
+ end
137
+ end
138
+ end
139
+
140
+ def include_patterns
141
+ (include_pattern_string || '').split(',').map { |part| part.strip }
142
+ end
143
+
144
+ def exclude_patterns
145
+ (exclude_pattern_string || '').split(',').map { |part| part.strip }
146
+ end
147
+
148
+ def wildcard_pattern?(pattern)
149
+ pattern.end_with?('*')
150
+ end
151
+
152
+ def wildcard_pattern_match?(pattern, name)
153
+ wildcard_pattern?(pattern) && name.start_with?(pattern.gsub('*', ''))
154
+ end
155
+
156
+ def pattern_match?(pattern, name)
157
+ pattern == name
158
+ end
159
+ end