Sha256: ef093e5ffa5f5d2807d9f7af8533168e82fb944c1c718685673186cfba901925

Contents?: true

Size: 1.47 KB

Versions: 36

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

require_relative "filter/path_matcher"

module ShopifyCLI
  module Theme
    class IncludeFilter
      include Filter::PathMatcher

      attr_reader :globs, :regexes

      def initialize(root, patterns = [])
        @root = Pathname.new(root)
        @patterns = patterns.nil? ? [] : patterns.compact.reject(&:empty?)

        regexes, globs = patterns_to_regexes_and_globs(@patterns)

        @regexes = regexes
        @globs = globs
      end

      def match?(path)
        return true unless present?(@patterns)

        path = path.to_s
        return true if path.empty?

        path = @root.join(path).to_s

        regexes.each do |regex|
          return true if regex_match?(regex, path)
        end

        globs.each do |glob|
          return true if glob_match?(glob, path)
        end

        false
      end

      private

      def present?(patterns)
        !patterns.nil? && !patterns.empty?
      end

      # Take in string patterns and convert them to either
      # regex patterns or glob patterns so that they are handled in an expected manner.
      def patterns_to_regexes_and_globs(patterns)
        new_regexes = []
        new_globs = []

        patterns
          .map(&:strip)
          .each do |pattern|
            if regex?(pattern)
              new_regexes << as_regex(pattern)
            else
              new_globs << as_glob(pattern)
            end
          end

        [new_regexes, new_globs]
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
shopify-cli-2.36.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.35.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.34.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.33.1 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.33.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.32.1 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.32.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.31.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.30.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.29.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.28.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.27.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.26.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.25.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.24.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.23.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.22.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.21.0 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.20.1 lib/shopify_cli/theme/include_filter.rb
shopify-cli-2.20.0 lib/shopify_cli/theme/include_filter.rb