-
Notifications
You must be signed in to change notification settings - Fork 21
/
examples.rs
70 lines (66 loc) · 2.76 KB
/
examples.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::{rustup::SanitizeEnvironment, CommandExt};
use anyhow::{anyhow, Context, Result};
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
pub fn build() -> Result<()> {
// smoelius: The examples use `dylint-link` as the linker, so it must be built first.
#[cfg_attr(dylint_lib = "general", allow(abs_home_path))]
crate::cargo::build("dylint-link")
.build()
.sanitize_environment()
.current_dir(Path::new(env!("CARGO_MANIFEST_DIR")).join("../dylint-link"))
.success()?;
for example in iter(true)? {
let example = example?;
let file_name = example
.file_name()
.ok_or_else(|| anyhow!("Could not get file name"))?;
crate::cargo::build(&format!("example `{}`", file_name.to_string_lossy()))
.build()
.sanitize_environment()
.current_dir(&example)
.success()?;
}
Ok(())
}
/// Returns an iterator over the example libraries' directories.
/// - If the `workspace` argument is true, workspace directories (e.g., general and supplementary)
/// are included, but their member directories are not.
/// - If the `workspace` argument is false, the member directories are included, but the workspace
/// directories are not.
pub fn iter(workspace: bool) -> Result<impl Iterator<Item = Result<PathBuf>>> {
#[cfg_attr(dylint_lib = "general", allow(abs_home_path))]
let path_buf = Path::new(env!("CARGO_MANIFEST_DIR")).join("../examples");
// smoelius: Use `cargo_util::paths::normalize_path` instead of `canonicalize` so as not to
// "taint" the path with a path prefix on Windows.
let examples = cargo_util::paths::normalize_path(&path_buf);
let iter = WalkDir::new(examples)
.into_iter()
.filter_entry(|entry| entry.depth() <= 2);
Ok(iter
.map(move |entry| -> Result<Option<PathBuf>> {
let entry = entry?;
let path = entry.path();
let rust_toolchain_path = path.join("rust-toolchain");
let cargo_toml_path = path.join("Cargo.toml");
if entry.depth() < 1 || !path.is_dir() {
return Ok(None);
}
if workspace
&& rust_toolchain_path.try_exists().with_context(|| {
format!("Could not determine whether {rust_toolchain_path:?} exists")
})?
{
return Ok(Some(path.to_path_buf()));
}
if !workspace
&& cargo_toml_path.try_exists().with_context(|| {
format!("Could not determine whether {cargo_toml_path:?} exists")
})?
{
return Ok(Some(path.to_path_buf()));
}
Ok(None)
})
.filter_map(Result::transpose))
}