Skip to content

Commit e141b85

Browse files
committed
Added munchers for Go, Shell, CICD, fixed no-ext file names
1 parent fa6ff80 commit e141b85

30 files changed

+264
-48
lines changed

stackmuncher_lib/src/code_rules.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ impl CodeRules {
5252
let mut code_rules = CodeRules {
5353
files_types: BTreeMap::new(),
5454
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(),
5660
new_munchers: None,
5761
};
5862

@@ -65,6 +69,7 @@ impl CodeRules {
6569
.expect(format!("Invalid file_type contents: {}", file).as_str());
6670

6771
if let Some(ft) = FileType::new(&file, contents) {
72+
debug!("File type def found: {}", ft.file_ext);
6873
code_rules.files_types.insert(ft.file_ext.clone(), ft);
6974
}
7075
}
@@ -74,10 +79,22 @@ impl CodeRules {
7479

7580
/// Return the right muncher for the file extension extracted from the full path.
7681
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
7884
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);
7995
// 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);
8198
// try to find a matching muncher
8299
if let Some(muncher_name) = file_type.get_muncher_name(file_path) {
83100
// load the muncher from its file on the first use
@@ -104,6 +121,8 @@ impl CodeRules {
104121

105122
return self.munchers.get(&muncher_name).unwrap().as_ref();
106123
}
124+
} else {
125+
debug!("File-type is unknown");
107126
}
108127
}
109128

stackmuncher_lib/src/file_type.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ impl FileType {
4444
};
4545

4646
// set the file ext from the file name
47-
// e.g. `cs.json` -> `.cs`
48-
let file_ext = &file_name[..file_name.len() - 5];
49-
file_def.file_ext = [".", file_ext].concat();
47+
// e.g. `cs.json` -> `cs`
48+
file_def.file_ext = file_name[..file_name.len() - 5].to_lowercase();
5049

5150
// compile regex on matches (FileTypeMatch)
5251
if let Some(file_type_matches) = file_def.matches.as_mut() {
@@ -55,7 +54,7 @@ impl FileType {
5554
let muncher_name = match file_type_match.muncher.as_ref() {
5655
Some(v) => v,
5756
None => {
58-
error!("Missing muncher name for {}", file_ext);
57+
error!("Missing muncher name for {}", file_def.file_ext);
5958
return None;
6059
}
6160
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "https://gist.githubusercontent.com/rimutaka/e47dc710fa05367ad5d9a286a28a6f62/raw/2864315c6a537bc76db364ff72f5517e26933c1e/stm_file-type.json",
3+
"line_endings": "unix",
4+
"matches": [
5+
{
6+
"muncher": "docker"
7+
}
8+
]
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "https://gist.githubusercontent.com/rimutaka/e47dc710fa05367ad5d9a286a28a6f62/raw/2864315c6a537bc76db364ff72f5517e26933c1e/stm_file-type.json",
3+
"line_endings": "unix",
4+
"matches": [
5+
{
6+
"muncher": "docker",
7+
"in_path": [
8+
"dockerignore$"
9+
]
10+
}
11+
]
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/stackmuncher/stm_app/master/stackmuncher_lib/stm_rules/schemas/file-type.json",
3+
"line_endings": "unix",
4+
"matches": [
5+
{
6+
"muncher": "go"
7+
}
8+
]
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/stackmuncher/stm_app/master/stackmuncher_lib/stm_rules/schemas/file-type.json",
3+
"line_endings": "unix",
4+
"matches": [
5+
{
6+
"muncher": "shell"
7+
}
8+
]
9+
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
{
2-
"$schema": "https://raw.githubusercontent.com/stackmuncher/stm_app/master/stackmuncher_lib/stm_rules/schemas/file-type.json",
2+
"$schema": "https://gist.githubusercontent.com/rimutaka/e47dc710fa05367ad5d9a286a28a6f62/raw/2864315c6a537bc76db364ff72f5517e26933c1e/stm_file-type.json",
33
"line_endings": "unix",
44
"matches": [
55
{
66
"muncher": "rust.cargo.toml",
77
"in_path": [
88
"Cargo\\.toml$"
99
]
10+
},
11+
{
12+
"muncher": "go.gopkg.toml",
13+
"in_path": [
14+
"Gopkg\\.toml$"
15+
]
1016
}
1117
]
1218
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "https://gist.githubusercontent.com/rimutaka/e47dc710fa05367ad5d9a286a28a6f62/raw/2864315c6a537bc76db364ff72f5517e26933c1e/stm_file-type.json",
3+
"line_endings": "unix",
4+
"matches": [
5+
{
6+
"muncher": "cicd.appveyor.yml",
7+
"in_path": [
8+
"appveyor\\.yml$"
9+
]
10+
},
11+
{
12+
"muncher": "cicd.circleci.yml",
13+
"in_path": [
14+
"circle\\.yml$"
15+
]
16+
},
17+
{
18+
"muncher": "cicd.codecov.yml",
19+
"in_path": [
20+
"codecov\\.yml$"
21+
]
22+
},
23+
{
24+
"muncher": "cicd.travis.yml",
25+
"in_path": [
26+
"travis\\.yml$"
27+
]
28+
}
29+
]
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/stackmuncher/stm_app/master/stackmuncher_lib/stm_rules/schemas/muncher.json",
3+
"line_endings": "unix",
4+
"language": "AppVeyor",
5+
"line_comments": [
6+
"^[[:blank:]]*#.{5,}"
7+
],
8+
"inline_comments": [
9+
"[.[^#\\s]]+[[:blank:]]+#.{5,}"
10+
],
11+
"bracket_only": [
12+
"^[[:blank:]]*[{}\\[\\]\\(\\)][[:blank:];,)]*$",
13+
"^[[:blank:]]*#[[:blank:]]*.{0,5}$"
14+
]
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/stackmuncher/stm_app/master/stackmuncher_lib/stm_rules/schemas/muncher.json",
3+
"line_endings": "unix",
4+
"language": "CircleCI",
5+
"line_comments": [
6+
"^[[:blank:]]*#.{5,}"
7+
],
8+
"inline_comments": [
9+
"[.[^#\\s]]+[[:blank:]]+#.{5,}"
10+
],
11+
"bracket_only": [
12+
"^[[:blank:]]*[{}\\[\\]\\(\\)][[:blank:];,)]*$",
13+
"^[[:blank:]]*#[[:blank:]]*.{0,5}$"
14+
]
15+
}

0 commit comments

Comments
 (0)