Skip to content

Commit 8c70f00

Browse files
committed
[API] Generator: Generates all spec code in elasticsearch-api
1 parent 0d9f91b commit 8c70f00

File tree

4 files changed

+33
-71
lines changed

4 files changed

+33
-71
lines changed

Diff for: elasticsearch-api/utils/thor/generate_source.rb

+16-32
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module API
4040
# here.
4141
#
4242
class SourceGenerator < Thor
43-
namespace 'api:code'
43+
namespace 'code'
4444
include Thor::Actions
4545
include EndpointSpecifics
4646

@@ -49,26 +49,20 @@ class SourceGenerator < Thor
4949
desc 'generate', 'Generate source code and tests from the REST API JSON specification'
5050
method_option :verbose, type: :boolean, default: false, desc: 'Output more information'
5151
method_option :tests, type: :boolean, default: false, desc: 'Generate test files'
52-
method_option :api, type: :array, default: %w[oss xpack], desc: 'APIs to generate (oss, x-pack)'
5352

5453
def generate
5554
self.class.source_root File.expand_path(__dir__)
56-
@xpack = options[:api].include? 'xpack'
57-
@oss = options[:api].include? 'oss'
58-
59-
__generate_source(:xpack) if @xpack
60-
__generate_source(:oss) if @oss
55+
generate_source
6156
# -- Tree output
6257
print_tree if options[:verbose]
6358
end
6459

6560
private
6661

67-
def __generate_source(api)
68-
@current_api = api
69-
@output = FilesHelper.output_dir(api)
62+
def generate_source
63+
@output = FilesHelper.output_dir
7064

71-
FilesHelper.files(api).each do |filepath|
65+
FilesHelper.files.each do |filepath|
7266
@path = Pathname(filepath)
7367
@json = MultiJson.load(File.read(@path))
7468
@spec = @json.values.first
@@ -91,7 +85,6 @@ def __generate_source(api)
9185
@http_path = __http_path
9286
@required_parts = __required_parts
9387

94-
@module_namespace.shift if @module_namespace.first == 'xpack'
9588
@path_to_file = @output.join(@module_namespace.join('/')).join("#{@method_name}.rb")
9689
dir = @output.join(@module_namespace.join('/'))
9790
empty_directory(dir, verbose: false)
@@ -106,23 +99,18 @@ def __generate_source(api)
10699
puts
107100
end
108101

109-
run_rubocop(api)
102+
run_rubocop
110103
end
111104

112105
def __full_namespace
113106
names = @endpoint_name.split('.')
114-
if @current_api == :xpack
115-
names = (names.first == 'xpack' ? names : ['xpack', names].flatten)
116-
# Return an array to expand 'ccr', 'ilm', 'ml' and 'slm'
117-
names.map do |name|
118-
name
119-
.gsub(/^ml$/, 'machine_learning')
120-
.gsub(/^ilm$/, 'index_lifecycle_management')
121-
.gsub(/^ccr/, 'cross_cluster_replication')
122-
.gsub(/^slm/, 'snapshot_lifecycle_management')
123-
end
124-
else
125-
names
107+
# Return an array to expand 'ccr', 'ilm', 'ml' and 'slm'
108+
names.map do |name|
109+
name
110+
.gsub(/^ml$/, 'machine_learning')
111+
.gsub(/^ilm$/, 'index_lifecycle_management')
112+
.gsub(/^ccr/, 'cross_cluster_replication')
113+
.gsub(/^slm/, 'snapshot_lifecycle_management')
126114
end
127115
end
128116

@@ -194,7 +182,7 @@ def __http_path
194182
def __parse_path(path)
195183
path.gsub(/^\//, '')
196184
.gsub(/\/$/, '')
197-
.gsub('{', "\#{#{__utils}.__listify(_")
185+
.gsub('{', "\#{Utils.__listify(_")
198186
.gsub('}', ')}')
199187
end
200188

@@ -288,12 +276,8 @@ def print_tree
288276
say_status('tree', lines.first + "\n" + lines[1, lines.size].map { |l| ' ' * 14 + l }.join("\n"))
289277
end
290278

291-
def __utils
292-
(@current_api == :xpack) ? 'Elasticsearch::API::Utils' : 'Utils'
293-
end
294-
295-
def run_rubocop(api)
296-
system("rubocop -c ./thor/.rubocop.yml --format autogenconf -x #{FilesHelper::output_dir(api)}")
279+
def run_rubocop
280+
system("rubocop -c ./thor/.rubocop.yml --format autogenconf -x #{FilesHelper::output_dir}")
297281
end
298282
end
299283
end

Diff for: elasticsearch-api/utils/thor/generator/files_helper.rb

+13-35
Original file line numberDiff line numberDiff line change
@@ -20,53 +20,26 @@
2020

2121
module Elasticsearch
2222
module API
23+
# Helper with file related methods for code generation
2324
module FilesHelper
2425
PROJECT_PATH = File.join(File.dirname(__FILE__), '..')
2526
SRC_PATH = File.join(PROJECT_PATH, '..', '..', '..', 'tmp/elasticsearch/rest-api-spec/src/main/resources/rest-api-spec/api/')
26-
OSS_OUTPUT_DIR = '../../elasticsearch-api/lib/elasticsearch/api/actions'.freeze
27-
XPACK_OUTPUT_DIR = '../../elasticsearch-xpack/lib/elasticsearch/xpack/api/actions'.freeze
28-
29-
TESTS_DIRECTORIES = {
30-
oss: "#{PROJECT_PATH}/../../../tmp/rest-api-spec/test/free",
31-
xpack: "#{PROJECT_PATH}/../../../tmp/rest-api-spec/test/platinum"
32-
}.freeze
27+
OUTPUT_DIR = '../../elasticsearch-api/lib/elasticsearch/api/actions'.freeze
28+
TESTS_DIRECTORY = "#{PROJECT_PATH}/../../../tmp/rest-api-spec/test/free".freeze
3329

3430
# Only get JSON files and remove hidden files
35-
def self.files(api)
36-
json_files = if api == :xpack
37-
xpack_files
38-
else
39-
Dir.entries(SRC_PATH) - xpack_files
40-
end
31+
def self.files
32+
json_files = Dir.entries(SRC_PATH)
33+
4134
json_files.reject do |file|
4235
File.extname(file) != '.json' ||
4336
File.basename(file) == '_common.json'
4437
end.map { |file| "#{SRC_PATH}#{file}" }
4538
end
4639

47-
XPACK_ENDPOINTS = [
48-
'autoscaling', 'cross_cluster_replication', 'ccr', 'data_frame_transform_deprecated',
49-
'enrich', 'eql', 'fleet', 'ilm', 'logstash', 'migration', 'watcher', 'slm'
50-
]
51-
XPACK_ENDPOINTS_REGEXP = /data_stream|ml_|reload_search_analyzers|transform/
52-
53-
def self.xpack_files
54-
xpack_tests = Dir.entries(TESTS_DIRECTORIES[:xpack])
55-
Dir.entries(SRC_PATH).map do |entry|
56-
filename = entry.split('.').first
57-
next if filename == 'snapshot'
58-
59-
if xpack_tests.include?(filename) ||
60-
XPACK_ENDPOINTS.include?(filename) ||
61-
entry.match?(XPACK_ENDPOINTS_REGEXP)
62-
entry
63-
end
64-
end.compact
65-
end
66-
6740
# Path to directory to copy generated files
68-
def self.output_dir(api)
69-
api == :xpack ? Pathname(XPACK_OUTPUT_DIR) : Pathname(OSS_OUTPUT_DIR)
41+
def self.output_dir
42+
Pathname(OUTPUT_DIR)
7043
end
7144

7245
def self.documentation_url(documentation_url)
@@ -82,6 +55,11 @@ def self.documentation_url(documentation_url)
8255
documentation_url.gsub(/\/(current|master)\//, "/#{version}/")
8356
end
8457
end
58+
59+
def cleanup_output_dir!
60+
FileUtils.remove_dir(OUTPUT_DIR)
61+
Dir.mkdir(OUTPUT_DIR)
62+
end
8563
end
8664
end
8765
end

Diff for: elasticsearch-api/utils/thor/templates/_method_setup.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<%- end -%>
4444
<%= ' '*(@namespace_depth+4) %>path = <%= @http_path %>
4545
<%- unless @params.empty? -%>
46-
<%= ' '*(@namespace_depth+4) %>params = <%= __utils %>.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
46+
<%= ' '*(@namespace_depth+4) %>params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
4747
<%- else -%>
4848
<%= ' '*(@namespace_depth+4) %>params = {}
4949
<%- end -%>

Diff for: elasticsearch-api/utils/thor/templates/_perform_request.erb

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ when 'mtermvectors'
2828
<%- when 'cluster.reroute', 'cluster.put_settings' %>
2929
body = arguments[:body] || {}
3030
<%- when 'ml.find_file_structure' %>
31-
body = <%= __utils %>.__bulkify(arguments.delete(:body))
31+
body = Utils.__bulkify(arguments.delete(:body))
3232
<%- else -%>
3333
<%= ' '*(@namespace_depth+3) %>body = <%= @spec['body'].nil? ? 'nil' : 'arguments[:body]' %>
3434
<%- end -%>
@@ -40,12 +40,12 @@ when 'mtermvectors'
4040
<%= ping_perform_request %>
4141
<%- else -%>
4242
<%- if needs_ignore_404?(@endpoint_name) %>
43-
<%= __utils %>.__rescue_from_not_found do
43+
Utils.__rescue_from_not_found do
4444
perform_request(method, path, params, body, headers).status == 200 ? true : false
4545
end
4646
<%- elsif needs_complex_ignore_404?(@endpoint_name) -%>
4747
if Array(arguments[:ignore]).include?(404)
48-
<%= __utils %>.__rescue_from_not_found { perform_request(method, path, params, body, headers).body }
48+
Utils.__rescue_from_not_found { perform_request(method, path, params, body, headers).body }
4949
else
5050
perform_request(method, path, params, body, headers).body
5151
end

0 commit comments

Comments
 (0)