Sha256: 457058a934f1046c0d540097d772aee8fb707c0711202ab6b8bf49b3ba15ac08

Contents?: true

Size: 1.14 KB

Versions: 17

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'set'
require 'pathname'

module Buildkite
  module Builder
    class Manifest::Rule
      GLOB_OPTIONS = File::FNM_PATHNAME | File::FNM_DOTMATCH | File::FNM_EXTGLOB

      attr_reader :exclude
      attr_reader :glob

      def initialize(root, pattern)
        @root = Pathname.new(root)
        @exclude = false
        @glob = @root

        if pattern[0] == '!'
          @exclude = true
          pattern = pattern[1..-1]
        end

        if pattern.start_with?('/')
          pattern = pattern[1..-1]
        else
          @glob = @glob.join('**')
        end

        @glob = @glob.join(pattern).to_s
      end

      def files
        @files ||= begin
          matched = Dir.glob(glob, GLOB_OPTIONS)
          matched.map! { |file| Pathname.new(file) }
          matched.reject!(&:directory?)
          matched.map! { |file| file.relative_path_from(Builder.root) }
          SortedSet.new(matched)
        end
      end

      def match?(file)
        file = Pathname.new(file)
        file = @root.join(file) unless file.absolute?

        File.fnmatch?(glob, file.to_s, GLOB_OPTIONS)
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
buildkite-builder-2.0.0 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-2.0.0.beta4 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-2.0.0.beta3 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-2.0.0.beta2 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-2.0.0.beta1 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-1.4.1 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-1.4.0 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-1.3.1 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-1.3.0 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-1.2.0 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-1.1.0 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-1.0.0 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-1.0.0.beta.5 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-1.0.0.beta.4 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-1.0.0.beta.3 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-1.0.0.beta.2 lib/buildkite/builder/manifest/rule.rb
buildkite-builder-1.0.0.beta.1 lib/buildkite/builder/manifest/rule.rb