Skip to content

Commit ffa3d84

Browse files
committed
[API] Generator: Refactor - remove bash script, move to Thor task
1 parent 8c6eefb commit ffa3d84

File tree

4 files changed

+86
-65
lines changed

4 files changed

+86
-65
lines changed

elasticsearch-api/utils/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ gem 'multi_json'
1111
gem 'thor'
1212
gem 'json'
1313
gem 'pry'
14+
gem 'rubocop'

elasticsearch-api/utils/thor/generate_source.rb

Lines changed: 55 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require 'multi_json'
1313
require 'coderay'
1414
require 'pry'
15+
require_relative 'generator/files_helper'
1516

1617
module Elasticsearch
1718
module API
@@ -23,39 +24,23 @@ module API
2324
#
2425
class SourceGenerator < Thor
2526
namespace 'api:code'
26-
2727
include Thor::Actions
2828

29-
SRC_PATH = '../../../../tmp/elasticsearch/rest-api-spec/src/main/resources/rest-api-spec/api/'.freeze
30-
__root = Pathname( File.expand_path('../../..', __FILE__) )
31-
32-
desc "generate", "Generate source code and tests from the REST API JSON specification"
33-
method_option :force, type: :boolean, default: false, desc: 'Overwrite the output'
34-
method_option :verbose, type: :boolean, default: false, desc: 'Output more information'
35-
method_option :input, default: File.expand_path(SRC_PATH, __FILE__), desc: 'Path to directory with JSON API specs'
36-
method_option :output, default: File.expand_path('../../../tmp/out', __FILE__), desc: 'Path to output directory'
37-
38-
def generate(*files)
39-
self.class.source_root File.expand_path('../', __FILE__)
29+
__root = Pathname(File.expand_path('../../..', __FILE__))
4030

41-
@input = Pathname(options[:input])
42-
@output = Pathname(options[:output])
31+
desc 'generate', 'Generate source code and tests from the REST API JSON specification'
32+
method_option :verbose, type: :boolean, default: false, desc: 'Output more information'
33+
method_option :tests, type: :boolean, default: false, desc: 'Generate test files'
4334

44-
# -- Test helper
45-
copy_file "templates/test_helper.rb", @output.join('test').join('test_helper.rb')
35+
def generate
36+
self.class.source_root File.expand_path(__dir__)
37+
@input = FilesHelper.input_dir
38+
@output = FilesHelper.output_dir
4639

47-
# Remove unwanted files
48-
files = Dir.entries(@input.to_s).reject do |f|
49-
f.start_with?('.') ||
50-
f.start_with?('_') ||
51-
File.extname(f) != '.json'
52-
end
53-
54-
files.each do |filepath|
55-
file = @input.join(filepath)
56-
@path = Pathname(file)
57-
@json = MultiJson.load( File.read(@path) )
58-
@spec = @json.values.first
40+
FilesHelper.files.each do |filepath|
41+
@path = Pathname(@input.join(filepath))
42+
@json = MultiJson.load(File.read(@path))
43+
@spec = @json.values.first
5944
say_status 'json', @path, :yellow
6045

6146
@spec['url'] ||= {}
@@ -74,45 +59,25 @@ def generate(*files)
7459
@http_path = __http_path
7560
@required_parts = __required_parts
7661

77-
@path_to_file = @output.join('api').join( @module_namespace.join('/') ).join("#{@method_name}.rb")
78-
79-
empty_directory @output.join('api').join( @module_namespace.join('/') )
62+
@path_to_file = @output.join(@module_namespace.join('/')).join("#{@method_name}.rb")
8063

81-
template "templates/method.erb", @path_to_file
64+
dir = @output.join(@module_namespace.join('/'))
65+
empty_directory(dir, verbose: false)
8266

83-
if options[:verbose]
84-
colorized_output = CodeRay.scan_file(@path_to_file, :ruby).terminal
85-
lines = colorized_output.split("\n")
86-
say_status 'ruby',
87-
lines.first + "\n" + lines[1, lines.size].map { |l| ' '*14 + l }.join("\n"),
88-
:yellow
89-
end
67+
# Write the file with the ERB template:
68+
template('templates/method.erb', @path_to_file, { force: true })
9069

91-
# --- Test files
70+
print_source_code(@path_to_file) if options[:verbose]
9271

93-
@test_directory = @output.join('test/api').join( @module_namespace.join('/') )
94-
@test_file = @test_directory.join("#{@method_name}_test.rb")
72+
generate_tests if options[:tests]
9573

96-
empty_directory @test_directory
97-
template "templates/test.erb", @test_file
98-
99-
if options[:verbose]
100-
colorized_output = colorized_output = CodeRay.scan_file(@test_file, :ruby).terminal
101-
lines = colorized_output.split("\n")
102-
say_status 'ruby',
103-
lines.first + "\n" + lines[1, lines.size].map { |l| ' '*14 + l }.join("\n"),
104-
:yellow
105-
say '▬'*terminal_width
106-
end
74+
puts
10775
end
10876

109-
# -- Tree output
77+
run_rubocop
11078

111-
if options[:verbose] && `which tree > /dev/null 2>&1; echo $?`.to_i < 1
112-
lines = `tree #{@output}`.split("\n")
113-
say_status 'tree',
114-
lines.first + "\n" + lines[1, lines.size].map { |l| ' '*14 + l }.join("\n")
115-
end
79+
# -- Tree output
80+
print_tree if options[:verbose]
11681
end
11782

11883
private
@@ -182,14 +147,44 @@ def __extract_path_variables(path)
182147
#
183148
def __required_parts
184149
required = []
185-
# TODO Looks like this is not right:
186150
required << 'body' if (@spec['body'] && @spec['body']['required'])
187151

188152
# Get required variables from paths:
189153
req_variables = __path_variables.inject(:&) # find intersection
190154
required << req_variables unless req_variables.empty?
191155
required.flatten
192156
end
157+
158+
def generate_tests
159+
copy_file 'templates/test_helper.rb', @output.join('test').join('test_helper.rb')
160+
161+
@test_directory = @output.join('test/api').join(@module_namespace.join('/'))
162+
@test_file = @test_directory.join("#{@method_name}_test.rb")
163+
164+
empty_directory @test_directory
165+
template 'templates/test.erb', @test_file
166+
167+
print_source_code(@test_file) if options[:verbose]
168+
end
169+
170+
def print_source_code(path_to_file)
171+
colorized_output = CodeRay.scan_file(path_to_file, :ruby).terminal
172+
lines = colorized_output.split("\n")
173+
formatted = lines.first + "\n" + lines[1, lines.size].map { |l| ' ' * 14 + l }.join("\n")
174+
175+
say_status('ruby', formatted, :yellow)
176+
end
177+
178+
def print_tree
179+
return unless `which tree > /dev/null 2>&1; echo $?`.to_i < 1
180+
181+
lines = `tree #{@output}`.split("\n")
182+
say_status('tree', lines.first + "\n" + lines[1, lines.size].map { |l| ' ' * 14 + l }.join("\n"))
183+
end
184+
185+
def run_rubocop
186+
system("rubocop -x #{FilesHelper::OUTPUT_DIR}")
187+
end
193188
end
194189
end
195190
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'pathname'
2+
3+
module Elasticsearch
4+
module API
5+
module FilesHelper
6+
SRC_PATH = '../../../../../tmp/elasticsearch/rest-api-spec/src/main/resources/rest-api-spec/api/'.freeze
7+
OUTPUT_DIR = '../../elasticsearch-api/lib/elasticsearch/api/actions'.freeze
8+
9+
# Path to directory with JSON API specs
10+
def self.input_dir
11+
input_dir = File.expand_path(SRC_PATH, __FILE__)
12+
Pathname(input_dir)
13+
end
14+
15+
# Path to directory to copy generated files
16+
def self.output_dir
17+
Pathname(OUTPUT_DIR)
18+
end
19+
20+
# Only get JSON files and remove hidden files
21+
def self.files
22+
Dir.entries(input_dir.to_s).reject do |f|
23+
f.start_with?('.') ||
24+
f.start_with?('_') ||
25+
File.extname(f) != '.json'
26+
end
27+
end
28+
end
29+
end
30+
end

elasticsearch-api/utils/update_files.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)