diff --git a/Cargo.lock b/Cargo.lock index 799127891f..e8f10b9385 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1919,18 +1919,18 @@ checksum = "06069a848f95fceae3e5e03c0ddc8cb78452b56654ee0c8e68f938cf790fb9e3" [[package]] name = "xflags" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddb4b07c0db813f8e2b5e1b2189ef56fcddb27a6f9ef71314dbf8cc50096a5db" +checksum = "222e914b43cec5d7305ac5116d10a14b3a52c50e9062d642c92631f3beabc729" dependencies = [ "xflags-macros", ] [[package]] name = "xflags-macros" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8e168a99d6ce9d5dd0d0913f1bded279377843952dd8ff83f81b862a1dad0e1" +checksum = "52f18f5b4aa7f95e209d5b9274f6164c3938920b4d5c75f97f0dd16daee25ddd" dependencies = [ "proc-macro2", ] diff --git a/xtask/src/flags.rs b/xtask/src/flags.rs index 5710fbdb5d..2ca05d3dfe 100644 --- a/xtask/src/flags.rs +++ b/xtask/src/flags.rs @@ -1,5 +1,7 @@ #![allow(unreachable_pub)] +use crate::install::{ClientOpt, Malloc, ServerOpt}; + xflags::args_parser! { /// Run custom build command. cmd xtask { @@ -137,3 +139,34 @@ impl Xtask { } } // generated end + +impl Install { + pub(crate) fn validate(&self) -> xflags::Result<()> { + if let Some(code_bin) = &self.code_bin { + if let Err(err) = code_bin.parse::() { + return Err(xflags::Error::new(format!("failed to parse `--code-bin`: {}", err))); + } + } + Ok(()) + } + pub(crate) fn server(&self) -> Option { + if self.client && !self.server { + return None; + } + let malloc = if self.mimalloc { + Malloc::Mimalloc + } else if self.jemalloc { + Malloc::Jemalloc + } else { + Malloc::System + }; + Some(ServerOpt { malloc }) + } + pub(crate) fn client(&self) -> Option { + if !self.client && self.server { + return None; + } + let client_opt = self.code_bin.as_ref().and_then(|it| it.parse().ok()).unwrap_or_default(); + Some(client_opt) + } +} diff --git a/xtask/src/install.rs b/xtask/src/install.rs index ea21942485..3e8fbe0a62 100644 --- a/xtask/src/install.rs +++ b/xtask/src/install.rs @@ -5,12 +5,24 @@ use std::{env, path::PathBuf, str}; use anyhow::{bail, format_err, Context, Result}; use xshell::{cmd, pushd}; +use crate::flags; + // Latest stable, feel free to send a PR if this lags behind. const REQUIRED_RUST_VERSION: u32 = 50; -pub(crate) struct InstallCmd { - pub(crate) client: Option, - pub(crate) server: Option, +impl flags::Install { + pub(crate) fn run(self) -> Result<()> { + if cfg!(target_os = "macos") { + fix_path_for_mac().context("Fix path for mac")? + } + if let Some(server) = self.server() { + install_server(server).context("install server")?; + } + if let Some(client) = self.client() { + install_client(client).context("install client")?; + } + Ok(()) + } } #[derive(Clone, Copy)] @@ -70,21 +82,6 @@ pub(crate) enum Malloc { Jemalloc, } -impl InstallCmd { - pub(crate) fn run(self) -> Result<()> { - if cfg!(target_os = "macos") { - fix_path_for_mac().context("Fix path for mac")? - } - if let Some(server) = self.server { - install_server(server).context("install server")?; - } - if let Some(client) = self.client { - install_client(client).context("install client")?; - } - Ok(()) - } -} - fn fix_path_for_mac() -> Result<()> { let mut vscode_path: Vec = { const COMMON_APP_PATH: &str = diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 130867e235..ca27b6cec4 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -28,11 +28,7 @@ use std::{ use walkdir::{DirEntry, WalkDir}; use xshell::{cmd, cp, pushd, pushenv}; -use crate::{ - codegen::Mode, - dist::DistCmd, - install::{InstallCmd, Malloc, ServerOpt}, -}; +use crate::{codegen::Mode, dist::DistCmd}; fn main() -> Result<()> { let _d = pushd(project_root())?; @@ -43,30 +39,9 @@ fn main() -> Result<()> { println!("{}", flags::Xtask::HELP); return Ok(()); } - flags::XtaskCmd::Install(flags) => { - if flags.server && flags.client { - eprintln!( - "error: The argument `--server` cannot be used with `--client`\n\n\ - For more information try --help" - ); - return Ok(()); - } - - let malloc = if flags.mimalloc { - Malloc::Mimalloc - } else if flags.jemalloc { - Malloc::Jemalloc - } else { - Malloc::System - }; - - let client_bin = flags.code_bin.map(|it| it.parse()).transpose()?; - - InstallCmd { - client: if flags.server { None } else { Some(client_bin).unwrap_or_default() }, - server: if flags.client { None } else { Some(ServerOpt { malloc }) }, - } - .run() + flags::XtaskCmd::Install(cmd) => { + cmd.validate()?; + cmd.run() } flags::XtaskCmd::Codegen(cmd) => cmd.run(), flags::XtaskCmd::Lint(_) => run_clippy(),