tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 501HTML 254C 19JavaScript 81414 Definitions

⎇ main scripts/import-yoga-tests/src/main.rs Rust · 79 lines · 3.0 KB History Who touched what raw

Block: return margin-collapsing outputs from vertical axis `ComputeSize` calls (#976) b9067e743 · Nico Burns ·

1use std::collections::HashSet;
2use std::fs;
3use std::path::{Path, PathBuf};
4use walkdir::WalkDir;
5
6struct YogaFixture {
7    #[allow(dead_code)]
8    file_name: PathBuf,
9    name: String,
10    content: String,
11}
12
13fn main() {
14    // Get Taffy fixtures dir
15    let script_root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
16    let taffy_repo_root = script_root_dir.parent().and_then(Path::parent).unwrap();
17    let taffy_fixtures_dir = taffy_repo_root.join("test_fixtures");
18
19    // Get Taffy fixture names
20    let taffy_fixture_names = WalkDir::new(&taffy_fixtures_dir)
21        .into_iter()
22        .filter_map(|a| a.ok())
23        .map(|f| {
24            let file_name = f.file_name();
25            let file_name = if file_name.to_string_lossy().starts_with('x') {
26                file_name.to_string_lossy()[1..].replace(".html", "")
27            } else {
28                file_name.to_string_lossy().replace(".html", "")
29            };
30            file_name
31        })
32        .collect::<HashSet<String>>();
33
34    // Get Taffy fixture template
35    let fixture_template_path = script_root_dir.join("FIXTURE_TEMPLATE.html");
36    let fixture_template = fs::read_to_string(fixture_template_path).unwrap();
37
38    // Get Yoga fixtures dir
39    let yoga_fixtures_dir =
40        PathBuf::from(std::env::var("YOGA_FIXTURE_DIR").expect("YOGA_FIXTURE_DIR env var must be set"));
41    let fixtures_file_list = fs::read_dir(yoga_fixtures_dir).unwrap();
42
43    let yoga_fixtures: Vec<YogaFixture> = fixtures_file_list
44        .filter_map(|a| a.ok())
45        .filter(|f| f.path().is_file() && f.path().extension().map(|p| p == "html").unwrap_or(false))
46        .flat_map(|f| {
47            let file_name: PathBuf = f.file_name().into();
48
49            let file_content = fs::read_to_string(f.path()).unwrap();
50
51            file_content
52                .split("\n\n")
53                .map(|snippet| {
54                    let snippet = snippet.trim();
55                    let first_quote_idx = snippet.chars().position(|c| c == '"').unwrap();
56                    let name: String = snippet.chars().skip(first_quote_idx + 1).take_while(|c| *c != '"').collect();
57                    let name_replace_snippet = snippet.replace(&name, "test-root");
58
59                    let taffy_name = {
60                        let name = name.replace("row_gap", "gap_row_gap");
61                        name.replace("column_gap", "gap_column_gap")
62                    };
63                    YogaFixture { file_name: file_name.clone(), name: taffy_name, content: name_replace_snippet }
64                })
65                .collect::<Vec<_>>()
66        })
67        .collect();
68
69    for fixture in yoga_fixtures {
70        if !taffy_fixture_names.contains(&fixture.name) {
71            let mut new_fixture_path = taffy_fixtures_dir.join(&fixture.name);
72            new_fixture_path.set_extension("html");
73            let new_fixture_content = fixture_template.replace("__HTML_GOES_HERE__", &fixture.content);
74
75            println!("Writing new fixture {}", fixture.name);
76            fs::write(new_fixture_path, new_fixture_content).unwrap();
77        }
78    }
79}