Skip to content

Commit 102eadd

Browse files
authored
Merge pull request #8 from alayshahh/automaticLang
Automatic Language Detection
2 parents f72867a + 0480cdf commit 102eadd

File tree

2 files changed

+65
-47
lines changed

2 files changed

+65
-47
lines changed

src/cli.rs

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clap::{App, ArgMatches};
22
use env_logger::Builder;
33
use log::LevelFilter;
4+
use std::path::Path;
45

56
#[derive(Debug)]
67
pub enum SupportedLanguage {
@@ -37,55 +38,68 @@ impl Cli {
3738
Builder::new().filter_level(LevelFilter::Info).init();
3839
}
3940

40-
let mut watch_patterns: Vec<String> = Vec::new();
41-
let project_language: SupportedLanguage;
42-
let mut exec_commands: Vec<String> = Vec::new();
43-
44-
match matches.subcommand() {
45-
("rust", Some(_)) => {
46-
debug!("Configuring for rust mode...");
47-
project_language = SupportedLanguage::Rust;
48-
watch_patterns.push("*.rs".to_string());
49-
watch_patterns.push("Cargo.toml".to_string());
50-
exec_commands.push("cargo build".to_string());
51-
exec_commands.push("cargo run".to_string());
52-
}
53-
("node", Some(_)) => {
54-
debug!("Configuring for node mode...");
55-
project_language = SupportedLanguage::Node;
56-
watch_patterns.push("*.js".to_string());
57-
watch_patterns.push("*.jsx".to_string());
58-
exec_commands.push("npm start".to_string());
59-
}
60-
("python", Some(_)) => {
61-
error!("Argument configuration not yet supported!");
62-
std::process::exit(1);
63-
}
64-
("shell", Some(sub_matcher)) => {
65-
debug!("Configuring for shell mode...");
66-
project_language = SupportedLanguage::Shell;
67-
debug!("Script Path = {:?}", sub_matcher.value_of("script"));
68-
debug!("Watch Pattern = {:?}", sub_matcher.value_of("watch"));
69-
let split = sub_matcher.value_of("watch").unwrap().split(',');
70-
for s in split {
71-
watch_patterns.push(format!("*{}", s.to_string()));
72-
}
73-
exec_commands.push(format!("bash {}", sub_matcher.value_of("script").unwrap()));
74-
debug!("{:?}", exec_commands);
75-
}
41+
let config: Option<Cli> = match matches.subcommand() {
42+
("rust", Some(_)) => Some(Self::build_rust_config()),
43+
("node", Some(_)) => Some(Self::build_node_config()),
44+
("python", Some(_)) => None,
45+
("shell", Some(sub_matcher)) => Some(Self::build_shell_config(sub_matcher)),
7646
_ => {
77-
error!("Argument configuration not yet supported!");
78-
std::process::exit(1);
47+
//automatic lang detection
48+
// if Path::new("lightmon.toml").exists(){
49+
// //TODO
50+
// } else if Path::new("nodemon.json").exists() {
51+
//TODO
52+
// }
53+
if Path::new("package.json").exists() {
54+
Some(Self::build_node_config())
55+
} else if Path::new("Cargo.toml").exists() {
56+
Some(Self::build_rust_config())
57+
} else {
58+
None
59+
}
7960
}
61+
};
62+
63+
if config.is_none() {
64+
error!("Argument configuration not yet supported!");
65+
std::process::exit(1);
8066
}
67+
config.unwrap()
68+
}
8169

82-
let ret = Cli {
70+
pub fn build_node_config() -> Self {
71+
debug!("Configuring for node mode...");
72+
Cli {
73+
watch_patterns: vec!["*.jsx".to_string(), ".js".to_string()],
74+
project_language: SupportedLanguage::Node,
75+
exec_commands: vec!["npm start".to_string()],
76+
}
77+
}
78+
79+
pub fn build_rust_config() -> Self {
80+
debug!("Configuring for rust mode...");
81+
Cli {
82+
watch_patterns: vec!["*.rs".to_string(), "Cargo.toml".to_string()],
83+
project_language: SupportedLanguage::Rust,
84+
exec_commands: vec!["cargo build".to_string(), "cargo run".to_string()],
85+
}
86+
}
87+
pub fn build_shell_config(sub_matcher: &ArgMatches) -> Self {
88+
let mut watch_patterns: Vec<String> = Vec::new();
89+
let mut exec_commands: Vec<String> = Vec::new();
90+
debug!("Configuring for shell mode...");
91+
debug!("Script Path = {:?}", sub_matcher.value_of("script"));
92+
debug!("Watch Pattern = {:?}", sub_matcher.value_of("watch"));
93+
let split = sub_matcher.value_of("watch").unwrap().split(',');
94+
for s in split {
95+
watch_patterns.push(format!("*{}", s.to_string()));
96+
}
97+
exec_commands.push(format!("bash {}", sub_matcher.value_of("script").unwrap()));
98+
debug!("{:?}", exec_commands);
99+
Cli {
83100
watch_patterns,
84-
project_language,
101+
project_language: SupportedLanguage::Shell,
85102
exec_commands,
86-
};
87-
debug!("Parsed params = {:?}", ret);
88-
89-
ret
103+
}
90104
}
91105
}

tests/cli.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extern crate assert_cmd;
22
extern crate predicates;
3+
use std::path::Path;
34

45
use assert_cmd::prelude::*;
56
//use predicates::prelude::*;
@@ -8,9 +9,12 @@ use std::process::Command;
89
#[test]
910
fn no_configuration_fails() -> Result<(), Box<dyn std::error::Error>> {
1011
let mut cmd = Command::cargo_bin("lightmon")?;
11-
cmd.assert().failure();
12-
13-
Ok(())
12+
if Path::new("package.json").exists() || Path::new("Cargo.toml").exists() {
13+
Ok(())
14+
} else {
15+
cmd.assert().failure();
16+
Ok(())
17+
}
1418
}
1519

1620
#[test]

0 commit comments

Comments
 (0)