Cleanup install command

This commit is contained in:
Aleksey Kladov 2021-03-05 11:51:32 +03:00
parent 97b1550dda
commit 142f9a03fd
4 changed files with 56 additions and 51 deletions

8
Cargo.lock generated
View file

@ -1919,18 +1919,18 @@ checksum = "06069a848f95fceae3e5e03c0ddc8cb78452b56654ee0c8e68f938cf790fb9e3"
[[package]] [[package]]
name = "xflags" name = "xflags"
version = "0.1.3" version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddb4b07c0db813f8e2b5e1b2189ef56fcddb27a6f9ef71314dbf8cc50096a5db" checksum = "222e914b43cec5d7305ac5116d10a14b3a52c50e9062d642c92631f3beabc729"
dependencies = [ dependencies = [
"xflags-macros", "xflags-macros",
] ]
[[package]] [[package]]
name = "xflags-macros" name = "xflags-macros"
version = "0.1.3" version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8e168a99d6ce9d5dd0d0913f1bded279377843952dd8ff83f81b862a1dad0e1" checksum = "52f18f5b4aa7f95e209d5b9274f6164c3938920b4d5c75f97f0dd16daee25ddd"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]

View file

@ -1,5 +1,7 @@
#![allow(unreachable_pub)] #![allow(unreachable_pub)]
use crate::install::{ClientOpt, Malloc, ServerOpt};
xflags::args_parser! { xflags::args_parser! {
/// Run custom build command. /// Run custom build command.
cmd xtask { cmd xtask {
@ -137,3 +139,34 @@ impl Xtask {
} }
} }
// generated end // 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::<ClientOpt>() {
return Err(xflags::Error::new(format!("failed to parse `--code-bin`: {}", err)));
}
}
Ok(())
}
pub(crate) fn server(&self) -> Option<ServerOpt> {
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<ClientOpt> {
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)
}
}

View file

@ -5,12 +5,24 @@ use std::{env, path::PathBuf, str};
use anyhow::{bail, format_err, Context, Result}; use anyhow::{bail, format_err, Context, Result};
use xshell::{cmd, pushd}; use xshell::{cmd, pushd};
use crate::flags;
// Latest stable, feel free to send a PR if this lags behind. // Latest stable, feel free to send a PR if this lags behind.
const REQUIRED_RUST_VERSION: u32 = 50; const REQUIRED_RUST_VERSION: u32 = 50;
pub(crate) struct InstallCmd { impl flags::Install {
pub(crate) client: Option<ClientOpt>, pub(crate) fn run(self) -> Result<()> {
pub(crate) server: Option<ServerOpt>, 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)] #[derive(Clone, Copy)]
@ -70,21 +82,6 @@ pub(crate) enum Malloc {
Jemalloc, 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<()> { fn fix_path_for_mac() -> Result<()> {
let mut vscode_path: Vec<PathBuf> = { let mut vscode_path: Vec<PathBuf> = {
const COMMON_APP_PATH: &str = const COMMON_APP_PATH: &str =

View file

@ -28,11 +28,7 @@ use std::{
use walkdir::{DirEntry, WalkDir}; use walkdir::{DirEntry, WalkDir};
use xshell::{cmd, cp, pushd, pushenv}; use xshell::{cmd, cp, pushd, pushenv};
use crate::{ use crate::{codegen::Mode, dist::DistCmd};
codegen::Mode,
dist::DistCmd,
install::{InstallCmd, Malloc, ServerOpt},
};
fn main() -> Result<()> { fn main() -> Result<()> {
let _d = pushd(project_root())?; let _d = pushd(project_root())?;
@ -43,30 +39,9 @@ fn main() -> Result<()> {
println!("{}", flags::Xtask::HELP); println!("{}", flags::Xtask::HELP);
return Ok(()); return Ok(());
} }
flags::XtaskCmd::Install(flags) => { flags::XtaskCmd::Install(cmd) => {
if flags.server && flags.client { cmd.validate()?;
eprintln!( cmd.run()
"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::Codegen(cmd) => cmd.run(), flags::XtaskCmd::Codegen(cmd) => cmd.run(),
flags::XtaskCmd::Lint(_) => run_clippy(), flags::XtaskCmd::Lint(_) => run_clippy(),