Skip to content

Commit

Permalink
Import scripts used for code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pieter Noordhuis committed Aug 14, 2014
1 parent 71c53d0 commit 5a0e65e
Show file tree
Hide file tree
Showing 6 changed files with 1,043 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gen/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "https://rubygems.org"

gem "nokogiri"
12 changes: 12 additions & 0 deletions gen/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
GEM
remote: https://rubygems.org/
specs:
mini_portile (0.6.0)
nokogiri (1.6.3.1)
mini_portile (= 0.6.0)

PLATFORMS
ruby

DEPENDENCIES
nokogiri
34 changes: 34 additions & 0 deletions gen/gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

# Copyright (c) 2014 VMware, Inc. All Rights Reserved.
#
# 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.

set -e

dst=../vim25

pkgs=$(echo $dst/{types,methods,tasks,mo})
mkdir -p $pkgs

bundle exec ruby gen_from_wsdl.rb $dst
bundle exec ruby gen_from_vmodl.rb $dst

for p in $pkgs
do
echo $p
cd $p
goimports -w *.go
go install
cd -
done
205 changes: 205 additions & 0 deletions gen/gen_from_vmodl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# Copyright (c) 2014 VMware, Inc. All Rights Reserved.
#
# 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.

$:.unshift(File.expand_path(File.dirname(__FILE__)))

require "vim_wsdl"

require "test/unit"

PATH = File.expand_path("../rbvmomi", __FILE__)

def read(file)
File.open(File.join(PATH, file))
end

class Prop
def initialize(vmodl, data)
@vmodl = vmodl
@data = data
end

def slice?
@data["is-array"]
end

def optional?
@data["is-optional"]
end

def name
@data["name"]
end

def var_field
n = name
n[0].capitalize + n[1..-1]
end

def var_type_prefix(base=false)
if slice?
"[]"
else
if optional? && !base
"*"
else
""
end
end
end

def var_type
type = @data["wsdl_type"]
if @vmodl.managed_hash.has_key?(type)
type = "ManagedObjectReference"
end

# Fix up type from vmodl
case type
when "TypeName", "MethodName"
type = "xsd:string"
when "ManagedObject"
type = "ManagedObjectReference"
when "xsd:anyType"
type = "AnyType"
end

if type =~ /^xsd:(.*)$/
type = $1
case type
when "string"
when "int"
when "boolean"
type ="bool"
when "long"
type ="int64"
when "dateTime"
type ="time.Time"
prefix += "*" if !slice? && optional?
when "byte"
when "double"
type ="float64"
when "float"
type ="float32"
when "short"
type ="int16"
when "base64Binary"
type ="[]byte"
else
raise "unknown type: %s" % type
end
else
if Peek.base?(type)
type = "Base" + type
base = true
end
type = "types." + type
end

var_type_prefix(base) + type
end

def var_tag
"mo:\"%s\"" % name
end

def dump(io)
io.print "%s %s `%s`\n" % [var_field, var_type, var_tag]
end
end

class Managed
def initialize(vmodl, name, data)
@vmodl = vmodl
@name = name
@data = data
end

def name
@name
end

def props
@data["props"].map do |p|
Prop.new(@vmodl, p)
end
end

def dump(io)
if !props.empty?
io.print "type %s struct {\n" % name

case @data["wsdl_base"]
when nil, "ManagedObject", "View"
# skip
else
if @data["wsdl_base"]
io.print "%s\n\n" % @data["wsdl_base"]
end
end

props.each do |p|
p.dump(io)
end
io.print "}\n\n"
end
end
end

class Vmodl
def initialize(data)
@data = Marshal.load(data)
end

def managed_hash
@managed_hash ||= begin
h = {}
managed.each do |m|
h[m.name] = m
end
h
end
end

def managed
@data.map do |k,v|
next if !v.is_a?(Hash)
next if v["kind"] != "managed"
next if k =~ /^pbm/i

Managed.new(self, k, v)
end.compact
end
end

if !File.directory?(ARGV.first)
raise "first argument not a directory"
end

wsdl = WSDL.new(WSDL.read "vim.wsdl")
wsdl.validate_assumptions!
wsdl.peek()

mo_go = File.open(File.join(ARGV.first, "mo/mo.go"), "w")
io = mo_go
io.print "package mo\n\n"

vmodl = Vmodl.new(read "vmodl.db")

vmodl.
managed.
sort_by { |m| m.name }.
each { |m| m.dump(io) }

exit(0)
76 changes: 76 additions & 0 deletions gen/gen_from_wsdl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright (c) 2014 VMware, Inc. All Rights Reserved.
#
# 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.

$:.unshift(File.expand_path(File.dirname(__FILE__)))

require "vim_wsdl"

if !File.directory?(ARGV.first)
raise "first argument not a directory"
end

wsdl = WSDL.new(WSDL.read "vim.wsdl")
wsdl.validate_assumptions!
wsdl.peek()

ifs = Peek.types.keys.select { |name| Peek.base?(name) }.size()
puts "%d classes, %d interfaces" % [Peek.types.size(), ifs]

types_go = File.open(File.join(ARGV.first, "types/types.go"), "w")
io = types_go
io.print "package types\n\n"

wsdl.
types.
sort_by { |x| x.name }.
uniq { |x| x.name }.
select { |x| x.name[0] == x.name[0].upcase }. # Only capitalized methods for now...
select { |t| t.is_enum? }.
each { |e| e.dump(io); e.dump_init(io) }

wsdl.
types.
sort_by { |x| x.name }.
uniq { |x| x.name }.
select { |x| x.name[0] == x.name[0].upcase }. # Only capitalized methods for now...
select { |t| !t.is_enum? }.
each { |e| e.dump(io); e.dump_init(io) }

if_go = File.open(File.join(ARGV.first, "types/if.go"), "w")
io = if_go
io.print "package types\n\n"
Peek.dump_interfaces(io)

methods_go = File.open(File.join(ARGV.first, "methods/methods.go"), "w")
io = methods_go
io.print "package methods\n\n"

wsdl.
operations.
sort_by { |x| x.name }.
select { |x| x.name[0] == x.name[0].upcase }. # Only capitalized methods for now...
each { |e| e.dump(io) }

methods_go = File.open(File.join(ARGV.first, "tasks/tasks.go"), "w")
io = methods_go
io.print "package tasks\n\n"

wsdl.
operations.
sort_by { |x| x.name }.
select { |x| x.name[0] == x.name[0].upcase }. # Only capitalized methods for now...
select { |x| x.name =~ /_Task$/ }.
each { |e| e.dump_task(io) }

exit(0)
Loading

0 comments on commit 5a0e65e

Please sign in to comment.