Class: A2A::Rails::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/a2a/rails/generators/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#a2a_versionObject (private)



203
204
205
# File 'lib/a2a/rails/generators/install_generator.rb', line 203

def a2a_version
  A2A::VERSION
end

#add_gem_dependenciesObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/a2a/rails/generators/install_generator.rb', line 88

def add_gem_dependencies
  gem_content = []

  case options[:storage]
  when "database"
    gem_content << "# A2A database storage dependencies"
    gem_content << "gem 'activerecord', '>= 6.0'"
  when "redis"
    gem_content << "# A2A Redis storage dependencies"
    gem_content << "gem 'redis', '~> 5.0'"
    gem_content << "gem 'connection_pool', '~> 2.4'"
  end

  if options[:with_authentication]
    gem_content << "# A2A authentication dependencies"
    gem_content << "gem 'jwt', '~> 2.0'"
  end

  return unless gem_content.any?

  append_to_file "Gemfile" do
    "\n# A2A Ruby SDK dependencies\n#{gem_content.join("\n")}\n"
  end
  say "Added gem dependencies to Gemfile", :green
  say "Run 'bundle install' to install new dependencies", :yellow
end

#add_routesObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/a2a/rails/generators/install_generator.rb', line 47

def add_routes
  return if options[:skip_routes]

  route_content = generate_route_content

  if File.exist?("config/routes.rb")
    inject_into_file "config/routes.rb", after: "Rails.application.routes.draw do\n" do
      route_content
    end
    say "Added A2A routes", :green
  else
    say "Could not find config/routes.rb", :red
  end
end

#authentication_strategyObject (private)



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/a2a/rails/generators/install_generator.rb', line 166

def authentication_strategy
  if options[:with_authentication]
    # Try to detect existing authentication gems
    if gem_exists?("devise")
      "devise"
    elsif gem_exists?("jwt")
      "jwt"
    else
      "api_key"
    end
  else
    "none"
  end
end

#create_application_controller_exampleObject



62
63
64
65
66
67
68
# File 'lib/a2a/rails/generators/install_generator.rb', line 62

def create_application_controller_example
  return unless options[:with_authentication]

  template "application_controller_with_auth.rb",
           "app/controllers/concerns/a2a_authentication.rb"
  say "Created A2A authentication concern", :green
end

#create_database_storageObject (private)



151
152
153
154
155
# File 'lib/a2a/rails/generators/install_generator.rb', line 151

def create_database_storage
  template "migration.rb",
           "db/migrate/#{migration_timestamp}_create_a2a_tables.rb"
  say "Created A2A database migration", :green
end

#create_example_agentObject



70
71
72
73
74
# File 'lib/a2a/rails/generators/install_generator.rb', line 70

def create_example_agent
  template "example_agent_controller.rb",
           "app/controllers/example_agent_controller.rb"
  say "Created example A2A agent controller", :green
end

#create_initializerObject



40
41
42
43
44
45
# File 'lib/a2a/rails/generators/install_generator.rb', line 40

def create_initializer
  return if options[:skip_initializer]

  template "initializer.rb", "config/initializers/a2a.rb"
  say "Created A2A initializer", :green
end

#create_redis_storageObject (private)



157
158
159
160
# File 'lib/a2a/rails/generators/install_generator.rb', line 157

def create_redis_storage
  template "redis_config.yml", "config/redis.yml"
  say "Created Redis configuration", :green
end

#create_storage_configurationObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/a2a/rails/generators/install_generator.rb', line 76

def create_storage_configuration
  case options[:storage]
  when "database"
    create_database_storage
  when "redis"
    create_redis_storage
  else
    # Memory storage is default, no additional setup needed
    say "Using in-memory storage (default)", :yellow
  end
end

#gem_exists?(gem_name) ⇒ Boolean (private)

Returns:



181
182
183
184
185
# File 'lib/a2a/rails/generators/install_generator.rb', line 181

def gem_exists?(gem_name)
  File.read("Gemfile").include?(gem_name) if File.exist?("Gemfile")
rescue StandardError
  false
end

#generate_route_contentObject (private)



140
141
142
143
144
145
146
147
148
149
# File 'lib/a2a/rails/generators/install_generator.rb', line 140

def generate_route_content
  mount_path = options[:mount_path]

  "\n    # A2A Protocol endpoints\n    mount A2A::Rails::Engine => \"\#{mount_path}\"\n\n  RUBY\nend\n"

#migration_timestampObject (private)



162
163
164
# File 'lib/a2a/rails/generators/install_generator.rb', line 162

def migration_timestamp
  Time.now.strftime("%Y%m%d%H%M%S")
end

#mount_pathObject (private)



191
192
193
# File 'lib/a2a/rails/generators/install_generator.rb', line 191

def mount_path
  options[:mount_path]
end

#rails_versionObject (private)



199
200
201
# File 'lib/a2a/rails/generators/install_generator.rb', line 199

def rails_version
  ::Rails.version
end

#show_post_install_instructionsObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/a2a/rails/generators/install_generator.rb', line 115

def show_post_install_instructions
  say "\n#{'=' * 60}", :green
  say "A2A Rails integration installed successfully!", :green
  say "=" * 60, :green

  say "\nNext steps:", :yellow
  say "1. Run 'bundle install' if new gems were added"
  say "2. Review and customize config/initializers/a2a.rb"
  say "3. Check the example agent at app/controllers/example_agent_controller.rb"

  if options[:storage] == "database"
    say "4. Run 'rails generate a2a:migration' to create database tables"
    say "5. Run 'rails db:migrate' to apply migrations"
  end

  say "\nA2A endpoints will be available at:", :yellow
  say "  #{options[:mount_path]}/rpc (JSON-RPC)"
  say "  #{options[:mount_path]}/agent-card (Agent Card)"
  say "  #{options[:mount_path]}/health (Health Check)"

  say "\nFor more information, visit: https://a2a-protocol.org/sdk/ruby/", :blue
end

#storage_backendObject (private)



187
188
189
# File 'lib/a2a/rails/generators/install_generator.rb', line 187

def storage_backend
  options[:storage]
end

#with_authentication?Boolean (private)

Returns:



195
196
197
# File 'lib/a2a/rails/generators/install_generator.rb', line 195

def with_authentication?
  options[:with_authentication]
end