console_runner 0.1.13 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/command_line_parser.rb +33 -14
- data/lib/console_runner.rb +43 -35
- data/lib/console_runner/version.rb +1 -1
- data/lib/console_runner_error.rb +9 -1
- data/lib/file_parser.rb +8 -8
- metadata +1 -2
- data/lib/runner.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39d8e37c222218501547763179826453d99b5208
|
4
|
+
data.tar.gz: 2cdc022889b7ce2bfa05ed217b4fa77a333ec5a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 581eca4768176a9458c70cde0e670b187cb0462215ef7d94cf9165f88efc7bac233a5d87e8a1b1c8b922d5d67acd1e389c8d9185c440392dfc35567ebae90044
|
7
|
+
data.tar.gz: 6127da22aeeb5bf37d8b0a65e697d5b82325e7be977683274f9c927aede4198fc18177256ecb41e95642b228d5a24912eeb447cdcd5e7abf1d0fcab328f2a6df
|
data/lib/command_line_parser.rb
CHANGED
@@ -8,23 +8,43 @@ class CommandLineParser
|
|
8
8
|
# Generate tool help menu.
|
9
9
|
# IMPORTANT! Should be executed before ARGV.shift
|
10
10
|
def initialize(file_parser)
|
11
|
-
@file_parser
|
12
|
-
@sub_commands
|
13
|
-
@
|
14
|
-
|
11
|
+
@file_parser = file_parser
|
12
|
+
@sub_commands = @file_parser.runnable_methods.map { |m| m.name.to_s }
|
13
|
+
@sub_commands_text = @file_parser.runnable_methods.map do |m|
|
14
|
+
[
|
15
|
+
m.name.to_s,
|
16
|
+
FileParser.select_runnable_tags(m).map(&:text).join("\n")
|
17
|
+
]
|
18
|
+
end.to_h
|
19
|
+
@parser = Trollop::Parser.new
|
20
|
+
@banner = generate_banner
|
15
21
|
@parser.stop_on @sub_commands
|
22
|
+
@init_method = nil
|
16
23
|
end
|
17
24
|
|
18
|
-
def
|
19
|
-
|
25
|
+
def generate_banner
|
26
|
+
result = FileParser.select_runnable_tags(@file_parser.clazz).map(&:text).join("\n")
|
27
|
+
result += "\n\n\tAvailable actions:\n"
|
28
|
+
result += @sub_commands_text.map do |c, text|
|
29
|
+
t = "\t\t- #{c}"
|
30
|
+
t += "\n\t\t\t#{text}" if text != ''
|
31
|
+
t
|
32
|
+
end.join("\n")
|
33
|
+
@parser.banner(result)
|
34
|
+
result
|
35
|
+
end
|
36
|
+
|
37
|
+
def raise_help
|
38
|
+
if %w(-h --help).include?(ARGV[0].to_s.strip)
|
20
39
|
Trollop::with_standard_exception_handling(@parser) { raise Trollop::HelpNeeded }
|
21
40
|
end
|
22
|
-
|
23
|
-
|
24
|
-
|
41
|
+
return if @sub_commands.include?(ARGV[0].to_s.strip)
|
42
|
+
raise ConsoleRunnerError, "You must provide one of available actions: #{@sub_commands.join ', '}"
|
43
|
+
end
|
25
44
|
|
26
|
-
|
27
|
-
|
45
|
+
def run(action)
|
46
|
+
raise_help
|
47
|
+
@init_method ||= MethodParser.new(@file_parser.initialize_method) if @file_parser.initialize_method
|
28
48
|
@method = MethodParser.new action
|
29
49
|
[@init_method, @method].each do |method|
|
30
50
|
next unless method
|
@@ -42,9 +62,8 @@ class CommandLineParser
|
|
42
62
|
end
|
43
63
|
method.required_parameters.each do |required_param|
|
44
64
|
next if method.options_group? required_param
|
45
|
-
|
46
|
-
|
47
|
-
end
|
65
|
+
next if method.cmd_opts[required_param.to_sym]
|
66
|
+
raise ConsoleRunnerError, "You must specify required parameter: #{required_param}"
|
48
67
|
end
|
49
68
|
ARGV.shift
|
50
69
|
end
|
data/lib/console_runner.rb
CHANGED
@@ -1,55 +1,58 @@
|
|
1
1
|
require 'file_parser'
|
2
2
|
require 'command_line_parser'
|
3
|
-
require 'runner'
|
4
3
|
require 'console_runner/version'
|
5
4
|
|
6
5
|
# console_runner logic is here
|
7
6
|
module ConsoleRunner
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
7
|
+
begin
|
8
|
+
start_time = Time.now
|
9
|
+
success_status = true
|
10
|
+
puts "Start Time: #{start_time}"
|
11
|
+
|
12
|
+
file_from_arg = ARGV.shift
|
13
|
+
raise ConsoleRunnerError, 'Specify file to be executed' unless file_from_arg
|
14
|
+
file_path = File.realpath file_from_arg
|
15
|
+
file_parser = FileParser.new(file_path)
|
16
|
+
cmd = ARGV[0]
|
17
|
+
actions = file_parser.runnable_methods.select { |m| m.name.to_s == cmd }
|
18
|
+
if actions.count > 1
|
19
|
+
raise(
|
20
|
+
ConsoleRunnerError,
|
21
|
+
"Class and Instance methods have the same name (#{cmd}). Actions names should be unique"
|
22
|
+
)
|
23
|
+
end
|
24
|
+
action = actions.first
|
25
|
+
action ||= file_parser.run_method
|
26
|
+
c_line_parser = CommandLineParser.new(file_parser)
|
27
|
+
c_line_parser.run(action)
|
24
28
|
|
25
29
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
30
|
+
puts '======================================================='
|
31
|
+
puts 'Global options:'
|
32
|
+
if file_parser.initialize_method
|
33
|
+
puts "INIT: #{file_parser.initialize_method.name}"
|
34
|
+
puts 'INIT options:'
|
35
|
+
puts c_line_parser.init_method.cmd_opts.map { |k, v| " #{k} = #{v}" }.join("\n")
|
36
|
+
end
|
37
|
+
puts "Subcommand: #{action.name}"
|
38
|
+
puts 'Subcommand options:'
|
39
|
+
puts c_line_parser.method.cmd_opts.map { |k, v| " #{k} = #{v}" }.join("\n")
|
40
|
+
puts "Remaining arguments: #{ARGV.inspect}" if ARGV != []
|
41
|
+
puts '======================================================='
|
38
42
|
|
39
43
|
|
40
|
-
Runner.run {
|
41
44
|
require file_path
|
42
45
|
class_full_name = file_parser.clazz.title
|
43
46
|
raise ConsoleRunnerError, "#{class_full_name} is not defined" unless Module.const_defined?(class_full_name)
|
44
47
|
klass_obj = Module.const_get(class_full_name)
|
45
48
|
method_type = action.scope
|
46
|
-
method_params =
|
49
|
+
method_params = c_line_parser.method.params_array
|
47
50
|
|
48
51
|
case method_type
|
49
52
|
when :class
|
50
53
|
klass_obj.send(action.name, *method_params)
|
51
54
|
when :instance
|
52
|
-
init_method =
|
55
|
+
init_method = c_line_parser.init_method
|
53
56
|
init_params = []
|
54
57
|
init_params = init_method.params_array if init_method
|
55
58
|
obj = klass_obj.new(*init_params)
|
@@ -57,7 +60,12 @@ module ConsoleRunner
|
|
57
60
|
else
|
58
61
|
raise ConsoleRunnerError, "Unknown method type: #{method_type}"
|
59
62
|
end
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
+
rescue => e
|
64
|
+
success_status = false
|
65
|
+
raise e
|
66
|
+
ensure
|
67
|
+
finish_time = Time.now
|
68
|
+
puts "Execution status: #{success_status ? 'Success' : 'Error' }"
|
69
|
+
puts "Finish Time: #{finish_time} (Duration: #{((finish_time - start_time) / 60).round(2) } minutes)"
|
70
|
+
end
|
63
71
|
end
|
data/lib/console_runner_error.rb
CHANGED
data/lib/file_parser.rb
CHANGED
@@ -27,6 +27,14 @@ class FileParser
|
|
27
27
|
@run_method = all_methods.find { |m| m.name == :run }
|
28
28
|
end
|
29
29
|
|
30
|
+
# Select all @runnable tags from of specified object
|
31
|
+
#
|
32
|
+
# @param [YARD::CodeObjects::MethodObject, YARD::CodeObjects::ClassObject] yard_object YARD object
|
33
|
+
# @return [Array(YARD::Tags::Tag)]
|
34
|
+
def self.select_runnable_tags(yard_object)
|
35
|
+
yard_object.tags.select { |t| t.tag_name == RUNNABLE_TAG.to_s }
|
36
|
+
end
|
37
|
+
|
30
38
|
private
|
31
39
|
|
32
40
|
# List of methods
|
@@ -72,14 +80,6 @@ class FileParser
|
|
72
80
|
end
|
73
81
|
end
|
74
82
|
|
75
|
-
# Select all @runnable tags from of specified object
|
76
|
-
#
|
77
|
-
# @param [YARD::CodeObjects::MethodObject, YARD::CodeObjects::ClassObject] yard_object YARD object
|
78
|
-
# @return [Array(YARD::Tags::Tag)]
|
79
|
-
def self.select_runnable_tags(yard_object)
|
80
|
-
yard_object.tags.select { |t| t.tag_name == RUNNABLE_TAG.to_s }
|
81
|
-
end
|
82
|
-
|
83
83
|
# Select all @option tags from of specified object
|
84
84
|
#
|
85
85
|
# @param [YARD::CodeObjects::MethodObject, YARD::CodeObjects::ClassObject] yard_object YARD object
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: console_runner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Karpovich
|
@@ -163,7 +163,6 @@ files:
|
|
163
163
|
- lib/console_runner_error.rb
|
164
164
|
- lib/file_parser.rb
|
165
165
|
- lib/method_parser.rb
|
166
|
-
- lib/runner.rb
|
167
166
|
homepage: https://github.com/yuri-karpovich/console_runner
|
168
167
|
licenses:
|
169
168
|
- MIT
|
data/lib/runner.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
module Runner
|
2
|
-
|
3
|
-
def self.run
|
4
|
-
start_time = Time.now
|
5
|
-
puts "Start Time: #{start_time}"
|
6
|
-
Thread.current[:id] = 'main'
|
7
|
-
|
8
|
-
yield
|
9
|
-
|
10
|
-
trap 'SIGCHLD' do
|
11
|
-
loop do
|
12
|
-
pid = Process.waitpid(-1, Process::WNOHANG) rescue nil
|
13
|
-
break unless pid
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
finish_time = Time.now
|
18
|
-
puts "Finish Time: #{finish_time} (Duration: #{((finish_time - start_time) / 60).round(2) } minutes)"
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|