|
1 | 1 | use std::env;
|
2 | 2 | use std::error::Error;
|
3 | 3 | use std::fs;
|
4 |
| -use std::io::{self, Read, Seek, Write}; |
| 4 | +use std::io::{Read, Seek, SeekFrom, Write}; |
5 | 5 | use std::path::PathBuf;
|
6 | 6 |
|
7 |
| -/// This is printed to the file before the rest of the contents. |
8 |
| -const PRELUDE: &str = r#"// This file is autogenerated. |
9 |
| -// |
10 |
| -// To add bindings, edit windows_sys.lst then use `./x run generate-windows-sys` to |
11 |
| -// regenerate the bindings. |
12 |
| -// |
13 |
| -// ignore-tidy-filelength |
14 |
| -"#; |
15 |
| - |
16 | 7 | fn main() -> Result<(), Box<dyn Error>> {
|
17 | 8 | let mut path: PathBuf =
|
18 | 9 | env::args_os().nth(1).expect("a path to the rust repository is required").into();
|
19 | 10 | path.push("library/std/src/sys/windows/c");
|
20 | 11 | env::set_current_dir(&path)?;
|
21 | 12 |
|
22 |
| - let info = windows_bindgen::bindgen(["--etc", "windows_sys.lst"])?; |
| 13 | + sort_bindings("bindings.txt")?; |
| 14 | + |
| 15 | + let info = windows_bindgen::bindgen(["--etc", "bindings.txt"])?; |
23 | 16 | println!("{info}");
|
24 | 17 |
|
25 |
| - // add some gunk to the output file. |
26 |
| - let mut f = fs::File::options().read(true).write(true).open("windows_sys.rs")?; |
| 18 | + let mut f = std::fs::File::options().append(true).open("windows_sys.rs")?; |
| 19 | + writeln!(&mut f, "// ignore-tidy-filelength")?; |
| 20 | + |
| 21 | + Ok(()) |
| 22 | +} |
| 23 | + |
| 24 | +fn sort_bindings(file_name: &str) -> Result<(), Box<dyn Error>> { |
| 25 | + let mut f = fs::File::options().read(true).write(true).open(file_name)?; |
27 | 26 | let mut bindings = String::new();
|
28 | 27 | f.read_to_string(&mut bindings)?;
|
29 |
| - f.seek(io::SeekFrom::Start(0))?; |
30 |
| - f.write_all(PRELUDE.as_bytes())?; |
31 |
| - f.write_all(bindings.as_bytes())?; |
| 28 | + f.set_len(0)?; |
| 29 | + f.seek(SeekFrom::Start(0))?; |
32 | 30 |
|
| 31 | + let mut lines = bindings.split_inclusive('\n'); |
| 32 | + for line in &mut lines { |
| 33 | + f.write(line.as_bytes())?; |
| 34 | + if line.contains("--filter") { |
| 35 | + break; |
| 36 | + } |
| 37 | + } |
| 38 | + let mut bindings = Vec::new(); |
| 39 | + for line in &mut lines { |
| 40 | + if !line.trim().is_empty() { |
| 41 | + bindings.push(line); |
| 42 | + } |
| 43 | + } |
| 44 | + bindings.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase())); |
| 45 | + for line in bindings { |
| 46 | + f.write(line.as_bytes())?; |
| 47 | + } |
33 | 48 | Ok(())
|
34 | 49 | }
|
0 commit comments