|
@@ -4,7 +4,7 @@
|
|
|
|
|
|
use crate::Result;
|
|
|
use clap::Parser;
|
|
|
-use colored::Colorize;
|
|
|
+use colored::{ColoredString, Colorize};
|
|
|
use dialoguer::{theme::ColorfulTheme, Confirm};
|
|
|
use serde::Deserialize;
|
|
|
use std::{
|
|
@@ -92,6 +92,18 @@ pub enum Status {
|
|
|
Error,
|
|
|
}
|
|
|
|
|
|
+impl Status {
|
|
|
+ fn color<S: AsRef<str>>(&self, s: S) -> ColoredString {
|
|
|
+ let s = s.as_ref();
|
|
|
+ match self {
|
|
|
+ Status::Neutral => s.normal(),
|
|
|
+ Status::Success => s.green(),
|
|
|
+ Status::Warning => s.yellow(),
|
|
|
+ Status::Error => s.red(),
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
impl Display for Status {
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
write!(
|
|
@@ -107,15 +119,55 @@ impl Display for Status {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+#[derive(Default)]
|
|
|
+pub enum ActionResult {
|
|
|
+ Full {
|
|
|
+ description: String,
|
|
|
+ status: Status,
|
|
|
+ },
|
|
|
+ Description(String),
|
|
|
+ #[default]
|
|
|
+ None,
|
|
|
+}
|
|
|
+
|
|
|
+impl From<String> for ActionResult {
|
|
|
+ fn from(value: String) -> Self {
|
|
|
+ ActionResult::Description(value)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl From<(String, Status)> for ActionResult {
|
|
|
+ fn from(value: (String, Status)) -> Self {
|
|
|
+ ActionResult::Full {
|
|
|
+ description: value.0,
|
|
|
+ status: value.1,
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl From<Option<String>> for ActionResult {
|
|
|
+ fn from(value: Option<String>) -> Self {
|
|
|
+ value.map(ActionResult::Description).unwrap_or_default()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl From<Option<(String, Status)>> for ActionResult {
|
|
|
+ fn from(value: Option<(String, Status)>) -> Self {
|
|
|
+ value
|
|
|
+ .map(|v| ActionResult::Full {
|
|
|
+ description: v.0,
|
|
|
+ status: v.1,
|
|
|
+ })
|
|
|
+ .unwrap_or_default()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
pub struct SectionItem {
|
|
|
/// If description is none, the item is skipped
|
|
|
description: Option<String>,
|
|
|
status: Status,
|
|
|
- /// This closure return will be assigned to status and description
|
|
|
- action: Box<dyn FnMut() -> Option<(String, Status)>>,
|
|
|
- /// This closure return will be assigned to status and description
|
|
|
- action_if_err: Box<dyn FnMut() -> Option<(String, Status)>>,
|
|
|
- has_action_if_err: bool,
|
|
|
+ action: Option<Box<dyn FnMut() -> ActionResult>>,
|
|
|
+ action_if_err: Option<Box<dyn FnMut() -> ActionResult>>,
|
|
|
}
|
|
|
|
|
|
impl Display for SectionItem {
|
|
@@ -131,29 +183,66 @@ impl Display for SectionItem {
|
|
|
}
|
|
|
|
|
|
impl SectionItem {
|
|
|
- fn new<
|
|
|
- F1: FnMut() -> Option<(String, Status)> + 'static,
|
|
|
- F2: FnMut() -> Option<(String, Status)> + 'static,
|
|
|
- >(
|
|
|
- action: F1,
|
|
|
- action_if_err: F2,
|
|
|
- has_action_if_err: bool,
|
|
|
- ) -> Self {
|
|
|
+ fn new() -> Self {
|
|
|
Self {
|
|
|
- action: Box::new(action),
|
|
|
- action_if_err: Box::new(action_if_err),
|
|
|
- has_action_if_err,
|
|
|
+ action: None,
|
|
|
+ action_if_err: None,
|
|
|
description: None,
|
|
|
status: Status::Neutral,
|
|
|
}
|
|
|
}
|
|
|
- fn run(&mut self, interactive: bool) -> Status {
|
|
|
- if let Some(ret) = (self.action)() {
|
|
|
- self.description = Some(ret.0);
|
|
|
- self.status = ret.1;
|
|
|
+
|
|
|
+ fn action<F: FnMut() -> ActionResult + 'static>(mut self, action: F) -> Self {
|
|
|
+ self.action = Some(Box::new(action));
|
|
|
+ self
|
|
|
+ }
|
|
|
+
|
|
|
+ // fn action_if_err<F: FnMut() -> ActionResult + 'static>(mut self, action: F) -> Self {
|
|
|
+ // self.action_if_err = Some(Box::new(action));
|
|
|
+ // self
|
|
|
+ // }
|
|
|
+
|
|
|
+ fn description<S: AsRef<str>>(mut self, description: S) -> Self {
|
|
|
+ self.description = Some(description.as_ref().to_string());
|
|
|
+ self
|
|
|
+ }
|
|
|
+
|
|
|
+ fn run_action(&mut self) {
|
|
|
+ let mut res = ActionResult::None;
|
|
|
+ if let Some(action) = &mut self.action {
|
|
|
+ res = action();
|
|
|
}
|
|
|
+ self.apply_action_result(res);
|
|
|
+ }
|
|
|
|
|
|
- if self.status == Status::Error && interactive && self.has_action_if_err {
|
|
|
+ fn run_action_if_err(&mut self) {
|
|
|
+ let mut res = ActionResult::None;
|
|
|
+ if let Some(action) = &mut self.action_if_err {
|
|
|
+ res = action();
|
|
|
+ }
|
|
|
+ self.apply_action_result(res);
|
|
|
+ }
|
|
|
+
|
|
|
+ fn apply_action_result(&mut self, result: ActionResult) {
|
|
|
+ match result {
|
|
|
+ ActionResult::Full {
|
|
|
+ description,
|
|
|
+ status,
|
|
|
+ } => {
|
|
|
+ self.description = Some(description);
|
|
|
+ self.status = status;
|
|
|
+ }
|
|
|
+ ActionResult::Description(description) => {
|
|
|
+ self.description = Some(description);
|
|
|
+ }
|
|
|
+ ActionResult::None => {}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fn run(&mut self, interactive: bool) -> Status {
|
|
|
+ self.run_action();
|
|
|
+
|
|
|
+ if self.status == Status::Error && interactive && self.action_if_err.is_some() {
|
|
|
if let Some(description) = &self.description {
|
|
|
let confirmed = Confirm::with_theme(&ColorfulTheme::default())
|
|
|
.with_prompt(format!(
|
|
@@ -163,13 +252,11 @@ impl SectionItem {
|
|
|
.interact()
|
|
|
.unwrap_or(false);
|
|
|
if confirmed {
|
|
|
- if let Some(ret) = (self.action_if_err)() {
|
|
|
- self.description = Some(ret.0);
|
|
|
- self.status = ret.1;
|
|
|
- }
|
|
|
+ self.run_action_if_err()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
self.status
|
|
|
}
|
|
|
}
|
|
@@ -192,12 +279,7 @@ impl Section<'_> {
|
|
|
}
|
|
|
|
|
|
let status_str = format!("[{status}]");
|
|
|
- let status = match status {
|
|
|
- Status::Neutral => status_str.normal(),
|
|
|
- Status::Success => status_str.green(),
|
|
|
- Status::Warning => status_str.yellow(),
|
|
|
- Status::Error => status_str.red(),
|
|
|
- };
|
|
|
+ let status = status.color(status_str);
|
|
|
|
|
|
println!();
|
|
|
println!("{} {}", status, self.label.bold().yellow());
|
|
@@ -239,7 +321,7 @@ pub fn command(options: Options) -> Result<()> {
|
|
|
};
|
|
|
environment.items.extend(env_system::items());
|
|
|
environment.items.extend(env_rust::items());
|
|
|
- let (items, yarn_version) = env_nodejs::items(&metadata);
|
|
|
+ let items = env_nodejs::items(&metadata);
|
|
|
environment.items.extend(items);
|
|
|
|
|
|
let mut packages = Section {
|
|
@@ -252,7 +334,7 @@ pub fn command(options: Options) -> Result<()> {
|
|
|
.extend(packages_rust::items(app_dir, tauri_dir.clone()));
|
|
|
packages
|
|
|
.items
|
|
|
- .extend(packages_nodejs::items(app_dir, &metadata, yarn_version));
|
|
|
+ .extend(packages_nodejs::items(app_dir, &metadata));
|
|
|
|
|
|
let mut app = Section {
|
|
|
label: "App",
|