@@ -52,7 +52,11 @@ impl CodeRules {
52
52
let mut code_rules = CodeRules {
53
53
files_types : BTreeMap :: new ( ) ,
54
54
munchers : BTreeMap :: new ( ) ,
55
- file_ext_regex : Regex :: new ( "\\ .[a-zA-Z0-1_]+$" ) . unwrap ( ) ,
55
+ // c:/dir/foo.bar -> bar
56
+ // c:/dir/.bar -> bar
57
+ // c:/dir/foo -> foo
58
+ // dir\foo -> foo
59
+ file_ext_regex : Regex :: new ( r#"[\.\\/][a-zA-Z0-1_]+$|^[a-zA-Z0-1_]+$"# ) . unwrap ( ) ,
56
60
new_munchers : None ,
57
61
} ;
58
62
@@ -65,6 +69,7 @@ impl CodeRules {
65
69
. expect ( format ! ( "Invalid file_type contents: {}" , file) . as_str ( ) ) ;
66
70
67
71
if let Some ( ft) = FileType :: new ( & file, contents) {
72
+ debug ! ( "File type def found: {}" , ft. file_ext) ;
68
73
code_rules. files_types . insert ( ft. file_ext . clone ( ) , ft) ;
69
74
}
70
75
}
@@ -74,10 +79,22 @@ impl CodeRules {
74
79
75
80
/// Return the right muncher for the file extension extracted from the full path.
76
81
pub fn get_muncher ( & mut self , file_path : & String ) -> Option < & Muncher > {
77
- // try to get file extension
82
+ debug ! ( "Getting a muncher for: {}" , file_path) ;
83
+ // try to get file extension or the file name if it has no extension like Dockerfile
78
84
if let Some ( ext) = self . file_ext_regex . find ( & file_path) {
85
+ // the file ext regex returns the ext with the separator, which is a ., but if the file has no extension it returns
86
+ // the file name with the leading separator, which can be / or \
87
+ // if the file has chars outside what the regex expects in a valid ext or file name it returns nothing
88
+ let ext = ext
89
+ . as_str ( )
90
+ . trim_start_matches ( "." )
91
+ . trim_start_matches ( "\\ " )
92
+ . trim_start_matches ( "/" )
93
+ . to_lowercase ( ) ;
94
+ debug ! ( "Extracted file extension: {}" , ext) ;
79
95
// try to find a file_type match for the ext
80
- if let Some ( file_type) = self . files_types . get ( ext. as_str ( ) ) {
96
+ if let Some ( file_type) = self . files_types . get ( & ext) {
97
+ debug ! ( "Matching file-type: {}" , file_type. file_ext) ;
81
98
// try to find a matching muncher
82
99
if let Some ( muncher_name) = file_type. get_muncher_name ( file_path) {
83
100
// load the muncher from its file on the first use
@@ -104,6 +121,8 @@ impl CodeRules {
104
121
105
122
return self . munchers . get ( & muncher_name) . unwrap ( ) . as_ref ( ) ;
106
123
}
124
+ } else {
125
+ debug ! ( "File-type is unknown" ) ;
107
126
}
108
127
}
109
128
0 commit comments