Sha256: d6ba4b19986d21c1af76a1c3a6282b82c8da1dff8456d2f70d398b877734e867

Contents?: true

Size: 1.52 KB

Versions: 14

Compression:

Stored size: 1.52 KB

Contents

require 'fileutils'
require 'haveapi/client'

module HaveAPI::GoClient
  class Generator
    # Destination directory
    # @return [String]
    attr_reader :dst

    # Go module name
    # @return [String]
    attr_reader :module

    # Go package name
    # @return [String]
    attr_reader :package

    # @param url [String] API URL
    # @param dst [String] destination directory
    # @param opts [Hash]
    # @option opts [String] :version
    # @option opts [String] :module
    # @option opts [String] :package
    def initialize(url, dst, opts)
      @dst = dst
      @module = opts[:module]
      @package = opts[:package]

      conn = HaveAPI::Client::Communicator.new(url)
      @api = ApiVersion.new(conn.describe_api(opts[:version]))
    end

    def generate
      FileUtils.mkpath(dst)

      if self.module
        ErbTemplate.render_to_if_changed(
          'go.mod',
          { mod: self.module },
          File.join(dst, 'go.mod')
        )

        @dst = File.join(dst, package)
        FileUtils.mkpath(dst)
      end

      %w[client authentication request response types].each do |v|
        ErbTemplate.render_to_if_changed(
          "#{v}.go",
          {
            package:,
            api:
          },
          File.join(dst, "#{v}.go")
        )
      end

      api.resources.each { |r| r.generate(self) }
      api.auth_methods.each { |v| v.generate(self) }
    end

    def go_fmt
      return if system('go', 'fmt', chdir: dst)

      raise 'go fmt failed'
    end

    protected

    attr_reader :api
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
haveapi-go-client-0.25.0 lib/haveapi/go_client/generator.rb
haveapi-go-client-0.24.0 lib/haveapi/go_client/generator.rb
haveapi-go-client-0.23.7 lib/haveapi/go_client/generator.rb
haveapi-go-client-0.23.6 lib/haveapi/go_client/generator.rb
haveapi-go-client-0.23.5 lib/haveapi/go_client/generator.rb
haveapi-go-client-0.23.4 lib/haveapi/go_client/generator.rb
haveapi-go-client-0.23.3 lib/haveapi/go_client/generator.rb
haveapi-go-client-0.23.2 lib/haveapi/go_client/generator.rb
haveapi-go-client-0.23.1 lib/haveapi/go_client/generator.rb
haveapi-go-client-0.23.0 lib/haveapi/go_client/generator.rb
haveapi-go-client-0.22.1 lib/haveapi/go_client/generator.rb
haveapi-go-client-0.22.0 lib/haveapi/go_client/generator.rb
haveapi-go-client-0.21.1 lib/haveapi/go_client/generator.rb
haveapi-go-client-0.21.0 lib/haveapi/go_client/generator.rb