main.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. use dioxus_cli_config::DioxusConfig;
  2. use std::path::PathBuf;
  3. use anyhow::anyhow;
  4. use clap::Parser;
  5. use dioxus_cli::*;
  6. #[cfg(feature = "plugin")]
  7. use dioxus_cli::plugin::PluginManager;
  8. use Commands::*;
  9. fn get_bin(bin: Option<String>) -> Result<PathBuf> {
  10. let metadata = cargo_metadata::MetadataCommand::new()
  11. .exec()
  12. .map_err(Error::CargoMetadata)?;
  13. let package = if let Some(bin) = bin {
  14. metadata
  15. .workspace_packages()
  16. .into_iter()
  17. .find(|p| p.name == bin)
  18. .ok_or(format!("no such package: {}", bin))
  19. .map_err(Error::CargoError)?
  20. } else {
  21. metadata
  22. .root_package()
  23. .ok_or("no root package?".into())
  24. .map_err(Error::CargoError)?
  25. };
  26. let crate_dir = package
  27. .manifest_path
  28. .parent()
  29. .ok_or("couldn't take parent dir".into())
  30. .map_err(Error::CargoError)?;
  31. Ok(crate_dir.into())
  32. }
  33. #[tokio::main]
  34. async fn main() -> anyhow::Result<()> {
  35. let args = Cli::parse();
  36. set_up_logging();
  37. let bin = get_bin(args.bin);
  38. if let Ok(bin) = &bin {
  39. let _dioxus_config = DioxusConfig::load(Some(bin.clone()))
  40. .map_err(|e| anyhow!("Failed to load Dioxus config because: {e}"))?
  41. .unwrap_or_else(|| {
  42. log::warn!("You appear to be creating a Dioxus project from scratch; we will use the default config");
  43. DioxusConfig::default()
  44. });
  45. #[cfg(feature = "plugin")]
  46. PluginManager::init(_dioxus_config.plugin)
  47. .map_err(|e| anyhow!("🚫 Plugin system initialization failed: {e}"))?;
  48. }
  49. match args.action {
  50. Translate(opts) => opts
  51. .translate()
  52. .map_err(|e| anyhow!("🚫 Translation of HTML into RSX failed: {}", e)),
  53. Build(opts) if bin.is_ok() => opts
  54. .build(Some(bin.unwrap().clone()), None)
  55. .map_err(|e| anyhow!("🚫 Building project failed: {}", e)),
  56. Clean(opts) if bin.is_ok() => opts
  57. .clean(Some(bin.unwrap().clone()))
  58. .map_err(|e| anyhow!("🚫 Cleaning project failed: {}", e)),
  59. Serve(opts) if bin.is_ok() => opts
  60. .serve(Some(bin.unwrap().clone()))
  61. .await
  62. .map_err(|e| anyhow!("🚫 Serving project failed: {}", e)),
  63. Create(opts) => opts
  64. .create()
  65. .map_err(|e| anyhow!("🚫 Creating new project failed: {}", e)),
  66. Config(opts) => opts
  67. .config()
  68. .map_err(|e| anyhow!("🚫 Configuring new project failed: {}", e)),
  69. Bundle(opts) if bin.is_ok() => opts
  70. .bundle(Some(bin.unwrap().clone()))
  71. .map_err(|e| anyhow!("🚫 Bundling project failed: {}", e)),
  72. #[cfg(feature = "plugin")]
  73. Plugin(opts) => opts
  74. .plugin()
  75. .await
  76. .map_err(|e| anyhow!("🚫 Error with plugin: {}", e)),
  77. Autoformat(opts) => opts
  78. .autoformat()
  79. .await
  80. .map_err(|e| anyhow!("🚫 Error autoformatting RSX: {}", e)),
  81. Check(opts) => opts
  82. .check()
  83. .await
  84. .map_err(|e| anyhow!("🚫 Error checking RSX: {}", e)),
  85. Version(opt) => {
  86. let version = opt.version();
  87. println!("{}", version);
  88. Ok(())
  89. }
  90. _ => Err(anyhow::anyhow!(bin.unwrap_err())),
  91. }
  92. }