-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.rs
107 lines (96 loc) · 2.99 KB
/
lib.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::process::{ExitStatus, Stdio};
pub use std::process::Command;
/// A task is a command which inherits stdout and stderr from the the standard io.
pub trait Task {
/// like run but with an ending note
fn run_with_note(&mut self, note: &'static str) -> ExitStatus;
/// returns true when success (zero exit code)
fn run(&mut self) -> ExitStatus;
}
fn get_status(command: &mut Command) -> ExitStatus {
command
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
.unwrap()
.status
}
impl Task for Command {
fn run_with_note(&mut self, note: &'static str) -> ExitStatus {
let status = get_status(self);
println!("{}", note);
status
}
fn run(&mut self) -> ExitStatus {
get_status(self)
}
}
// simplified version of cargo-expand
// expects `cargo rustc` and `rustfmt` to exist
pub fn generate_readme() {
let mut builder = tempfile::Builder::new();
builder.prefix("yew-interop-readme-gen");
let outdir = builder.tempdir().expect("failed to create tmp file");
let outfile_path = outdir.path().join("expanded");
let cmd = Command::new("cargo")
.args([
"+nightly",
"rustc",
"--features",
"yew-stable",
"--features",
"script",
"-p",
"yew-interop",
"--",
"-Zunpretty=expanded",
"-o",
outfile_path.to_str().unwrap(),
])
.stdout(Stdio::inherit())
.stderr(Stdio::null())
.output()
.unwrap()
.status;
assert!(
cmd.success(),
"rustc failed to expand yew-interop/src/lib.rs"
);
let cmd = Command::new("rustfmt")
.args([
outfile_path.to_str().unwrap(),
"--edition=2021",
"--config",
"normalize_doc_attributes=true",
])
.run();
assert!(cmd.success(), "rustfmt failed to normalize doc strings");
let mut output = String::new();
let content = std::fs::read_to_string(&outfile_path).unwrap();
let lines = content.split('\n');
let mut is_fence = false;
let mut fence: String = String::new();
lines
.filter_map(|line| line.strip_prefix("//!"))
.for_each(|line| {
if line.starts_with("```rust") {
is_fence = true;
fence.push_str("```rust\n");
} else if line.starts_with("```") && is_fence {
output.push_str(&fence);
output.push_str(line);
output.push('\n');
is_fence = false;
fence.clear();
} else if is_fence {
if !line.starts_with("# ") && line != "#" {
fence.push_str(line);
fence.push('\n');
}
} else {
output.push_str(line);
output.push('\n')
}
});
std::fs::write("README.md", output).unwrap();
}