main.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. use std::path::PathBuf;
  2. use anyhow::anyhow;
  3. use clap::Parser;
  4. use dioxus_cli::*;
  5. #[cfg(feature = "plugin")]
  6. use dioxus_cli::plugin::PluginManager;
  7. use Commands::*;
  8. fn get_bin(bin: Option<String>) -> Result<Option<PathBuf>> {
  9. const ERR_MESSAGE: &str = "The `--bin` flag has to be ran in a Cargo workspace.";
  10. if let Some(ref bin) = bin {
  11. let manifest = cargo_toml::Manifest::from_path("./Cargo.toml")
  12. .map_err(|_| Error::CargoError(ERR_MESSAGE.to_string()))?;
  13. if let Some(workspace) = manifest.workspace {
  14. for item in workspace.members.iter() {
  15. let path = PathBuf::from(item);
  16. if !path.exists() {
  17. continue;
  18. }
  19. if !path.is_dir() {
  20. continue;
  21. }
  22. if path.ends_with(bin.clone()) {
  23. return Ok(Some(path));
  24. }
  25. }
  26. } else {
  27. return Err(Error::CargoError(ERR_MESSAGE.to_string()));
  28. }
  29. }
  30. // If the bin exists but we couldn't find it
  31. if bin.is_some() {
  32. return Err(Error::CargoError(
  33. "The specified bin does not exist.".to_string(),
  34. ));
  35. }
  36. Ok(None)
  37. }
  38. #[tokio::main]
  39. async fn main() -> anyhow::Result<()> {
  40. let args = Cli::parse();
  41. set_up_logging();
  42. let bin = get_bin(args.bin)?;
  43. let _dioxus_config = DioxusConfig::load(bin.clone())
  44. .map_err(|e| anyhow!("Failed to load Dioxus config because: {e}"))?
  45. .unwrap_or_else(|| {
  46. log::warn!("You appear to be creating a Dioxus project from scratch; we will use the default config");
  47. DioxusConfig::default()
  48. });
  49. #[cfg(feature = "plugin")]
  50. PluginManager::init(_dioxus_config.plugin)
  51. .map_err(|e| anyhow!("🚫 Plugin system initialization failed: {e}"))?;
  52. match args.action {
  53. Translate(opts) => opts
  54. .translate()
  55. .map_err(|e| anyhow!("🚫 Translation of HTML into RSX failed: {}", e)),
  56. Build(opts) => opts
  57. .build(bin.clone())
  58. .map_err(|e| anyhow!("🚫 Building project failed: {}", e)),
  59. Clean(opts) => opts
  60. .clean(bin.clone())
  61. .map_err(|e| anyhow!("🚫 Cleaning project failed: {}", e)),
  62. Serve(opts) => opts
  63. .serve(bin.clone())
  64. .await
  65. .map_err(|e| anyhow!("🚫 Serving project failed: {}", e)),
  66. Create(opts) => opts
  67. .create()
  68. .map_err(|e| anyhow!("🚫 Creating new project failed: {}", e)),
  69. Config(opts) => opts
  70. .config()
  71. .map_err(|e| anyhow!("🚫 Configuring new project failed: {}", e)),
  72. Bundle(opts) => opts
  73. .bundle(bin.clone())
  74. .map_err(|e| anyhow!("🚫 Bundling project failed: {}", e)),
  75. #[cfg(feature = "plugin")]
  76. Plugin(opts) => opts
  77. .plugin()
  78. .await
  79. .map_err(|e| anyhow!("🚫 Error with plugin: {}", e)),
  80. Autoformat(opts) => opts
  81. .autoformat()
  82. .await
  83. .map_err(|e| anyhow!("🚫 Error autoformatting RSX: {}", e)),
  84. Check(opts) => opts
  85. .check()
  86. .await
  87. .map_err(|e| anyhow!("🚫 Error checking RSX: {}", e)),
  88. Version(opt) => {
  89. let version = opt.version();
  90. println!("{}", version);
  91. Ok(())
  92. }
  93. }
  94. }