23
23
require 'coderay'
24
24
require 'pry'
25
25
require_relative 'generator/build_hash_helper'
26
+ require_relative 'generator/docs_helper'
26
27
require_relative 'generator/files_helper'
27
28
require_relative './endpoint_spec'
28
29
@@ -42,6 +43,7 @@ class SourceGenerator < Thor
42
43
namespace 'code'
43
44
include Thor ::Actions
44
45
include EndpointSpecifics
46
+ include DocsHelper
45
47
46
48
desc 'generate' , 'Generate source code and tests from the REST API JSON specification'
47
49
method_option :verbose , type : :boolean , default : false , desc : 'Output more information'
@@ -65,68 +67,24 @@ def generate_source
65
67
FilesHelper . files . each do |filepath |
66
68
@spec = EndpointSpec . new ( filepath )
67
69
say_status 'json' , @spec . path , :yellow
68
-
69
70
# Don't generate code for internal APIs:
70
71
next if @spec . module_namespace . flatten . first == '_internal'
71
72
72
73
path_to_file = output . join ( @spec . module_namespace . join ( '/' ) ) . join ( "#{ @spec . method_name } .rb" )
73
74
dir = output . join ( @spec . module_namespace . join ( '/' ) )
74
-
75
75
empty_directory ( dir , verbose : false )
76
76
77
77
# Write the file with the ERB template:
78
78
template ( 'templates/method.erb' , path_to_file , force : true )
79
79
80
+ # Optionals:
80
81
print_source_code ( path_to_file ) if options [ :verbose ]
81
-
82
82
generate_tests if options [ :tests ]
83
-
84
- puts
85
83
end
86
-
87
84
run_rubocop
88
85
BuildHashHelper . add_hash ( @build_hash )
89
86
end
90
87
91
- def docs_helper ( name , info )
92
- info [ 'type' ] = 'String' if info [ 'type' ] == 'enum' # Rename 'enums' to 'strings'
93
- info [ 'type' ] = 'Integer' if info [ 'type' ] == 'int' # Rename 'int' to 'Integer'
94
- tipo = info [ 'type' ] ? info [ 'type' ] . capitalize : 'String'
95
- description = info [ 'description' ] ? info [ 'description' ] . strip : '[TODO]'
96
- options = info [ 'options' ] ? "(options: #{ info [ 'options' ] . join ( ', ' ) . strip } )" : nil
97
- required = info [ 'required' ] ? '(*Required*)' : ''
98
- deprecated = info [ 'deprecated' ] ? '*Deprecated*' : ''
99
- optionals = [ required , deprecated , options ] . join ( ' ' ) . strip
100
-
101
- "# @option arguments [#{ tipo } ] :#{ name } #{ description } #{ optionals } \n "
102
- end
103
-
104
- def stability_doc_helper ( stability )
105
- return if stability == 'stable'
106
-
107
- if stability == 'experimental'
108
- <<~MSG
109
- # This functionality is Experimental and may be changed or removed
110
- # completely in a future release. Elastic will take a best effort approach
111
- # to fix any issues, but experimental features are not subject to the
112
- # support SLA of official GA features.
113
- MSG
114
- elsif stability == 'beta'
115
- <<~MSG
116
- # This functionality is in Beta and is subject to change. The design and
117
- # code is less mature than official GA features and is being provided
118
- # as-is with no warranties. Beta features are not subject to the support
119
- # SLA of official GA features.
120
- MSG
121
- else
122
- <<~MSG
123
- # This functionality is subject to potential breaking changes within a
124
- # minor version, meaning that your referencing code may break when this
125
- # library is upgraded.
126
- MSG
127
- end
128
- end
129
-
130
88
def generate_tests
131
89
copy_file 'templates/test_helper.rb' , @output . join ( 'test' ) . join ( 'test_helper.rb' )
132
90
0 commit comments