| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | # Copyright 2015 Google Inc. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | # you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | # You may obtain a copy of the License at | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #      http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | # See the License for the specific language governing permissions and | 
					
						
							|  |  |  | # limitations under the License. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require 'google/apis/discovery_v1' | 
					
						
							|  |  |  | require 'google/apis/generator/annotator' | 
					
						
							|  |  |  | require 'google/apis/generator/model' | 
					
						
							|  |  |  | require 'google/apis/generator/template' | 
					
						
							| 
									
										
										
										
											2021-01-01 20:29:10 +00:00
										 |  |  | require 'google/apis/generator/updater' | 
					
						
							| 
									
										
										
										
											2020-12-31 22:55:59 +00:00
										 |  |  | require 'google/apis/generator/version' | 
					
						
							| 
									
										
										
										
											2021-01-01 20:29:10 +00:00
										 |  |  | require 'active_support' | 
					
						
							|  |  |  | require 'active_support/core_ext' | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  | require 'active_support/inflector' | 
					
						
							|  |  |  | require 'yaml' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Google | 
					
						
							|  |  |  |   module Apis | 
					
						
							|  |  |  |     # Generates ruby classes for APIs from discovery documents | 
					
						
							|  |  |  |     # @private | 
					
						
							|  |  |  |     class Generator | 
					
						
							|  |  |  |       Discovery = Google::Apis::DiscoveryV1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-31 22:55:59 +00:00
										 |  |  |       Result = Struct.new :files, :version_path, :changelog_path, :gem_name, :revision | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  |       # Load templates | 
					
						
							| 
									
										
										
										
											2017-08-18 22:35:22 +00:00
										 |  |  |       def initialize(api_names: nil, api_names_out: nil) | 
					
						
							| 
									
										
										
										
											2020-12-31 22:55:59 +00:00
										 |  |  |         @names = Google::Apis::Generator::Names.new(api_names_out || File.join(Google::Apis::ROOT, "api_names_out.yaml"), | 
					
						
							|  |  |  |                                                     api_names || File.join(Google::Apis::ROOT, "api_names.yaml")) | 
					
						
							|  |  |  |         @templates = {} | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # Generates ruby source for an API | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # @param [String] json | 
					
						
							|  |  |  |       #  API Description, as JSON text | 
					
						
							|  |  |  |       # @return [Hash<String,String>] | 
					
						
							|  |  |  |       #  Hash of generated files keyed by path | 
					
						
							|  |  |  |       def render(json) | 
					
						
							|  |  |  |         api = parse_description(json) | 
					
						
							|  |  |  |         Annotator.process(api, @names) | 
					
						
							|  |  |  |         context = { | 
					
						
							| 
									
										
										
										
											2020-12-31 22:55:59 +00:00
										 |  |  |           "api" => api, | 
					
						
							|  |  |  |           "generator_version" => Google::Apis::Generator::VERSION | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-31 22:55:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         base_path = api.gem_name | 
					
						
							|  |  |  |         lib_path = File.join(base_path, "lib") | 
					
						
							|  |  |  |         spec_path = File.join(base_path, "spec") | 
					
						
							|  |  |  |         module_path = File.join(lib_path, ActiveSupport::Inflector.underscore(api.qualified_name)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         result = Result.new({}, | 
					
						
							|  |  |  |                             File.join(module_path, "gem_version.rb"), | 
					
						
							|  |  |  |                             File.join(base_path, "CHANGELOG.md"), | 
					
						
							|  |  |  |                             api.gem_name, | 
					
						
							|  |  |  |                             api.revision) | 
					
						
							|  |  |  |         result.files[File.join(base_path, ".rspec")] = render_template("dot-rspec", context) | 
					
						
							|  |  |  |         result.files[File.join(base_path, ".yardopts")] = render_template("dot-yardopts", context) | 
					
						
							|  |  |  |         result.files[result.changelog_path] = render_template("initial-changelog.md", context) | 
					
						
							|  |  |  |         result.files[File.join(base_path, "Gemfile")] = render_template("gemfile", context) | 
					
						
							|  |  |  |         result.files[File.join(base_path, "#{api.gem_name}.gemspec")] = render_template("gemspec", context) | 
					
						
							|  |  |  |         result.files[File.join(base_path, "LICENSE.md")] = render_template("license.md", context) | 
					
						
							|  |  |  |         result.files[File.join(base_path, "OVERVIEW.md")] = render_template("overview.md", context) | 
					
						
							|  |  |  |         result.files[File.join(base_path, "Rakefile")] = render_template("rakefile", context) | 
					
						
							|  |  |  |         result.files[File.join(lib_path, "#{api.gem_name}.rb")] = render_template("entry-point.rb", context) | 
					
						
							|  |  |  |         result.files[module_path + ".rb"] = render_template("module.rb", context) | 
					
						
							|  |  |  |         result.files[File.join(module_path, "classes.rb")] = render_template("classes.rb", context) | 
					
						
							|  |  |  |         result.files[File.join(module_path, "representations.rb")] = render_template("representations.rb", context) | 
					
						
							|  |  |  |         result.files[File.join(module_path, "service.rb")] = render_template("service.rb", context) | 
					
						
							|  |  |  |         result.files[result.version_path] = render_template("initial-gem_version.rb", context) | 
					
						
							|  |  |  |         result.files[File.join(spec_path, "generated_spec.rb")] = render_template("generated_spec.rb", context) | 
					
						
							|  |  |  |         result | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def render_template(name, context) | 
					
						
							|  |  |  |         (@templates[name] ||= Template.load(name)).render(context) | 
					
						
							| 
									
										
										
										
											2015-04-17 00:28:38 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # Dump mapping of API names | 
					
						
							|  |  |  |       # @return [String] Mapping of paths to ruby names in YAML format | 
					
						
							|  |  |  |       def dump_api_names | 
					
						
							|  |  |  |         @names.dump | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def parse_description(json) | 
					
						
							|  |  |  |         Discovery::RestDescription::Representation.new(Discovery::RestDescription.new).from_json(json) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |