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

View File

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