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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e60a3b7f4eac3e952a5d3e75fccd3e257253721
4
- data.tar.gz: 10098a2cdeb54901b0f3b191b9ad0fadf9d0868c
3
+ metadata.gz: 39d8e37c222218501547763179826453d99b5208
4
+ data.tar.gz: 2cdc022889b7ce2bfa05ed217b4fa77a333ec5a4
5
5
  SHA512:
6
- metadata.gz: 3c14f4e5dd3c23510073f83e5afc77293e6136d62586c40d9d96e67ddb06ebcb6b2c7dd69837a62872b1b97345f618704012ef3bc591c5146a7e93f39acbeace
7
- data.tar.gz: 5c523a463ef96312d304e7b1d98886545ed1a83aaed01a58eb18436e94c910c5c32eacb49b0531c5c43a011514a39827e32f517ac9691ae271091b186a7b767b
6
+ metadata.gz: 581eca4768176a9458c70cde0e670b187cb0462215ef7d94cf9165f88efc7bac233a5d87e8a1b1c8b922d5d67acd1e389c8d9185c440392dfc35567ebae90044
7
+ data.tar.gz: 6127da22aeeb5bf37d8b0a65e697d5b82325e7be977683274f9c927aede4198fc18177256ecb41e95642b228d5a24912eeb447cdcd5e7abf1d0fcab328f2a6df
@@ -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 = file_parser
12
- @sub_commands = @file_parser.runnable_methods.map { |m| m.name.to_s }
13
- @parser = Trollop::Parser.new
14
- @parser.banner(FileParser.select_runnable_tags(@file_parser.clazz).map { |t| (t.text + "\n") }.join("\n"))
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 run(action)
19
- if %w(-h --help).include?(ARGV[0].strip)
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
- unless @sub_commands.include?(ARGV[0].strip)
23
- raise ConsoleRunnerError, "You must provide one of available actions: #{@sub_commands.join ', '}"
24
- end
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
- @init_method = nil
27
- @init_method = MethodParser.new(@file_parser.initialize_method) if @file_parser.initialize_method
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
- unless method.cmd_opts[required_param.to_sym]
46
- raise ConsoleRunnerError, "You must specify required parameter: #{required_param}"
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
@@ -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
- file_from_arg = ARGV.shift
9
- raise ConsoleRunnerError, 'Specify file to be executed' unless file_from_arg
10
- file_path = File.realpath file_from_arg
11
- file_parser = FileParser.new(file_path)
12
- cmd = ARGV[0]
13
- actions = file_parser.runnable_methods.select { |m| m.name.to_s == cmd }
14
- if actions.count > 1
15
- raise(
16
- ConsoleRunnerError,
17
- "Class and Instance methods have the same name (#{cmd}). Actions names should be unique"
18
- )
19
- end
20
- action = actions.first
21
- action ||= file_parser.run_method
22
- trol_config = CommandLineParser.new(file_parser)
23
- trol_config.run(action)
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
- puts '======================================================='
27
- puts 'Global options:'
28
- if file_parser.initialize_method
29
- puts "INIT: #{file_parser.initialize_method.name}"
30
- puts 'INIT options:'
31
- puts trol_config.init_method.cmd_opts.map { |k, v| " #{k} = #{v}" }.join("\n")
32
- end
33
- puts "Subcommand: #{action.name}"
34
- puts 'Subcommand options:'
35
- puts trol_config.method.cmd_opts.map { |k, v| " #{k} = #{v}" }.join("\n")
36
- puts "Remaining arguments: #{ARGV.inspect}" if ARGV != []
37
- puts '======================================================='
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 = trol_config.method.params_array
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 = trol_config.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
@@ -1,3 +1,3 @@
1
1
  module ConsoleRunner
2
- VERSION = '0.1.13'
2
+ VERSION = '0.1.14'
3
3
  end
@@ -1 +1,9 @@
1
- class ConsoleRunnerError < Exception; end
1
+ class ConsoleRunnerError < StandardError
2
+
3
+ unless ENV['DEBUG'].to_s == 'true'
4
+ def backtrace
5
+ @object
6
+ end
7
+ end
8
+
9
+ end
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.13
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