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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::env;
use std::collections::HashMap;
use std::process::Command;


// Parse CLI arguments
//---------------------------
const DEFINED_ARGS: [(&str, bool); 5] = [
    ("install" , false),
    ("data", true),
    ("po", true),
    ("prefix"  , true),
    ("verbose" , false)
];

pub fn parse_varargs<'a>() -> HashMap<&'a str, String> {
    let args: Vec<String> = env::args().collect();
    let mut parsed_map: HashMap<&str, String> = HashMap::new();

    for param in args {
        for arg in DEFINED_ARGS.iter() {
            if param[2..].starts_with(arg.0)  {
                if arg.1 == false {
                    &parsed_map
                        .insert(arg.0, "true".to_string());
                } else if param.contains('=') {
                    &parsed_map
                        .insert(arg.0, param[arg.0.len()+3..].to_string());
                } else {
                    println!("Error parsing argument!");
                    std::process::exit(1)
                }
            }
        }
    }

    if parsed_map.len() == 0 {
        println!("No argument passed!");
        std::process::exit(1)
    }

    parsed_map
}


pub fn get_pkg_metadata() ->  HashMap<String, String> {
    let cmd = Command::new("cargo")
        .arg("metadata")
        .arg("--format-version=1")
        .arg(format!("--manifest-path={}", "Cargo.toml"))
        .output()
        .expect("failed to execute mkdir");

    let output = String::from_utf8_lossy(&cmd.stdout);
    let parsed = json::parse(&output).expect("Error getting metadata")
        ["packages"][0]["metadata"]["pkg"].to_owned();

    let mut variables = HashMap::<String, String>::new();
    for (k, v) in parsed.entries() {
        if let Some(value) = v.as_str().map(String::from) {
            variables.insert(
                k.to_uppercase(),
                value
            );
        }
        
    };

    variables
}


pub fn locale_files(
    po_dir: &str,
    locale_dir: &str
) -> Result<(), std::io::Error> {

    let entries = std::fs::read_dir(po_dir)?
        .map(|res| res.map(|e| e.file_name().into_string().unwrap().replace(".po", "")))
        .collect::<Result<Vec<_>, std::io::Error>>()?;

    for name in entries {
        if !name.contains("LINGUAS") && !name.contains("POTFILES") {
            let msgs_dir = &format!("{0}/{1}/LC_MESSAGES", locale_dir, name);
            std::fs::create_dir_all(&msgs_dir).expect("Error creating directory!");
            Command::new("msgfmt").args(&[
                &format!("{}/{}.po", po_dir, name),
                "-o"
            ]).arg(&format!("{}/{}.po", msgs_dir, name))
            .status().expect("Error executing msgfmt");
        }
    }

    Ok(())
}

pub fn gschema_file(
    in_path: &str,
    out_path: &str,
    schema_path: &str,
    map: &HashMap<String, String>
) {
    process_config_file(in_path, out_path, map);
    Command::new("glib-compile-schemas").args(&[
        schema_path,
    ]).status().expect("Error executing glib-compile-resources");
}

pub fn gresource_file(
    in_path: &str,
    out_path: &str,
    resource_dir: &str,
    map: &HashMap<String, String>
) {
    process_config_file(in_path, in_path, map);
    Command::new("glib-compile-resources").args(&[
        in_path,
        "--sourcedir",
        resource_dir,
        "--internal",
        "--generate",
        "--target",
        out_path
    ]).status().expect("Error executing glib-compile-resources");
}

pub fn appdata_file(
    in_path: &str,
    out_path: &str,
    po_dir: &str,
    map: &HashMap<String, String>
) {
    let tmp_file = &format!("{}.in", out_path);
    process_config_file(in_path, tmp_file, map);
    Command::new("msgfmt").args(&[
        "--xml",
        "--template",
        tmp_file,
        "-d", po_dir,
        "-o", out_path,
    ]).status().expect("Error executing msgfmt");

    std::fs::remove_file(tmp_file)
        .expect("Error removing temp appdata file");
}

pub fn desktop_file(
    in_path: &str,
    out_path: &str,
    po_dir: &str,
    map: &HashMap<String, String>
) {
    let tmp_file = &format!("{}.in", out_path);
    process_config_file(in_path, tmp_file, map);
    Command::new("msgfmt").args(&[
        "--desktop",
        "--template",
        tmp_file,
        "-d", po_dir,
        "-o", out_path
    ]).status().expect("Error executing msgfmt");

    std::fs::remove_file(tmp_file)
        .expect("Error removing temp desktop file");
}

pub fn process_config_file(
    from: &str,
    to: &str,
    map: &HashMap<String, String>) {

    let mut data = std::fs::read_to_string(&from)
        .expect("Unable to read file.");

    
    for (key, value) in map.iter() {   
        data = data.replace(key, &value);
    };

    std::fs::write(&to, data)
        .expect("Unable to write to file.");
}