<commit_before>require "fileutils"

module DataMapperSalesforce
  class SoapWrapper
    class ClassesFailedToGenerate < StandardError; end

    def initialize(module_name, driver_name, wsdl_path, api_dir)
      @module_name, @driver_name, @wsdl_path, @api_dir = module_name, driver_name, File.expand_path(wsdl_path), File.expand_path(api_dir)
      generate_soap_classes
      driver
    end
    attr_reader :module_name, :driver_name, :wsdl_path, :api_dir

    def driver
      @driver ||= Object.const_get(module_name).const_get(driver_name).new
    end

    def generate_soap_classes
      unless File.file?(wsdl_path)
        raise Errno::ENOENT, "Could not find the WSDL at #{wsdl_path}"
      end

      unless File.directory?(wsdl_api_dir)
        FileUtils.mkdir_p wsdl_api_dir
      end

      generate_files unless files_exist?

      $:.push wsdl_api_dir
      require "#{module_name}Driver"
      $:.delete wsdl_api_dir
    end

    # Good candidate for shipping out into a Rakefile.
    def generate_files
      require 'wsdl/soap/wsdl2ruby'

      wsdl2ruby          = WSDL::SOAP::WSDL2Ruby.new
      wsdl2ruby.logger   = $LOG
      wsdl2ruby.location = wsdl_path
      wsdl2ruby.basedir  = wsdl_api_dir

      wsdl2ruby.opt.merge!({
        'classdef'         => module_name,
        'module_path'      => module_name,
        'mapping_registry' => nil,
        'driver'           => nil,
        'client_skelton'   => nil,
      })

      wsdl2ruby.run
    end

    def files_exist?
      ["#{module_name}.rb", "#{module_name}MappingRegistry.rb", "#{module_name}Driver.rb"].all? do |name|
        File.exist?("#{wsdl_api_dir}/#{name}")
      end<commit_msg>Raise (previously defined but unused) ClassesFailedToGenerate if files don't generate, and only set wsdl2ruby's horrible logger if $LOG is set (local convention).<commit_after>require "fileutils"

module DataMapperSalesforce
  class SoapWrapper
    class ClassesFailedToGenerate < StandardError; end

    attr_reader :module_name, :driver_name, :wsdl_path, :api_dir

    def initialize(module_name, driver_name, wsdl_path, api_dir)
      @module_name, @driver_name, @wsdl_path, @api_dir = module_name, driver_name, File.expand_path(wsdl_path), File.expand_path(api_dir)
      generate_soap_classes
      driver
    end

    def driver
      @driver ||= Object.const_get(module_name).const_get(driver_name).new
    end

    def generate_soap_classes
      unless File.file?(wsdl_path)
        raise Errno::ENOENT, "Could not find the WSDL at #{wsdl_path}"
      end

      unless File.directory?(wsdl_api_dir)
        FileUtils.mkdir_p wsdl_api_dir
      end

      generate_files unless files_exist?

      $:.push wsdl_api_dir
      require "#{module_name}Driver"
      $:.delete wsdl_api_dir
    end

    # Good candidate for shipping out into a Rakefile.
    def generate_files
      require 'wsdl/soap/wsdl2ruby'

      wsdl2ruby          = WSDL::SOAP::WSDL2Ruby.new
      wsdl2ruby.logger   = $LOG if $LOG
      wsdl2ruby.location = wsdl_path
      wsdl2ruby.basedir  = wsdl_api_dir

      wsdl2ruby.opt.merge!({
        'classdef'         => module_name,
        'module_path'      => module_name,
        'mapping_registry' => nil,
        'driver'           => nil,
        'client_skelton'   => nil,
      })

      wsdl2ruby.run

      raise ClassesFailedToGenerate unless files_exist?
    end

    def files_exist?
      ["#{module_name}.rb", "#{module_name}MappingRegistry.rb", "#{module_name}Driver.rb"].all? do |name|
        File.exist?("#{wsdl_api_dir}/#{name}")
      end
{'fraction_non_alphanumeric': 0.09744453805110924, 'fraction_numerical': 0.004773939904521202, 'mean_word_length': 2.8759521218715998, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2470445', 'n_tokens_mistral': 1257, 'n_tokens_neox': 1162, 'n_words': 240}