github.com/vmware/govmomi@v0.37.2/gen/gen_from_wsdl.rb (about)

     1  # Copyright (c) 2014 VMware, Inc. All Rights Reserved.
     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.
    14  
    15  $:.unshift(File.expand_path(File.dirname(__FILE__)))
    16  
    17  require "vim_wsdl"
    18  
    19  require "yaml"
    20  
    21  if !File.directory?(ARGV.first)
    22    raise "first argument not a directory"
    23  end
    24  
    25  target = ARGV[1]
    26  
    27  # Load the vijson yaml to fetch vijson schemas.
    28  vijson_path = File.join(File.expand_path("../sdk", __FILE__), target+".yaml")
    29  vijson = nil
    30  if File.exists?(vijson_path)
    31    vijson = YAML::load(File.open(vijson_path))["components"]["schemas"]
    32  end
    33  
    34  wsdl = WSDL.new(WSDL.read(target+".wsdl"), vijson)
    35  wsdl.validate_assumptions!
    36  wsdl.peek()
    37  
    38  ifs = Peek.types.keys.select { |name| Peek.base?(name) }.size()
    39  puts "%d classes, %d interfaces" % [Peek.types.size(), ifs]
    40  
    41  File.open(File.join(ARGV.first, "types/enum.go"), "w") do |io|
    42    io.print WSDL.header("types")
    43  
    44    wsdl.
    45      types.
    46      sort_by { |x| x.name }.
    47      uniq { |x| x.name }.
    48      select { |t| t.is_enum? }.
    49      each { |e| e.dump(io); e.dump_init(io) }
    50  end
    51  
    52  File.open(File.join(ARGV.first, "types/types.go"), "w") do |io|
    53    io.print WSDL.header("types")
    54    if target != "vim"
    55      io.print <<EOF
    56  import (
    57          "context"
    58          "github.com/vmware/govmomi/vim25/types"
    59  )
    60  EOF
    61    end
    62  
    63    wsdl.
    64      types.
    65      sort_by { |x| x.name }.
    66      uniq { |x| x.name }.
    67      select { |t| !t.is_enum? }.
    68      each { |e| e.dump(io); e.dump_init(io) }
    69  end
    70  
    71  File.open(File.join(ARGV.first, "types/if.go"), "w") do |io|
    72    io.print WSDL.header("types")
    73  
    74    Peek.dump_interfaces(io)
    75  end
    76  
    77  File.open(File.join(ARGV.first, "methods/methods.go"), "w") do |io|
    78    io.print WSDL.header("methods")
    79    if target == "vim"
    80      target += "25"
    81    end
    82  
    83    io.print <<EOF
    84  import (
    85          "context"
    86          "github.com/vmware/govmomi/#{target}/types"
    87          "github.com/vmware/govmomi/vim25/soap"
    88  )
    89  EOF
    90  
    91    wsdl.
    92      operations.
    93      sort_by { |x| x.name }.
    94      each { |e| e.dump(io) }
    95  end
    96  
    97  exit(0)