refactor: Improve File Contains precondition (#1016)

This commit is contained in:
Liam 2025-02-04 12:40:45 -06:00 committed by GitHub
parent a7766820d2
commit c173596406
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 25 deletions

View File

@ -111,12 +111,10 @@ fn default_true() -> bool {
} }
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "snake_case")]
enum EntryType { enum EntryType {
#[serde(rename = "entries")]
Entries(Vec<Entry>), Entries(Vec<Entry>),
#[serde(rename = "command")]
Command(String), Command(String),
#[serde(rename = "script")]
Script(PathBuf), Script(PathBuf),
} }
@ -132,15 +130,16 @@ impl Entry {
match data { match data {
SystemDataType::Environment(var_name) => std::env::var(var_name) SystemDataType::Environment(var_name) => std::env::var(var_name)
.is_ok_and(|var| values.contains(&var) == *matches), .is_ok_and(|var| values.contains(&var) == *matches),
SystemDataType::File(path) => path.exists() == *matches, SystemDataType::ContainingFile(file) => std::fs::read_to_string(file)
.is_ok_and(|data| {
values
.iter()
.all(|matching| data.contains(matching) == *matches)
}),
SystemDataType::CommandExists => values SystemDataType::CommandExists => values
.iter() .iter()
.all(|command| which::which(command).is_ok() == *matches), .all(|command| which::which(command).is_ok() == *matches),
SystemDataType::FileContains { file, contains } => { SystemDataType::FileExists => values.iter().all(|p| Path::new(p).is_file()),
std::fs::read_to_string(file)
.map(|content| content.contains(contains) == *matches)
.unwrap_or(false)
}
} }
}, },
) )
@ -158,15 +157,12 @@ struct Precondition {
} }
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "snake_case")]
enum SystemDataType { enum SystemDataType {
#[serde(rename = "environment")]
Environment(String), Environment(String),
#[serde(rename = "file")] ContainingFile(PathBuf),
File(PathBuf), FileExists,
#[serde(rename = "command_exists")]
CommandExists, CommandExists,
#[serde(untagged)]
FileContains { file: PathBuf, contains: String },
} }
fn filter_entries(entries: &mut Vec<Entry>) { fn filter_entries(entries: &mut Vec<Entry>) {

View File

@ -23,15 +23,15 @@ task_list = "I MP"
multi_select = false multi_select = false
[[data.entries]] [[data.entries]]
name ="Linux Neptune for SteamDeck" name = "Linux Neptune for SteamDeck"
description = "Valve's fork of Linux Kernel for the SteamDeck" description = "Valve's fork of Linux Kernel for the SteamDeck"
script = "arch/linux-neptune.sh" script = "arch/linux-neptune.sh"
task_list = "I PFM K" task_list = "I PFM K"
[[data.entries.preconditions]] [[data.entries.preconditions]]
matches = true matches = true
data = { file = "/sys/devices/virtual/dmi/id/board_vendor" } data = { containing_file = "/sys/devices/virtual/dmi/id/board_vendor" }
values = [ "Valve" ] values = ["Valve"]
[[data.entries]] [[data.entries]]
name = "Nvidia Drivers && Hardware Acceleration" name = "Nvidia Drivers && Hardware Acceleration"
@ -62,8 +62,8 @@ name = "Debian"
[[data.preconditions]] [[data.preconditions]]
matches = true matches = true
data = { file = "/etc/os-release", contains = "ID=debian" } data = { containing_file = "/etc/os-release" }
values = [] values = ["ID=debian"]
[[data.entries]] [[data.entries]]
name = "Hyprland JaKooLit" name = "Hyprland JaKooLit"
@ -130,15 +130,15 @@ task_list = "I PFM SS"
[[data.preconditions]] [[data.preconditions]]
matches = true matches = true
data = "command_exists" data = "command_exists"
values = [ "btrfs" ] values = ["btrfs"]
[[data]] [[data]]
name = "Ubuntu" name = "Ubuntu"
[[data.preconditions]] [[data.preconditions]]
matches = true matches = true
data = { file = "/etc/os-release", contains = "ID=ubuntu" } data = { containing_file = "/etc/os-release" }
values = [] values = ["ID=ubuntu"]
[[data.entries]] [[data.entries]]
name = "Hyprland JaKooLit" name = "Hyprland JaKooLit"
@ -148,8 +148,8 @@ task_list = "I MP"
[[data.preconditions]] [[data.preconditions]]
matches = true matches = true
data = { file = "/etc/os-release", contains = 'VERSION_ID="24.04"' } data = { containing_file = "/etc/os-release" }
values = [] values = ['VERSION_ID="24.04"']
[[data]] [[data]]
name = "Build Prerequisites" name = "Build Prerequisites"