Skip to content

Commit c82ee58

Browse files
committed
[DOCS] Updates DOCS generator
1 parent bf922a6 commit c82ee58

File tree

1 file changed

+60
-15
lines changed

1 file changed

+60
-15
lines changed

rake_tasks/doc_generator.rake

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
require 'json'
1919
require 'fileutils'
20+
require 'logger'
2021

2122
namespace :docs do
2223
SRC_FILE = "#{__dir__}/docs/parsed_alternative_report.json".freeze
@@ -29,26 +30,33 @@ namespace :docs do
2930
FileUtils.remove_dir(TARGET_DIR)
3031
Dir.mkdir(TARGET_DIR)
3132

32-
# Only select the files in the EXAMPLES_TO_PARSE array and that are not
33-
# console result examples
34-
entries = json_data.select do |d|
35-
EXAMPLES_TO_PARSE.include? d['source_location']['file']
36-
end.select { |d| d['lang'] == 'console' }
37-
38-
entries.each do |entry|
33+
entries = json_data.select { |d| d['lang'] == 'console' }
34+
start_time = Time.now.to_i
35+
entries.each_with_index do |entry, index|
36+
percentage = index * 100 / entries.length
37+
hourglass = index.even? ? '⌛ ' : '⏳ '
38+
print "\r" + ("\e[A\e[K" * 2) if index > 0
39+
puts "📝 Generating file #{index + 1} of #{entries.length} - #{percentage}% complete"
40+
puts hourglass + '▩' * (percentage / 2) + '⬚' * (50 - percentage / 2) + ' ' + hourglass
3941
generate_docs(entry)
4042
end
43+
puts "Finished generating #{entries.length} files in #{Time.now.to_i - start_time} seconds"
4144
end
4245

4346
def json_data
4447
JSON.parse(File.read(SRC_FILE))
4548
end
4649

4750
def generate_docs(entry)
48-
file_name = "#{entry['digest']}.asciidoc"
49-
api = entry['parsed_source'].first['api']
50-
code = build_client_query(api, entry)
51-
write_file(code, file_name)
51+
require 'elasticsearch'
52+
53+
filename = "#{entry['digest']}.asciidoc"
54+
unless entry['parsed_source'].empty?
55+
api = entry['parsed_source'].first['api']
56+
code = build_client_query(api, entry)
57+
TestDocs::perform(code, filename)
58+
write_file(code, filename)
59+
end
5260
end
5361

5462
def self.build_client_query(api, entry)
@@ -61,8 +69,12 @@ namespace :docs do
6169
request_body << show_parameters(params) if params
6270
body = entry&.[]('body')
6371
request_body << show_body(body) if body
64-
request_body = request_body.compact.join(",\n")
65-
code = "response = client.#{api}(\n#{request_body}\n)\nputs response"
72+
request_body = request_body.compact.join(",\n").gsub('null', 'nil')
73+
code = if api.include? '_internal'
74+
"response = client.perform_request('#{entry['method']}', '#{api}', #{request_body})"
75+
else
76+
"response = client.#{api}(\n#{request_body}\n)\nputs response"
77+
end
6678
client_query << format_code(code)
6779
end
6880
client_query.join("\n\n")
@@ -73,8 +85,12 @@ namespace :docs do
7385
File.open('temp.rb', 'w') do |f|
7486
f.puts code
7587
end
76-
# Format code:
77-
system("rubocop --config #{__dir__}/docs_rubocop_config.yml --format autogenconf -a ./temp.rb")
88+
# Format code with Rubocop
89+
require 'rubocop'
90+
options = "--config #{__dir__}/docs_rubocop_config.yml -o /dev/null -a ./temp.rb".split
91+
cli = RuboCop::CLI.new
92+
cli.run(options)
93+
7894
# Read it back
7995
template = File.read('./temp.rb')
8096
File.delete('./temp.rb')
@@ -120,3 +136,32 @@ namespace :docs do
120136
end
121137
end
122138
end
139+
140+
#
141+
# Test module to run the generated code
142+
#
143+
module TestDocs
144+
@formatter = -> (_, d, _, msg) { "[#{d}] : #{msg}" }
145+
146+
def self.perform(code, filename)
147+
# Eval the example code, but remove printing out the response
148+
response = eval(code.gsub('puts response', ''))
149+
if response.status == 200
150+
logger = Logger.new('log/200-ok.log')
151+
logger.formatter = -> (_, _, _, msg) { "#{msg} " }
152+
logger.info(filename)
153+
end
154+
rescue Elastic::Transport::Transport::Error => e
155+
logger = Logger.new('log/docs-generation-elasticsearch.log')
156+
logger.formatter = @formatter
157+
logger.info("Located in #{filename}: #{e.message}\n")
158+
rescue ArgumentError, NoMethodError, TypeError => e
159+
logger = Logger.new('log/docs-generation-client.log')
160+
logger.formatter = @formatter
161+
logger.info("Located in #{filename}: #{e.message}\n")
162+
end
163+
164+
def self.client
165+
@client ||= Elasticsearch::Client.new(trace: false, log: false)
166+
end
167+
end

0 commit comments

Comments
 (0)