main.rs 3.3 KB

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