Class: MCP::Rails::ServerGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp/rails/server_generator.rb

Defined Under Namespace

Classes: RouteCollector, ServerWriter

Class Method Summary collapse

Class Method Details

.generate_files(config = MCP::Rails.configuration) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mcp/rails/server_generator.rb', line 5

def generate_files(config = MCP::Rails.configuration)
  base_url = config.base_url
  bypass_csrf_key = BypassKeyManager.create_new_key

  all_routes = RouteCollector.collect_routes(::Rails.application.routes.routes).flatten
  grouped_routes = all_routes.group_by { |r| r[:engine] }

  generated_files = []

  # Process main app routes
  main_app_routes = RouteCollector.process_routes(grouped_routes[nil] || [])
  if main_app_routes.any?
    file_path = ServerWriter.write_server(
      main_app_routes,
      config.for_engine(nil),
      base_url,
      bypass_csrf_key
    )
    generated_files << file_path
  end

  # Process each engine's routes
  grouped_routes.each do |engine, routes|
    next unless engine

    engine_routes = RouteCollector.process_routes(routes)
    next unless engine_routes.any?

    engine_config = config.for_engine(engine)
    file_path = ServerWriter.write_server(
      engine_routes,
      engine_config,
      base_url,
      bypass_csrf_key,
      engine
    )
    generated_files << file_path
  end

  generated_files
end