Sha256: 81a061877bc00a292ea5772f75343724f602295a66731b0d493e6e890f92c275

Contents?: true

Size: 1.27 KB

Versions: 41

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/simple_cache'

module EacRubyUtils
  module Envs
    class Executable
      include ::EacRubyUtils::SimpleCache

      attr_reader :env, :name, :check_args

      def initialize(env, name, *check_args)
        @env = env
        @name = name
        @check_args = check_args
      end

      def exist?
        exist
      end

      def validate
        return nil if exist?

        "Program \"#{::Shellwords.join(executable_args)}\" not found in environment #{env}"
      end

      def validate!
        message = validate

        raise ProgramNotFoundError, message if message
      end

      def command(*command_args)
        validate!
        env.command(*executable_args, *command_args)
      end

      def executable_args
        executable_args_from_envvar || [name]
      end

      def executable_args_envvar
        "#{name}_command".variableize.upcase
      end

      def executable_args_from_envvar
        ENV[executable_args_envvar].if_present { |v| ::Shellwords.split(v) }
      end

      private

      def exist_uncached
        env.command(*executable_args, *check_args).execute!
        true
      rescue Errno::ENOENT
        false
      end

      class ProgramNotFoundError < ::StandardError; end
    end
  end
end

Version data entries

41 entries across 41 versions & 3 rubygems

Version Path
avm-tools-0.114.0 sub/eac_ruby_utils/lib/eac_ruby_utils/envs/executable.rb
eac_ruby_utils-0.85.0 lib/eac_ruby_utils/envs/executable.rb
eac_ruby_utils-0.84.0 lib/eac_ruby_utils/envs/executable.rb
eac_ruby_utils-0.83.0 lib/eac_ruby_utils/envs/executable.rb
avm-tools-0.113.6 sub/eac_ruby_utils/lib/eac_ruby_utils/envs/executable.rb
eac_ruby_utils-0.82.1 lib/eac_ruby_utils/envs/executable.rb
avm-tools-0.113.5 sub/eac_ruby_utils/lib/eac_ruby_utils/envs/executable.rb
avm-tools-0.113.4 sub/eac_ruby_utils/lib/eac_ruby_utils/envs/executable.rb
avm-tools-0.113.3 sub/eac_ruby_utils/lib/eac_ruby_utils/envs/executable.rb
eac_ruby_utils-0.82.0 lib/eac_ruby_utils/envs/executable.rb
eac_ruby_utils-0.81.0 lib/eac_ruby_utils/envs/executable.rb
avm-tools-0.113.2 sub/eac_ruby_utils/lib/eac_ruby_utils/envs/executable.rb
eac_ruby_utils-0.80.1 lib/eac_ruby_utils/envs/executable.rb
eac_ruby_utils-0.80.0 lib/eac_ruby_utils/envs/executable.rb
eac_ruby_utils-0.79.0 lib/eac_ruby_utils/envs/executable.rb
ehbrs-tools-0.28.3 vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/executable.rb
eac_ruby_utils-0.78.0 lib/eac_ruby_utils/envs/executable.rb
avm-tools-0.110.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/executable.rb
eac_ruby_utils-0.77.1 lib/eac_ruby_utils/envs/executable.rb
avm-tools-0.109.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/executable.rb