1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use super::*;
use std::{
    fs::create_dir_all,
    io::Write,
    path::PathBuf,
    process::{Command, Stdio},
};

/// Run the WASM project on dev-server
#[derive(Clone, Debug, Parser)]
#[clap(name = "serve")]
pub struct Serve {
    #[clap(flatten)]
    pub serve: ConfigOptsServe,
}

impl Serve {
    pub async fn serve(self) -> Result<()> {
        let mut crate_config = crate::CrateConfig::new()?;

        // change the relase state.
        crate_config.with_hot_reload(self.serve.hot_reload);
        crate_config.with_release(self.serve.release);
        crate_config.with_verbose(self.serve.verbose);

        if self.serve.example.is_some() {
            crate_config.as_example(self.serve.example.unwrap());
        }

        if self.serve.profile.is_some() {
            crate_config.set_profile(self.serve.profile.unwrap());
        }

        let platform = self.serve.platform.unwrap_or_else(|| {
            crate_config
                .dioxus_config
                .application
                .default_platform
                .clone()
        });

        if platform.as_str() == "desktop" {
            crate::builder::build_desktop(&crate_config, true)?;

            match &crate_config.executable {
                crate::ExecutableType::Binary(name)
                | crate::ExecutableType::Lib(name)
                | crate::ExecutableType::Example(name) => {
                    let mut file = crate_config.out_dir.join(name);
                    if cfg!(windows) {
                        file.set_extension("exe");
                    }
                    Command::new(file.to_str().unwrap())
                        .stdout(Stdio::inherit())
                        .output()?;
                }
            }
            return Ok(());
        } else if platform != "web" {
            return custom_error!("Unsupported platform target.");
        }

        // generate dev-index page
        Serve::regen_dev_page(&crate_config)?;

        // start the develop server
        server::startup(self.serve.port, crate_config.clone()).await?;

        Ok(())
    }

    pub fn regen_dev_page(crate_config: &CrateConfig) -> Result<()> {
        let serve_html = gen_page(&crate_config.dioxus_config, true);

        let dist_path = crate_config.crate_dir.join(
            crate_config
                .dioxus_config
                .application
                .out_dir
                .clone()
                .unwrap_or_else(|| PathBuf::from("dist")),
        );
        if !dist_path.is_dir() {
            create_dir_all(&dist_path)?;
        }
        let index_path = dist_path.join("index.html");
        let mut file = std::fs::File::create(index_path)?;
        file.write_all(serve_html.as_bytes())?;

        Ok(())
    }
}