]> git.lizzy.rs Git - rust.git/blob - src/tools/unicode-table-generator/src/unicode_download.rs
Auto merge of #103690 - GuillaumeGomez:visibility-on-demand, r=notriddle
[rust.git] / src / tools / unicode-table-generator / src / unicode_download.rs
1 use crate::UNICODE_DIRECTORY;
2 use std::path::Path;
3 use std::process::{Command, Output};
4
5 static URL_PREFIX: &str = "https://www.unicode.org/Public/UCD/latest/ucd/";
6
7 static README: &str = "ReadMe.txt";
8
9 static RESOURCES: &[&str] =
10     &["DerivedCoreProperties.txt", "PropList.txt", "UnicodeData.txt", "SpecialCasing.txt"];
11
12 #[track_caller]
13 fn fetch(url: &str) -> Output {
14     let output = Command::new("curl").arg(URL_PREFIX.to_owned() + url).output().unwrap();
15     if !output.status.success() {
16         panic!(
17             "Failed to run curl to fetch {url}: stderr: {}",
18             String::from_utf8_lossy(&output.stderr)
19         );
20     }
21     output
22 }
23
24 pub fn fetch_latest() {
25     let directory = Path::new(UNICODE_DIRECTORY);
26     if directory.exists() {
27         eprintln!(
28             "Not refetching unicode data, already exists, please delete {directory:?} to regenerate",
29         );
30         return;
31     }
32     if let Err(e) = std::fs::create_dir_all(directory) {
33         panic!("Failed to create {UNICODE_DIRECTORY:?}: {e}");
34     }
35     let output = fetch(README);
36     let current = std::fs::read_to_string(directory.join(README)).unwrap_or_default();
37     if current.as_bytes() != &output.stdout[..] {
38         std::fs::write(directory.join(README), output.stdout).unwrap();
39     }
40
41     for resource in RESOURCES {
42         let output = fetch(resource);
43         std::fs::write(directory.join(resource), output.stdout).unwrap();
44     }
45 }