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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/lit_pry.rb', line 12
def require_relative relative_path, current_directory = nil
return false if relative_path.nil?
if relative_path.start_with?('/') && File.exist?(relative_path)
absolute_path = relative_path.split('/')
file_name = absolute_path.pop
else
if current_directory.nil?
absolute_path = caller_locations.first.absolute_path.split('/')
absolute_path.pop
else
absolute_path = current_directory.split('/')
end
if relative_path.start_with?('./')
relative_path.delete_prefix! './'
end
while relative_path.start_with?('../')
relative_path.delete_prefix! '../'
absolute_path.pop
end
if relative_path.split('/').count > 1
child_path = relative_path.split('/')
relative_path = child_path.pop
child_path.each do |dir|
absolute_path << dir
end
end
file_name = relative_path
end
unless file_name.end_with? '.rb'
file_name << '.rb'
end
file_path = File.join(absolute_path.join('/'), file_name)
unless @@lit_processed_paths.include? file_path
@@lit_processed_paths.add file_path
new_lines = ''
line_count = 0
new_lines_count = 0
File.foreach(file_path) do |line|
line_count = line_count + 1
if line.strip.start_with? 'require_relative '
line = line.strip + ", '#{absolute_path.join('/')}'\n"
new_lines << line
elsif lit_line = Kernel.add_lit_binding(line)
new_lines << lit_line
else
new_lines << line
end
end
eval(new_lines, get_binding(__dir__ = absolute_path), file_path)
return true
else
return false
end
end
|