gapic-generator 0.1.3 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +49 -0
  3. data/lib/gapic/formatting_utils.rb +160 -0
  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 +298 -0
  21. data/lib/gapic/resource_lookup.rb +8 -1
  22. data/lib/gapic/schema/api.rb +24 -0
  23. data/lib/gapic/schema/wrappers.rb +30 -8
  24. data/templates/default/gem/gemspec.erb +1 -1
  25. data/templates/default/gem/gitignore.erb +2 -0
  26. data/templates/default/gem/readme.erb +2 -2
  27. data/templates/default/gem/rubocop.erb +2 -0
  28. data/templates/default/helpers/default_helper.rb +2 -8
  29. data/templates/default/helpers/filepath_helper.rb +2 -21
  30. data/templates/default/helpers/namespace_helper.rb +2 -27
  31. data/templates/default/layouts/_ruby.erb +1 -3
  32. data/templates/default/service/client/_client.erb +7 -1
  33. data/templates/default/service/client/_paths.erb +1 -0
  34. data/templates/default/service/client/method/def/_options_defaults.erb +1 -1
  35. data/templates/default/service/client/method/def/_response_normal.erb +1 -1
  36. metadata +17 -14
  37. data/templates/default/helpers/presenter_helper.rb +0 -24
  38. data/templates/default/helpers/presenters/field_presenter.rb +0 -146
  39. data/templates/default/helpers/presenters/file_presenter.rb +0 -53
  40. data/templates/default/helpers/presenters/gem_presenter.rb +0 -140
  41. data/templates/default/helpers/presenters/message_presenter.rb +0 -66
  42. data/templates/default/helpers/presenters/method_presenter.rb +0 -293
  43. data/templates/default/helpers/presenters/package_presenter.rb +0 -65
  44. data/templates/default/helpers/presenters/resource_presenter.rb +0 -92
  45. data/templates/default/helpers/presenters/sample_presenter.rb +0 -74
  46. 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: ceba0ece82d90e96c30c312431c323b819876875dc8a4b9617bf91a96fb2bcae
4
- data.tar.gz: '0499a196f065e571e43d0224bd0d1ff97e0093f3913cb1e7a6ed538629c34f98'
3
+ metadata.gz: 7c88e9ae5990030e6fd090eaebcc037a128121621b2be5442e12353985530f90
4
+ data.tar.gz: 3dd8af72aecf6cb4b2b2fedf692e987bf8aa74c7484e40ee1de677020a141e0b
5
5
  SHA512:
6
- metadata.gz: 15df407d48360df1fb913e60d8008a4055eddb3da2d1a5f942d5f56199b8b99003845e510d79b7eb69812e99a66306b05cb36374c0fa15f93034241bbc352196
7
- data.tar.gz: 37ae32e33d1cee983c188ebefa5a5841687ec8d518d73e85a7df25e2ee10ef838adc670a50699c5bbf3e21430940fe68a88ae178f03e95ced681f79c5eee0d98
6
+ metadata.gz: 0f508a5ab35d5a342c9f6a208bd888277cf82e3ba15215120b2bbdd71a01dff675fe54758e78e5f0a3240dfe3528aefcb120f7cb8b1caa9477f3f6dd5e6a9e5b
7
+ data.tar.gz: c1188319beee864fa260f0c6ef5f2b9e34ec0b3778de3901cf544e8ab151075dfb450c45159cff4ae1c666c838f7b2ce105f5cb29c07cbe6f2ff9ff5f815f9ef
@@ -1,5 +1,54 @@
1
1
  # Release History for gapic-generator
2
2
 
3
+ ### 0.2.2 / 2020-03-31
4
+
5
+ * Updates to common protos, especially core types which were more than a year old.
6
+
7
+ ### 0.2.1 / 2020-03-26
8
+
9
+ * Fix service address for LRO operation clients.
10
+ * Tweak sample task names for wrapper-gem Rakefiles.
11
+
12
+ ### 0.2.0 / 2020-03-23
13
+
14
+ Generation updates:
15
+
16
+ * Proto service documentation is rendered into client class YARD docs.
17
+ * Generated Apache license is now well-formatted markdown.
18
+ * YARD titles updated with the word "API".
19
+ * Tweaks to the default summary and description text.
20
+ * Generated rakefiles can run acceptance tests if present.
21
+ * Switch the order of summary and description in the readmes.
22
+ * Exempt a few more files (notably gemspec and rakefile) from rubocop.
23
+ * Several formatting tweaks, including removal of redundant whitespace.
24
+
25
+ Internal changes:
26
+
27
+ * New configuration allows path helpers to be generated from outputs.
28
+ * Moved presenters into lib so they can be changed/subclassed more easily.
29
+ * Several additions to GemPresenter to support new features.
30
+
31
+ ### 0.1.7 / 2020-03-18
32
+
33
+ * Path modules extend self so helpers can be invoked on them directly
34
+ * Trigger IAM dependency for IAM V1 files other than iam_policy.proto
35
+
36
+ ### 0.1.6 / 2020-03-17
37
+
38
+ * Generated libraries now depend on gapic-common 0.2
39
+ * Convert cross-reference syntax in proto docs to YARD cross-reference links.
40
+ * Preserve call options in LROs
41
+ * Fix implicit kwarg warnings under Ruby 2.7
42
+
43
+ ### 0.1.5 / 2020-03-13
44
+
45
+ * More improvements to escaping of curly braces.
46
+
47
+ ### 0.1.4 / 2020-03-12
48
+
49
+ * Fixed over-escaping of curly braces in preformatted blocks in the yardocs.
50
+ * Fixed typo in authentication document.
51
+
3
52
  ### 0.1.3 / 2020-03-11
4
53
 
5
54
  * Generated numeric constants are formatted with underscores if necessary.
@@ -0,0 +1,160 @@
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(?<pre>[^`]*(`[^`]*`[^`]*)*[^`\\])?\{(?<inside>[^\s][^}]*)\}(?<post>.*)\z/m
25
+ @xref_detector = /\A(?<pre>[^`]*(`[^`]*`[^`]*)*)?\[(?<text>[\w\.]+)\]\[(?<addr>[\w\.]+)\](?<post>.*)\z/m
26
+ @list_element_detector = /\A\s*(\*|\+|-|[0-9a-zA-Z]+\.)\s/
27
+
28
+ class << self
29
+ ##
30
+ # Given an enumerable of lines, performs yardoc formatting, including:
31
+ # * Interpreting cross-references identified as described in AIP 192
32
+ # * Escaping literal braces that look like yardoc type links
33
+ #
34
+ # Tries to be smart about exempting preformatted text blocks.
35
+ #
36
+ # @param api [Gapic::Schema::Api]
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.
41
+ # @return [Enumerable<String>]
42
+ #
43
+ def format_doc_lines api, lines, disable_xrefs: false
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
+ if in_block == false
59
+ line = escape_line_braces line
60
+ line = format_line_xrefs api, line, disable_xrefs
61
+ end
62
+ end
63
+ line
64
+ end
65
+ end
66
+
67
+ ##
68
+ # Given a number, format it in such a way that Rubocop will be happy.
69
+ # Specifically, we add underscores if the magnitude is at least 10_000.
70
+ # This works for both integers and floats.
71
+ #
72
+ # @param value [Numeric]
73
+ # @return [String]
74
+ #
75
+ def format_number value
76
+ return value.to_s if value.abs < 10_000
77
+ str = value.is_a?(Integer) ? value.to_s : BigDecimal(value.to_f.to_s).to_s("F")
78
+ re = /^(-?\d+)(\d\d\d)([_\.][_\.\d]+)?$/
79
+ while (m = re.match str)
80
+ str = "#{m[1]}_#{m[2]}#{m[3]}"
81
+ end
82
+ str
83
+ end
84
+
85
+ private
86
+
87
+ def update_indent_state in_block, base_indent, line, indent
88
+ if in_block != true && @list_element_detector =~ line
89
+ in_block = false
90
+ indent = base_indent if indent > base_indent
91
+ base_indent = (indent + 7) / 4 * 4
92
+ else
93
+ in_block = indent >= base_indent + 4 unless in_block == false
94
+ base_indent = indent / 4 * 4 if in_block == false && indent < base_indent
95
+ end
96
+ [in_block, base_indent]
97
+ end
98
+
99
+ def line_indent line
100
+ m = /^( *)\S/.match line
101
+ m.nil? ? nil : m[1].length
102
+ end
103
+
104
+ def escape_line_braces line
105
+ while (m = @brace_detector.match line)
106
+ line = "#{m[:pre]}\\\\{#{m[:inside]}}#{m[:post]}"
107
+ end
108
+ line
109
+ end
110
+
111
+ def format_line_xrefs api, line, disable_xrefs
112
+ while (m = @xref_detector.match line)
113
+ entity = api.lookup m[:addr]
114
+ return line if entity.nil?
115
+ text = m[:text]
116
+ yard_link = disable_xrefs ? text : yard_link_for_entity(entity, text)
117
+ return line if yard_link.nil?
118
+ line = "#{m[:pre]}#{yard_link}#{m[:post]}"
119
+ end
120
+ line
121
+ end
122
+
123
+ ##
124
+ # Generate a YARD-style cross-reference for the given entity.
125
+ #
126
+ # @param entity [Gapic::Schema::Proto] the entity to link to
127
+ # @param text [String] the text for the link
128
+ # @return [String] YARD cross-reference syntax
129
+ #
130
+ def yard_link_for_entity entity, text
131
+ # As a special case, omit the service "google.longrunning.Operations"
132
+ # and its methods. This is because the generator creates
133
+ # service-specific copies of the operations client, rather than a
134
+ # Google::Longrunning::Operations::Client class, and there is in
135
+ # general no way to tell what the actual service-specific namespace is.
136
+ return text if entity.address[0, 3] == ["google", "longrunning", "Operations"]
137
+
138
+ case entity
139
+ when Gapic::Schema::Service
140
+ "{#{convert_address_to_ruby entity}::Client #{text}}"
141
+ when Gapic::Schema::Method
142
+ "{#{convert_address_to_ruby entity.parent}::Client##{entity.name.underscore} #{text}}"
143
+ when Gapic::Schema::Message, Gapic::Schema::Enum, Gapic::Schema::EnumValue
144
+ "{#{convert_address_to_ruby entity} #{text}}"
145
+ when Gapic::Schema::Field
146
+ "{#{convert_address_to_ruby entity.parent}##{entity.name} #{text}}"
147
+ end
148
+ end
149
+
150
+ def convert_address_to_ruby entity
151
+ file = entity.containing_file
152
+ api = file.containing_api
153
+ address = entity.address
154
+ address = address.join "." if address.is_a? Array
155
+ address = address.sub file.package, file.ruby_package if file.ruby_package&.present?
156
+ address.split(".").reject(&:empty?).map(&:camelize).map { |node| api.fix_namespace node }.join("::")
157
+ end
158
+ end
159
+ end
160
+ end
@@ -16,6 +16,6 @@
16
16
 
17
17
  module Gapic
18
18
  module Generator
19
- VERSION = "0.1.3"
19
+ VERSION = "0.2.2"
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