Class: Rage::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/rage/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/rage/cli.rb', line 7

def self.exit_on_failure?
  true
end

Instance Method Details

#consoleObject



103
104
105
106
107
108
109
110
111
112
# File 'lib/rage/cli.rb', line 103

def console
  return help("console") if options.help?

  set_env(options)

  require "irb"
  environment
  ARGV.clear
  IRB.start
end

#new(path) ⇒ Object



12
13
14
15
# File 'lib/rage/cli.rb', line 12

def new(path)
  require "rage/all"
  NewAppGenerator.start([path])
end

#routesObject



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rage/cli.rb', line 46

def routes
  return help("routes") if options.help?
  # the result would be something like this:
  # Verb  Path  Controller#Action
  # GET   /     application#index

  # load config/application.rb
  set_env(options)
  environment

  routes = Rage.__router.routes
  pattern = options[:grep]
  routes.unshift({ method: "Verb", path: "Path", meta: { raw_handler: "Controller#Action" } })

  grouped_routes = routes.each_with_object({}) do |route, memo|
    if pattern && !memo.empty?
      next unless route[:path].match?(pattern) || route[:meta][:raw_handler].to_s.match?(pattern) || route[:method].match?(pattern)
    end

    key = [route[:path], route[:meta][:raw_handler]]

    if route[:meta][:mount]
      memo[key] = route.merge(method: "") unless route[:path].end_with?("*")
      next
    end

    if memo[key]
      memo[key][:method] += "|#{route[:method]}"
    else
      memo[key] = route
    end
  end

  longest_path = longest_method = 0
  grouped_routes.each do |_, route|
    longest_path = route[:path].length if route[:path].length > longest_path
    longest_method = route[:method].length if route[:method].length > longest_method
  end

  margin = 3
  longest_path += margin
  longest_method += margin

  grouped_routes.each_with_index do |(_, route), i|
    meta = route[:constraints]
    meta.merge!(route[:defaults]) if route[:defaults]

    handler = route[:meta][:raw_handler]
    handler = "#{handler} #{meta}" unless meta&.empty?

    puts format("%-#{longest_method}s%-#{longest_path}s%s", route[:method], route[:path], handler)
    puts "\n" if i == 0
  end
end

#serverObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rage/cli.rb', line 23

def server
  return help("server") if options.help?

  set_env(options)

  app = ::Rack::Builder.parse_file(options[:config] || "config.ru")
  app = app[0] if app.is_a?(Array)

  port = options[:port] || Rage.config.server.port
  address = options[:binding] || (Rage.env.production? ? "0.0.0.0" : "localhost")
  timeout = Rage.config.server.timeout
  max_clients = Rage.config.server.max_clients

  ::Iodine.listen service: :http, handler: app, port: port, address: address, timeout: timeout, max_clients: max_clients
  ::Iodine.threads = Rage.config.server.threads_count
  ::Iodine.workers = Rage.config.server.workers_count

  ::Iodine.start
end