fix: dist setting

This commit is contained in:
mrxiaozhuox committed 2022-01-23 10:54:24 +08:00
1 parent f80303c00b
commit 5395e7693c
7 files changed
+57 -22

No files matched your search

+5 -4
View File
@@ -2,10 +2,10 @@ use crate::{
config::{CrateConfig, ExecutableType}, config::{CrateConfig, ExecutableType},
error::{Error, Result}, error::{Error, Result},
}; };
use std::{io::Write, process::Command}; use std::{io::Write, path::PathBuf, process::Command};
use wasm_bindgen_cli_support::Bindgen; use wasm_bindgen_cli_support::Bindgen;
pub fn build(config: &CrateConfig) -> Result<()> { pub fn build(config: &CrateConfig, dist: PathBuf) -> Result<()> {
/* /*
[1] Build the project with cargo, generating a wasm32-unknown-unknown target (is there a more specific, better target to leverage?) [1] Build the project with cargo, generating a wasm32-unknown-unknown target (is there a more specific, better target to leverage?)
[2] Generate the appropriate build folders [2] Generate the appropriate build folders
@@ -15,7 +15,6 @@ pub fn build(config: &CrateConfig) -> Result<()> {
*/ */
let CrateConfig { let CrateConfig {
out_dir,
crate_dir, crate_dir,
target_dir, target_dir,
static_dir, static_dir,
@@ -23,10 +22,12 @@ pub fn build(config: &CrateConfig) -> Result<()> {
.. ..
} = config; } = config;
let out_dir = crate_dir.join(dist);
let t_start = std::time::Instant::now(); let t_start = std::time::Instant::now();
// [1] Build the .wasm module // [1] Build the .wasm module
log::info!("🚅 Running build commands..."); log::info!("🚅 Running build command...");
let mut cmd = Command::new("cargo"); let mut cmd = Command::new("cargo");
cmd.current_dir(&crate_dir) cmd.current_dir(&crate_dir)
.arg("build") .arg("build")
+6 -1
View File
@@ -1,3 +1,5 @@
use std::path::PathBuf;
use crate::cfg::ConfigOptsBuild; use crate::cfg::ConfigOptsBuild;
use structopt::StructOpt; use structopt::StructOpt;
@@ -16,7 +18,10 @@ impl Build {
// change the relase state. // change the relase state.
crate_config.with_release(self.build.release); crate_config.with_release(self.build.release);
crate::builder::build(&crate_config)?; crate::builder::build(
&crate_config,
self.build.dist.unwrap_or(PathBuf::from("dist")),
)?;
Ok(()) Ok(())
} }
+27 -7
View File
@@ -1,14 +1,18 @@
use std::{process::{Command, Stdio}, fs::File, path::PathBuf, io::Write}; use std::{
fs::{File, OpenOptions},
io::Write,
path::PathBuf,
process::{Command, Stdio},
};
use structopt::StructOpt; use structopt::StructOpt;
use crate::{error::{Error, Result}, cargo}; use crate::error::{Error, Result};
/// Build the Rust WASM app and all of its assets. /// Build the Rust WASM app and all of its assets.
#[derive(Clone, Debug, StructOpt)] #[derive(Clone, Debug, StructOpt)]
#[structopt(name = "init")] #[structopt(name = "init")]
pub struct Init { pub struct Init {
/// Init project path /// Init project path
#[structopt(default_value = ".")] #[structopt(default_value = ".")]
path: String, path: String,
@@ -16,24 +20,26 @@ pub struct Init {
/// Template path /// Template path
#[structopt(default_value = "default", long)] #[structopt(default_value = "default", long)]
template: String, template: String,
} }
impl Init { impl Init {
pub fn init(self) -> Result<()> { pub fn init(self) -> Result<()> {
log::info!("🔧 Start to init a new project '{}'.", self.path); log::info!("🔧 Start to init a new project '{}'.", self.path);
let project_path = PathBuf::from(&self.path); let project_path = PathBuf::from(&self.path);
if project_path.join("Cargo.toml").is_file() {
log::warn!("Path '{}' is initialized.", self.path);
return Ok(());
}
let output = Command::new("cargo") let output = Command::new("cargo")
.arg("init") .arg("init")
.arg(&self.path) .arg(&self.path)
.arg("--bin") .arg("--bin")
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::piped()) .stderr(Stdio::piped())
.output()? .output()?;
;
if !output.status.success() { if !output.status.success() {
return Err(Error::CargoError("Cargo init failed".into())); return Err(Error::CargoError("Cargo init failed".into()));
@@ -54,7 +60,21 @@ impl Init {
// log::info!("🎯 Project initialization completed."); // log::info!("🎯 Project initialization completed.");
if !Command::new("cargo")
.args(["add", "dioxus", "--features web"])
.output()?
.status
.success()
{
let mut file = OpenOptions::new()
.append(true)
.open(project_path.join("Cargo.toml"))?;
file.write_all("dioxus = { version = \"0.1.7\", features = [\"web\"] }".as_bytes())?;
}
log::info!("\n💡 Project initialized:");
log::info!("🎯> cd {}", self.path);
log::info!("🎯> dioxus serve");
Ok(()) Ok(())
} }
+1 -2
View File
@@ -3,9 +3,9 @@ use structopt::StructOpt;
pub mod build; pub mod build;
pub mod cfg; pub mod cfg;
pub mod clean; pub mod clean;
pub mod init;
pub mod serve; pub mod serve;
pub mod translate; pub mod translate;
pub mod init;
/// Build, bundle, & ship your Dioxus app. /// Build, bundle, & ship your Dioxus app.
/// ///
@@ -36,7 +36,6 @@ pub enum Commands {
Serve(serve::Serve), Serve(serve::Serve),
/// Init a new project for Dioxus. /// Init a new project for Dioxus.
Init(init::Init), Init(init::Init),
// /// Clean output artifacts. // /// Clean output artifacts.
// Clean(clean::Clean), // Clean(clean::Clean),
+7 -4
View File
@@ -1,5 +1,5 @@
use crate::{cfg::ConfigOptsServe, server}; use crate::{cfg::ConfigOptsServe, server};
use std::io::Write; use std::{io::Write, path::PathBuf};
use structopt::StructOpt; use structopt::StructOpt;
/// Run the WASM project on dev-server /// Run the WASM project on dev-server
@@ -16,15 +16,18 @@ impl Serve {
// change the relase state. // change the relase state.
crate_config.with_release(self.serve.release); crate_config.with_release(self.serve.release);
crate::builder::build(&crate_config).expect("build failed"); let dist_path = self.serve.dist.clone().unwrap_or(PathBuf::from("dist"));
crate::builder::build(&crate_config, dist_path.clone()).expect("build failed");
let serve_html = String::from(include_str!("../../server/serve.html")); let serve_html = String::from(include_str!("../../server/serve.html"));
let mut file = std::fs::File::create(crate_config.out_dir.join("index.html"))?; let mut file =
std::fs::File::create(crate_config.crate_dir.join(dist_path).join("index.html"))?;
file.write_all(serve_html.as_bytes())?; file.write_all(serve_html.as_bytes())?;
// start the develop server // start the develop server
server::startup(crate_config.clone()).await?; server::startup(crate_config.clone(), &self.serve).await?;
Ok(()) Ok(())
} }
+10 -3
View File
@@ -6,8 +6,11 @@ use axum::{
AddExtensionLayer, Router, AddExtensionLayer, Router,
}; };
use notify::{watcher, DebouncedEvent, Watcher}; use notify::{watcher, DebouncedEvent, Watcher};
use std::sync::{mpsc::channel, Arc, Mutex};
use std::time::Duration; use std::time::Duration;
use std::{
path::PathBuf,
sync::{mpsc::channel, Arc, Mutex},
};
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use crate::{builder, CrateConfig}; use crate::{builder, CrateConfig};
@@ -22,7 +25,10 @@ impl WsRelodState {
} }
} }
pub async fn startup(config: CrateConfig) -> anyhow::Result<()> { pub async fn startup(
config: CrateConfig,
opts: &crate::cfg::ConfigOptsServe,
) -> anyhow::Result<()> {
log::info!("🚀 Starting development server..."); log::info!("🚀 Starting development server...");
let (tx, rx) = channel(); let (tx, rx) = channel();
@@ -40,6 +46,7 @@ pub async fn startup(config: CrateConfig) -> anyhow::Result<()> {
let watcher_conf = config.clone(); let watcher_conf = config.clone();
let watcher_ws_state = ws_reload_state.clone(); let watcher_ws_state = ws_reload_state.clone();
let dist_path = opts.dist.clone().unwrap_or(PathBuf::from("dist"));
tokio::spawn(async move { tokio::spawn(async move {
loop { loop {
if let Ok(v) = rx.recv() { if let Ok(v) = rx.recv() {
@@ -48,7 +55,7 @@ pub async fn startup(config: CrateConfig) -> anyhow::Result<()> {
| DebouncedEvent::Write(_) | DebouncedEvent::Write(_)
| DebouncedEvent::Remove(_) | DebouncedEvent::Remove(_)
| DebouncedEvent::Rename(_, _) => { | DebouncedEvent::Rename(_, _) => {
if let Ok(_) = builder::build(&watcher_conf) { if let Ok(_) = builder::build(&watcher_conf, dist_path.clone()) {
// change the websocket reload state to true; // change the websocket reload state to true;
// the page will auto-reload. // the page will auto-reload.
watcher_ws_state.lock().unwrap().change(); watcher_ws_state.lock().unwrap().change();
+1 -1
View File
@@ -1,7 +1,7 @@
use dioxus::prelude::*; use dioxus::prelude::*;
fn main() { fn main() {
dioxus::desktop::launch(app); dioxus::web::launch(app);
} }
fn app(cx: Scope) -> Element { fn app(cx: Scope) -> Element {