Skip to content

Commit c8ede6c

Browse files
committed
[API] Code generator: Refactors build hash code into helper
1 parent 6b594a1 commit c8ede6c

File tree

2 files changed

+68
-33
lines changed

2 files changed

+68
-33
lines changed

elasticsearch-api/utils/thor/generate_source.rb

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
require 'multi_json'
2323
require 'coderay'
2424
require 'pry'
25+
require_relative 'generator/build_hash_helper'
2526
require_relative 'generator/files_helper'
2627
require_relative './endpoint_spec'
2728

@@ -47,14 +48,10 @@ class SourceGenerator < Thor
4748
method_option :tests, type: :boolean, default: false, desc: 'Generate test files'
4849

4950
def generate
50-
@build_hash = if ENV['BUILD_HASH']
51-
File.read(File.expand_path('../../../tmp/rest-api-spec/build_hash',__dir__))
52-
else
53-
original_build_hash
54-
end
55-
51+
@build_hash = BuildHashHelper.build_hash
5652
self.class.source_root File.expand_path(__dir__)
5753
generate_source
54+
5855
# -- Tree output
5956
print_tree if options[:verbose]
6057
end
@@ -88,33 +85,7 @@ def generate_source
8885
end
8986

9087
run_rubocop
91-
add_hash
92-
end
93-
94-
def add_hash
95-
Dir.glob("#{FilesHelper.output_dir}/**/*.rb").each do |file|
96-
content = File.read(file)
97-
new_content = content.gsub(/(^#\sunder\sthe\sLicense.\n#)/) do |_|
98-
match = Regexp.last_match
99-
"#{match[1]}\n#{build_hash_comment}"
100-
end
101-
File.open(file, 'w') { |f| f.puts new_content }
102-
end
103-
end
104-
105-
def build_hash_comment
106-
[
107-
"Auto generated from build hash #{@build_hash}",
108-
'@see https://github.com/elastic/elasticsearch/tree/main/rest-api-spec',
109-
''
110-
].map { |b| "# #{b}" }.join("\n").strip
111-
end
112-
113-
def original_build_hash
114-
content = File.read("#{FilesHelper.output_dir}/info.rb")
115-
return unless (match = content.match(/Auto generated from build hash ([a-f0-9]+)/))
116-
117-
match[1]
88+
BuildHashHelper.add_hash(@build_hash)
11889
end
11990

12091
# Create the hierarchy of directories based on API namespaces
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
module Elasticsearch
19+
module API
20+
# Helper for the Elasticsearch build hash in source code docs
21+
module BuildHashHelper
22+
class << self
23+
def build_hash
24+
if ENV['BUILD_HASH']
25+
File.read(File.expand_path('../../../tmp/rest-api-spec/build_hash',__dir__))
26+
else
27+
original_build_hash
28+
end
29+
end
30+
31+
def add_hash(build_hash)
32+
Dir.glob("#{FilesHelper.output_dir}/**/*.rb").each do |file|
33+
content = File.read(file)
34+
new_content = content.gsub(/(^#\sunder\sthe\sLicense.\n#)/) do |_|
35+
match = Regexp.last_match
36+
"#{match[1]}\n#{build_hash_comment(build_hash)}"
37+
end
38+
File.open(file, 'w') { |f| f.puts new_content }
39+
end
40+
end
41+
42+
def build_hash_comment(build_hash)
43+
[
44+
"Auto generated from build hash #{build_hash}",
45+
'@see https://github.com/elastic/elasticsearch/tree/main/rest-api-spec',
46+
''
47+
].map { |b| "# #{b}" }.join("\n").strip
48+
end
49+
50+
private
51+
52+
def original_build_hash
53+
content = File.read("#{FilesHelper.output_dir}/info.rb")
54+
55+
return unless (match = content.match(/Auto generated from build hash ([a-f0-9]+)/))
56+
57+
match[1]
58+
rescue
59+
return 'Unavailable'
60+
end
61+
end
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)