Skip to content

Commit

Permalink
Merge pull request #4 from deftsp/shihpin
Browse files Browse the repository at this point in the history
  • Loading branch information
lencx authored Apr 29, 2022
2 parents fbe883c + 3074f83 commit 313cabc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ serde = "1.0.133"
serde_derive = "1.0.133"
toml = "0.5.8"
which = "4.2.4"
ignore = "0.4.18"
ignore = "0.4.18"
tokio = { version = "1.18.0", features = ["macros", "rt-multi-thread"] }
13 changes: 8 additions & 5 deletions src/core/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;

use crate::config::{CrateConfig, RswConfig};
use crate::core::{Build, Clean, Create, Init, Link, RswInfo, Watch};
Expand Down Expand Up @@ -52,7 +53,7 @@ impl Cli {
Cli::rsw_build();
}
Commands::Watch => {
Cli::rsw_watch(Some(Box::new(|a, b| {
Cli::rsw_watch(Some(Arc::new(|a, b| {
let name = &a.name;
let path = &b.to_string_lossy().to_string();
let info_content = format!(
Expand All @@ -72,11 +73,13 @@ impl Cli {
}
}
pub fn rsw_build() {
Cli::wp_build(Rc::new(Cli::parse_toml()), "build", false);
Cli::wp_build(Arc::new(Cli::parse_toml()), "build", false);
}
pub fn rsw_watch(callback: Option<Box<dyn Fn(&CrateConfig, std::path::PathBuf)>>) {
pub fn rsw_watch(
callback: Option<Arc<dyn Fn(&CrateConfig, std::path::PathBuf) + Send + Sync + 'static>>,
) {
// initial build
let config = Rc::new(Cli::parse_toml());
let config = Arc::new(Cli::parse_toml());
Cli::wp_build(config.clone(), "watch", true);

Watch::new(config, callback.unwrap()).init();
Expand Down Expand Up @@ -117,7 +120,7 @@ impl Cli {

config
}
pub fn wp_build(config: Rc<RswConfig>, rsw_type: &str, is_link: bool) {
pub fn wp_build(config: Arc<RswConfig>, rsw_type: &str, is_link: bool) {
let crates_map = Rc::new(RefCell::new(HashMap::new()));

let cli = &config.cli.to_owned().unwrap();
Expand Down
50 changes: 34 additions & 16 deletions src/core/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use notify::{DebouncedEvent::*, RecursiveMode::*, Watcher};
use path_clean::PathClean;
use regex::Regex;
use std::{
collections::HashMap, path::PathBuf, rc::Rc, sync::mpsc::channel, thread::sleep, time::Duration,
collections::HashMap, path::PathBuf, sync::mpsc::channel, sync::Arc, thread::sleep,
time::Duration,
};

use crate::config::{CrateConfig, RswConfig};
Expand All @@ -14,12 +15,15 @@ use crate::core::{Build, RswErr, RswInfo};
use crate::utils::{get_root, print};

pub struct Watch {
config: Rc<RswConfig>,
callback: Box<dyn Fn(&CrateConfig, PathBuf)>,
config: Arc<RswConfig>,
callback: Arc<dyn Fn(&CrateConfig, PathBuf) + Send + Sync + 'static>,
}

impl Watch {
pub fn new(config: Rc<RswConfig>, callback: Box<dyn Fn(&CrateConfig, PathBuf)>) -> Watch {
pub fn new(
config: Arc<RswConfig>,
callback: Arc<dyn Fn(&CrateConfig, PathBuf) + Send + Sync + 'static>,
) -> Watch {
Watch { config, callback }
}
pub fn init(self) {
Expand Down Expand Up @@ -55,6 +59,7 @@ impl Watch {
print(RswInfo::SplitLine);

let (gitignore, _) = Gitignore::new("./.watchignore");
let mut build_task_join_handle: Option<tokio::task::JoinHandle<()>> = None;

loop {
let first_event = rx.recv().unwrap();
Expand All @@ -67,7 +72,7 @@ impl Watch {

match event {
Write(path) | Remove(path) | Rename(_, path) => {
let path = Rc::new(path);
let path = Arc::new(path);

let path2 = path.to_string_lossy().to_string();
let project_path_split = path2.split(&root);
Expand All @@ -82,20 +87,33 @@ impl Watch {
.unwrap()
.is_match(path.to_owned().to_str().unwrap())
{
let crate_config = *crate_map.get(key).unwrap();
let crate_config = (*crate_map.get(key).unwrap()).clone();
print(RswInfo::CrateChange(path.clone().to_path_buf()));
let is_ok = Build::new(
crate_config.clone(),
"watch",
config.cli.to_owned().unwrap(),
false,
)
.init();

if is_ok {
caller(crate_config, path.clone().to_path_buf());

if let Some(join_handle) = build_task_join_handle {
debug!("abort building task");
join_handle.abort();
}

let config = config.clone();
let caller = caller.clone();
let path = path.clone();
let join_handle = tokio::spawn(async move {
let is_ok = Build::new(
crate_config.clone(),
"watch",
config.cli.to_owned().unwrap(),
false,
)
.init();

if is_ok {
caller(&crate_config, path.clone().to_path_buf());
}
});

build_task_join_handle = Some(join_handle);

break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rsw;

fn main() {
#[tokio::main]
async fn main() {
rsw::rsw_cli();
}

0 comments on commit 313cabc

Please sign in to comment.