1212require 'multi_json'
1313require 'coderay'
1414require 'pry'
15+ require_relative 'generator/files_helper'
1516
1617module 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
195190end
0 commit comments