Sha256: 902d846f4ab6910e586f56211611557d4052f7441c5a6dfa9b90d46fccf78eec

Contents?: true

Size: 1.42 KB

Versions: 5

Compression:

Stored size: 1.42 KB

Contents

module Wukong
  class Runner

    # Defines methods to help a Runner class load code passed in
    # dynamically on the command-line.
    #
    # The default behavior of code in this module is to load any Ruby
    # files (ending with `.rb`) passed in the command-line.
    module CodeLoader
      
      # Loads all code, whether from a deploy pack or additionally
      # passed on the command line.
      def load_args
        (args_to_load || []).each do |path|
          load_ruby_file(path)
        end
      end

      private
      
      # Load any additional code that we found out about on the
      # command-line.
      #
      # @return [Array<String>] paths to load culled from the ARGV.
      def args_to_load
        ruby_file_args || []
      end

      # Returns all pre-resolved arguments which are Ruby files.
      #
      # @return [Array<String>]
      def ruby_file_args
        ARGV.find_all { |arg| arg.to_s =~ /\.rb$/ && arg.to_s !~ /^--/ }
      end
      
      # Loads a single Ruby file, capturing LoadError and SyntaxError
      # and raising Wukong::Error instead (so it can be easily captured
      # by the Runner).
      #
      # @param [String] path
      # @raise [Wukong::Error] if there is an error
      def load_ruby_file path
        return unless path
        begin
          Kernel.load path
        rescue LoadError, SyntaxError => e
          raise Error.new(e)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
ul-wukong-4.1.1 lib/wukong/runner/code_loader.rb
ul-wukong-4.1.0 lib/wukong/runner/code_loader.rb
wukong-4.0.0 lib/wukong/runner/code_loader.rb
wukong-3.0.1 lib/wukong/runner/code_loader.rb
wukong-3.0.0 lib/wukong/runner/code_loader.rb