main.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. use dioxus_cli_config::DioxusConfig;
  2. use std::path::PathBuf;
  3. use anyhow::Context;
  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(Error::CargoError(format!("no such package: {}", bin)))?
  19. } else {
  20. metadata
  21. .root_package()
  22. .ok_or(Error::CargoError("no root package?".to_string()))?
  23. };
  24. let crate_dir = package
  25. .manifest_path
  26. .parent()
  27. .ok_or(Error::CargoError("couldn't take parent dir".to_string()))?;
  28. Ok(crate_dir.into())
  29. }
  30. /// Simplifies error messages that use the same pattern.
  31. fn error_wrapper(message: &str) -> String {
  32. format!("🚫 {message}:")
  33. }
  34. #[tokio::main]
  35. async fn main() -> anyhow::Result<()> {
  36. let args = Cli::parse();
  37. set_up_logging();
  38. match args.action {
  39. Translate(opts) => opts
  40. .translate()
  41. .context(error_wrapper("Translation of HTML into RSX failed")),
  42. Create(opts) => opts
  43. .create()
  44. .context(error_wrapper("Creating new project failed")),
  45. Init(opts) => opts
  46. .init()
  47. .context(error_wrapper("Initialising a new project failed")),
  48. Config(opts) => opts
  49. .config()
  50. .context(error_wrapper("Configuring new project failed")),
  51. #[cfg(feature = "plugin")]
  52. Plugin(opts) => opts
  53. .plugin()
  54. .await
  55. .context(error_wrapper("Error with plugin")),
  56. Autoformat(opts) => opts
  57. .autoformat()
  58. .await
  59. .context(error_wrapper("Error autoformatting RSX")),
  60. Check(opts) => opts
  61. .check()
  62. .await
  63. .context(error_wrapper("Error checking RSX")),
  64. Version(opt) => {
  65. let version = opt.version();
  66. println!("{}", version);
  67. Ok(())
  68. }
  69. action => {
  70. let bin = get_bin(args.bin)?;
  71. let _dioxus_config = DioxusConfig::load(Some(bin.clone()))
  72. .context("Failed to load Dioxus config because")?
  73. .unwrap_or_else(|| {
  74. log::info!("You appear to be creating a Dioxus project from scratch; we will use the default config");
  75. DioxusConfig::default()
  76. });
  77. #[cfg(feature = "plugin")]
  78. PluginManager::init(_dioxus_config.plugin)
  79. .context(error_wrapper("Plugin system initialization failed"))?;
  80. match action {
  81. Build(opts) => opts
  82. .build(Some(bin.clone()), None)
  83. .context(error_wrapper("Building project failed")),
  84. Clean(opts) => opts
  85. .clean(Some(bin.clone()))
  86. .context(error_wrapper("Cleaning project failed")),
  87. Serve(opts) => opts
  88. .serve(Some(bin.clone()))
  89. .await
  90. .context(error_wrapper("Serving project failed")),
  91. Bundle(opts) => opts
  92. .bundle(Some(bin.clone()))
  93. .context(error_wrapper("Bundling project failed")),
  94. _ => unreachable!(),
  95. }
  96. }
  97. }
  98. }