forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 3
/
compiler.rb
executable file
·88 lines (74 loc) · 2.81 KB
/
compiler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env ruby
# Copyright 2017 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.
$LOAD_PATH.unshift File.dirname(__FILE__)
# Run from compiler dir so all references are relative to the compiler
# executable. This allows the following command line:
# puppet-google-compute$ ../puppet-codegen/compiler products/compute $PWD
Dir.chdir(File.dirname(__FILE__))
# Our default timezone is UTC, to avoid local time compromise test code seed
# generation.
ENV['TZ'] = 'UTC'
require 'api/compiler'
require 'optparse'
require 'provider/ansible'
require 'provider/ansible/bundle'
require 'provider/chef'
require 'provider/chef/bundle'
require 'provider/example'
require 'provider/puppet'
require 'provider/puppet/bundle'
require 'provider/terraform'
require 'pp' if ENV['COMPILER_DEBUG']
catalog = nil
output = nil
provider = nil
types_to_generate = []
ARGV << '-h' if ARGV.empty?
OptionParser.new do |opt|
opt.on('-p', '--product PRODUCT', 'Folder with product catalog') do |p|
catalog = p
end
opt.on('-o', '--output OUTPUT', 'Folder for module output') do |o|
output = o
end
opt.on('-e', '--engine ENGINE', 'Technology to build for') do |e|
provider = "#{e}.yaml"
end
opt.on('-t', '--type TYPE[,TYPE...]', Array, 'Types to generate') do |t|
types_to_generate = t
end
opt.on('-h', '--help', 'Show this message') do
puts opt
exit
end
end.parse!
raise 'Option -p/--product is a required parameter' if catalog.nil?
raise 'Option -o/--output is a required parameter' if output.nil?
raise 'Option -e/--engine is a required parameter' if provider.nil?
raise "Product '#{catalog}' does not have api.yaml" \
unless File.exist?(File.join(catalog, 'api.yaml'))
raise "Product '#{catalog}' does not have #{provider} settings" \
unless File.exist?(File.join(catalog, provider))
raise "Output '#{output}' is not a directory" unless Dir.exist?(output)
Google::LOGGER.info "Compiling '#{catalog}' output to '#{output}'"
Google::LOGGER.info \
"Generating types: #{types_to_generate.empty? ? 'ALL' : types_to_generate}"
api = Api::Compiler.new(File.join(catalog, 'api.yaml')).run
api.validate
pp api if ENV['COMPILER_DEBUG']
config = Provider::Config.parse(File.join(catalog, provider), api)
config.validate
pp config if ENV['COMPILER_DEBUG']
provider = config.provider.new(config, api)
provider.generate output, types_to_generate