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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
extern crate codegen;
extern crate inflector;
extern crate regex;

use codegen::Scope;
use codegen::Type;
use inflector::Inflector;
use lazy_static::lazy_static;
use regex::Regex;
use std::fmt;
use std::fs::File;
use std::fs::{self};
use std::io;
use std::io::prelude::*;
use std::path::Path;
use std::process::Command;

#[derive(Clone, Debug)]
enum IconType {
    SOLID,
    REGULAR,
    BRANDS,
}

impl fmt::Display for IconType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

#[derive(Clone)]
struct IconSource {
    path: std::path::PathBuf,
    icon_type: IconType,
}

lazy_static! {
    static ref REGX: Regex = Regex::new(r"(.*)/fa([^/]+)\.js$").unwrap();
    static ref REGX_W: Regex = Regex::new("A-Z").unwrap();
}

fn write_rs(name: String, icon_type: IconType) -> io::Result<()> {
    let mut scope = Scope::new();

    let type_class = match icon_type {
        IconType::SOLID => "fas",
        IconType::REGULAR => "far",
        IconType::BRANDS => "fab",
    };

    let kebab_case_name: String = name.to_kebab_case();

    scope.import("seed::prelude", "i");
    scope.import("seed::prelude", "C");

    scope
        .new_fn("icon")
        .vis("pub")
        .generic("T")
        .ret("Node<T>")
        .arg("classes", Type::new("Vec<&str>"))
        .line(format!(
            r#"i![C!["{}", "fa-{}", classes]]"#,
            type_class, kebab_case_name
        ));

    let filename = format!(
        "src/fa/{}/{}.rs",
        icon_type.to_string().to_lowercase(),
        name
    );
    let mut file = File::create(filename)?;
    file.write_all(scope.to_string().as_bytes())?;
    Ok(())
}

fn visit_folder(dir: &str, icon_type: IconType) -> Result<Vec<IconSource>, io::Error> {
    let entries = fs::read_dir(dir)?
        .map(|res| {
            res.map(|e| IconSource {
                path: e.path(),
                icon_type: icon_type.clone(),
            })
        })
        .collect::<Result<Vec<_>, io::Error>>()?;
    Ok(entries)
}

fn visit() -> Result<Vec<IconSource>, io::Error> {
    let mut icons = visit_folder(
        "./Font-Awesome/js-packages/@fortawesome/free-solid-svg-icons/",
        IconType::SOLID,
    )?;
    let regular = visit_folder(
        "./Font-Awesome/js-packages/@fortawesome/free-regular-svg-icons/",
        IconType::REGULAR,
    )?;
    let brands = visit_folder(
        "./Font-Awesome/js-packages/@fortawesome/free-brands-svg-icons/",
        IconType::BRANDS,
    )?;
    icons.extend(regular.iter().cloned());
    icons.extend(brands.iter().cloned());
    Ok(icons)
}

fn write_if_js(path_f: &str, icon_type: IconType) -> Option<Result<(), io::Error>> {
    let res = REGX
        .captures_iter(path_f)
        .filter_map(|cap| {
            let groups = (cap.get(2), cap.get(0));
            let res: Option<Result<(), io::Error>> = match groups {
                (Some(_), Some(_)) => {
                    let base = &cap[2];
                    let file = format!("{}.rs", base);
                    match write_rs(String::from(base), icon_type.clone()) {
                        Ok(_) => Some(Ok(())),
                        Err(whatever) => {
                            println!("cargo:warning=Failed to write file {}: {}", file, whatever);
                            Some(Err(whatever))
                        }
                    }
                }
                _ => None,
            };
            res
        })
        .next();
    res
}

pub fn write_all() -> std::io::Result<()> {
    clone_if_not();
    match visit().map(|files| {
        let strings = files.into_iter();
        strings
            .into_iter()
            .map(|icon| {
                let path = icon.path.to_str();
                match path {
                    None => {
                        println!("cargo:warning=Cannot get one of the files");
                        Ok(())
                    }
                    Some(path_f) => match write_if_js(path_f, icon.icon_type) {
                        None => Ok(()),
                        Some(res) => res,
                    },
                }
            })
            .collect()
    }) {
        Ok(Ok(())) => Ok(()),
        Ok(Err(writeerr)) => Err(writeerr),
        Err(readerr) => Err(readerr),
    }
}

fn clone_if_not() {
    if !Path::new("./Font-Awesome").exists() {
        Command::new("git")
            .arg("clone")
            .arg("https://github.com/FortAwesome/Font-Awesome")
            .output()
            .expect("failed to execute process");
    }
}