config.rs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. use dioxus_cli_config::crate_root;
  2. use super::*;
  3. /// Dioxus config file controls
  4. #[derive(Clone, Debug, Deserialize, Subcommand)]
  5. #[clap(name = "config")]
  6. pub enum Config {
  7. /// Init `Dioxus.toml` for project/folder.
  8. Init {
  9. /// Init project name
  10. name: String,
  11. /// Cover old config
  12. #[clap(long)]
  13. #[serde(default)]
  14. force: bool,
  15. /// Project default platform
  16. #[clap(long, default_value = "web")]
  17. platform: String,
  18. },
  19. /// Format print Dioxus config.
  20. FormatPrint {},
  21. /// Create a custom html file.
  22. CustomHtml {},
  23. }
  24. impl Config {
  25. pub fn config(self) -> Result<()> {
  26. let crate_root = crate_root()?;
  27. match self {
  28. Config::Init {
  29. name,
  30. force,
  31. platform,
  32. } => {
  33. let conf_path = crate_root.join("Dioxus.toml");
  34. if conf_path.is_file() && !force {
  35. tracing::warn!(
  36. "config file `Dioxus.toml` already exist, use `--force` to overwrite it."
  37. );
  38. return Ok(());
  39. }
  40. let mut file = File::create(conf_path)?;
  41. let content = String::from(include_str!("../assets/dioxus.toml"))
  42. .replace("{{project-name}}", &name)
  43. .replace("{{default-platform}}", &platform);
  44. file.write_all(content.as_bytes())?;
  45. tracing::info!("🚩 Init config file completed.");
  46. }
  47. Config::FormatPrint {} => {
  48. println!(
  49. "{:#?}",
  50. dioxus_cli_config::CrateConfig::new(None)?.dioxus_config
  51. );
  52. }
  53. Config::CustomHtml {} => {
  54. let html_path = crate_root.join("index.html");
  55. let mut file = File::create(html_path)?;
  56. let content = include_str!("../assets/index.html");
  57. file.write_all(content.as_bytes())?;
  58. tracing::info!("🚩 Create custom html file done.");
  59. }
  60. }
  61. Ok(())
  62. }
  63. }