Skip to content

Commit 196588e

Browse files
committed
Automatically find repo root in cargo unstable-api.
1 parent 1009bc8 commit 196588e

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

tools/unstable-api/src/main.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{env, path::PathBuf};
22

3-
use anyhow::Error;
3+
use anyhow::{Context, Error};
44
use structopt::StructOpt;
55

66
mod util;
@@ -14,33 +14,23 @@ mod visit;
1414
struct Opt {
1515
/// Repository root of `rust-lang/rust`.
1616
#[structopt(long, parse(from_os_str))]
17-
repo_root: PathBuf,
17+
repo_root: Option<PathBuf>,
1818
#[structopt(long)]
1919
feature: String,
2020
}
2121

2222
fn main() -> Result<(), Error> {
2323
let opt = Opt::from_iter(env::args().filter(|arg| arg != "unstable-api"));
2424

25+
let repo_root = match opt.repo_root {
26+
Some(p) => p,
27+
None => find_repo_root()?,
28+
};
29+
2530
let libs = vec![
26-
{
27-
let mut lib_core = opt.repo_root.clone();
28-
lib_core.push("library");
29-
lib_core.push("core");
30-
lib_core
31-
},
32-
{
33-
let mut lib_alloc = opt.repo_root.clone();
34-
lib_alloc.push("library");
35-
lib_alloc.push("alloc");
36-
lib_alloc
37-
},
38-
{
39-
let mut lib_std = opt.repo_root.clone();
40-
lib_std.push("library");
41-
lib_std.push("std");
42-
lib_std
43-
},
31+
repo_root.clone().join("library/core"),
32+
repo_root.clone().join("library/alloc"),
33+
repo_root.clone().join("library/std"),
4434
];
4535

4636
for crate_root in libs {
@@ -49,3 +39,16 @@ fn main() -> Result<(), Error> {
4939

5040
Ok(())
5141
}
42+
43+
fn find_repo_root() -> Result<PathBuf, Error> {
44+
let path = std::process::Command::new("cargo")
45+
.arg("locate-project")
46+
.arg("--workspace")
47+
.arg("--message-format=plain")
48+
.output()
49+
.context("unable to find repository root")?
50+
.stdout;
51+
let mut path = PathBuf::from(String::from_utf8(path)?);
52+
path.pop();
53+
Ok(path)
54+
}

0 commit comments

Comments
 (0)