-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsource_file.rb
More file actions
63 lines (48 loc) · 1.16 KB
/
Copy pathsource_file.rb
File metadata and controls
63 lines (48 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true
# Source file and metadata
class SourceFile
def initialize(file)
@path = file
end
def basename
@basename ||= File.basename(@path)
end
def basename_no_extension
@basename_no_extension ||= File.basename(@path, File.extname(@path))
end
def path
@path ||= File.absolute_path(@path)
end
def header?
false # TODO: Implement check extension
end
def parent_component
@parent_component ||= File.dirname(@path).split('/').last
end
def includes
@includes ||= all_includes
end
def loc
@loc ||= file_contents.lines.count
end
def exists?
File.exist?(path)
end
private
def all_includes
@all_includes ||= scan_includes
end
def scan_includes
includes = file_contents.scan(/#include ["|<](.+)["|>]/) # TODO: use compiler lib to scan includes? llvm/clang?
includes.uniq!
includes.flatten
end
def file_contents
@file_contents ||= sanitised_file_contents
end
def sanitised_file_contents
contents = File.read(@path)
return contents if contents.valid_encoding?
contents.encode('UTF-16be', invalid: :replace, replace: '?').encode('UTF-8')
end
end