Skip to content

Commit d5a9f89

Browse files
rami3lr7kamura
andcommitted
feat(cli/rustup-mode): add rustup ci subcommand
Co-authored-by: r7kamura <r7kamura@gmail.com>
1 parent 531b744 commit d5a9f89

7 files changed

Lines changed: 187 additions & 0 deletions

File tree

src/cli.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// The CLI specific code lives in the cli module and sub-modules.
22
#[macro_use]
33
pub mod log;
4+
mod ci;
45
pub mod common;
56
mod docs;
67
pub mod errors;

src/cli/ci.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use std::{fmt, io::Write};
2+
3+
use anyhow::Result;
4+
use clap::{ValueEnum, builder::PossibleValue};
5+
6+
use crate::{config::Cfg, utils::ExitCode};
7+
8+
pub(crate) fn env(cfg: &Cfg<'_>) -> Result<ExitCode> {
9+
print_str(cfg, include_str!("ci/base.env"))
10+
}
11+
12+
pub(crate) fn problem_matcher(flavor: Flavor, cfg: &Cfg<'_>) -> Result<ExitCode> {
13+
print_str(cfg, flavor.problem_matcher())
14+
}
15+
16+
fn print_str(cfg: &Cfg<'_>, s: &str) -> Result<ExitCode> {
17+
let stdout = cfg.process.stdout();
18+
write!(stdout.lock(), "{s}")?;
19+
Ok(ExitCode::SUCCESS)
20+
}
21+
22+
#[derive(Copy, Clone, Debug, PartialEq)]
23+
pub(crate) enum Flavor {
24+
Github,
25+
}
26+
27+
impl Flavor {
28+
fn problem_matcher(&self) -> &'static str {
29+
match self {
30+
Self::Github => include_str!("ci/matcher/github.json"),
31+
}
32+
}
33+
}
34+
35+
impl ValueEnum for Flavor {
36+
fn value_variants<'a>() -> &'a [Self] {
37+
&[Self::Github]
38+
}
39+
40+
fn to_possible_value<'a>(&self) -> Option<PossibleValue> {
41+
Some(match self {
42+
Self::Github => PossibleValue::new("github"),
43+
})
44+
}
45+
}
46+
47+
impl fmt::Display for Flavor {
48+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49+
match self.to_possible_value() {
50+
Some(v) => write!(f, "{}", v.get_name()),
51+
None => unreachable!(),
52+
}
53+
}
54+
}

src/cli/ci/base.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CARGO_INCREMENTAL=0
2+
CARGO_PROFILE_DEV_DEBUG=0
3+
CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
4+
CARGO_TERM_COLOR=always
5+
CARGO_UNSTABLE_SPARSE_REGISTRY=true
6+
RUST_BACKTRACE=short

src/cli/ci/matcher/github.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"problemMatcher": [
3+
{
4+
"owner": "rust-compiler",
5+
"pattern": [
6+
{
7+
"regexp": "^(?:\\x1B\\[[0-9;]*[a-zA-Z])*(warning|warn|error)(\\[(\\S*)\\])?(?:\\x1B\\[[0-9;]*[a-zA-Z])*: (.*?)(?:\\x1B\\[[0-9;]*[a-zA-Z])*$",
8+
"severity": 1,
9+
"message": 4,
10+
"code": 3
11+
},
12+
{
13+
"regexp": "^(?:\\x1B\\[[0-9;]*[a-zA-Z])*\\s+(?:\\x1B\\[[0-9;]*[a-zA-Z])*-->\\s(?:\\x1B\\[[0-9;]*[a-zA-Z])*(\\S+):(\\d+):(\\d+)(?:\\x1B\\[[0-9;]*[a-zA-Z])*$",
14+
"file": 1,
15+
"line": 2,
16+
"column": 3
17+
}
18+
]
19+
},
20+
{
21+
"owner": "rust-formatter",
22+
"pattern": [
23+
{
24+
"regexp": "^(Diff in (\\S+)) at line (\\d+):",
25+
"message": 1,
26+
"file": 2,
27+
"line": 3
28+
}
29+
]
30+
},
31+
{
32+
"owner": "rust-panic",
33+
"pattern": [
34+
{
35+
"regexp": "^.*panicked\\s+at\\s+'(.*)',\\s+(.*):(\\d+):(\\d+)$",
36+
"message": 1,
37+
"file": 2,
38+
"line": 3,
39+
"column": 4
40+
}
41+
]
42+
}
43+
]
44+
}

src/cli/rustup_mode.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use tracing_subscriber::{EnvFilter, Registry, reload::Handle};
2828

2929
use crate::{
3030
cli::{
31+
ci,
3132
common::{self, PackageUpdate, update_console_filter},
3233
docs,
3334
errors::CliError,
@@ -296,6 +297,13 @@ enum RustupSubcmd {
296297
#[arg(default_value = "rustup")]
297298
command: CompletionCommand,
298299
},
300+
301+
/// Generate CI-specific configurations
302+
#[command(hide = true)]
303+
Ci {
304+
#[command(subcommand)]
305+
subcmd: CiSubcmd,
306+
},
299307
}
300308

301309
fn update_toolchain_value_parser(s: &str) -> Result<PartialToolchainDesc> {
@@ -318,6 +326,7 @@ impl RustupSubcmd {
318326
// These subcommands don't require the active toolchain, so auto-installing it should be
319327
// disabled to avoid surprises.
320328
Self::Check { .. }
329+
| Self::Ci { .. }
321330
| Self::Completions { .. }
322331
| Self::Component { .. }
323332
| Self::Default { .. }
@@ -344,6 +353,7 @@ impl RustupSubcmd {
344353
// For all other subcommands, the hint may be useful if rustup is still unusable after
345354
// the command has completed.
346355
Self::Check { .. }
356+
| Self::Ci { .. }
347357
| Self::Component { .. }
348358
| Self::Default { .. }
349359
| Self::Doc { .. }
@@ -666,6 +676,20 @@ enum SetSubcmd {
666676
},
667677
}
668678

679+
#[derive(Debug, Subcommand)]
680+
#[command(arg_required_else_help = true, subcommand_required = true)]
681+
enum CiSubcmd {
682+
/// Show the recommended environment variables in `.env` format
683+
Env,
684+
685+
/// Show the example problem matcher for the given CI flavor
686+
#[command(alias = "matcher")]
687+
ProblemMatcher {
688+
#[arg(value_enum)]
689+
flavor: ci::Flavor,
690+
},
691+
}
692+
669693
#[tracing::instrument(level = "trace", fields(args = format!("{:?}", process.args_os().collect::<Vec<_>>())), skip(process, console_filter))]
670694
pub async fn main(
671695
current_dir: PathBuf,
@@ -857,6 +881,10 @@ pub async fn main(
857881
RustupSubcmd::Completions { shell, command } => {
858882
output_completion_script(shell, command, process)
859883
}
884+
RustupSubcmd::Ci { subcmd } => match subcmd {
885+
CiSubcmd::Env => ci::env(cfg),
886+
CiSubcmd::ProblemMatcher { flavor } => ci::problem_matcher(flavor, cfg),
887+
},
860888
}?;
861889

862890
if should_warn && cfg.list_toolchains()?.is_empty() && cfg.get_default()?.is_none() {

tests/suite/cli_rustup_ui.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,11 @@ fn rustup_upgrade_cmd_help_flag() {
535535
test_help("rustup_upgrade_cmd_help_flag", &["upgrade", "--help"]);
536536
}
537537

538+
#[test]
539+
fn rustup_ci_cmd_help_flag() {
540+
test_help("rustup_ci_cmd_help_flag", &["ci", "--help"]);
541+
}
542+
538543
#[test]
539544
fn rustup_which_cmd_help_flag() {
540545
test_help("rustup_which_cmd_help_flag", &["which", "--help"]);
Lines changed: 49 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)