gapic-generator 0.1.7 → 0.2.0

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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +19 -0
  3. data/lib/gapic/formatting_utils.rb +9 -4
  4. data/lib/gapic/generator/version.rb +1 -1
  5. data/lib/gapic/generators/base_generator.rb +0 -8
  6. data/lib/gapic/generators/default_generator.rb +2 -4
  7. data/lib/gapic/helpers/filepath_helper.rb +45 -0
  8. data/lib/gapic/helpers/namespace_helper.rb +51 -0
  9. data/lib/gapic/presenters.rb +44 -0
  10. data/{templates/default/helpers → lib/gapic}/presenters/enum_presenter.rb +19 -14
  11. data/{templates/default/helpers → lib/gapic}/presenters/enum_value_presenter.rb +19 -12
  12. data/lib/gapic/presenters/field_presenter.rb +154 -0
  13. data/lib/gapic/presenters/file_presenter.rb +59 -0
  14. data/lib/gapic/presenters/gem_presenter.rb +170 -0
  15. data/lib/gapic/presenters/message_presenter.rb +73 -0
  16. data/lib/gapic/presenters/method_presenter.rb +298 -0
  17. data/lib/gapic/presenters/package_presenter.rb +72 -0
  18. data/lib/gapic/presenters/resource_presenter.rb +99 -0
  19. data/lib/gapic/presenters/sample_presenter.rb +84 -0
  20. data/lib/gapic/presenters/service_presenter.rb +297 -0
  21. data/lib/gapic/resource_lookup.rb +8 -1
  22. data/lib/gapic/schema/api.rb +16 -0
  23. data/lib/gapic/schema/wrappers.rb +9 -2
  24. data/templates/default/gem/readme.erb +2 -2
  25. data/templates/default/helpers/filepath_helper.rb +2 -21
  26. data/templates/default/helpers/namespace_helper.rb +2 -27
  27. data/templates/default/layouts/_ruby.erb +1 -3
  28. data/templates/default/service/client/_client.erb +7 -1
  29. metadata +16 -14
  30. data/templates/default/helpers/presenter_helper.rb +0 -24
  31. data/templates/default/helpers/presenters/field_presenter.rb +0 -146
  32. data/templates/default/helpers/presenters/file_presenter.rb +0 -53
  33. data/templates/default/helpers/presenters/gem_presenter.rb +0 -140
  34. data/templates/default/helpers/presenters/message_presenter.rb +0 -66
  35. data/templates/default/helpers/presenters/method_presenter.rb +0 -293
  36. data/templates/default/helpers/presenters/package_presenter.rb +0 -65
  37. data/templates/default/helpers/presenters/resource_presenter.rb +0 -92
  38. data/templates/default/helpers/presenters/sample_presenter.rb +0 -74
  39. data/templates/default/helpers/presenters/service_presenter.rb +0 -276
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7758f2d15a52b89f57555f192cf9d477cbfdbd9788578ba30b5e04d9c468ed85
4
- data.tar.gz: ef2c5bd9924646eea488db7060dc654dc0cc2ff2ad1d5736e4f04cad217e0fe4
3
+ metadata.gz: 7d4dfb48094e9e52bcc6563460bfc9bf458496d81607777cecd231f497025e1a
4
+ data.tar.gz: a52c7434b5497d85543ac55149d54987ebfa950c191b1af7673a1f32fa58dd39
5
5
  SHA512:
6
- metadata.gz: 6d28f3903643f3b86770f83538dcaa669668bcd9f89e85592bbc6680c95c13fc043b323cf406fa0fa37b0ad0ad21ed30bb56ed42c7a2be0b423cbfc4c0b7c423
7
- data.tar.gz: a98b3d1af7be74a680e0b738b4d0753130f90c51ef71fb181a94189d1806b947338558826cd3e4f5b8c3315897546d2e36a1ce5d99d58dd501fd5aa5e8c3a76d
6
+ metadata.gz: be4a0672c23603bc0df2174f52d5cdc5ddd60264f322079f9c96e03bafcfa2b7fed187103ad864b19cc1a7e1ee3bb50eecc0e64a29e2534d4183c574fa5d1cc7
7
+ data.tar.gz: a80ec4c7604511c4b7d87fb4868dd928676aaaec205a32fc7cbda8facd52e7f9a347d1104ba8840df80f2e3df81f87b831483615540761ffdffd45575a129aa4
@@ -1,5 +1,24 @@
1
1
  # Release History for gapic-generator
2
2
 
3
+ ### 0.2.0 / 2020-03-23
4
+
5
+ Generation updates:
6
+
7
+ * Proto service documentation is rendered into client class YARD docs.
8
+ * Generated Apache license is now well-formatted markdown.
9
+ * YARD titles updated with the word "API".
10
+ * Tweaks to the default summary and description text.
11
+ * Generated rakefiles can run acceptance tests if present.
12
+ * Switch the order of summary and description in the readmes.
13
+ * Exempt a few more files (notably gemspec and rakefile) from rubocop.
14
+ * Several formatting tweaks, including removal of redundant whitespace.
15
+
16
+ Internal changes:
17
+
18
+ * New configuration allows path helpers to be generated from outputs.
19
+ * Moved presenters into lib so they can be changed/subclassed more easily.
20
+ * Several additions to GemPresenter to support new features.
21
+
3
22
  ### 0.1.7 / 2020-03-18
4
23
 
5
24
  * Path modules extend self so helpers can be invoked on them directly
@@ -33,10 +33,14 @@ module Gapic
33
33
  #
34
34
  # Tries to be smart about exempting preformatted text blocks.
35
35
  #
36
+ # @param api [Gapic::Schema::Api]
36
37
  # @param lines [Enumerable<String>]
38
+ # @param disable_xrefs [Boolean] (default is `false`) Disable linking to
39
+ # cross-references, and render them simply as text. This can be used if
40
+ # it is known that the targets are not present in the current library.
37
41
  # @return [Enumerable<String>]
38
42
  #
39
- def format_doc_lines api, lines
43
+ def format_doc_lines api, lines, disable_xrefs: false
40
44
  # To detect preformatted blocks, this tracks the "expected" base indent
41
45
  # according to Markdown. Specifically, this is the effective indent of
42
46
  # previous block, which is normally 0 except if we're in a list item.
@@ -53,7 +57,7 @@ module Gapic
53
57
  in_block, base_indent = update_indent_state in_block, base_indent, line, indent
54
58
  if in_block == false
55
59
  line = escape_line_braces line
56
- line = format_line_xrefs api, line
60
+ line = format_line_xrefs api, line, disable_xrefs
57
61
  end
58
62
  end
59
63
  line
@@ -104,11 +108,12 @@ module Gapic
104
108
  line
105
109
  end
106
110
 
107
- def format_line_xrefs api, line
111
+ def format_line_xrefs api, line, disable_xrefs
108
112
  while (m = @xref_detector.match line)
109
113
  entity = api.lookup m[:addr]
110
114
  return line if entity.nil?
111
- yard_link = yard_link_for_entity entity, m[:text]
115
+ text = m[:text]
116
+ yard_link = disable_xrefs ? text : yard_link_for_entity(entity, text)
112
117
  return line if yard_link.nil?
113
118
  line = "#{m[:pre]}#{yard_link}#{m[:post]}"
114
119
  end
@@ -16,6 +16,6 @@
16
16
 
17
17
  module Gapic
18
18
  module Generator
19
- VERSION = "0.1.7"
19
+ VERSION = "0.2.0"
20
20
  end
21
21
  end
@@ -55,14 +55,6 @@ module Gapic
55
55
  controller.class.all_helpers_from_path helpers_path
56
56
  end
57
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
58
  def controller
67
59
  # Each controller gets a new class, so multiple generators can be
68
60
  # active at the same time without stomping on each other.
@@ -15,6 +15,7 @@
15
15
  # limitations under the License.
16
16
 
17
17
  require "gapic/generators/base_generator"
18
+ require "gapic/presenters"
18
19
 
19
20
  module Gapic
20
21
  module Generators
@@ -29,9 +30,6 @@ module Gapic
29
30
 
30
31
  # Configure to use a custom templates directory
31
32
  use_templates! File.join __dir__, "../../../templates/default"
32
-
33
- # Configure these helper method to be used by the generator
34
- use_helpers! :gem_presenter
35
33
  end
36
34
 
37
35
  # Disable Rubocop because we expect generate to grow and violate more
@@ -46,7 +44,7 @@ module Gapic
46
44
  def generate
47
45
  files = []
48
46
 
49
- gem = gem_presenter @api
47
+ gem = Gapic::Presenters.gem_presenter @api
50
48
 
51
49
  gem.packages.each do |package|
52
50
  # Package level files
@@ -0,0 +1,45 @@
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
+ module Helpers
21
+ ##
22
+ # Helpers related to generating paths to ruby files (e.g. for require)
23
+ #
24
+ module FilepathHelper
25
+ ##
26
+ # Converts a ruby namespace string to a file path string.
27
+ def ruby_file_path api, namespace
28
+ file_path = ruby_file_path_for_namespace namespace
29
+ fix_file_path api, file_path
30
+ end
31
+
32
+ ##
33
+ # Converts a ruby namespace string to a file path string.
34
+ def ruby_file_path_for_namespace namespace
35
+ ActiveSupport::Inflector.underscore namespace
36
+ end
37
+
38
+ ##
39
+ # Corrects a namespace by replacing known bad values with good values.
40
+ def fix_file_path api, file_path
41
+ file_path.split("/").map { |node| api.fix_file_path node }.join("/")
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,51 @@
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
+ module Helpers
21
+ ##
22
+ # Helpers related to generating ruby namespaces
23
+ #
24
+ module NamespaceHelper
25
+ ##
26
+ # Looks up the ruby_package for a dot-separated address string to a new string
27
+ # and creates the corrected Ruby namespace
28
+ def ruby_namespace api, address
29
+ file = api.file_for address
30
+ address = address.dup
31
+ address[file.package] = file.ruby_package if file.ruby_package.present?
32
+ namespace = ruby_namespace_for_address address
33
+ fix_namespace api, namespace
34
+ end
35
+
36
+ ##
37
+ # Converts an array or dot-separated address string to a new string with
38
+ # Ruby double-semicolon separators.
39
+ def ruby_namespace_for_address address
40
+ address = address.split "." if address.is_a? String
41
+ address.reject(&:empty?).map(&:camelize).join "::"
42
+ end
43
+
44
+ ##
45
+ # Corrects a namespace by replacing known bad values with good values.
46
+ def fix_namespace api, namespace
47
+ namespace.split("::").map { |node| api.fix_namespace node }.join("::")
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,44 @@
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 "gapic/presenters/enum_presenter"
18
+ require "gapic/presenters/enum_value_presenter"
19
+ require "gapic/presenters/field_presenter"
20
+ require "gapic/presenters/file_presenter"
21
+ require "gapic/presenters/gem_presenter"
22
+ require "gapic/presenters/message_presenter"
23
+ require "gapic/presenters/method_presenter"
24
+ require "gapic/presenters/package_presenter"
25
+ require "gapic/presenters/resource_presenter"
26
+ require "gapic/presenters/sample_presenter"
27
+ require "gapic/presenters/service_presenter"
28
+
29
+ module Gapic
30
+ ##
31
+ # A namespace for presenter classes.
32
+ #
33
+ module Presenters
34
+ ##
35
+ # Return a gem presenter
36
+ #
37
+ # @param api [Gapic::Schema::Api] The api schema object
38
+ # @return [Gapic::Presenters::GemPresenter]
39
+ #
40
+ def self.gem_presenter api
41
+ GemPresenter.new api
42
+ end
43
+ end
44
+ end
@@ -14,22 +14,27 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
- require_relative "enum_value_presenter"
17
+ module Gapic
18
+ module Presenters
19
+ ##
20
+ # A presenter for proto enums.
21
+ #
22
+ class EnumPresenter
23
+ def initialize enum
24
+ @enum = enum
25
+ end
18
26
 
19
- class EnumPresenter
20
- def initialize enum
21
- @enum = enum
22
- end
23
-
24
- def name
25
- @enum.name
26
- end
27
+ def name
28
+ @enum.name
29
+ end
27
30
 
28
- def doc_description
29
- @enum.docs_leading_comments
30
- end
31
+ def doc_description
32
+ @enum.docs_leading_comments
33
+ end
31
34
 
32
- def values
33
- @values ||= @enum.values.map { |v| EnumValuePresenter.new v }
35
+ def values
36
+ @values ||= @enum.values.map { |v| EnumValuePresenter.new v }
37
+ end
38
+ end
34
39
  end
35
40
  end
@@ -14,20 +14,27 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
- class EnumValuePresenter
18
- def initialize value
19
- @value = value
20
- end
17
+ module Gapic
18
+ module Presenters
19
+ ##
20
+ # A presenter for proto enum values.
21
+ #
22
+ class EnumValuePresenter
23
+ def initialize value
24
+ @value = value
25
+ end
21
26
 
22
- def name
23
- @value.name
24
- end
27
+ def name
28
+ @value.name
29
+ end
25
30
 
26
- def doc_description
27
- @value.docs_leading_comments
28
- end
31
+ def doc_description
32
+ @value.docs_leading_comments
33
+ end
29
34
 
30
- def number
31
- @value.number
35
+ def number
36
+ @value.number
37
+ end
38
+ end
32
39
  end
33
40
  end
@@ -0,0 +1,154 @@
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
+ require "gapic/helpers/namespace_helper"
19
+
20
+ module Gapic
21
+ module Presenters
22
+ ##
23
+ # A presenter for proto fields.
24
+ #
25
+ class FieldPresenter
26
+ include Gapic::Helpers::NamespaceHelper
27
+
28
+ def initialize api, message, field
29
+ @api = api
30
+ @message = message
31
+ @field = field
32
+ end
33
+
34
+ def name
35
+ @field.name
36
+ end
37
+
38
+ def doc_types
39
+ field_doc_types @field, false
40
+ end
41
+
42
+ def doc_attribute_type
43
+ mode = @field.output_only? ? "r" : "rw"
44
+ "@!attribute [#{mode}] #{@field.name}"
45
+ end
46
+
47
+ def output_doc_types
48
+ field_doc_types @field, true
49
+ end
50
+
51
+ def doc_description
52
+ @field.docs_leading_comments
53
+ end
54
+
55
+ def default_value
56
+ single = default_singular_value
57
+ return "[#{single}]" if @field.repeated? && !@field.map?
58
+ single
59
+ end
60
+
61
+ def as_kwarg value: nil
62
+ "#{name}: #{value || name}"
63
+ end
64
+
65
+ # TODO: remove, only used in tests
66
+ def type_name
67
+ @field.type_name
68
+ end
69
+
70
+ def type_name_full
71
+ return nil if type_name.blank?
72
+ ruby_namespace @api, type_name
73
+ end
74
+
75
+ def message?
76
+ @field.message?
77
+ end
78
+
79
+ def enum?
80
+ @field.enum?
81
+ end
82
+
83
+ def repeated?
84
+ @field.repeated?
85
+ end
86
+
87
+ def map?
88
+ @field.map?
89
+ end
90
+
91
+ protected
92
+
93
+ def field_doc_types field, output
94
+ return field_map_type field.message, output if field.map?
95
+ base_type =
96
+ if field.message?
97
+ type = message_ruby_type field.message
98
+ output ? type : "#{type} | Hash"
99
+ elsif field.enum?
100
+ # TODO: handle when arg message is nil and enum is the type
101
+ message_ruby_type field.enum
102
+ else
103
+ case field.type
104
+ when 1, 2 then "Float"
105
+ when 3, 4, 5, 6, 7, 13, 15, 16, 17, 18 then "Integer"
106
+ when 9, 12 then "String"
107
+ when 8 then "Boolean"
108
+ else
109
+ "Object"
110
+ end
111
+ end
112
+ field.repeated? ? "Array<#{base_type}>" : base_type
113
+ end
114
+
115
+ def field_map_type entry_message, output
116
+ key_field = value_field = nil
117
+ entry_message.fields.each do |field|
118
+ key_field = field if field.name == "key"
119
+ value_field = field if field.name == "value"
120
+ end
121
+ class_name = output ? "Google::Protobuf::Map" : "Hash"
122
+ if key_field && value_field
123
+ key_type = field_doc_types key_field, output
124
+ value_type = field_doc_types value_field, output
125
+ "#{class_name}{#{key_type} => #{value_type}}"
126
+ else
127
+ class_name
128
+ end
129
+ end
130
+
131
+ def default_singular_value
132
+ if @field.message?
133
+ "{}"
134
+ elsif @field.enum?
135
+ # TODO: select the first non-0 enum value
136
+ ":#{@field.enum.values.first.name}"
137
+ else
138
+ case @field.type
139
+ when 1, 2 then "3.5"
140
+ when 3, 4, 5, 6, 7, 13, 15, 16, 17, 18 then "42"
141
+ when 9, 12 then "\"hello world\""
142
+ when 8 then "true"
143
+ else
144
+ "Object"
145
+ end
146
+ end
147
+ end
148
+
149
+ def message_ruby_type message
150
+ ruby_namespace @api, message.address.join(".")
151
+ end
152
+ end
153
+ end
154
+ end