11local fs = require " endpoint.utils.fs"
2+ local class = require " endpoint.lib.middleclass"
23
34--- @class endpoint.Detector
4- local Detector = {}
5- Detector .__index = Detector
6-
7- --- Creates a new Detector instance with optional fields
8- function Detector :new (detection_name , fields )
9- local detector = setmetatable ({}, self )
10- detector .detection_name = detection_name or " unknown"
11-
12- -- Set additional fields if provided
13- if fields then
14- for key , value in pairs (fields ) do
15- detector [key ] = value
16- end
17- end
18-
19- return detector
20- end
5+ local Detector = class " Detector"
216
22- --- Creates a new Detector for dependency-based detection
23- function Detector :new_dependency_detector (required_dependencies , manifest_files , detection_name )
24- local detector = self :new (detection_name or " dependency_based_detection" , {
25- required_dependencies = required_dependencies or {},
26- manifest_files = manifest_files or {},
27- })
28- return detector
7+ function Detector :initialize (required_dependencies , manifest_files , detection_name )
8+ self .detection_name = detection_name or " dependency_detection"
9+ self .required_dependencies = required_dependencies or {}
10+ self .manifest_files = manifest_files or {}
2911end
3012
31- --- Detects if any of the required dependencies are present in manifest files
3213function Detector :is_target_detected ()
33- -- If we have dependencies and manifest files, use dependency detection
34- if self .required_dependencies and self .manifest_files then
35- -- First check root level manifest files
36- for _ , manifest_file_path in ipairs (self .manifest_files ) do
37- if fs .has_file { manifest_file_path } then
38- if self :_check_manifest_file_for_dependencies (manifest_file_path ) then
39- return true
40- end
14+ for _ , manifest_file_path in ipairs (self .manifest_files ) do
15+ if fs .has_file { manifest_file_path } then
16+ if self :_check_manifest_file_for_dependencies (manifest_file_path ) then
17+ return true
4118 end
4219 end
20+ end
4321
44- -- For Maven projects, also check submodules
45- if self :_should_check_submodules () then
46- local submodule_manifest_files = self :_find_submodule_manifest_files ()
47- for _ , submodule_manifest_path in ipairs (submodule_manifest_files ) do
48- if self :_check_manifest_file_for_dependencies (submodule_manifest_path ) then
49- return true
50- end
22+ if self :_should_check_submodules () then
23+ local submodule_manifest_files = self :_find_submodule_manifest_files ()
24+ for _ , submodule_manifest_path in ipairs (submodule_manifest_files ) do
25+ if self :_check_manifest_file_for_dependencies (submodule_manifest_path ) then
26+ return true
5127 end
5228 end
53-
54- return false
5529 end
5630
57- -- Default implementation for subclasses
58- error (" is_target_detected() must be implemented by subclass: " .. self .detection_name )
31+ return false
5932end
6033
6134--- Checks a specific manifest file for required dependencies
@@ -74,89 +47,66 @@ function Detector:get_name()
7447 return self .detection_name
7548end
7649
77- --- Gets detailed information about what was detected
7850function Detector :get_detection_details ()
7951 if not self :is_target_detected () then
8052 return nil
8153 end
8254
83- local base_details = {
84- detector_name = self .detection_name ,
85- detected_at = os.time (),
86- is_detected = true ,
87- }
55+ local detected_dependencies = {}
56+ local searched_manifest_files = {}
57+
58+ for _ , manifest_file_path in ipairs (self .manifest_files ) do
59+ if fs .has_file { manifest_file_path } then
60+ table.insert (searched_manifest_files , manifest_file_path )
8861
89- -- Add dependency-specific details if applicable
90- if self .required_dependencies and self .manifest_files then
91- local detected_dependencies = {}
92- local searched_manifest_files = {}
93-
94- for _ , manifest_file_path in ipairs (self .manifest_files ) do
95- if fs .has_file { manifest_file_path } then
96- table.insert (searched_manifest_files , manifest_file_path )
97-
98- for _ , required_dependency_identifier in ipairs (self .required_dependencies ) do
99- if fs .file_contains (manifest_file_path , required_dependency_identifier ) then
100- table.insert (detected_dependencies , {
101- dependency_identifier = required_dependency_identifier ,
102- found_in_manifest = manifest_file_path ,
103- })
104- end
62+ for _ , required_dependency_identifier in ipairs (self .required_dependencies ) do
63+ if fs .file_contains (manifest_file_path , required_dependency_identifier ) then
64+ table.insert (detected_dependencies , {
65+ dependency_identifier = required_dependency_identifier ,
66+ found_in_manifest = manifest_file_path ,
67+ })
10568 end
10669 end
10770 end
108-
109- base_details .detected_dependencies = detected_dependencies
110- base_details .searched_manifest_files = searched_manifest_files
111- base_details .total_required_dependencies = # self .required_dependencies
112- base_details .total_detected_dependencies = # detected_dependencies
11371 end
11472
115- return base_details
73+ return {
74+ detector_name = self .detection_name ,
75+ detected_at = os.time (),
76+ is_detected = true ,
77+ detected_dependencies = detected_dependencies ,
78+ searched_manifest_files = searched_manifest_files ,
79+ total_required_dependencies = # self .required_dependencies ,
80+ total_detected_dependencies = # detected_dependencies ,
81+ }
11682end
11783
118- --- Adds additional required dependencies to the detection criteria
11984function Detector :add_required_dependencies (additional_dependencies )
120- if not self .required_dependencies then
121- self .required_dependencies = {}
122- end
12385 for _ , additional_dependency_identifier in ipairs (additional_dependencies ) do
12486 table.insert (self .required_dependencies , additional_dependency_identifier )
12587 end
12688end
12789
128- --- Adds additional manifest files to search in
12990function Detector :add_manifest_files (additional_manifest_files )
130- if not self .manifest_files then
131- self .manifest_files = {}
132- end
13391 for _ , additional_manifest_file_path in ipairs (additional_manifest_files ) do
13492 table.insert (self .manifest_files , additional_manifest_file_path )
13593 end
13694end
13795
138- --- Gets the list of required dependencies
13996function Detector :get_required_dependencies ()
140- return vim .deepcopy (self .required_dependencies or {} )
97+ return vim .deepcopy (self .required_dependencies )
14198end
14299
143- --- Gets the list of manifest files to search
144100function Detector :get_manifest_files ()
145- return vim .deepcopy (self .manifest_files or {} )
101+ return vim .deepcopy (self .manifest_files )
146102end
147103
148104function Detector :_should_check_submodules ()
149- if not self .manifest_files then
150- return false
151- end
152-
153- -- Check if this is a Maven project
154105 for _ , manifest_file in ipairs (self .manifest_files ) do
155106 if manifest_file == " pom.xml" then
156107 return fs .has_file { " pom.xml" }
157108 end
158109 end
159-
160110 return false
161111end
162112
0 commit comments