From 142f9a03fd4bad366439b18d8de7f2237bed65ab Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 5 Mar 2021 11:51:32 +0300 Subject: Cleanup install command --- xtask/src/flags.rs | 33 +++++++++++++++++++++++++++++++++ xtask/src/install.rs | 33 +++++++++++++++------------------ xtask/src/main.rs | 33 ++++----------------------------- 3 files changed, 52 insertions(+), 47 deletions(-) (limited to 'xtask') diff --git a/xtask/src/flags.rs b/xtask/src/flags.rs index 5710fbdb5..2ca05d3df 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 ea2194248..3e8fbe0a6 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 130867e23..ca27b6cec 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(), -- cgit v1.2.3