gapic-generator 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +13 -0
  3. data/CHANGELOG.md +43 -0
  4. data/CODE_OF_CONDUCT.md +43 -0
  5. data/CONTRIBUTING.md +28 -0
  6. data/LICENSE +202 -0
  7. data/README.md +72 -0
  8. data/bin/gapic-generator +103 -0
  9. data/bin/protoc-gen-ruby_gapic +33 -0
  10. data/default-rubocop.yml +10 -0
  11. data/gem_templates/binary.erb +20 -0
  12. data/gem_templates/dockerfile.erb +39 -0
  13. data/gem_templates/entrypoint.erb +24 -0
  14. data/gem_templates/gapic_sh.erb +97 -0
  15. data/gem_templates/gemfile.erb +8 -0
  16. data/gem_templates/gemspec.erb +36 -0
  17. data/gem_templates/generator.erb +37 -0
  18. data/gem_templates/gitignore.erb +10 -0
  19. data/gem_templates/rakefile.erb +29 -0
  20. data/gem_templates/readme.erb +69 -0
  21. data/gem_templates/rubocop.erb +16 -0
  22. data/gem_templates/shared/_header.erb +4 -0
  23. data/gem_templates/shared/_license.erb +13 -0
  24. data/gem_templates/shared/_warning.erb +1 -0
  25. data/gem_templates/test_generator.erb +13 -0
  26. data/gem_templates/test_helper.erb +25 -0
  27. data/gem_templates/version.erb +10 -0
  28. data/lib/gapic/file_formatter.rb +62 -0
  29. data/lib/gapic/formatting_utils.rb +109 -0
  30. data/lib/gapic/gem_builder.rb +98 -0
  31. data/lib/gapic/generator.rb +30 -0
  32. data/lib/gapic/generator/version.rb +21 -0
  33. data/lib/gapic/generators/base_generator.rb +91 -0
  34. data/lib/gapic/generators/default_generator.rb +101 -0
  35. data/lib/gapic/grpc_service_config/method_config.rb +49 -0
  36. data/lib/gapic/grpc_service_config/parser.rb +218 -0
  37. data/lib/gapic/grpc_service_config/parsing_error.rb +25 -0
  38. data/lib/gapic/grpc_service_config/retry_policy.rb +51 -0
  39. data/lib/gapic/grpc_service_config/service_config.rb +42 -0
  40. data/lib/gapic/path_template.rb +35 -0
  41. data/lib/gapic/path_template/parser.rb +83 -0
  42. data/lib/gapic/path_template/segment.rb +67 -0
  43. data/lib/gapic/resource_lookup.rb +91 -0
  44. data/lib/gapic/runner.rb +76 -0
  45. data/lib/gapic/schema.rb +17 -0
  46. data/lib/gapic/schema/api.rb +264 -0
  47. data/lib/gapic/schema/loader.rb +269 -0
  48. data/lib/gapic/schema/wrappers.rb +717 -0
  49. data/lib/google/api/annotations.pb.rb +39 -0
  50. data/lib/google/api/client.pb.rb +43 -0
  51. data/lib/google/api/field_behavior.pb.rb +51 -0
  52. data/lib/google/api/http.pb.rb +60 -0
  53. data/lib/google/api/resource.pb.rb +80 -0
  54. data/lib/google/longrunning/operations.pb.rb +115 -0
  55. data/lib/google/protobuf/any.pb.rb +40 -0
  56. data/lib/google/protobuf/compiler/plugin.pb.rb +72 -0
  57. data/lib/google/protobuf/descriptor.pb.rb +359 -0
  58. data/lib/google/protobuf/empty.pb.rb +36 -0
  59. data/lib/google/rpc/status.pb.rb +46 -0
  60. data/templates/default/gem/_version.erb +2 -0
  61. data/templates/default/gem/changelog.erb +3 -0
  62. data/templates/default/gem/gemfile.erb +4 -0
  63. data/templates/default/gem/gemspec.erb +37 -0
  64. data/templates/default/gem/gitignore.erb +20 -0
  65. data/templates/default/gem/license.erb +22 -0
  66. data/templates/default/gem/rakefile.erb +27 -0
  67. data/templates/default/gem/readme.erb +24 -0
  68. data/templates/default/gem/rubocop.erb +59 -0
  69. data/templates/default/gem/version.erb +6 -0
  70. data/templates/default/gem/yardopts.erb +12 -0
  71. data/templates/default/helpers/default_helper.rb +50 -0
  72. data/templates/default/helpers/filepath_helper.rb +38 -0
  73. data/templates/default/helpers/namespace_helper.rb +44 -0
  74. data/templates/default/helpers/presenter_helper.rb +24 -0
  75. data/templates/default/helpers/presenters/enum_presenter.rb +35 -0
  76. data/templates/default/helpers/presenters/enum_value_presenter.rb +33 -0
  77. data/templates/default/helpers/presenters/field_presenter.rb +146 -0
  78. data/templates/default/helpers/presenters/file_presenter.rb +53 -0
  79. data/templates/default/helpers/presenters/gem_presenter.rb +140 -0
  80. data/templates/default/helpers/presenters/message_presenter.rb +66 -0
  81. data/templates/default/helpers/presenters/method_presenter.rb +293 -0
  82. data/templates/default/helpers/presenters/package_presenter.rb +65 -0
  83. data/templates/default/helpers/presenters/resource_presenter.rb +92 -0
  84. data/templates/default/helpers/presenters/sample_presenter.rb +74 -0
  85. data/templates/default/helpers/presenters/service_presenter.rb +276 -0
  86. data/templates/default/layouts/_ruby.erb +20 -0
  87. data/templates/default/package.erb +6 -0
  88. data/templates/default/proto_docs/_enum.erb +13 -0
  89. data/templates/default/proto_docs/_message.erb +23 -0
  90. data/templates/default/proto_docs/_proto_file.erb +9 -0
  91. data/templates/default/proto_docs/proto_file.erb +6 -0
  92. data/templates/default/proto_docs/readme.erb +5 -0
  93. data/templates/default/service.erb +8 -0
  94. data/templates/default/service/client.erb +6 -0
  95. data/templates/default/service/client/_client.erb +137 -0
  96. data/templates/default/service/client/_config.erb +155 -0
  97. data/templates/default/service/client/_credentials.erb +21 -0
  98. data/templates/default/service/client/_helpers.erb +9 -0
  99. data/templates/default/service/client/_operations.erb +88 -0
  100. data/templates/default/service/client/_paths.erb +8 -0
  101. data/templates/default/service/client/_requires.erb +1 -0
  102. data/templates/default/service/client/_resource.erb +6 -0
  103. data/templates/default/service/client/_self_configure.erb +9 -0
  104. data/templates/default/service/client/_self_configure_defaults.erb +22 -0
  105. data/templates/default/service/client/_self_configure_retry_policy.erb +15 -0
  106. data/templates/default/service/client/method/_def.erb +21 -0
  107. data/templates/default/service/client/method/def/_options_defaults.erb +29 -0
  108. data/templates/default/service/client/method/def/_request.erb +6 -0
  109. data/templates/default/service/client/method/def/_request_normal.erb +4 -0
  110. data/templates/default/service/client/method/def/_request_streaming.erb +9 -0
  111. data/templates/default/service/client/method/def/_rescue.erb +1 -0
  112. data/templates/default/service/client/method/def/_response.erb +6 -0
  113. data/templates/default/service/client/method/def/_response_normal.erb +8 -0
  114. data/templates/default/service/client/method/def/_response_paged.erb +9 -0
  115. data/templates/default/service/client/method/docs/_error.erb +2 -0
  116. data/templates/default/service/client/method/docs/_request.erb +6 -0
  117. data/templates/default/service/client/method/docs/_request_field.erb +7 -0
  118. data/templates/default/service/client/method/docs/_request_normal.erb +20 -0
  119. data/templates/default/service/client/method/docs/_request_streaming.erb +5 -0
  120. data/templates/default/service/client/method/docs/_response.erb +6 -0
  121. data/templates/default/service/client/method/docs/_sample.erb +20 -0
  122. data/templates/default/service/client/method/docs/_sample_response.erb +24 -0
  123. data/templates/default/service/client/method/docs/_samples.erb +6 -0
  124. data/templates/default/service/client/method/docs/request_field/_arg.erb +10 -0
  125. data/templates/default/service/client/method/docs/request_field/_hash.erb +19 -0
  126. data/templates/default/service/client/method/docs/sample_response/_comment.erb +5 -0
  127. data/templates/default/service/client/method/docs/sample_response/_define.erb +2 -0
  128. data/templates/default/service/client/method/docs/sample_response/_loop.erb +12 -0
  129. data/templates/default/service/client/method/docs/sample_response/_print.erb +2 -0
  130. data/templates/default/service/client/method/docs/sample_response/_write_file.erb +2 -0
  131. data/templates/default/service/client/resource/_def.erb +6 -0
  132. data/templates/default/service/client/resource/_doc.erb +8 -0
  133. data/templates/default/service/client/resource/_multi.erb +28 -0
  134. data/templates/default/service/client/resource/_single.erb +11 -0
  135. data/templates/default/service/credentials.erb +6 -0
  136. data/templates/default/service/operations.erb +6 -0
  137. data/templates/default/service/paths.erb +6 -0
  138. data/templates/default/service/test/client.erb +24 -0
  139. data/templates/default/service/test/client_operations.erb +24 -0
  140. data/templates/default/service/test/method/_assert_response.erb +11 -0
  141. data/templates/default/service/test/method/_bidi.erb +100 -0
  142. data/templates/default/service/test/method/_client.erb +84 -0
  143. data/templates/default/service/test/method/_normal.erb +69 -0
  144. data/templates/default/service/test/method/_server.erb +85 -0
  145. data/templates/default/service/test/method/_setup.erb +21 -0
  146. data/templates/default/service/test/smoke.erb +12 -0
  147. data/templates/default/shared/_header.erb +4 -0
  148. data/templates/default/shared/_license.erb +21 -0
  149. data/templates/default/shared/_warning.erb +1 -0
  150. metadata +351 -0
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ <%= render partial: "shared/license" %>
4
+ <%= render partial: "shared/warning" -%>
@@ -0,0 +1,13 @@
1
+ # Copyright [yyyy] [name of copyright owner]
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
@@ -0,0 +1 @@
1
+ # Auto-generated by gapic-generator-ruby. DO NOT EDIT!
@@ -0,0 +1,13 @@
1
+ <%= render partial: "shared/header" -%>
2
+
3
+ require "test_helper"
4
+ require "gapic/generators/<%= gem_name %>_generator"
5
+
6
+ class <%= gem_class_prefix %>GeneratorTest < GeneratorTest
7
+ def test_speech_generate
8
+ generator = Gapic::Generators::<%= gem_class_prefix %>Generator.new api(:speech)
9
+ generator.generate.each do |file|
10
+ assert_equal expected_content("<%= gem_name %>/#{file.name}"), file.content
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ <%= render partial: "shared/header" -%>
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
+ require "gapic/generators/<%= gem_name %>_generator"
5
+
6
+ require "minitest/autorun"
7
+ require "minitest/focus"
8
+
9
+ class GeneratorTest < Minitest::Test
10
+ def proto_input service
11
+ File.binread "proto_input/#{service}_desc.bin"
12
+ end
13
+
14
+ def request service
15
+ Google::Protobuf::Compiler::CodeGeneratorRequest.decode proto_input(service)
16
+ end
17
+
18
+ def api service
19
+ Gapic::Schema::Api.new request(service)
20
+ end
21
+
22
+ def expected_content filename
23
+ File.read "expected_output/#{filename}"
24
+ end
25
+ end
@@ -0,0 +1,10 @@
1
+ <%= render partial: "shared/header" -%>
2
+
3
+
4
+ module Gapic
5
+ module Generator
6
+ module <%= gem_class_prefix %>
7
+ VERSION = "0.1.0"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "tmpdir"
18
+ require "fileutils"
19
+
20
+ module Gapic
21
+ ##
22
+ # File Formatter
23
+ class FileFormatter
24
+ attr_reader :configuration, :files
25
+
26
+ ##
27
+ # Create a new file formatter object
28
+ #
29
+ # @param path_template [String] The URI path template to be parsed.
30
+ def initialize configuration, files
31
+ @configuration = configuration
32
+ @files = format! configuration, files
33
+ end
34
+
35
+ protected
36
+
37
+ def format! configuration, files
38
+ Dir.mktmpdir do |dir|
39
+ files.each do |file|
40
+ write_file dir, file
41
+ end
42
+
43
+ system "rubocop -x #{dir} -o #{dir}/rubocop.out -c #{configuration}"
44
+
45
+ files.each do |file|
46
+ read_file dir, file
47
+ end
48
+ end
49
+ end
50
+
51
+ def write_file dir, file
52
+ tmp_file = File.join dir, file.name
53
+ FileUtils.mkdir_p File.dirname tmp_file
54
+ File.write tmp_file, file.content
55
+ end
56
+
57
+ def read_file dir, file
58
+ tmp_file = File.join dir, file.name
59
+ file.content = File.read tmp_file
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "bigdecimal"
18
+
19
+ module Gapic
20
+ ##
21
+ # Various string formatting utils
22
+ #
23
+ module FormattingUtils
24
+ @brace_detector = /\A([^`]*(`[^`]*`[^`]*)*[^`\\])?\{([\w,]+)\}(.*)\z/m
25
+ @list_element_detector = /\A\s*(\*|\+|-|[0-9a-zA-Z]+\.)\s/
26
+
27
+ class << self
28
+ ##
29
+ # Given an enumerable of lines, escape braces that look like yardoc type
30
+ # links. Tries to be smart about handling only braces that would be
31
+ # interpreted by yard (i.e. those that are not part of preformatted text
32
+ # blocks).
33
+ #
34
+ # @param lines [Enumerable<String>]
35
+ # @return [Enumerable<String>]
36
+ #
37
+ def escape_braces lines
38
+ # This looks for braces that:
39
+ # * Are opened and closed on the same line
40
+ # * Are not nested
41
+ # * Are not located between backticks
42
+ # * Are not in a preformatted block
43
+ #
44
+ # To detect preformatted blocks, this tracks the "expected" base indent
45
+ # according to Markdown. Specifically, this is the effective indent of
46
+ # previous block, which is normally 0 except if we're in a list item.
47
+ # Then, if a block is indented at least 4 spaces past that expected
48
+ # indent (and as long as it remains so), those lines are considered
49
+ # preformatted.
50
+ in_block = nil
51
+ base_indent = 0
52
+ lines.map do |line|
53
+ indent = line_indent line
54
+ if indent.nil?
55
+ in_block = nil
56
+ else
57
+ in_block, base_indent = update_indent_state in_block, base_indent, line, indent
58
+ line = escape_line_braces line if in_block == false
59
+ end
60
+ line
61
+ end
62
+ end
63
+
64
+ ##
65
+ # Given a number, format it in such a way that Rubocop will be happy.
66
+ # Specifically, we add underscores if the magnitude is at least 10_000.
67
+ # This works for both integers and floats.
68
+ #
69
+ # @param value [Numeric]
70
+ # @return [String]
71
+ #
72
+ def format_number value
73
+ return value.to_s if value.abs < 10_000
74
+ str = value.is_a?(Integer) ? value.to_s : BigDecimal(value.to_f.to_s).to_s("F")
75
+ re = /^(-?\d+)(\d\d\d)([_\.][_\.\d]+)?$/
76
+ while (m = re.match str)
77
+ str = "#{m[1]}_#{m[2]}#{m[3]}"
78
+ end
79
+ str
80
+ end
81
+
82
+ private
83
+
84
+ def update_indent_state in_block, base_indent, line, indent
85
+ if in_block != true && @list_element_detector =~ line
86
+ in_block = false
87
+ indent = base_indent if indent > base_indent
88
+ base_indent = (indent + 7) / 4 * 4
89
+ else
90
+ in_block = indent >= base_indent + 4 unless in_block == false
91
+ base_indent = indent / 4 * 4 if in_block == false && indent < base_indent
92
+ end
93
+ [in_block, base_indent]
94
+ end
95
+
96
+ def line_indent line
97
+ m = /^( *)\S/.match line
98
+ m.nil? ? nil : m[1].length
99
+ end
100
+
101
+ def escape_line_braces line
102
+ while (m = @brace_detector.match line)
103
+ line = "#{m[1]}\\\\{#{m[3]}}#{m[4]}"
104
+ end
105
+ line
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "fileutils"
18
+ require "action_controller"
19
+ require "active_support/inflector"
20
+
21
+ module Gapic
22
+ ##
23
+ # The builder created files for new extension gems.
24
+ class GemBuilder
25
+ ##
26
+ # Initializes the builder.
27
+ #
28
+ # @param name [String] The name of the new extension gem.
29
+ # @param path [String] The directory to write the gem files.
30
+ #
31
+ def initialize name, path
32
+ @name = name
33
+ @path = path
34
+ end
35
+
36
+ ##
37
+ # Writes all the files for the gem.
38
+ #
39
+ def bootstrap
40
+ gen "readme.erb", "README.md"
41
+ gen "gemspec.erb", "gapic-generator-#{gem_name}.gemspec"
42
+ gen "rakefile.erb", "Rakefile"
43
+ gen "gemfile.erb", "Gemfile"
44
+ gen "gitignore.erb", ".gitignore"
45
+ gen "rubocop.erb", ".rubocop.yml"
46
+ gen "dockerfile.erb", "Dockerfile"
47
+ gen "entrypoint.erb", "docker-entrypoint.sh"
48
+ gen "gapic_sh.erb", "gapic.sh"
49
+ gen "binary.erb", "bin/protoc-gen-ruby_#{gem_name}"
50
+ gen "generator.erb",
51
+ "lib/gapic/generators/#{gem_name}_generator.rb"
52
+ gen "version.erb",
53
+ "lib/gapic/generator/#{gem_name}/version.rb"
54
+ gen "test_helper.erb", "test/test_helper.rb"
55
+ gen "test_generator.erb", "test/generators/#{gem_name}_test.rb"
56
+ cp "speech_desc.bin", "proto_input/speech_desc.bin"
57
+ end
58
+
59
+ private
60
+
61
+ def template_path
62
+ File.join __dir__, "../../gem_templates"
63
+ end
64
+
65
+ def gem_name
66
+ ActiveSupport::Inflector.underscore @name
67
+ end
68
+
69
+ def gem_class_prefix
70
+ ActiveSupport::Inflector.camelize @name
71
+ end
72
+
73
+ def controller
74
+ @controller ||= Class.new(ActionController::Base).new.tap do |c|
75
+ # Configure the controller to know about the templates
76
+ c.prepend_view_path template_path
77
+ end
78
+ end
79
+
80
+ def cp from, to
81
+ input_path = File.join template_path, from
82
+ target_path = File.join @path, to
83
+ FileUtils.mkdir_p File.dirname(target_path)
84
+ FileUtils.cp input_path, target_path
85
+ end
86
+
87
+ def gen template, filename
88
+ content = controller.render_to_string(
89
+ template: template,
90
+ formats: :text,
91
+ locals: { gem_name: gem_name, gem_class_prefix: gem_class_prefix }
92
+ )
93
+ target_path = File.join @path, filename
94
+ FileUtils.mkdir_p File.dirname(target_path)
95
+ File.write target_path, content
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "active_support/inflector"
18
+
19
+ module Gapic
20
+ # TODO: Enter docs
21
+ # Dooooooooocs!!!
22
+ module Generator
23
+ def self.find type = nil
24
+ type ||= "default"
25
+ type_const = ActiveSupport::Inflector.camelize type
26
+ require "gapic/generators/#{type}_generator"
27
+ Kernel.const_get "Gapic::Generators::#{type_const}Generator"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Gapic
18
+ module Generator
19
+ VERSION = "0.1.4"
20
+ end
21
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "gapic/file_formatter"
18
+ require "action_controller"
19
+ require "google/protobuf/compiler/plugin.pb"
20
+ require "tempfile"
21
+
22
+ module Gapic
23
+ module Generators
24
+ # The generator orchestrates the rendering of templates.
25
+ class BaseGenerator
26
+ # Initializes the generator.
27
+ #
28
+ # @param api [Gapic::Schema::Api] The API model/context to
29
+ # generate.
30
+ def initialize api
31
+ @api = api
32
+ end
33
+
34
+ # Generates all the files for the API.
35
+ #
36
+ # @return [Array<
37
+ # Google::Protobuf::Compiler::CodeGeneratorResponse::File>]
38
+ # The files that were generated for the API.
39
+ def generate
40
+ raise "must implement"
41
+ end
42
+
43
+ private
44
+
45
+ def use_templates! template_path
46
+ template_path = File.expand_path template_path
47
+ helpers_path = File.join template_path, "helpers"
48
+
49
+ # Configure Dependencies to know how to load helpers
50
+ ActiveSupport::Dependencies.autoload_paths.prepend helpers_path
51
+
52
+ # Configure the controller to know about the templates and helpers
53
+ controller.prepend_view_path template_path
54
+ controller.class.helper \
55
+ controller.class.all_helpers_from_path helpers_path
56
+ end
57
+
58
+ def use_helpers! *helper_methods
59
+ helper_methods.each do |helper_method|
60
+ define_singleton_method helper_method do |*args|
61
+ controller.class.helpers.method(helper_method).call(*args)
62
+ end
63
+ end
64
+ end
65
+
66
+ def controller
67
+ # Each controller gets a new class, so multiple generators can be
68
+ # active at the same time without stomping on each other.
69
+ @controller ||= Class.new(ActionController::Base).new
70
+ end
71
+
72
+ def generate_file template, filename, **args
73
+ content = controller.render_to_string(
74
+ template: template, formats: :text, locals: args
75
+ )
76
+ Google::Protobuf::Compiler::CodeGeneratorResponse::File.new(
77
+ name: filename, content: content
78
+ )
79
+ end
80
+ alias_method :g, :generate_file
81
+
82
+ def format_files files
83
+ FileFormatter.new(format_config, files).files
84
+ end
85
+
86
+ def format_config
87
+ File.expand_path File.join __dir__, "../../../default-rubocop.yml"
88
+ end
89
+ end
90
+ end
91
+ end