Skip to content

Commit

Permalink
Do cleanup before build starts
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Aug 26, 2022
1 parent ec407ff commit 2111d62
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/backlog_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn run_script(build_type: BuildType, config: &Config) -> Result<(), BuildError>

if let Some(post_build_runner) = &config.post_build_runner {
post_build_runner
.run()
.run(script_runner::Event::BeforeAssetHash)
.map_err(BuildError::PostBuildRunner)?;
}

Expand Down
50 changes: 50 additions & 0 deletions src/cleaner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::ProjectInfo;
use std::fs;
use std::io;
use std::path::PathBuf;

#[derive(Debug, Clone)]
pub struct Config {
pub dist_path: PathBuf,
pub web_project_path: PathBuf,
}

impl Config {
pub fn from_project_info(project_info: &ProjectInfo) -> Self {
Self {
dist_path: project_info.dist_path.clone(),
web_project_path: project_info.web_project_path.clone(),
}
}

fn web_project_wasm_path(&self) -> PathBuf {
self.web_project_path.join("wasm")
}
}

#[derive(Debug)]
pub enum Error {
CreateDistDir(io::Error),
CreateWebWasmDir(io::Error),
}

pub struct Cleaner {
config: Config,
}

impl Cleaner {
pub fn new(config: Config) -> Self {
Self { config }
}

pub fn run(&self) -> Result<(), Error> {
let _ = fs::remove_dir_all(&self.config.dist_path);
fs::create_dir_all(&self.config.dist_path).map_err(Error::CreateDistDir)?;

let web_project_wasm_path = self.config.web_project_wasm_path();
let _ = fs::remove_dir_all(&web_project_wasm_path);
fs::create_dir_all(&web_project_wasm_path).map_err(Error::CreateWebWasmDir)?;

Ok(())
}
}
19 changes: 17 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod asset_hasher;
mod backlog_builder;
mod build;
mod cleaner;
mod exec;
mod project;
mod project_info;
Expand All @@ -13,6 +14,7 @@ mod watch;
use crate::asset_hasher::AssetHasher;
use crate::backlog_builder::BacklogBuilder;
use crate::build::Runner;
use crate::cleaner::Cleaner;
use crate::project::Project;
use crate::rust_builder::RustBuilder;
use crate::script_runner::ScriptRunner;
Expand Down Expand Up @@ -91,20 +93,25 @@ fn main() {

print_project_info(&project_info);

let cleaner = Cleaner::new(cleaner::Config::from_project_info(&project_info));

let rust_builder =
RustBuilder::new(rust_builder::Config::from_project_info(&env, &project_info));

let typescript_builder = TypeScriptBuilder::new(
typescript_builder::Config::from_project_info(&env, &project_info),
);

cleaner.run().expect("Cleaner failed");
rust_builder.run().expect("Rust build failed");
typescript_builder.run().expect("TypeScript build failed");

if let Some(script_name) = script {
if let Some(script_name) = &script {
let script_path = current_dir.join(script_name);
let script_runner = ScriptRunner::new(script_path, &env);
script_runner.run().expect("Post build runner failed");
script_runner
.run(script_runner::Event::BeforeAssetHash)
.expect("Post build runner failed");
}

if hash_assets {
Expand All @@ -115,6 +122,14 @@ fn main() {

rust_builder.run().expect("Rust build failed");
typescript_builder.run().expect("TypeScript build failed");

if let Some(script_name) = &script {
let script_path = current_dir.join(script_name);
let script_runner = ScriptRunner::new(script_path, &env);
script_runner
.run(script_runner::Event::AfterAssetHash)
.expect("Post build runner failed");
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/rust_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,9 @@ impl RustBuilder {
}

fn prepare_dirs(&self) -> Result<(), Error> {
let _ = fs::remove_dir_all(&self.config.dist_path);
fs::create_dir_all(&self.config.dist_path).map_err(Error::CreateDistDir)?;

let web_project_wasm_path = self.config.web_project_wasm_path();
let _ = fs::remove_dir_all(&web_project_wasm_path);
fs::create_dir_all(&web_project_wasm_path).map_err(Error::CreateWebWasmDir)?;
fs::create_dir_all(&self.config.web_project_wasm_path())
.map_err(Error::CreateWebWasmDir)?;

Ok(())
}
Expand All @@ -146,7 +143,10 @@ impl RustBuilder {
fs_extra::dir::copy(
&self.config.web_project_wasm_path(),
&self.config.dist_path,
&fs_extra::dir::CopyOptions::new(),
&fs_extra::dir::CopyOptions {
overwrite: true,
..fs_extra::dir::CopyOptions::default()
},
)
.map_err(Error::CopyWasmToDist)?;

Expand Down
23 changes: 18 additions & 5 deletions src/script_runner.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
use crate::build::Env;
use crate::build::Runner;
use crate::exec;
use std::fmt;
use std::fmt::Display;
use std::path::PathBuf;

#[derive(Debug)]
pub enum Error {
Exec(exec::Error),
}

#[derive(Debug)]
pub enum Event {
BeforeAssetHash,
AfterAssetHash,
}

impl Display for Event {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Event::BeforeAssetHash => write!(f, "before_asset_hash"),
Event::AfterAssetHash => write!(f, "after_asset_hash"),
}
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self {
Expand All @@ -30,14 +45,12 @@ impl ScriptRunner {
env: env.clone(),
}
}
}

impl Runner<Error> for ScriptRunner {
fn run(&self) -> Result<(), Error> {
pub fn run(&self, event: Event) -> Result<(), Error> {
exec::run(&exec::Config {
work_dir: ".".into(),
cmd: self.script_path.to_string_lossy().into(),
args: vec![self.env.to_string()],
args: vec![self.env.to_string(), event.to_string()],
})
.map_err(Error::Exec)?;

Expand Down

0 comments on commit 2111d62

Please sign in to comment.