gapic-generator 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +13 -0
- data/CHANGELOG.md +34 -0
- data/CODE_OF_CONDUCT.md +43 -0
- data/CONTRIBUTING.md +28 -0
- data/LICENSE +202 -0
- data/README.md +72 -0
- data/bin/gapic-generator +103 -0
- data/bin/protoc-gen-ruby_gapic +33 -0
- data/default-rubocop.yml +10 -0
- data/gem_templates/binary.erb +20 -0
- data/gem_templates/dockerfile.erb +39 -0
- data/gem_templates/entrypoint.erb +24 -0
- data/gem_templates/gapic_sh.erb +97 -0
- data/gem_templates/gemfile.erb +8 -0
- data/gem_templates/gemspec.erb +36 -0
- data/gem_templates/generator.erb +37 -0
- data/gem_templates/gitignore.erb +10 -0
- data/gem_templates/rakefile.erb +29 -0
- data/gem_templates/readme.erb +69 -0
- data/gem_templates/rubocop.erb +16 -0
- data/gem_templates/shared/_header.erb +4 -0
- data/gem_templates/shared/_license.erb +13 -0
- data/gem_templates/shared/_warning.erb +1 -0
- data/gem_templates/test_generator.erb +13 -0
- data/gem_templates/test_helper.erb +25 -0
- data/gem_templates/version.erb +10 -0
- data/lib/gapic/file_formatter.rb +62 -0
- data/lib/gapic/gem_builder.rb +98 -0
- data/lib/gapic/generator.rb +30 -0
- data/lib/gapic/generator/version.rb +21 -0
- data/lib/gapic/generators/base_generator.rb +91 -0
- data/lib/gapic/generators/default_generator.rb +101 -0
- data/lib/gapic/grpc_service_config/method_config.rb +49 -0
- data/lib/gapic/grpc_service_config/parser.rb +218 -0
- data/lib/gapic/grpc_service_config/parsing_error.rb +25 -0
- data/lib/gapic/grpc_service_config/retry_policy.rb +51 -0
- data/lib/gapic/grpc_service_config/service_config.rb +42 -0
- data/lib/gapic/path_template.rb +35 -0
- data/lib/gapic/path_template/parser.rb +83 -0
- data/lib/gapic/path_template/segment.rb +67 -0
- data/lib/gapic/resource_lookup.rb +91 -0
- data/lib/gapic/runner.rb +76 -0
- data/lib/gapic/schema.rb +17 -0
- data/lib/gapic/schema/api.rb +264 -0
- data/lib/gapic/schema/loader.rb +269 -0
- data/lib/gapic/schema/wrappers.rb +718 -0
- data/lib/google/api/annotations.pb.rb +39 -0
- data/lib/google/api/client.pb.rb +43 -0
- data/lib/google/api/field_behavior.pb.rb +51 -0
- data/lib/google/api/http.pb.rb +60 -0
- data/lib/google/api/resource.pb.rb +80 -0
- data/lib/google/longrunning/operations.pb.rb +115 -0
- data/lib/google/protobuf/any.pb.rb +40 -0
- data/lib/google/protobuf/compiler/plugin.pb.rb +72 -0
- data/lib/google/protobuf/descriptor.pb.rb +359 -0
- data/lib/google/protobuf/empty.pb.rb +36 -0
- data/lib/google/rpc/status.pb.rb +46 -0
- data/templates/default/gem/_version.erb +2 -0
- data/templates/default/gem/changelog.erb +3 -0
- data/templates/default/gem/gemfile.erb +4 -0
- data/templates/default/gem/gemspec.erb +37 -0
- data/templates/default/gem/gitignore.erb +18 -0
- data/templates/default/gem/license.erb +22 -0
- data/templates/default/gem/rakefile.erb +27 -0
- data/templates/default/gem/readme.erb +24 -0
- data/templates/default/gem/rubocop.erb +59 -0
- data/templates/default/gem/version.erb +6 -0
- data/templates/default/gem/yardopts.erb +12 -0
- data/templates/default/helpers/default_helper.rb +45 -0
- data/templates/default/helpers/filepath_helper.rb +38 -0
- data/templates/default/helpers/namespace_helper.rb +44 -0
- data/templates/default/helpers/presenter_helper.rb +24 -0
- data/templates/default/helpers/presenters/enum_presenter.rb +35 -0
- data/templates/default/helpers/presenters/enum_value_presenter.rb +33 -0
- data/templates/default/helpers/presenters/field_presenter.rb +146 -0
- data/templates/default/helpers/presenters/file_presenter.rb +53 -0
- data/templates/default/helpers/presenters/gem_presenter.rb +140 -0
- data/templates/default/helpers/presenters/message_presenter.rb +66 -0
- data/templates/default/helpers/presenters/method_presenter.rb +293 -0
- data/templates/default/helpers/presenters/package_presenter.rb +65 -0
- data/templates/default/helpers/presenters/resource_presenter.rb +92 -0
- data/templates/default/helpers/presenters/sample_presenter.rb +74 -0
- data/templates/default/helpers/presenters/service_presenter.rb +276 -0
- data/templates/default/layouts/_ruby.erb +20 -0
- data/templates/default/package.erb +6 -0
- data/templates/default/proto_docs/_enum.erb +13 -0
- data/templates/default/proto_docs/_message.erb +23 -0
- data/templates/default/proto_docs/_proto_file.erb +9 -0
- data/templates/default/proto_docs/proto_file.erb +6 -0
- data/templates/default/proto_docs/readme.erb +5 -0
- data/templates/default/service.erb +8 -0
- data/templates/default/service/client.erb +6 -0
- data/templates/default/service/client/_client.erb +137 -0
- data/templates/default/service/client/_config.erb +155 -0
- data/templates/default/service/client/_credentials.erb +21 -0
- data/templates/default/service/client/_helpers.erb +9 -0
- data/templates/default/service/client/_operations.erb +88 -0
- data/templates/default/service/client/_paths.erb +8 -0
- data/templates/default/service/client/_requires.erb +1 -0
- data/templates/default/service/client/_resource.erb +6 -0
- data/templates/default/service/client/_self_configure.erb +9 -0
- data/templates/default/service/client/_self_configure_defaults.erb +22 -0
- data/templates/default/service/client/_self_configure_retry_policy.erb +15 -0
- data/templates/default/service/client/method/_def.erb +21 -0
- data/templates/default/service/client/method/def/_options_defaults.erb +29 -0
- data/templates/default/service/client/method/def/_request.erb +6 -0
- data/templates/default/service/client/method/def/_request_normal.erb +4 -0
- data/templates/default/service/client/method/def/_request_streaming.erb +9 -0
- data/templates/default/service/client/method/def/_rescue.erb +1 -0
- data/templates/default/service/client/method/def/_response.erb +6 -0
- data/templates/default/service/client/method/def/_response_normal.erb +8 -0
- data/templates/default/service/client/method/def/_response_paged.erb +9 -0
- data/templates/default/service/client/method/docs/_error.erb +2 -0
- data/templates/default/service/client/method/docs/_request.erb +6 -0
- data/templates/default/service/client/method/docs/_request_field.erb +7 -0
- data/templates/default/service/client/method/docs/_request_normal.erb +20 -0
- data/templates/default/service/client/method/docs/_request_streaming.erb +5 -0
- data/templates/default/service/client/method/docs/_response.erb +6 -0
- data/templates/default/service/client/method/docs/_sample.erb +20 -0
- data/templates/default/service/client/method/docs/_sample_response.erb +24 -0
- data/templates/default/service/client/method/docs/_samples.erb +6 -0
- data/templates/default/service/client/method/docs/request_field/_arg.erb +10 -0
- data/templates/default/service/client/method/docs/request_field/_hash.erb +19 -0
- data/templates/default/service/client/method/docs/sample_response/_comment.erb +5 -0
- data/templates/default/service/client/method/docs/sample_response/_define.erb +2 -0
- data/templates/default/service/client/method/docs/sample_response/_loop.erb +12 -0
- data/templates/default/service/client/method/docs/sample_response/_print.erb +2 -0
- data/templates/default/service/client/method/docs/sample_response/_write_file.erb +2 -0
- data/templates/default/service/client/resource/_def.erb +6 -0
- data/templates/default/service/client/resource/_doc.erb +8 -0
- data/templates/default/service/client/resource/_multi.erb +28 -0
- data/templates/default/service/client/resource/_single.erb +11 -0
- data/templates/default/service/credentials.erb +6 -0
- data/templates/default/service/operations.erb +6 -0
- data/templates/default/service/paths.erb +6 -0
- data/templates/default/service/test/client.erb +24 -0
- data/templates/default/service/test/client_operations.erb +24 -0
- data/templates/default/service/test/method/_assert_response.erb +11 -0
- data/templates/default/service/test/method/_bidi.erb +100 -0
- data/templates/default/service/test/method/_client.erb +84 -0
- data/templates/default/service/test/method/_normal.erb +69 -0
- data/templates/default/service/test/method/_server.erb +85 -0
- data/templates/default/service/test/method/_setup.erb +21 -0
- data/templates/default/service/test/smoke.erb +12 -0
- data/templates/default/shared/_header.erb +4 -0
- data/templates/default/shared/_license.erb +21 -0
- data/templates/default/shared/_warning.erb +1 -0
- metadata +350 -0
@@ -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,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,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.2"
|
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
|
@@ -0,0 +1,101 @@
|
|
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/base_generator"
|
18
|
+
|
19
|
+
module Gapic
|
20
|
+
module Generators
|
21
|
+
# The generator orchestrates the rendering of templates.
|
22
|
+
class DefaultGenerator < BaseGenerator
|
23
|
+
# Initializes the generator.
|
24
|
+
#
|
25
|
+
# @param api [Gapic::Schema::Api] The API model/context to
|
26
|
+
# generate.
|
27
|
+
def initialize api
|
28
|
+
super
|
29
|
+
|
30
|
+
# Configure to use a custom templates directory
|
31
|
+
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
|
+
end
|
36
|
+
|
37
|
+
# Disable Rubocop because we expect generate to grow and violate more
|
38
|
+
# and more style rules.
|
39
|
+
# rubocop:disable all
|
40
|
+
|
41
|
+
# Generates all the files for the API.
|
42
|
+
#
|
43
|
+
# @return [Array<
|
44
|
+
# Google::Protobuf::Compiler::CodeGeneratorResponse::File>]
|
45
|
+
# The files that were generated for the API.
|
46
|
+
def generate
|
47
|
+
files = []
|
48
|
+
|
49
|
+
gem = gem_presenter @api
|
50
|
+
|
51
|
+
gem.packages.each do |package|
|
52
|
+
# Package level files
|
53
|
+
files << g("package.erb", "lib/#{package.package_file_path}", package: package)
|
54
|
+
|
55
|
+
package.services.each do |service|
|
56
|
+
# Service level files
|
57
|
+
files << g("service.erb", "lib/#{service.service_file_path}", service: service)
|
58
|
+
files << g("service/client.erb", "lib/#{service.client_file_path}", service: service)
|
59
|
+
files << g("service/credentials.erb", "lib/#{service.credentials_file_path}", service: service)
|
60
|
+
files << g("service/paths.erb", "lib/#{service.paths_file_path}", service: service) if service.paths?
|
61
|
+
files << g("service/operations.erb", "lib/#{service.operations_file_path}", service: service) if service.lro?
|
62
|
+
files << g("service/test/client.erb", "test/#{service.test_client_file_path}", service: service)
|
63
|
+
files << g("service/test/client_operations.erb", "test/#{service.test_client_operations_file_path}", service: service) if service.lro?
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Gem level files
|
68
|
+
files << g("gem/gitignore.erb", ".gitignore", gem: gem)
|
69
|
+
files << g("gem/version.erb", "lib/#{gem.version_file_path}", gem: gem)
|
70
|
+
files << g("gem/gemspec.erb", "#{gem.name}.gemspec", gem: gem)
|
71
|
+
files << g("gem/gemfile.erb", "Gemfile", gem: gem)
|
72
|
+
files << g("gem/rakefile.erb", "Rakefile", gem: gem)
|
73
|
+
files << g("gem/readme.erb", "README.md", gem: gem)
|
74
|
+
files << g("gem/changelog.erb", "CHANGELOG.md", gem: gem)
|
75
|
+
files << g("gem/rubocop.erb", ".rubocop.yml", gem: gem)
|
76
|
+
files << g("gem/yardopts.erb", ".yardopts", gem: gem)
|
77
|
+
files << g("gem/license.erb", "LICENSE.md", gem: gem)
|
78
|
+
|
79
|
+
gem.proto_files.each do |proto_file|
|
80
|
+
files << g("proto_docs/proto_file.erb", "proto_docs/#{proto_file.docs_file_path}", file: proto_file)
|
81
|
+
end
|
82
|
+
files << g("proto_docs/readme.erb", "proto_docs/README.md", gem: gem)
|
83
|
+
|
84
|
+
format_files files
|
85
|
+
|
86
|
+
files
|
87
|
+
end
|
88
|
+
|
89
|
+
# rubocop:enable all
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
##
|
94
|
+
# Override the default rubocop config file to be used.
|
95
|
+
def format_config
|
96
|
+
@api.configuration[:format_config] ||
|
97
|
+
super
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|