Sha256: b12883756107412af70b9cfda7e57d0e184ece46ca4e4cae65e254f65e61649a

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

require 'rake/tasklib'

module Rake
  module Funnel
    module Tasks
      class BinPath < Rake::TaskLib
        attr_accessor :name, :search_pattern, :path_modifier

        def initialize(*args, &task_block)
          setup_ivars(args)

          define(args, &task_block)
        end

        private

        def setup_ivars(args)
          @name = args.shift || :bin_path
          @search_pattern = %w(tools/* tools/*/bin packages/**/tools)
          @path_modifier = proc { |paths| paths }
        end

        def define(args, &task_block) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
          desc 'Add local binaries to PATH environment variable' unless Rake.application.last_description

          task(name, *args) do |_, task_args|
            yield(*[self, task_args].slice(0, task_block.arity)) if task_block

            next unless paths.any?

            prepend_pattern_to_path_environment(paths)
            $stderr.print "Added the following paths to the PATH environment variable:\n"
            paths.each do |p|
              $stderr.print "  - #{p}\n"
            end
          end

          self
        end

        def paths
          @paths ||= @path_modifier.call(Dir[*search_pattern].select { |path| File.directory?(path) })
                                   .map { |path| File.expand_path(path) }
                                   .sort
        end

        def prepend_pattern_to_path_environment(paths)
          ENV['PATH'] = ([] << paths << ENV['PATH']).flatten.join(File::PATH_SEPARATOR)
          paths
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rake-funnel-0.24.0 lib/rake/funnel/tasks/bin_path.rb
rake-funnel-0.23.0 lib/rake/funnel/tasks/bin_path.rb
rake-funnel-0.22.3 lib/rake/funnel/tasks/bin_path.rb