Class: Dash::Dockerfile::Dockerignore

Inherits:
Object
  • Object
show all
Defined in:
lib/dash/dockerfile/dockerignore.rb

Overview

Just enough .dockerignore to answer "does this file ship in the build context?".

Not a reimplementation of BuildKit's matcher: negations are skipped rather than applied, so a path this says is covered might still ship. That direction is the safe one — it costs a piece of advice, never a false accusation.

Constant Summary collapse

FILENAME =
".dockerignore"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ Dockerignore

Returns a new instance of Dockerignore.



16
17
18
19
20
21
22
# File 'lib/dash/dockerfile/dockerignore.rb', line 16

def initialize(lines)
  @patterns = lines
    .map(&:strip)
    .reject { |line| line.empty? || line.start_with?("#", "!") }
    .map { |line| line.delete_prefix("**/").delete_prefix("./").delete_prefix("/").delete_suffix("/") }
    .reject(&:empty?)
end

Instance Attribute Details

#patterns ⇒ Object (readonly)

Returns the value of attribute patterns.



9
10
11
# File 'lib/dash/dockerfile/dockerignore.rb', line 9

def patterns
  @patterns
end

Class Method Details

.in(directory) ⇒ Object



11
12
13
14
# File 'lib/dash/dockerfile/dockerignore.rb', line 11

def self.in(directory)
  path = ::File.join(directory.to_s, FILENAME)
  new(::File.read(path).lines) if ::File.file?(path)
end

Instance Method Details

#covers?(path) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/dash/dockerfile/dockerignore.rb', line 24

def covers?(path)
  patterns.any? do |pattern|
    ::File.fnmatch?(pattern, path, ::File::FNM_DOTMATCH) || path.start_with?("#{pattern}/")
  end
end