skin_builder/makefile/
mod.rs

1pub mod actions;
2
3use std::path::Path;
4use std::process::{Command, Stdio};
5use std::str;
6
7use crate::config::SKIN_SKINNERS_DIRECTORY;
8use crate::error::throw;
9
10pub fn is_target_exist(filename: &str, target: &str) -> bool {
11	// Check if the Makefile exists
12	if !Path::new(filename).exists() {
13		return false;
14	}
15
16	// Execute make command and capture its stderr
17	let output = Command::new("make")
18		.arg("-f")
19		.arg(filename)
20		.arg("-n")
21		.arg(target)
22		.stderr(Stdio::piped()) // Capture stderr
23		.output();
24
25	// Handle errors in executing make
26	let output = match output {
27		Ok(output) => output,
28		Err(_) => {
29			eprintln!("Failed to execute make command");
30			return false;
31		}
32	};
33
34	let stderr_output = String::from_utf8_lossy(&output.stderr);
35	let stdout_output = String::from_utf8_lossy(&output.stdout);
36
37	!(stderr_output.contains("No rule to make target")
38		|| stderr_output.contains("Nothing to be done")
39		|| stdout_output.contains("No rule to make target")
40		|| stdout_output.contains("Nothing to be done"))
41}
42
43pub fn get_variable(filename: &str, name: &str) -> Option<String> {
44	// Check if file exist
45	let output = Command::new("make")
46		.arg("-p")
47		.arg("-f")
48		.arg(filename)
49		.output()
50		.expect("Failed to execute command");
51
52	if output.status.success() {
53		let stdout = str::from_utf8(&output.stdout).expect("Invalid UTF-8");
54
55		for line in stdout.lines() {
56			if line.trim().starts_with(&format!("{} =", name)) {
57				let parts: Vec<&str> = line.split('=').map(|s| s.trim()).collect();
58				if let Some(var_value) = parts.get(1) {
59					let trimmed_line = var_value.trim();
60					let start_index = trimmed_line.find('"').unwrap_or(trimmed_line.len());
61					let end_index = trimmed_line.rfind('"').unwrap_or(start_index);
62					let value = trimmed_line[start_index + 1..end_index].trim();
63					return Some(value.to_string());
64				}
65			}
66		}
67	} else {
68		throw!("Failed to execute command.", exit);
69	}
70
71	None
72}
73
74pub fn is_variable_exist(filename: &str, name: &str) -> bool {
75	get_variable(filename, name).is_some()
76}
77
78pub fn get_skinner_name(filename: &str) -> String {
79	if let Some(name) = get_skinner_name_from_filename(filename) {
80		return name;
81	}
82
83	if is_variable_exist(filename, "name") {
84		if let Some(name) = get_variable(filename, "name") {
85			name
86		} else {
87			throw!("The variable Name does not contain a value", exit);
88		}
89	} else {
90		throw!("Name variable not found", exit);
91	}
92}
93
94pub fn get_skinner_name_from_filename(filename: &str) -> Option<String> {
95	let path = Path::new(filename);
96	let file_name = path.file_name()?.to_string_lossy().to_lowercase();
97
98	if file_name.ends_with(".makefile") {
99		let skinner_name = file_name[0..file_name.len() - 9].to_string(); // 9 is the length of ".makefile"
100		Some(skinner_name)
101	} else {
102		None
103	}
104}
105
106pub fn is_skinner_exist(name: &str) -> bool {
107	let filepath = &format!("{SKIN_SKINNERS_DIRECTORY}/{}.makefile", name);
108	let path = Path::new(filepath);
109	path.exists()
110}
111
112pub fn get_skinner_file(name: &str) -> String {
113	format!("{SKIN_SKINNERS_DIRECTORY}/{}.makefile", name)
114}