Class: FileFinderHelper

Inherits:
Object show all
Defined in:
lib/ceedling/file_finder_helper.rb

Instance Method Summary collapse

Instance Method Details

#find_best_path_in_collection(pathname, path_list, complain, description) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ceedling/file_finder_helper.rb', line 52

def find_best_path_in_collection(pathname, path_list, complain, description)
  # search our collection for the specified exact path
  raise "No path list provided for #{description} search" if path_list.nil?
  return pathname if path_list.include?(pathname)

  # Determine the closest match by looking for matching path segments, especially paths ENDING the same
  best_match_index = 0
  best_match_value = 0
  reverse_original_pieces = pathname.split(/(?:\\|\/)/).reverse
  path_list.each_with_index do |p,i|
    reverse_match_pieces = p.split(/(?:\\|\/)/).reverse
    # 
    num = reverse_original_pieces.zip(reverse_match_pieces).inject(0){|s,v| v[0] == v[1] ? s+3 : s}
    num = reverse_original_pieces.inject(num){|s,v| reverse_match_pieces.include?(v) ? s+1 : s}
    if num > best_match_value
      best_match_index = i 
      best_match_value = num 
    end
  end

  # If none of the options were a good match, handle to the best of our ability
  if (best_match_value == 0) && (reverse_original_pieces.length > 0)
    case (complain)
      when :error
        raise CeedlingException.new( "Found no path `#{pathname}` in #{description} search paths." ) 
      when :warn
        warning = "Found no path `#{pathname}` in #{description} search paths."
        @loginator.log( warning, Verbosity::COMPLAIN )
      when :ignore 
        # nothing further to do
    end
  end

  return path_list[best_match_index]
end

#find_file_in_collection(filename, file_list, complain, original_filepath = "") ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ceedling/file_finder_helper.rb', line 17

def find_file_in_collection(filename, file_list, complain, original_filepath="")
  # Search our collection for the specified base filename
  matches = file_list.find_all {|v| File.basename(v) == filename }
  
  case matches.length 
    when 0 
      matches = file_list.find_all {|v| v =~ /(?:\\|\/|^)#{filename}$/i}
      if (matches.length > 0)
        blow_up(filename, "However, a filename having different capitalization was found: '#{matches[0]}'.")
      end

      return handle_missing_file(filename, complain)
    when 1
      return matches[0]
    else
      # Determine the closest match by looking for matching path segments, especially paths ENDING the same
      best_match_index = 0
      best_match_value = 0
      reverse_original_pieces = original_filepath.split(/(?:\\|\/)/).reverse
      matches.each_with_index do |m,i|
        reverse_match_pieces = m.split(/(?:\\|\/)/).reverse

        num = reverse_original_pieces.zip(reverse_match_pieces).inject(0){|s,v| v[0] == v[1] ? s+3 : s}
        num = reverse_original_pieces.inject(num){|s,v| reverse_match_pieces.include?(v) ? s+1 : s}
        if num > best_match_value
          best_match_index = i 
          best_match_value = num 
        end
      end
      return matches[best_match_index]
  end

  return nil
end

#handle_missing_file(filename, complain) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ceedling/file_finder_helper.rb', line 88

def handle_missing_file(filename, complain)
  case (complain)
    when :error then blow_up(filename) 
    when :warn
      gripe(filename)
      return nil
    when :ignore then return nil
  end

  return nil
end