Module: MiniCli::BaseModule::ClassMethods

Defined in:
lib/mini_cli/base_module.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(given_args = ARGV, config = {}) ⇒ Object

Shows execution time to the end of the run



54
55
56
57
58
59
# File 'lib/mini_cli/base_module.rb', line 54

def self.start(given_args = ARGV, config = {})
  config[:shell] ||= Thor::Base.shell.new
  require 'benchmark'
  bm = Benchmark.measure { |x| super given_args, config }
  config[:shell].say "\n#{bm.total.round(3)}s task run time"
end

Instance Method Details

#add_auto_rerunObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mini_cli/base_module.rb', line 22

def add_auto_rerun
  desc 'auto [COMMAND]', 'Re-runs the given command on any file change'
  long_desc 'Re-runs the given command on any file change.' +
    " I. e. \"#{$0} auto test\" will run the tests on each file save."
  define_method :auto do |*args|
    require 'rerun'

    options = Rerun::Options.parse [
      '--background', 
      '--name', "./cli #{args.first}",
      '--signal', 'ABRT',
      '--ignore', 'coverage/*',
    ]

    Rerun::Runner.keep_running("./cli #{args.join " "}", options)
  end
end

#add_console_pryObject



5
6
7
8
9
10
11
# File 'lib/mini_cli/base_module.rb', line 5

def add_console_pry
  desc 'console', 'Pry console with the app available'
  define_method :console do
    require'pry'
    Pry.start
  end
end

#add_start_puma(opts = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mini_cli/base_module.rb', line 40

def add_start_puma opts = {}
  opts = opts.dup
  puma_args = opts.delete(:puma_args) || []
  raise("Unrecognized options #{opts.inspect}") if opts.keys.any?

  desc 'start', 'Starts the Puma and any other required thread'
  define_method :start do
    require 'puma/cli'
    Puma::CLI.new(puma_args).run
  end
end

#add_startup_benchmarkObject



52
53
54
55
56
57
58
59
60
# File 'lib/mini_cli/base_module.rb', line 52

def add_startup_benchmark
  # Shows execution time to the end of the run
  def self.start(given_args = ARGV, config = {})
    config[:shell] ||= Thor::Base.shell.new
    require 'benchmark'
    bm = Benchmark.measure { |x| super given_args, config }
    config[:shell].say "\n#{bm.total.round(3)}s task run time"
  end
end

#add_test_rspecObject



13
14
15
16
17
18
19
20
# File 'lib/mini_cli/base_module.rb', line 13

def add_test_rspec
  desc 'test', 'Run the test suite'
  define_method :test do |*args|
    args = ['spec'] if args.empty?
    require 'rspec'
    RSpec::Core::Runner.run(args)
  end
end