11use std:: fs;
2+ use std:: path:: Path ;
23
34/// detect the framework used in the current project, such as spring boot, gofr, fastapi, etc.
45pub fn detect_framework ( ) -> Option < String > {
56 let current_dir = std:: env:: current_dir ( ) . ok ( ) ?;
6- if current_dir. join ( "pom.xml" ) . exists ( ) {
7- if let Ok ( file_content) = fs:: read_to_string ( "pom.xml" ) {
7+ detect_framework_in_dir ( & current_dir)
8+ }
9+
10+ /// detect the framework used in the specified directory
11+ pub fn detect_framework_in_dir ( dir : & Path ) -> Option < String > {
12+ if dir. join ( "pom.xml" ) . exists ( ) {
13+ if let Ok ( file_content) = fs:: read_to_string ( dir. join ( "pom.xml" ) ) {
814 return if file_content. contains ( "<groupId>org.springframework.boot</groupId>" ) {
915 Some ( "spring-boot" . to_string ( ) )
1016 } else {
1117 None
1218 } ;
1319 }
14- } else if current_dir. join ( "build.gradle" ) . exists ( ) {
15- if let Ok ( file_content) = fs:: read_to_string ( "build.gradle" ) {
20+ } else if dir. join ( "build.gradle" ) . exists ( ) || dir. join ( "build.gradle.kts" ) . exists ( ) {
21+ let gradle_file = if dir. join ( "build.gradle" ) . exists ( ) {
22+ dir. join ( "build.gradle" )
23+ } else {
24+ dir. join ( "build.gradle.kts" )
25+ } ;
26+
27+ if let Ok ( file_content) = fs:: read_to_string ( gradle_file) {
1628 return if file_content. contains ( "org.springframework.boot" ) {
1729 Some ( "spring-boot" . to_string ( ) )
1830 } else {
1931 None
2032 } ;
2133 }
22- } else if current_dir . join ( "go.mod" ) . exists ( ) {
23- if let Ok ( file_content) = fs:: read_to_string ( "go.mod" ) {
34+ } else if dir . join ( "go.mod" ) . exists ( ) {
35+ if let Ok ( file_content) = fs:: read_to_string ( dir . join ( "go.mod" ) ) {
2436 return if file_content. contains ( "gofr.dev" ) {
2537 Some ( "gofr" . to_string ( ) )
2638 } else {
2739 None
2840 } ;
2941 }
30- } else if current_dir . join ( "pyproject.toml" ) . exists ( ) {
31- if let Ok ( file_content) = fs:: read_to_string ( "pyproject.toml" ) {
42+ } else if dir . join ( "pyproject.toml" ) . exists ( ) {
43+ if let Ok ( file_content) = fs:: read_to_string ( dir . join ( "pyproject.toml" ) ) {
3244 return if file_content. contains ( "\" fastapi[standard]" ) {
3345 Some ( "fastapi" . to_string ( ) )
3446 } else if file_content. contains ( "\" flask" ) {
@@ -40,3 +52,118 @@ pub fn detect_framework() -> Option<String> {
4052 }
4153 None
4254}
55+
56+ #[ cfg( test) ]
57+ mod tests {
58+ use super :: * ;
59+ use std:: fs;
60+ use std:: io:: Write ;
61+ use tempfile:: TempDir ;
62+
63+ #[ test]
64+ fn test_detect_spring_boot_with_pom_xml ( ) {
65+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
66+ let pom_path = temp_dir. path ( ) . join ( "pom.xml" ) ;
67+
68+ let mut file = fs:: File :: create ( & pom_path) . unwrap ( ) ;
69+ file. write_all ( b"<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n <project>\n <groupId>org.springframework.boot</groupId>\n <artifactId>demo</artifactId>\n </project>" ) . unwrap ( ) ;
70+
71+ let result = detect_framework_in_dir ( temp_dir. path ( ) ) ;
72+
73+ assert_eq ! ( result, Some ( "spring-boot" . to_string( ) ) ) ;
74+ }
75+
76+ #[ test]
77+ fn test_detect_spring_boot_with_build_gradle ( ) {
78+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
79+ let gradle_path = temp_dir. path ( ) . join ( "build.gradle" ) ;
80+
81+ let mut file = fs:: File :: create ( & gradle_path) . unwrap ( ) ;
82+ file. write_all ( b"plugins {\n id 'org.springframework.boot' version '3.2.0'\n }\n " ) . unwrap ( ) ;
83+
84+ let result = detect_framework_in_dir ( temp_dir. path ( ) ) ;
85+
86+ assert_eq ! ( result, Some ( "spring-boot" . to_string( ) ) ) ;
87+ }
88+
89+ #[ test]
90+ fn test_detect_spring_boot_with_build_gradle_kts ( ) {
91+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
92+ let gradle_kts_path = temp_dir. path ( ) . join ( "build.gradle.kts" ) ;
93+
94+ let mut file = fs:: File :: create ( & gradle_kts_path) . unwrap ( ) ;
95+ file. write_all ( b"plugins {\n id(\" org.springframework.boot\" ) version \" 3.2.0\" \n }\n " ) . unwrap ( ) ;
96+
97+ let result = detect_framework_in_dir ( temp_dir. path ( ) ) ;
98+
99+ assert_eq ! ( result, Some ( "spring-boot" . to_string( ) ) ) ;
100+ }
101+
102+ #[ test]
103+ fn test_detect_gofr_framework ( ) {
104+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
105+ let go_mod_path = temp_dir. path ( ) . join ( "go.mod" ) ;
106+
107+ let mut file = fs:: File :: create ( & go_mod_path) . unwrap ( ) ;
108+ file. write_all ( b"module example.com/myapp\n \n require gofr.dev v1.0.0\n " ) . unwrap ( ) ;
109+
110+ let result = detect_framework_in_dir ( temp_dir. path ( ) ) ;
111+
112+ assert_eq ! ( result, Some ( "gofr" . to_string( ) ) ) ;
113+ }
114+
115+ #[ test]
116+ fn test_detect_fastapi_framework ( ) {
117+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
118+ let pyproject_path = temp_dir. path ( ) . join ( "pyproject.toml" ) ;
119+
120+ let mut file = fs:: File :: create ( & pyproject_path) . unwrap ( ) ;
121+ file. write_all ( b"[project]\n dependencies = [\" fastapi[standard]\" ]\n " ) . unwrap ( ) ;
122+
123+ let result = detect_framework_in_dir ( temp_dir. path ( ) ) ;
124+
125+ assert_eq ! ( result, Some ( "fastapi" . to_string( ) ) ) ;
126+ }
127+
128+ #[ test]
129+ fn test_detect_flask_framework ( ) {
130+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
131+ let pyproject_path = temp_dir. path ( ) . join ( "pyproject.toml" ) ;
132+
133+ let mut file = fs:: File :: create ( & pyproject_path) . unwrap ( ) ;
134+ file. write_all ( b"[project]\n dependencies = [\" flask\" ]\n " ) . unwrap ( ) ;
135+
136+ let result = detect_framework_in_dir ( temp_dir. path ( ) ) ;
137+
138+ assert_eq ! ( result, Some ( "flask" . to_string( ) ) ) ;
139+ }
140+
141+ #[ test]
142+ fn test_no_framework_detected ( ) {
143+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
144+
145+ let result = detect_framework_in_dir ( temp_dir. path ( ) ) ;
146+
147+ assert_eq ! ( result, None ) ;
148+ }
149+
150+ #[ test]
151+ fn test_build_gradle_takes_precedence ( ) {
152+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
153+
154+ // Create both build.gradle and build.gradle.kts
155+ let gradle_path = temp_dir. path ( ) . join ( "build.gradle" ) ;
156+ let gradle_kts_path = temp_dir. path ( ) . join ( "build.gradle.kts" ) ;
157+
158+ let mut file1 = fs:: File :: create ( & gradle_path) . unwrap ( ) ;
159+ file1. write_all ( b"plugins {\n id 'org.springframework.boot' version '3.2.0'\n }\n " ) . unwrap ( ) ;
160+
161+ let mut file2 = fs:: File :: create ( & gradle_kts_path) . unwrap ( ) ;
162+ file2. write_all ( b"// Kotlin DSL file\n " ) . unwrap ( ) ;
163+
164+ let result = detect_framework_in_dir ( temp_dir. path ( ) ) ;
165+
166+ // Should detect spring-boot from build.gradle (which comes first in the check)
167+ assert_eq ! ( result, Some ( "spring-boot" . to_string( ) ) ) ;
168+ }
169+ }
0 commit comments