Class: Onload::IgnoreFile

Inherits:
Object show all
Defined in:
lib/onload/ignore_file.rb

Constant Summary collapse

ONLOAD_SECTION_START =
"##### ONLOAD BUILD ARTIFACTS (AUTO-GENERATED) #####"
ONLOAD_SECTION_STOP =
"##### END ONLOAD BUILD ARTIFACTS #####"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest_path, ignored_paths) ⇒ IgnoreFile

Returns a new instance of IgnoreFile.



43
44
45
46
47
# File 'lib/onload/ignore_file.rb', line 43

def initialize(manifest_path, ignored_paths)
  @manifest_path = manifest_path
  @ignored_paths = ignored_paths
  @dirty = false
end

Instance Attribute Details

#dirtyObject (readonly) Also known as: dirty?

Returns the value of attribute dirty.



39
40
41
# File 'lib/onload/ignore_file.rb', line 39

def dirty
  @dirty
end

#manifest_pathObject (readonly)

Returns the value of attribute manifest_path.



39
40
41
# File 'lib/onload/ignore_file.rb', line 39

def manifest_path
  @manifest_path
end

Class Method Details

.load(manifest_path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/onload/ignore_file.rb', line 13

def load(manifest_path)
  manifest_path = ::File.expand_path(manifest_path)
  lines = ::File.read(manifest_path).split(/\r?\n/)
  start_idx = lines.index(ONLOAD_SECTION_START)
  stop_idx = lines.index(ONLOAD_SECTION_STOP)

  # Start and stop indices should either both be numbers or both be nil.
  # Anything else, and something weird is going on.
  if start_idx.class != stop_idx.class
    raise MalformedIgnoreFileError, "the ignore file at #{manifest_path} appears to be malformed and onload doesn't know how to modify it"
  end

  ignored_paths = if start_idx && stop_idx
    lines[start_idx..stop_idx].map(&:strip)
  else
    []
  end

  ignored_paths.reject! do |path|
    path.empty? || path.start_with?("#")
  end

  new(manifest_path, Set.new(ignored_paths))
end

Instance Method Details

#add(path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/onload/ignore_file.rb', line 55

def add(path)
  ignored_path = ::File.expand_path(path)
  ignored_path_segments = ignored_path.split(::File::SEPARATOR)

  if ignored_path_segments[0...manifest_dirname_segments.size] != manifest_dirname_segments
    raise "file to ignore #{path} is not relative to the specified ignore file at #{manifest_path}"
  end

  relative_ignored_path = ignored_path_segments[manifest_dirname_segments.size..-1].join(::File::SEPARATOR)
  @ignored_paths << relative_ignored_path

  @dirty = true

  nil
end

#includes?(path) ⇒ Boolean Also known as: include?

Returns:

  • (Boolean)


49
50
51
# File 'lib/onload/ignore_file.rb', line 49

def includes?(path)
  @ignored_paths.include?(path)
end

#persist!Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/onload/ignore_file.rb', line 71

def persist!
  return unless dirty?

  lines = ::File.read(manifest_path).split(/\r?\n/)
  start_idx = lines.index(ONLOAD_SECTION_START)
  stop_idx = lines.index(ONLOAD_SECTION_STOP)

  if start_idx && stop_idx
    lines[start_idx..stop_idx] = [
      ONLOAD_SECTION_START,
      *@ignored_paths,
      ONLOAD_SECTION_STOP
    ]
  else
    lines += [
      "",
      ONLOAD_SECTION_START,
      *@ignored_paths,
      ONLOAD_SECTION_STOP,
      ""
    ]
  end

  contents = lines.join("\n")
  contents << "\n" unless contents.end_with?("\n")

  ::File.write(manifest_path, contents)

  @dirty = false
end