progress.rs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //! Report progress about the build to the user. We use channels to report progress back to the CLI.
  2. use crate::{AppBundle, BuildRequest, BuildStage, Platform, TraceSrc};
  3. use cargo_metadata::CompilerMessage;
  4. use futures_channel::mpsc::{UnboundedReceiver, UnboundedSender};
  5. use std::path::PathBuf;
  6. pub(crate) type ProgressTx = UnboundedSender<BuildUpdate>;
  7. pub(crate) type ProgressRx = UnboundedReceiver<BuildUpdate>;
  8. #[derive(Debug)]
  9. #[allow(clippy::large_enum_variant)]
  10. pub(crate) enum BuildUpdate {
  11. Progress { stage: BuildStage },
  12. CompilerMessage { message: CompilerMessage },
  13. BuildReady { bundle: AppBundle },
  14. BuildFailed { err: crate::Error },
  15. }
  16. impl BuildRequest {
  17. pub(crate) fn status_wasm_bindgen_start(&self) {
  18. _ = self.progress.unbounded_send(BuildUpdate::Progress {
  19. stage: BuildStage::RunningBindgen {},
  20. });
  21. }
  22. pub(crate) fn status_start_bundle(&self) {
  23. _ = self.progress.unbounded_send(BuildUpdate::Progress {
  24. stage: BuildStage::Bundling {},
  25. });
  26. }
  27. pub(crate) fn status_build_diagnostic(&self, message: CompilerMessage) {
  28. _ = self
  29. .progress
  30. .unbounded_send(BuildUpdate::CompilerMessage { message });
  31. }
  32. pub(crate) fn status_build_message(&self, line: String) {
  33. tracing::trace!(dx_src = ?TraceSrc::Cargo, "{line}");
  34. }
  35. pub(crate) fn status_build_progress(&self, count: usize, total: usize, name: String) {
  36. _ = self.progress.unbounded_send(BuildUpdate::Progress {
  37. stage: BuildStage::Compiling {
  38. current: count,
  39. total,
  40. krate: name,
  41. is_server: self.is_server(),
  42. },
  43. });
  44. }
  45. pub(crate) fn status_starting_build(&self, crate_count: usize) {
  46. _ = self.progress.unbounded_send(BuildUpdate::Progress {
  47. stage: BuildStage::Starting {
  48. is_server: self.build.platform() == Platform::Server,
  49. crate_count,
  50. },
  51. });
  52. }
  53. pub(crate) fn status_copied_asset(
  54. progress: &UnboundedSender<BuildUpdate>,
  55. current: usize,
  56. total: usize,
  57. path: PathBuf,
  58. ) {
  59. _ = progress.unbounded_send(BuildUpdate::Progress {
  60. stage: BuildStage::CopyingAssets {
  61. current,
  62. total,
  63. path,
  64. },
  65. });
  66. }
  67. pub(crate) fn status_optimizing_wasm(&self) {
  68. _ = self.progress.unbounded_send(BuildUpdate::Progress {
  69. stage: BuildStage::OptimizingWasm {},
  70. });
  71. }
  72. pub(crate) fn status_installing_tooling(&self) {
  73. _ = self.progress.unbounded_send(BuildUpdate::Progress {
  74. stage: BuildStage::InstallingTooling {},
  75. });
  76. }
  77. pub(crate) fn is_server(&self) -> bool {
  78. self.build.platform() == Platform::Server
  79. }
  80. }