|
1 | 1 | use clap::{App, ArgMatches};
|
2 | 2 | use env_logger::Builder;
|
3 | 3 | use log::LevelFilter;
|
| 4 | +use std::path::Path; |
4 | 5 |
|
5 | 6 | #[derive(Debug)]
|
6 | 7 | pub enum SupportedLanguage {
|
@@ -37,55 +38,68 @@ impl Cli {
|
37 | 38 | Builder::new().filter_level(LevelFilter::Info).init();
|
38 | 39 | }
|
39 | 40 |
|
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)), |
76 | 46 | _ => {
|
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 | + } |
79 | 60 | }
|
| 61 | + }; |
| 62 | + |
| 63 | + if config.is_none() { |
| 64 | + error!("Argument configuration not yet supported!"); |
| 65 | + std::process::exit(1); |
80 | 66 | }
|
| 67 | + config.unwrap() |
| 68 | + } |
81 | 69 |
|
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 { |
83 | 100 | watch_patterns,
|
84 |
| - project_language, |
| 101 | + project_language: SupportedLanguage::Shell, |
85 | 102 | exec_commands,
|
86 |
| - }; |
87 |
| - debug!("Parsed params = {:?}", ret); |
88 |
| - |
89 |
| - ret |
| 103 | + } |
90 | 104 | }
|
91 | 105 | }
|
0 commit comments