Skip to content

Commit

Permalink
Added hot reload implementation, blocked by tide SSE
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenvervaeke committed Nov 2, 2020
1 parent 12c00b7 commit 59e49b3
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 38 deletions.
84 changes: 57 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ async-process = "0.1.1"
async-std = { version="1.6.3", features=["attributes", "unstable"] }
cargo_metadata = "0.11.3"
console = "0.13.0"
crossbeam-channel = "0.5"
dunce = "1.0.1"
envy = "0.4.1"
fs_extra = "1.2.0"
Expand All @@ -33,6 +34,7 @@ open = "1.4.0"
sass-rs = "0.2.2"
seahash = "4.0.1"
serde = { version="1", features=["derive"] }
serde_json = "1.0.59"
structopt = "0.3.18"
structopt-derive = "0.4.11"
surf = "2.0.0"
Expand Down
29 changes: 28 additions & 1 deletion src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use std::sync::Arc;

use anyhow::Result;
use async_std::fs;
use crossbeam_channel::Sender as CrossbeamSender;
use futures::channel::mpsc::Sender;
use indicatif::ProgressBar;
use serde::Serialize;

use crate::common::{BUILDING, ERROR, SUCCESS};
use crate::config::RtcBuild;
Expand All @@ -25,24 +27,31 @@ pub struct BuildSystem {
html_pipeline: Arc<HtmlPipeline>,
/// The build system progress bar for displaying the state of the build system overall.
progress: ProgressBar,
/// An optional channel to send build events to
build_event_tx: Option<CrossbeamSender<BuildEvent>>,
}

impl BuildSystem {
/// Create a new instance from the raw components.
///
/// Reducing the number of assumptions here should help us to stay flexible when adding new
/// commands, rafctoring and the like.
pub async fn new(cfg: Arc<RtcBuild>, progress: ProgressBar, ignore_chan: Option<Sender<PathBuf>>) -> Result<Self> {
pub async fn new(
cfg: Arc<RtcBuild>, progress: ProgressBar, ignore_chan: Option<Sender<PathBuf>>, build_event_tx: Option<CrossbeamSender<BuildEvent>>,
) -> Result<Self> {
let html_pipeline = Arc::new(HtmlPipeline::new(cfg.clone(), progress.clone(), ignore_chan)?);
Ok(Self {
cfg,
html_pipeline,
progress,
build_event_tx,
})
}

/// Build the application described in the given build data.
pub async fn build(&mut self) -> Result<()> {
self.send_build_event(|| BuildEvent::Building);

self.progress.reset();
self.progress.enable_steady_tick(100);
self.progress.set_prefix(&format!("{}", BUILDING));
Expand All @@ -52,18 +61,28 @@ impl BuildSystem {
self.progress.set_position(0);
match res {
Ok(_) => {
self.send_build_event(|| BuildEvent::Success);

self.progress.set_prefix(&format!("{}", SUCCESS));
self.progress.finish_with_message("success");
Ok(())
}
Err(err) => {
self.send_build_event(|| BuildEvent::Error(err.to_string()));

self.progress.set_prefix(&format!("{}", ERROR));
self.progress.finish_with_message("error");
Err(err)
}
}
}

fn send_build_event<F: FnOnce() -> BuildEvent>(&mut self, event_builder: F) {
if let Some(receiver) = &mut self.build_event_tx {
let _ = receiver.send(event_builder());
}
}

async fn do_build(&mut self) -> Result<()> {
// TODO: delete the contents of the `dist/.current` dir (currently in flight elsewhere).

Expand All @@ -76,3 +95,11 @@ impl BuildSystem {
Ok(())
}
}

#[derive(Serialize)]
#[serde(tag = "type")]
pub enum BuildEvent {
Building,
Success,
Error(String),
}
2 changes: 1 addition & 1 deletion src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Build {
impl Build {
pub async fn run(self, config: Option<PathBuf>) -> Result<()> {
let cfg = ConfigOpts::rtc_build(self.build, config).await?;
let mut system = BuildSystem::new(cfg, spinner(), None).await?;
let mut system = BuildSystem::new(cfg, spinner(), None, None).await?;
system.build().await?;
Ok(())
}
Expand Down
Loading

0 comments on commit 59e49b3

Please sign in to comment.