1 |
# frozen_string_literal: true
|
|
2 |
|
|
3 |
module Coverband |
14✔ |
4 |
module Utils |
14✔ |
5 |
class RelativeFileConverter |
14✔ |
6 |
def self.instance |
14✔ |
7 |
@instance ||= new(Coverband.configuration.all_root_paths) |
709✔ |
8 |
end
|
|
9 |
|
|
10 |
def self.reset |
14✔ |
11 |
@instance = nil |
× |
12 |
end
|
|
13 |
|
|
14 |
def self.convert(file) |
14✔ |
15 |
instance.convert(file) |
709✔ |
16 |
end
|
|
17 |
|
|
18 |
def initialize(roots) |
14✔ |
19 |
@cache = {}
|
14✔ |
20 |
@roots = normalize(roots)
|
14✔ |
21 |
end
|
|
22 |
|
|
23 |
def convert(file) |
14✔ |
24 |
@cache[file] ||= begin |
709✔ |
25 |
relative_file = file |
579✔ |
26 |
@roots.each do |root| |
579✔ |
27 |
relative_file = file.gsub(/^#{root}/, ".") |
579✔ |
28 |
break relative_file if relative_file.start_with?(".") |
579✔ |
29 |
end
|
|
30 |
relative_file |
579✔ |
31 |
end
|
|
32 |
end
|
|
33 |
|
|
34 |
private |
14✔ |
35 |
|
|
36 |
def normalize(paths) |
14✔ |
37 |
paths.map { |root| File.expand_path(root) }
|
28✔ |
38 |
end
|
|
39 |
end
|
|
40 |
end
|
|
41 |
end
|