Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduces autoloading strategy for service gems #3098

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,27 @@ def generated_src_warning
GENERATED_SRC_WARNING
end

# @return [String]
def module_name
@service.module_name
end

# @return [Boolean]
def customization_file_exists?
File.exist?("gems/aws-sdk-#{gem_name}/lib/aws-sdk-#{gem_name}/customizations/errors.rb")
end

# @return [String]
def customization_file_path
"aws-sdk-#{gem_name}/customizations/errors"
end

private

# @return [String]
def gem_name
module_name.split('::').last.downcase
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,26 @@ def identifiers?
@identifiers.size > 0
end

# @return [Boolean]
def customization_file_exists?
File.exist?("gems/#{gem_name}/lib/#{gem_name}/customizations/#{underscored_name}.rb")
end

# @return [String]
def resource_customization
"#{gem_name}/customizations/#{underscored_name}"
end

private

def gem_name
"aws-sdk-#{module_name.split('::').last.downcase}"
end

def underscored_name
class_name.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
end

def build_associations(options)
ResourceAssociation.build_list(
class_name: options.fetch(:class_name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def initialize(options)
paginators: options.fetch(:paginators)
)
@custom = options.fetch(:custom)
@name = @module_name.split('::').last.downcase
end

# @return [String]
Expand Down Expand Up @@ -53,6 +54,16 @@ def documentation?
actions? || associations?
end

# @return [Boolean]
def customization_file_exists?
File.exist?("gems/aws-sdk-#{@name}/lib/aws-sdk-#{@name}/customizations/resource.rb")
end

# @return [String]
def customization_file_path
"aws-sdk-#{@name}/customizations/resource"
end

end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ServiceModule < View
def initialize(options)
@service = options.fetch(:service)
@prefix = options.fetch(:prefix)
@codegenerated_plugins = options.fetch(:codegenerated_plugins)
@codegenerated_plugins = options.fetch(:codegenerated_plugins) || []
end

# @return [String|nil]
Expand Down Expand Up @@ -61,15 +61,13 @@ def require_core_guard?
end

# @return [Array<String>]
def relative_requires
def autoloads
paths = Set.new
paths << "#{@prefix}/types"
paths << "#{@prefix}/client_api"

# these must be required before the client
if @codegenerated_plugins
paths += @codegenerated_plugins.map { | p| p.path }
end
paths += @codegenerated_plugins.map { |p| p.path }

paths << "#{@prefix}/client"
paths << "#{@prefix}/errors"
Expand All @@ -94,13 +92,32 @@ def relative_requires
end
paths << "#{@prefix}/customizations"
if @service.api['metadata']['protocolSettings'] &&
@service.api['metadata']['protocolSettings']['h2'] == 'eventstream'
@service.api['metadata']['protocolSettings']['h2'] == 'eventstream'
paths << "#{@prefix}/async_client"
paths << "#{@prefix}/event_streams"
elsif eventstream_shape?
paths << "#{@prefix}/event_streams"
end
paths.to_a

plugin_paths = @codegenerated_plugins.map { |p| [p.path, p] }.to_h || {}

results = paths.map do |path|
class_name = File.basename(path).split('.').first.split('_').map(&:capitalize).join

# Handle the Db -> DB case for AWS database-related constants
class_name = class_name.gsub(/Db(?=[A-Z]|$)/, 'DB')
{
file_path: path,
class_name: class_name,
is_plugin: plugin_paths.key?(path)
}
end

results.reject { |r| r[:class_name].include?('Customizations') }
end

def service_identifier
@prefix
end

def example_var_name
Expand All @@ -109,6 +126,7 @@ def example_var_name

def example_operation_name
raise "no operations found for the service" if @service.api['operations'].empty?

underscore(@service.api['operations'].keys.first)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,20 @@ def eventstreams
end
end

# @return [Array<String>]
def types_customizations
Dir.glob(File.join("gems/#{gem_name}/lib/#{gem_name}/customizations/types", '*.rb')).map do |file|
filename = File.basename(file, '.rb')
"#{gem_name}/customizations/types/#{filename}"
end
end

private

def gem_name
"aws-sdk-#{module_name.split('::').last.downcase}"
end

def struct_members(shape)
return if shape['members'].nil?
members = shape['members'].map do |member_name, member_ref|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
{{#generated_src_warning}}
{{generated_src_warning}}
{{/generated_src_warning}}

require_relative 'types'

module {{module_name}}
# @api private
module ClientApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@ module {{module_name}}
{{/errors}}
end
end
{{#customization_file_exists?}}

# Load customizations if they exist
require '{{customization_file_path}}'
{{/customization_file_exists?}}
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,8 @@ module {{module_name}}
{{/batch_actions?}}
end
end
{{#customization_file_exists?}}

# Load customizations if they exist
require '{{resource_customization}}'
{{/customization_file_exists?}}
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ module {{module_name}}

end
end
{{#customization_file_exists?}}

# Load customizations if they exist
require '{{customization_file_path}}'
{{/customization_file_exists?}}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ require '{{.}}'
{{/requires}}
{{/require_core_guard?}}

{{#relative_requires}}
require_relative '{{.}}'
{{/relative_requires}}
require_relative '{{service_identifier}}/customizations'
require_relative '{{service_identifier}}/railtie' if defined?(Rails::Railtie)

# This module provides support for {{full_name}}. This module is available in the
# `{{gem_name}}` gem.
Expand Down Expand Up @@ -52,6 +51,16 @@ require_relative '{{.}}'
#
# @!group service
module {{module_name}}
{{#autoloads}}
{{#is_plugin}}
module Plugins
autoload :{{class_name}}, '{{file_path}}'
end
{{/is_plugin}}
{{^is_plugin}}
autoload :{{class_name}}, '{{file_path}}'
{{/is_plugin}}
{{/autoloads}}

GEM_VERSION = '{{gem_version}}'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ module {{module_name}}

end
end
{{#types_customizations}}
require "{{.}}"
{{/types_customizations}}
19 changes: 19 additions & 0 deletions gems/aws-sdk-accessanalyzer/lib/aws-sdk-accessanalyzer/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Aws
module AccessAnalyzer
class Railtie < Rails::Railtie
initializer 'aws-sdk-accessanalyzer.eager_load' do
config.before_eager_load do
config.eager_load_namespaces << Aws::AccessAnalyzer
end
end
end

def self.eager_load!
constants.each do |constant|
const_get(constant)
end
end
end
end
19 changes: 19 additions & 0 deletions gems/aws-sdk-account/lib/aws-sdk-account/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Aws
module Account
class Railtie < Rails::Railtie
initializer 'aws-sdk-account.eager_load' do
config.before_eager_load do
config.eager_load_namespaces << Aws::Account
end
end
end

def self.eager_load!
constants.each do |constant|
const_get(constant)
end
end
end
end
19 changes: 19 additions & 0 deletions gems/aws-sdk-acm/lib/aws-sdk-acm/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Aws
module ACM
class Railtie < Rails::Railtie
initializer 'aws-sdk-acm.eager_load' do
config.before_eager_load do
config.eager_load_namespaces << Aws::ACM
end
end
end

def self.eager_load!
constants.each do |constant|
const_get(constant)
end
end
end
end
19 changes: 19 additions & 0 deletions gems/aws-sdk-acmpca/lib/aws-sdk-acmpca/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Aws
module ACMPCA
class Railtie < Rails::Railtie
initializer 'aws-sdk-acmpca.eager_load' do
config.before_eager_load do
config.eager_load_namespaces << Aws::ACMPCA
end
end
end

def self.eager_load!
constants.each do |constant|
const_get(constant)
end
end
end
end
19 changes: 19 additions & 0 deletions gems/aws-sdk-amplify/lib/aws-sdk-amplify/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Aws
module Amplify
class Railtie < Rails::Railtie
initializer 'aws-sdk-amplify.eager_load' do
config.before_eager_load do
config.eager_load_namespaces << Aws::Amplify
end
end
end

def self.eager_load!
constants.each do |constant|
const_get(constant)
end
end
end
end
19 changes: 19 additions & 0 deletions gems/aws-sdk-amplifybackend/lib/aws-sdk-amplifybackend/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Aws
module AmplifyBackend
class Railtie < Rails::Railtie
initializer 'aws-sdk-amplifybackend.eager_load' do
config.before_eager_load do
config.eager_load_namespaces << Aws::AmplifyBackend
end
end
end

def self.eager_load!
constants.each do |constant|
const_get(constant)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Aws
module AmplifyUIBuilder
class Railtie < Rails::Railtie
initializer 'aws-sdk-amplifyuibuilder.eager_load' do
config.before_eager_load do
config.eager_load_namespaces << Aws::AmplifyUIBuilder
end
end
end

def self.eager_load!
constants.each do |constant|
const_get(constant)
end
end
end
end
Loading
Loading