gapic-generator-cloud 0.5.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 +7 -0
  2. data/CHANGELOG.md +117 -0
  3. data/CONTRIBUTING.md +28 -0
  4. data/LICENSE +202 -0
  5. data/README.md +84 -0
  6. data/bin/protoc-gen-ruby_cloud +33 -0
  7. data/bin/ruby-cloud-docker-entrypoint +130 -0
  8. data/cloud-rubocop.yml +14 -0
  9. data/lib/gapic/generator/cloud/version.rb +24 -0
  10. data/lib/gapic/generators/cloud_generator.rb +119 -0
  11. data/lib/gapic/presenters/wrapper_gem_presenter.rb +137 -0
  12. data/lib/gapic/presenters/wrapper_service_presenter.rb +53 -0
  13. data/templates/cloud/gem/authentication.erb +172 -0
  14. data/templates/cloud/gem/gemspec.erb +40 -0
  15. data/templates/cloud/gem/gitignore.erb +23 -0
  16. data/templates/cloud/gem/license.erb +204 -0
  17. data/templates/cloud/gem/rakefile.erb +157 -0
  18. data/templates/cloud/gem/readme.erb +93 -0
  19. data/templates/cloud/gem/repo-metadata.erb +6 -0
  20. data/templates/cloud/gem/rubocop.erb +36 -0
  21. data/templates/cloud/gem/yardopts.erb +13 -0
  22. data/templates/cloud/service/client/_credentials.erb +30 -0
  23. data/templates/cloud/service/client/_requires.erb +2 -0
  24. data/templates/cloud/service/client/_self_configure.erb +15 -0
  25. data/templates/cloud/service/client/method/def/_rescue.erb +3 -0
  26. data/templates/cloud/service/client/method/docs/_error.erb +2 -0
  27. data/templates/cloud/shared/_license.erb +13 -0
  28. data/templates/cloud/wrapper_gem/_main.erb +102 -0
  29. data/templates/cloud/wrapper_gem/client_test.erb +22 -0
  30. data/templates/cloud/wrapper_gem/entrypoint.erb +4 -0
  31. data/templates/cloud/wrapper_gem/gemfile.erb +14 -0
  32. data/templates/cloud/wrapper_gem/gemspec.erb +39 -0
  33. data/templates/cloud/wrapper_gem/main.erb +5 -0
  34. data/templates/cloud/wrapper_gem/rakefile.erb +190 -0
  35. data/templates/cloud/wrapper_gem/readme.erb +95 -0
  36. data/templates/cloud/wrapper_gem/rubocop.erb +23 -0
  37. data/templates/cloud/wrapper_gem/version_test.erb +11 -0
  38. data/templates/cloud/wrapper_gem/yardopts.erb +15 -0
  39. metadata +226 -0
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Copyright 2020 Google LLC
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require "optparse"
19
+ require "fileutils"
20
+
21
+ # Boolean options may have values "true" or "false" (as strings)
22
+ bool_option_map = {
23
+ ":gem.:free_tier" => "ruby-cloud-free-tier",
24
+ ":gem.:yard_strict" => "ruby-cloud-yard-strict"
25
+ }
26
+
27
+ # Value options always take string values.
28
+ value_option_map = {
29
+ "configuration" => "ruby-cloud-config",
30
+ ":gem.:name" => "ruby-cloud-gem-name",
31
+ ":gem.:namespace" => "ruby-cloud-gem-namespace",
32
+ ":gem.:title" => "ruby-cloud-title",
33
+ ":gem.:description" => "ruby-cloud-description",
34
+ ":gem.:summary" => "ruby-cloud-summary",
35
+ ":gem.:homepage" => "ruby-cloud-homepage",
36
+ ":gem.:env_prefix" => "ruby-cloud-env-prefix",
37
+ ":gem.:version_dependencies" => "ruby-cloud-wrapper-of",
38
+ ":gem.:migration_version" => "ruby-cloud-migration-version",
39
+ ":gem.:product_documentation_url" => "ruby-cloud-product-url",
40
+ ":gem.:api_id" => "ruby-cloud-api-id",
41
+ ":gem.:factory_method_suffix" => "ruby-cloud-factory-method-suffix"
42
+ }
43
+
44
+ # Path options take one or more file paths delimited by semicolons.
45
+ path_option_map = {
46
+ "samples" => ["ruby-cloud-samples", "samples"],
47
+ "grpc_service_config" => "ruby-cloud-grpc-service-config"
48
+ }
49
+
50
+ # Map options take one or more mappings delimited by semicolons. Each mapping
51
+ # must be of the form "key=value". Note that periods in keys are treated as
52
+ # literal periods, not subkey delimiters.
53
+ map_option_map = {
54
+ ":common_services" => "ruby-cloud-common-services",
55
+ ":overrides.:file_path" => "ruby-cloud-path-override",
56
+ ":overrides.:namespace" => "ruby-cloud-namespace-override"
57
+ }
58
+
59
+ # A set of files that, if generated, should be removed.
60
+ remove_paths = [
61
+ "lib/google/iam/v1/iam_policy_pb.rb",
62
+ "lib/google/cloud/common_resources_pb.rb"
63
+ ]
64
+
65
+ parameters = {}
66
+
67
+ OptionParser.new do |op|
68
+ bool_option_map.each do |key, flags|
69
+ flags = Array(flags).map { |f| "--#{f} BOOL" }
70
+ op.on(*flags) do |val|
71
+ val = val.downcase
72
+ parameters[key] = val if ["true", "false"].include? val
73
+ end
74
+ end
75
+ value_option_map.each do |key, flags|
76
+ flags = Array(flags).map { |f| "--#{f} VAL" }
77
+ op.on(*flags) do |val|
78
+ parameters[key] = val
79
+ end
80
+ end
81
+ path_option_map.each do |key, flags|
82
+ flags = Array(flags).map { |f| "--#{f} PATHS" }
83
+ op.on(*flags) do |paths|
84
+ parameters[key] = paths.split(";")
85
+ .map { |path| File.expand_path path, "/in" }
86
+ .join(";")
87
+ end
88
+ end
89
+ map_option_map.each do |key, flags|
90
+ flags = Array(flags).map { |f| "--#{f} MAPPING" }
91
+ op.on(*flags) do |mapping|
92
+ mapping.split(";").each do |mapping_kv|
93
+ mapping_key, mapping_val = mapping_kv.split("=", 2)
94
+ mapping_key.gsub!(".", "\\\\\\\\.")
95
+ parameters["#{key}.#{mapping_key}"] = mapping_val if mapping_val
96
+ end
97
+ end
98
+ end
99
+ end.parse! ARGV
100
+
101
+ parameter_strs =
102
+ parameters.map do |k, v|
103
+ v = v.gsub("\\", "\\\\\\\\").gsub(",", "\\\\,").gsub("=", "\\\\=")
104
+ "#{k}=#{v}"
105
+ end
106
+
107
+ ruby_plugin_args =
108
+ if parameters[":gem.:version_dependencies"]
109
+ []
110
+ else
111
+ ["--ruby_out=/out/lib", "--grpc_out=/out/lib"]
112
+ end
113
+
114
+ protoc_cmd = [
115
+ "grpc_tools_ruby_protoc",
116
+ # It is important for /in to come before /protos because all input files
117
+ # come from /in, and protoc will complain if any of those are also found
118
+ # earlier in the proto path.
119
+ "--proto_path=/in/", "--proto_path=/protos/"
120
+ ] + ruby_plugin_args + [
121
+ "--ruby_cloud_out=/out/",
122
+ "--ruby_cloud_opt", parameter_strs.join(",")
123
+ ] + ARGV + Dir.glob("/in/**/*.proto").sort
124
+
125
+ FileUtils.mkdir_p "/out/lib"
126
+ system(*protoc_cmd)
127
+
128
+ remove_paths.each do |path|
129
+ FileUtils.rm_f Dir.glob "/out/#{path}"
130
+ end
@@ -0,0 +1,14 @@
1
+ # This file is to be used as the config for formatting the generated cloud files.
2
+ # We can't reference the generated .rubocop.yml file because Rubocop is so clevar
3
+ # that it won't find the google-style gem unless the file is referenced here.
4
+ inherit_gem:
5
+ google-style: google-style.yml
6
+
7
+ AllCops:
8
+ Exclude:
9
+ - "acceptance/**/*"
10
+ - "*.gemspec"
11
+ - "lib/**/*_pb.rb"
12
+ - "Rakefile"
13
+ - "support/**/*"
14
+ - "test/**/*"
@@ -0,0 +1,24 @@
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
+
18
+ module Gapic
19
+ module Generator
20
+ module Cloud
21
+ VERSION = "0.5.0"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,119 @@
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/generators/default_generator"
18
+ require "gapic/presenters"
19
+ require "gapic/presenters/wrapper_gem_presenter"
20
+
21
+ module Gapic
22
+ module Generators
23
+ # The generator orchestrates the rendering of templates for Google Cloud
24
+ # projects.
25
+ class CloudGenerator < DefaultGenerator
26
+ # Initializes the generator.
27
+ #
28
+ # @param api [Gapic::Schema::Api] The API model/context to
29
+ # generate.
30
+ def initialize api
31
+ gem_config = api.configuration[:gem] ||= {}
32
+ gem_config[:homepage] ||= "https://github.com/googleapis/google-cloud-ruby"
33
+
34
+ super
35
+
36
+ # Configure to use prefer Google Cloud templates
37
+ use_templates! File.join __dir__, "../../../templates/cloud"
38
+ end
39
+
40
+ # Generates all the files for the API.
41
+ #
42
+ # @return [Array<
43
+ # Google::Protobuf::Compiler::CodeGeneratorResponse::File>]
44
+ # The files that were generated for the API.
45
+ def generate
46
+ gem_config = @api.configuration[:gem] ||= {}
47
+ return generate_wrapper if gem_config[:version_dependencies]
48
+
49
+ orig_files = super
50
+
51
+ cloud_files = []
52
+
53
+ gem = Gapic::Presenters.gem_presenter @api
54
+
55
+ # Additional Gem level files
56
+ cloud_files << g("gem/repo-metadata.erb", ".repo-metadata.json", gem: gem)
57
+ cloud_files << g("gem/authentication.erb", "AUTHENTICATION.md", gem: gem) unless gem.services.empty?
58
+
59
+ format_files cloud_files
60
+
61
+ orig_files + cloud_files
62
+ end
63
+
64
+ # Disable Rubocop because we expect generate to grow and violate more
65
+ # and more style rules.
66
+ # rubocop:disable all
67
+
68
+ # Generates the files for a wrapper.
69
+ #
70
+ # @return [Array<Google::Protobuf::Compiler::CodeGeneratorResponse::File>]
71
+ # The files that were generated for the API.
72
+ #
73
+ def generate_wrapper
74
+ files = []
75
+
76
+ gem = Gapic::Presenters.wrapper_gem_presenter @api
77
+
78
+ files << g("gem/gitignore.erb", ".gitignore", gem: gem)
79
+ files << g("gem/repo-metadata.erb", ".repo-metadata.json", gem: gem)
80
+ files << g("wrapper_gem/rubocop.erb", ".rubocop.yml", gem: gem)
81
+ files << g("wrapper_gem/yardopts.erb", ".yardopts", gem: gem)
82
+ files << g("gem/authentication.erb", "AUTHENTICATION.md", gem: gem)
83
+ files << g("gem/changelog.erb", "CHANGELOG.md", gem: gem)
84
+ files << g("wrapper_gem/gemfile.erb", "Gemfile", gem: gem)
85
+ files << g("gem/license.erb", "LICENSE.md", gem: gem)
86
+ files << g("wrapper_gem/rakefile.erb", "Rakefile", gem: gem)
87
+ files << g("wrapper_gem/readme.erb", "README.md", gem: gem)
88
+ files << g("wrapper_gem/gemspec.erb", "#{gem.name}.gemspec", gem: gem)
89
+ files << g("wrapper_gem/entrypoint.erb", "lib/#{gem.name}.rb", gem: gem) if gem.needs_entrypoint?
90
+ files << g("wrapper_gem/main.erb", "lib/#{gem.namespace_file_path}", gem: gem)
91
+ files << g("gem/version.erb", "lib/#{gem.version_file_path}", gem: gem)
92
+ files << g("gem/test_helper.erb", "test/helper.rb", gem: gem)
93
+ files << g("wrapper_gem/client_test.erb", "test/#{gem.namespace_require}/client_test.rb", gem: gem)
94
+ files << g("wrapper_gem/version_test.erb", "test/#{gem.namespace_require}/version_test.rb", gem: gem)
95
+
96
+ format_files files
97
+
98
+ files
99
+ end
100
+
101
+ # rubocop:enable all
102
+
103
+ private
104
+
105
+ ##
106
+ # Override the default rubocop config file to be used.
107
+ def format_config
108
+ @api.configuration[:format_config] ||
109
+ google_style_config
110
+ end
111
+
112
+ ##
113
+ # Path to the rubocop file for this project, which uses google-style
114
+ def google_style_config
115
+ File.expand_path File.join __dir__, "../../../cloud-rubocop.yml"
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,137 @@
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 "gapic/presenters"
18
+ require "gapic/presenters/wrapper_service_presenter"
19
+
20
+ module Gapic
21
+ module Presenters
22
+ ##
23
+ # A presenter for wrapper gems.
24
+ #
25
+ class WrapperGemPresenter < GemPresenter
26
+ def entrypoint_require
27
+ namespace_require
28
+ end
29
+
30
+ def services
31
+ @services ||= begin
32
+ files = @api.generate_files
33
+ files.map(&:services).flatten.map { |s| WrapperServicePresenter.new @api, s }
34
+ end
35
+ end
36
+
37
+ def namespace_require
38
+ ruby_file_path @api, namespace
39
+ end
40
+
41
+ def main_directory_name
42
+ namespace_require.split("/").last
43
+ end
44
+
45
+ def helpers_require
46
+ "#{namespace_require}/helpers"
47
+ end
48
+
49
+ def namespace_file_path
50
+ "#{namespace_require}.rb"
51
+ end
52
+
53
+ def needs_entrypoint?
54
+ name != namespace_file_path
55
+ end
56
+
57
+ def needs_default_config_block?
58
+ needs_entrypoint? && !google_cloud_short_name.nil?
59
+ end
60
+
61
+ def migration_version
62
+ gem_config :migration_version
63
+ end
64
+
65
+ # A description of the versions prior to the migration version.
66
+ # Could be "a.x" if the migration version is 1.0 or later, otherwise
67
+ # falls back to "pre-a.b".
68
+ def pre_migration_version
69
+ match = /^(\d)+\.0/.match migration_version.to_s
70
+ if match
71
+ major = match[1].to_i
72
+ return "#{major - 1}.x" if major.positive?
73
+ end
74
+ "pre-#{migration_version}"
75
+ end
76
+
77
+ def migration?
78
+ migration_version ? true : false
79
+ end
80
+
81
+ def extra_files
82
+ files = ["README.md", "AUTHENTICATION.md", "LICENSE.md", ".yardopts"]
83
+ files << "MIGRATING.md" if migration?
84
+ files
85
+ end
86
+
87
+ def factory_method_suffix
88
+ gem_config(:factory_method_suffix).to_s
89
+ end
90
+
91
+ def version_dependencies
92
+ gem_config(:version_dependencies).to_s.split(";").map { |str| str.split ":" }
93
+ end
94
+
95
+ def gem_version_dependencies
96
+ version_dependencies.sort_by { |version, _requirement| version }
97
+ .map { |version, requirement| ["#{name}-#{version}", requirement] }
98
+ end
99
+
100
+ def versioned_gems
101
+ gem_version_dependencies.map { |name, _requirement| name }
102
+ end
103
+
104
+ def default_version
105
+ version_dependencies.first&.first
106
+ end
107
+
108
+ def google_cloud_short_name
109
+ m = /^google-cloud-(.*)$/.match name
110
+ return nil unless m
111
+ m[1].tr "-", "_"
112
+ end
113
+
114
+ def docs_link version: nil, class_name: nil, text: nil
115
+ gem_name = version ? "#{name}-#{version}" : name
116
+ base_url = "https://googleapis.dev/ruby/#{gem_name}/latest"
117
+ if class_name
118
+ path = namespace.gsub "::", "/"
119
+ path = "#{path}/#{version.capitalize}" if version
120
+ class_path = class_name.gsub "::", "/"
121
+ text ||= namespaced_class class_name, version: version
122
+ return "[#{text}](#{base_url}/#{path}/#{class_path}.html)"
123
+ end
124
+ "[#{text || name}](#{base_url})"
125
+ end
126
+
127
+ def namespaced_class name, version: nil
128
+ base = version ? "#{namespace}::#{version.capitalize}" : namespace
129
+ "#{base}::#{name}"
130
+ end
131
+ end
132
+
133
+ def self.wrapper_gem_presenter api
134
+ WrapperGemPresenter.new api
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,53 @@
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 "gapic/presenters"
18
+ require "gapic/ruby_info"
19
+
20
+ module Gapic
21
+ module Presenters
22
+ ##
23
+ # A presenter for wrapper services.
24
+ #
25
+ class WrapperServicePresenter < ServicePresenter
26
+ def gem
27
+ WrapperGemPresenter.new @api
28
+ end
29
+
30
+ def factory_method_name
31
+ @factory_method_name ||= begin
32
+ method_name = ActiveSupport::Inflector.underscore name
33
+ suffix = gem.factory_method_suffix
34
+ method_name = "#{method_name}#{suffix}" unless method_name.end_with? suffix
35
+ method_name = "#{method_name}_client" if Gapic::RubyInfo.excluded_method_names.include? method_name
36
+ method_name
37
+ end
38
+ end
39
+
40
+ def create_client_call
41
+ "#{gem.namespace}.#{factory_method_name}"
42
+ end
43
+
44
+ def configure_client_call
45
+ "#{gem.namespace}.configure"
46
+ end
47
+
48
+ def credentials_class_xref
49
+ "`#{credentials_name_full}`"
50
+ end
51
+ end
52
+ end
53
+ end