Sha256: c09feef8d1c334619db11f18ca0b8334cd647f59c1f3508f8316f17322a2da27

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

require 'pathname'

module Adminos
  module Operations
    class ExtractEnvironment
      OPTIONAL = %w[RAILS_ENV VERBOSE DISABLE_SPRING PORT WEB_CONCURRENCY]

      def extensions
        %w[ru thor rake rb yml ruby yaml erb builder markerb slim haml cap]
      end

      # @return [{String => <{path: Pathname, line: Integer}>}]
      def call(globs = '*')
        results = Hash.new { |hash, key| hash[key] = [] }

        Array(globs).each do |glob|
          Pathname.glob(glob).each do |item|
            next results.merge!(call(item.join('*'))) if item.directory?

            next unless extensions.detect { |ext| item.extname.to_s[ext] }

            item.readlines.each_with_index do |line, ix|
              capture_variables(line).each do |variable|
                next if OPTIONAL.include?(variable)

                results[variable] << { path: item, line: ix.succ }
              end
            end
          end
        end

        Hash[results.sort_by(&:first)]
      end

      private

      def capture_variables(line)
        line.scan(/ENV(?:\[|\.fetch\()['"]([^'"]+)['"]/).flatten
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
adminos-1.0.0.pre.rc.5 lib/adminos/operations/extract_environment.rb
adminos-1.0.0.pre.rc.4 lib/adminos/operations/extract_environment.rb
adminos-1.0.0.pre.rc.3 lib/adminos/operations/extract_environment.rb
adminos-1.0.0.pre.rc.2 lib/adminos/operations/extract_environment.rb
adminos-1.0.0.pre.rc.1 lib/adminos/operations/extract_environment.rb