Skip to content

Commit 142f9a0

Browse files
committed
Cleanup install command
1 parent 97b1550 commit 142f9a0

File tree

4 files changed

+56
-51
lines changed

4 files changed

+56
-51
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

xtask/src/flags.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![allow(unreachable_pub)]
22

3+
use crate::install::{ClientOpt, Malloc, ServerOpt};
4+
35
xflags::args_parser! {
46
/// Run custom build command.
57
cmd xtask {
@@ -137,3 +139,34 @@ impl Xtask {
137139
}
138140
}
139141
// generated end
142+
143+
impl Install {
144+
pub(crate) fn validate(&self) -> xflags::Result<()> {
145+
if let Some(code_bin) = &self.code_bin {
146+
if let Err(err) = code_bin.parse::<ClientOpt>() {
147+
return Err(xflags::Error::new(format!("failed to parse `--code-bin`: {}", err)));
148+
}
149+
}
150+
Ok(())
151+
}
152+
pub(crate) fn server(&self) -> Option<ServerOpt> {
153+
if self.client && !self.server {
154+
return None;
155+
}
156+
let malloc = if self.mimalloc {
157+
Malloc::Mimalloc
158+
} else if self.jemalloc {
159+
Malloc::Jemalloc
160+
} else {
161+
Malloc::System
162+
};
163+
Some(ServerOpt { malloc })
164+
}
165+
pub(crate) fn client(&self) -> Option<ClientOpt> {
166+
if !self.client && self.server {
167+
return None;
168+
}
169+
let client_opt = self.code_bin.as_ref().and_then(|it| it.parse().ok()).unwrap_or_default();
170+
Some(client_opt)
171+
}
172+
}

xtask/src/install.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,24 @@ use std::{env, path::PathBuf, str};
55
use anyhow::{bail, format_err, Context, Result};
66
use xshell::{cmd, pushd};
77

8+
use crate::flags;
9+
810
// Latest stable, feel free to send a PR if this lags behind.
911
const REQUIRED_RUST_VERSION: u32 = 50;
1012

11-
pub(crate) struct InstallCmd {
12-
pub(crate) client: Option<ClientOpt>,
13-
pub(crate) server: Option<ServerOpt>,
13+
impl flags::Install {
14+
pub(crate) fn run(self) -> Result<()> {
15+
if cfg!(target_os = "macos") {
16+
fix_path_for_mac().context("Fix path for mac")?
17+
}
18+
if let Some(server) = self.server() {
19+
install_server(server).context("install server")?;
20+
}
21+
if let Some(client) = self.client() {
22+
install_client(client).context("install client")?;
23+
}
24+
Ok(())
25+
}
1426
}
1527

1628
#[derive(Clone, Copy)]
@@ -70,21 +82,6 @@ pub(crate) enum Malloc {
7082
Jemalloc,
7183
}
7284

73-
impl InstallCmd {
74-
pub(crate) fn run(self) -> Result<()> {
75-
if cfg!(target_os = "macos") {
76-
fix_path_for_mac().context("Fix path for mac")?
77-
}
78-
if let Some(server) = self.server {
79-
install_server(server).context("install server")?;
80-
}
81-
if let Some(client) = self.client {
82-
install_client(client).context("install client")?;
83-
}
84-
Ok(())
85-
}
86-
}
87-
8885
fn fix_path_for_mac() -> Result<()> {
8986
let mut vscode_path: Vec<PathBuf> = {
9087
const COMMON_APP_PATH: &str =

xtask/src/main.rs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ use std::{
2828
use walkdir::{DirEntry, WalkDir};
2929
use xshell::{cmd, cp, pushd, pushenv};
3030

31-
use crate::{
32-
codegen::Mode,
33-
dist::DistCmd,
34-
install::{InstallCmd, Malloc, ServerOpt},
35-
};
31+
use crate::{codegen::Mode, dist::DistCmd};
3632

3733
fn main() -> Result<()> {
3834
let _d = pushd(project_root())?;
@@ -43,30 +39,9 @@ fn main() -> Result<()> {
4339
println!("{}", flags::Xtask::HELP);
4440
return Ok(());
4541
}
46-
flags::XtaskCmd::Install(flags) => {
47-
if flags.server && flags.client {
48-
eprintln!(
49-
"error: The argument `--server` cannot be used with `--client`\n\n\
50-
For more information try --help"
51-
);
52-
return Ok(());
53-
}
54-
55-
let malloc = if flags.mimalloc {
56-
Malloc::Mimalloc
57-
} else if flags.jemalloc {
58-
Malloc::Jemalloc
59-
} else {
60-
Malloc::System
61-
};
62-
63-
let client_bin = flags.code_bin.map(|it| it.parse()).transpose()?;
64-
65-
InstallCmd {
66-
client: if flags.server { None } else { Some(client_bin).unwrap_or_default() },
67-
server: if flags.client { None } else { Some(ServerOpt { malloc }) },
68-
}
69-
.run()
42+
flags::XtaskCmd::Install(cmd) => {
43+
cmd.validate()?;
44+
cmd.run()
7045
}
7146
flags::XtaskCmd::Codegen(cmd) => cmd.run(),
7247
flags::XtaskCmd::Lint(_) => run_clippy(),

0 commit comments

Comments
 (0)