Skip to content

Commit efb49ca

Browse files
Shreyas Balakrishnashreyasbharath
Shreyas Balakrishna
authored andcommitted
Blah
1 parent bdf49a1 commit efb49ca

File tree

6 files changed

+36
-13
lines changed

6 files changed

+36
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module DirectoryParser
2+
def glob_files(path, extensions)
3+
path = File.join(path, File::SEPARATOR) + '*' + extensions
4+
Dir.glob(path).select { |entry| File.file?(entry) }.compact
5+
end
6+
end

lib/cpp_dependency_graph/include_component_dependency_graph.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ def initialize(project)
1212

1313
def all_links
1414
components = @project.source_components
15-
all_source_files = components.values.flat_map(&:source_files)
16-
all_source_files.map do |file|
15+
@project.source_files.map do |file|
1716
links = file.includes.map { |inc| Link.new(file.basename, inc, false) }
1817
[file.basename, links]
1918
end.to_h

lib/cpp_dependency_graph/include_file_dependency_graph.rb

+2-9
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,15 @@ def initialize(project)
1010
@project = project
1111
end
1212

13-
def all_links
14-
# TODO: Implement
15-
{}
16-
end
17-
1813
def all_cyclic_links
1914
# TODO: Implement
2015
end
2116

2217
def links(file_name)
23-
components = @project.source_components
24-
all_source_files = components.values.flat_map(&:source_files)
25-
files = all_source_files.select do |file|
18+
files = @project.source_files.select do |_, file|
2619
file.includes.include?(file_name)
2720
end
28-
files.map do |file|
21+
files.map do |_, file|
2922
links = [Link.new(file.basename, file_name, false)]
3023
[file.basename, links]
3124
end.to_h

lib/cpp_dependency_graph/project.rb

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def source_component(name)
2121
source_components[name]
2222
end
2323

24+
def source_files
25+
@source_files ||= build_source_files
26+
end
27+
2428
def dependencies(component)
2529
# TODO: This is repeating the same work twice! component_for_include is called when calling external_includes
2630
external_includes(component).map { |include| @include_resolver.component_for_include(include) }.reject(&:empty?).uniq
@@ -32,6 +36,14 @@ def external_includes(component)
3236

3337
private
3438

39+
def build_source_files
40+
# TODO: Breaking Demeter's law here
41+
files = source_components.values.flat_map(&:source_files)
42+
files.map do |file|
43+
[file.path, file]
44+
end.to_h
45+
end
46+
3547
def build_source_components
3648
# TODO: Dealing with source components with same dir name?
3749
dirs = fetch_all_dirs(@path)

lib/cpp_dependency_graph/source_component.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# frozen_string_literal: true
22

33
require_relative 'config'
4+
require_relative 'directory_parser'
45
require_relative 'source_file'
56

67
# Abstracts a source directory containing source files
78
class SourceComponent
89
include Config
10+
include DirectoryParser
911

1012
attr_reader :path
1113

@@ -32,7 +34,6 @@ def loc
3234
private
3335

3436
def parse_source_files(extensions)
35-
path = File.join(@path, File::SEPARATOR) + '*' + extensions
36-
Dir.glob(path).map { |e| SourceFile.new(e) if File.file?(e) }.compact
37+
glob_files(@path, extensions).map { |e| SourceFile.new(e) }.compact
3738
end
3839
end

spec/test/project_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,22 @@
1414
expect(component.source_files.size).to eq(0)
1515
end
1616

17+
it 'returns null component if no such component exists' do
18+
component = Project.new('spec/test/example_project').source_component('Unknown')
19+
expect(component.name).to eq('NULL')
20+
expect(component.source_files.size).to eq(0)
21+
end
22+
1723
it 'returns dependencies of component' do
1824
project = Project.new('spec/test/example_project')
1925
component = project.source_component('Engine')
2026
dependencies = project.dependencies(component)
2127
expect(dependencies).to include('Framework', 'UI', 'DataAccess')
2228
end
29+
30+
it 'all source files of project' do
31+
project = Project.new('spec/test/example_project')
32+
source_files = project.source_files.values.map(&:basename).sort
33+
expect(source_files).to eq(["DA.h", "Display.cpp", "Display.h", "Engine.cpp", "Engine.h", "Engine.h", "OldEngine.h", "System.cpp", "System.h", "framework.h", "main.cpp"])
34+
end
2335
end

0 commit comments

Comments
 (0)