Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove prettyplease dependency #2367

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/tools/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ publish = false

[dependencies]
metadata = { package = "windows-metadata", path = "../../libs/metadata" }
prettyplease = "0.1"
syn = "1.0"
14 changes: 2 additions & 12 deletions crates/tools/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@ use std::collections::*;
use std::io::*;

/// Formats the token string
pub fn format(namespace: &str, tokens: &mut String, use_rustfmt: bool) {
if use_rustfmt {
rustfmt(namespace, tokens);
} else {
let file = syn::parse_file(tokens).unwrap();
*tokens = prettyplease::unparse(&file);
}
}

/// Formats the token string with `rustfmt`
pub fn rustfmt(name: &str, tokens: &mut String) {
pub fn format(namespace: &str, tokens: &mut String) {
let mut child = std::process::Command::new("rustfmt").stdin(std::process::Stdio::piped()).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::null()).spawn().expect("Failed to spawn `rustfmt`");
let mut stdin = child.stdin.take().expect("Failed to open stdin");
stdin.write_all(tokens.as_bytes()).unwrap();
Expand All @@ -22,7 +12,7 @@ pub fn rustfmt(name: &str, tokens: &mut String) {
if output.status.success() {
*tokens = String::from_utf8(output.stdout).expect("Failed to parse UTF-8");
} else {
println!("rustfmt failed for `{name}` with status {}\nError:\n{}", output.status, String::from_utf8_lossy(&output.stderr));
println!("rustfmt failed for `{namespace}` with status {}\nError:\n{}", output.status, String::from_utf8_lossy(&output.stderr));
}
}

Expand Down
10 changes: 4 additions & 6 deletions crates/tools/sys/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ const EXCLUDE_NAMESPACES: [&str; 28] = [
];

fn main() {
let mut rustfmt = true;
let mut expect_namespace = false;
let mut namespace = String::new();
for arg in std::env::args() {
match arg.as_str() {
"-p" => rustfmt = false,
"-n" => expect_namespace = true,
_ => {
if expect_namespace {
Expand All @@ -58,14 +56,14 @@ fn main() {
let reader = &metadata::reader::Reader::new(&files);
if !namespace.is_empty() {
let tree = reader.tree(&namespace, &[]).expect("Namespace not found");
gen_tree(reader, &output, &tree, rustfmt);
gen_tree(reader, &output, &tree);
return;
}
let win32 = reader.tree("Windows.Win32", &EXCLUDE_NAMESPACES).expect("`Windows.Win32` namespace not found");
let wdk = reader.tree("Windows.Wdk", &EXCLUDE_NAMESPACES).expect("`Windows.Win32` namespace not found");
let root = metadata::reader::Tree { namespace: "Windows", nested: BTreeMap::from([("Win32", win32), ("Wdk", wdk)]) };
let trees = root.flatten();
trees.par_iter().for_each(|tree| gen_tree(reader, &output, tree, rustfmt));
trees.par_iter().for_each(|tree| gen_tree(reader, &output, tree));
output.pop();
output.push("Cargo.toml");
let mut file = std::fs::File::create(&output).unwrap();
Expand Down Expand Up @@ -113,7 +111,7 @@ default = []
}
}

fn gen_tree(reader: &metadata::reader::Reader, output: &std::path::Path, tree: &metadata::reader::Tree, rustfmt: bool) {
fn gen_tree(reader: &metadata::reader::Reader, output: &std::path::Path, tree: &metadata::reader::Tree) {
println!("{}", tree.namespace);
let mut path = std::path::PathBuf::from(output);
path.push(tree.namespace.replace('.', "/"));
Expand All @@ -125,6 +123,6 @@ fn gen_tree(reader: &metadata::reader::Reader, output: &std::path::Path, tree: &
gen.cfg = true;
gen.doc = true;
let mut tokens = bindgen::namespace(&gen, tree);
lib::format(tree.namespace, &mut tokens, rustfmt);
lib::format(tree.namespace, &mut tokens);
std::fs::write(path.join("mod.rs"), tokens).unwrap();
}
12 changes: 5 additions & 7 deletions crates/tools/windows/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ use std::io::prelude::*;
const EXCLUDE_NAMESPACES: [&str; 14] = ["Windows.AI.MachineLearning.Preview", "Windows.ApplicationModel.SocialInfo", "Windows.Devices.AllJoyn", "Windows.Devices.Perception", "Windows.Security.Authentication.Identity.Provider", "Windows.Services.Cortana", "Windows.System.Power.Diagnostics", "Windows.System.Preview", "Windows.UI.Xaml", "Windows.Win32.Interop", "Windows.Win32.System.Diagnostics.Debug.WebApp", "Windows.Win32.System.WinRT.Xaml", "Windows.Win32.Web.MsHtml", "Windows.Win32.UI.Xaml"];

fn main() {
let mut rustfmt = true;
let mut expect_namespace = false;
let mut namespace = String::new();
for arg in std::env::args() {
match arg.as_str() {
"-p" => rustfmt = false,
"-n" => expect_namespace = true,
_ => {
if expect_namespace {
Expand All @@ -28,12 +26,12 @@ fn main() {
let reader = &metadata::reader::Reader::new(&files);
if !namespace.is_empty() {
let tree = reader.tree(&namespace, &[]).expect("Namespace not found");
gen_tree(reader, &output, &tree, rustfmt);
gen_tree(reader, &output, &tree);
return;
}
let root = reader.tree("Windows", &EXCLUDE_NAMESPACES).expect("`Windows` namespace not found");
let trees = root.flatten();
trees.par_iter().for_each(|tree| gen_tree(reader, &output, tree, rustfmt));
trees.par_iter().for_each(|tree| gen_tree(reader, &output, tree));
output.pop();
output.push("Cargo.toml");
let mut file = std::fs::File::create(&output).unwrap();
Expand Down Expand Up @@ -88,7 +86,7 @@ implement = ["windows-implement", "windows-interface"]
}
}

fn gen_tree(reader: &metadata::reader::Reader, output: &std::path::Path, tree: &metadata::reader::Tree, rustfmt: bool) {
fn gen_tree(reader: &metadata::reader::Reader, output: &std::path::Path, tree: &metadata::reader::Tree) {
println!("{}", tree.namespace);
let mut path = std::path::PathBuf::from(output);
path.push(tree.namespace.replace('.', "/"));
Expand All @@ -100,9 +98,9 @@ fn gen_tree(reader: &metadata::reader::Reader, output: &std::path::Path, tree: &
gen.doc = true;
let mut tokens = bindgen::namespace(&gen, tree);
tokens.push_str(r#"#[cfg(feature = "implement")] ::core::include!("impl.rs");"#);
lib::format(tree.namespace, &mut tokens, rustfmt);
lib::format(tree.namespace, &mut tokens);
std::fs::write(path.join("mod.rs"), tokens).unwrap();
let mut tokens = bindgen::namespace_impl(&gen, tree);
lib::format(tree.namespace, &mut tokens, rustfmt);
lib::format(tree.namespace, &mut tokens);
std::fs::write(path.join("impl.rs"), tokens).unwrap();
}