feat: Runner is now a trait, implementors are CmdRunner and FuncRunner; changing remote url done via FuncRunner

This commit is contained in:
Gabriele Musco 2023-08-19 09:39:49 +00:00
commit ea33f634c9
17 changed files with 256 additions and 140 deletions

View file

@ -1,10 +1,10 @@
use crate::runner::Runner; use crate::runner::CmdRunner;
use std::path::Path; use std::path::Path;
pub fn get_adb_install_runner(apk_path: &String) -> Runner { pub fn get_adb_install_runner(apk_path: &String) -> CmdRunner {
let path = Path::new(apk_path); let path = Path::new(apk_path);
path.try_exists().expect("APK file provided does not exist"); path.try_exists().expect("APK file provided does not exist");
Runner::new( CmdRunner::new(
None, None,
"adb".into(), "adb".into(),
vec!["install".into(), path.to_str().unwrap().to_string()], vec!["install".into(), path.to_str().unwrap().to_string()],

View file

@ -1,4 +1,4 @@
use crate::runner::Runner; use crate::runner::CmdRunner;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -10,7 +10,7 @@ pub struct Cmake {
} }
impl Cmake { impl Cmake {
pub fn get_prepare_runner(&self) -> Runner { pub fn get_prepare_runner(&self) -> CmdRunner {
let mut args = vec![ let mut args = vec![
"-B".into(), "-B".into(),
self.build_dir.clone(), self.build_dir.clone(),
@ -30,19 +30,19 @@ impl Cmake {
} }
} }
args.push(self.source_dir.clone()); args.push(self.source_dir.clone());
Runner::new(self.env.clone(), "cmake".into(), args) CmdRunner::new(self.env.clone(), "cmake".into(), args)
} }
pub fn get_build_runner(&self) -> Runner { pub fn get_build_runner(&self) -> CmdRunner {
Runner::new( CmdRunner::new(
self.env.clone(), self.env.clone(),
"cmake".into(), "cmake".into(),
vec!["--build".into(), self.build_dir.clone()], vec!["--build".into(), self.build_dir.clone()],
) )
} }
pub fn get_install_runner(&self) -> Runner { pub fn get_install_runner(&self) -> CmdRunner {
Runner::new( CmdRunner::new(
self.env.clone(), self.env.clone(),
"cmake".into(), "cmake".into(),
vec!["--install".into(), self.build_dir.clone()], vec!["--install".into(), self.build_dir.clone()],

View file

@ -1,4 +1,8 @@
use crate::{profile::Profile, runner::Runner}; use crate::{
func_runner::{FuncRunner, FuncRunnerOut},
profile::Profile,
runner::CmdRunner,
};
use git2::Repository; use git2::Repository;
use std::path::Path; use std::path::Path;
@ -23,8 +27,8 @@ impl Git {
split.next().map(|s| s.into()) split.next().map(|s| s.into())
} }
pub fn get_reset_runner(&self) -> Runner { pub fn get_reset_runner(&self) -> CmdRunner {
Runner::new( CmdRunner::new(
None, None,
"git".into(), "git".into(),
vec![ vec![
@ -36,30 +40,52 @@ impl Git {
) )
} }
fn override_remote_url(&self) -> Result<(), git2::Error> { pub fn get_override_remote_url_runner(&self) -> FuncRunner {
if let Ok(repo) = Repository::open(&self.dir) { let dir = self.dir.clone();
let current_remote_url = self.get_repo(); let n_remote_url = self.get_repo();
let remote = repo.find_remote("origin")?; FuncRunner::new(Box::new(move || {
if remote.url().unwrap_or("") != current_remote_url { if let Ok(repo) = Repository::open(dir) {
repo.remote_set_url("origin", &current_remote_url)?; if let Ok(remote) = repo.find_remote("origin") {
if remote.url().unwrap_or("") != n_remote_url {
if repo.remote_set_url("origin", &n_remote_url).is_ok() {
return FuncRunnerOut {
success: true,
out: vec![],
};
} }
return FuncRunnerOut {
success: false,
out: vec!["Failed to set origin remote url".into()],
};
} }
Ok(()) } else {
return FuncRunnerOut {
success: false,
out: vec!["Could not find remote origin".into()],
};
}
return FuncRunnerOut {
success: true,
out: vec![],
};
}
FuncRunnerOut {
success: true,
out: vec![],
}
}))
} }
pub fn get_pull_runner(&self) -> Runner { pub fn get_pull_runner(&self) -> CmdRunner {
if self.override_remote_url().is_err() { CmdRunner::new(
println!("Warning: failed to override git repo remote URL!")
}
Runner::new(
None, None,
"git".into(), "git".into(),
vec!["-C".into(), self.dir.clone(), "pull".into()], vec!["-C".into(), self.dir.clone(), "pull".into()],
) )
} }
pub fn get_clone_runner(&self) -> Runner { pub fn get_clone_runner(&self) -> CmdRunner {
Runner::new( CmdRunner::new(
None, None,
"git".into(), "git".into(),
vec![ vec![
@ -71,9 +97,9 @@ impl Git {
) )
} }
pub fn get_checkout_ref_runner(&self) -> Option<Runner> { pub fn get_checkout_ref_runner(&self) -> Option<CmdRunner> {
self.get_ref().map(|r| { self.get_ref().map(|r| {
Runner::new( CmdRunner::new(
None, None,
"git".into(), "git".into(),
vec!["-C".into(), self.dir.clone(), "checkout".into(), r], vec!["-C".into(), self.dir.clone(), "checkout".into(), r],
@ -81,7 +107,7 @@ impl Git {
}) })
} }
pub fn get_clone_or_not_runner(&self) -> Option<Runner> { pub fn get_clone_or_not_runner(&self) -> Option<CmdRunner> {
let path_s = format!("{}/.git", self.dir.clone()); let path_s = format!("{}/.git", self.dir.clone());
let path = Path::new(&path_s); let path = Path::new(&path_s);
if path.is_dir() { if path.is_dir() {
@ -90,7 +116,7 @@ impl Git {
Some(self.get_clone_runner()) Some(self.get_clone_runner())
} }
pub fn clone_or_pull(&self, profile: &Profile) -> Option<Runner> { pub fn clone_or_pull(&self, profile: &Profile) -> Option<CmdRunner> {
match self.get_clone_or_not_runner() { match self.get_clone_or_not_runner() {
Some(r) => Some(r), Some(r) => Some(r),
None => match profile.pull_on_build { None => match profile.pull_on_build {

View file

@ -2,12 +2,12 @@ use crate::{
build_tools::{cmake::Cmake, git::Git}, build_tools::{cmake::Cmake, git::Git},
file_utils::rm_rf, file_utils::rm_rf,
profile::Profile, profile::Profile,
runner::Runner, runner::{CmdRunner, Runner},
}; };
use std::{collections::HashMap, path::Path}; use std::{collections::HashMap, path::Path};
pub fn get_build_basalt_runners(profile: &Profile, clean_build: bool) -> Vec<Runner> { pub fn get_build_basalt_runners(profile: &Profile, clean_build: bool) -> Vec<Box<dyn Runner>> {
let mut runners: Vec<Runner> = vec![]; let mut runners: Vec<Box<dyn Runner>> = vec![];
let git = Git { let git = Git {
repo: match profile.features.basalt.repo.as_ref() { repo: match profile.features.basalt.repo.as_ref() {
Some(r) => r.clone(), Some(r) => r.clone(),
@ -15,13 +15,14 @@ pub fn get_build_basalt_runners(profile: &Profile, clean_build: bool) -> Vec<Run
}, },
dir: profile.features.basalt.path.as_ref().unwrap().clone(), dir: profile.features.basalt.path.as_ref().unwrap().clone(),
}; };
runners.push(Box::new(git.get_override_remote_url_runner()));
if let Some(r) = git.clone_or_pull(profile) { if let Some(r) = git.clone_or_pull(profile) {
runners.push(r); runners.push(Box::new(r));
}; };
if let Some(r) = git.get_checkout_ref_runner() { if let Some(r) = git.get_checkout_ref_runner() {
runners.push(r); runners.push(Box::new(r));
if profile.pull_on_build { if profile.pull_on_build {
runners.push(git.get_pull_runner()); runners.push(Box::new(git.get_pull_runner()));
} }
} }
@ -45,21 +46,21 @@ pub fn get_build_basalt_runners(profile: &Profile, clean_build: bool) -> Vec<Run
source_dir: profile.features.basalt.path.as_ref().unwrap().clone(), source_dir: profile.features.basalt.path.as_ref().unwrap().clone(),
build_dir: build_dir.clone(), build_dir: build_dir.clone(),
}; };
runners.push(Runner::new(None, "bash".into(), vec![ runners.push(Box::new(CmdRunner::new(None, "bash".into(), vec![
"-c".into(), "-c".into(),
format!( format!(
"cd {repo}/thirdparty/Pangolin && git checkout include/pangolin/utils/picojson.h && curl -sSL 'https://aur.archlinux.org/cgit/aur.git/plain/279c17d9c9eb9374c89489b449f92cb93350e8cd.patch?h=basalt-monado-git' -o picojson_fix.patch && git apply picojson_fix.patch && sed -i '1s/^/#include <stdint.h>\\n/' include/pangolin/platform.h", "cd {repo}/thirdparty/Pangolin && git checkout include/pangolin/utils/picojson.h && curl -sSL 'https://aur.archlinux.org/cgit/aur.git/plain/279c17d9c9eb9374c89489b449f92cb93350e8cd.patch?h=basalt-monado-git' -o picojson_fix.patch && git apply picojson_fix.patch && sed -i '1s/^/#include <stdint.h>\\n/' include/pangolin/platform.h",
repo = git.dir repo = git.dir
), ),
])); ])));
if !Path::new(&build_dir).is_dir() || clean_build { if !Path::new(&build_dir).is_dir() || clean_build {
rm_rf(&build_dir); rm_rf(&build_dir);
runners.push(cmake.get_prepare_runner()); runners.push(Box::new(cmake.get_prepare_runner()));
} }
runners.push(cmake.get_build_runner()); runners.push(Box::new(cmake.get_build_runner()));
runners.push(cmake.get_install_runner()); runners.push(Box::new(cmake.get_install_runner()));
runners.push(Runner::new( runners.push(Box::new(CmdRunner::new(
None, None,
"mkdir".into(), "mkdir".into(),
vec![ vec![
@ -69,8 +70,8 @@ pub fn get_build_basalt_runners(profile: &Profile, clean_build: bool) -> Vec<Run
profile.prefix profile.prefix
), ),
], ],
)); )));
runners.push(Runner::new( runners.push(Box::new(CmdRunner::new(
None, None,
"cp".into(), "cp".into(),
vec![ vec![
@ -81,7 +82,7 @@ pub fn get_build_basalt_runners(profile: &Profile, clean_build: bool) -> Vec<Run
), ),
format!("{}/share/basalt/thirdparty", profile.prefix), format!("{}/share/basalt/thirdparty", profile.prefix),
], ],
)); )));
runners runners
} }

View file

@ -6,8 +6,8 @@ use crate::{
}; };
use std::{collections::HashMap, path::Path}; use std::{collections::HashMap, path::Path};
pub fn get_build_libsurvive_runners(profile: &Profile, clean_build: bool) -> Vec<Runner> { pub fn get_build_libsurvive_runners(profile: &Profile, clean_build: bool) -> Vec<Box<dyn Runner>> {
let mut runners: Vec<Runner> = vec![]; let mut runners: Vec<Box<dyn Runner>> = vec![];
let git = Git { let git = Git {
repo: match profile.features.libsurvive.repo.as_ref() { repo: match profile.features.libsurvive.repo.as_ref() {
Some(r) => r.clone(), Some(r) => r.clone(),
@ -15,13 +15,14 @@ pub fn get_build_libsurvive_runners(profile: &Profile, clean_build: bool) -> Vec
}, },
dir: profile.features.libsurvive.path.as_ref().unwrap().clone(), dir: profile.features.libsurvive.path.as_ref().unwrap().clone(),
}; };
runners.push(Box::new(git.get_override_remote_url_runner()));
if let Some(r) = git.clone_or_pull(profile) { if let Some(r) = git.clone_or_pull(profile) {
runners.push(r); runners.push(Box::new(r));
}; };
if let Some(r) = git.get_checkout_ref_runner() { if let Some(r) = git.get_checkout_ref_runner() {
runners.push(r); runners.push(Box::new(r));
if profile.pull_on_build { if profile.pull_on_build {
runners.push(git.get_pull_runner()); runners.push(Box::new(git.get_pull_runner()));
} }
} }
@ -47,10 +48,10 @@ pub fn get_build_libsurvive_runners(profile: &Profile, clean_build: bool) -> Vec
}; };
if !Path::new(&build_dir).is_dir() || clean_build { if !Path::new(&build_dir).is_dir() || clean_build {
rm_rf(&build_dir); rm_rf(&build_dir);
runners.push(cmake.get_prepare_runner()); runners.push(Box::new(cmake.get_prepare_runner()));
} }
runners.push(cmake.get_build_runner()); runners.push(Box::new(cmake.get_build_runner()));
runners.push(cmake.get_install_runner()); runners.push(Box::new(cmake.get_install_runner()));
runners runners
} }

View file

@ -1,8 +1,8 @@
use crate::{constants::pkg_data_dir, paths::get_cache_dir, profile::Profile, runner::Runner}; use crate::{constants::pkg_data_dir, paths::get_cache_dir, profile::Profile, runner::CmdRunner};
pub fn get_build_mercury_runner(profile: &Profile) -> Runner { pub fn get_build_mercury_runner(profile: &Profile) -> CmdRunner {
let args = vec![profile.prefix.clone(), get_cache_dir()]; let args = vec![profile.prefix.clone(), get_cache_dir()];
Runner::new( CmdRunner::new(
None, None,
format!( format!(
"{sysdata}/scripts/build_mercury.sh", "{sysdata}/scripts/build_mercury.sh",

View file

@ -6,8 +6,8 @@ use crate::{
}; };
use std::{collections::HashMap, path::Path}; use std::{collections::HashMap, path::Path};
pub fn get_build_monado_runners(profile: &Profile, clean_build: bool) -> Vec<Runner> { pub fn get_build_monado_runners(profile: &Profile, clean_build: bool) -> Vec<Box<dyn Runner>> {
let mut runners: Vec<Runner> = vec![]; let mut runners: Vec<Box<dyn Runner>> = vec![];
let git = Git { let git = Git {
repo: match profile.xrservice_repo.as_ref() { repo: match profile.xrservice_repo.as_ref() {
Some(r) => r.clone(), Some(r) => r.clone(),
@ -15,13 +15,14 @@ pub fn get_build_monado_runners(profile: &Profile, clean_build: bool) -> Vec<Run
}, },
dir: profile.xrservice_path.clone(), dir: profile.xrservice_path.clone(),
}; };
runners.push(Box::new(git.get_override_remote_url_runner()));
if let Some(r) = git.clone_or_pull(profile) { if let Some(r) = git.clone_or_pull(profile) {
runners.push(r); runners.push(Box::new(r));
} }
if let Some(r) = git.get_checkout_ref_runner() { if let Some(r) = git.get_checkout_ref_runner() {
runners.push(r); runners.push(Box::new(r));
if profile.pull_on_build { if profile.pull_on_build {
runners.push(git.get_pull_runner()); runners.push(Box::new(git.get_pull_runner()));
} }
} }
@ -53,10 +54,10 @@ pub fn get_build_monado_runners(profile: &Profile, clean_build: bool) -> Vec<Run
}; };
if !Path::new(&build_dir).is_dir() || clean_build { if !Path::new(&build_dir).is_dir() || clean_build {
rm_rf(&build_dir); rm_rf(&build_dir);
runners.push(cmake.get_prepare_runner()); runners.push(Box::new(cmake.get_prepare_runner()));
} }
runners.push(cmake.get_build_runner()); runners.push(Box::new(cmake.get_build_runner()));
runners.push(cmake.get_install_runner()); runners.push(Box::new(cmake.get_install_runner()));
runners runners
} }

View file

@ -6,8 +6,11 @@ use crate::{
}; };
use std::{collections::HashMap, path::Path}; use std::{collections::HashMap, path::Path};
pub fn get_build_opencomposite_runners(profile: &Profile, clean_build: bool) -> Vec<Runner> { pub fn get_build_opencomposite_runners(
let mut runners: Vec<Runner> = vec![]; profile: &Profile,
clean_build: bool,
) -> Vec<Box<dyn Runner>> {
let mut runners: Vec<Box<dyn Runner>> = vec![];
let git = Git { let git = Git {
repo: match profile.opencomposite_repo.as_ref() { repo: match profile.opencomposite_repo.as_ref() {
Some(r) => r.clone(), Some(r) => r.clone(),
@ -15,13 +18,14 @@ pub fn get_build_opencomposite_runners(profile: &Profile, clean_build: bool) ->
}, },
dir: profile.opencomposite_path.clone(), dir: profile.opencomposite_path.clone(),
}; };
runners.push(Box::new(git.get_override_remote_url_runner()));
if let Some(r) = git.clone_or_pull(profile) { if let Some(r) = git.clone_or_pull(profile) {
runners.push(r); runners.push(Box::new(r));
}; };
if let Some(r) = git.get_checkout_ref_runner() { if let Some(r) = git.get_checkout_ref_runner() {
runners.push(r); runners.push(Box::new(r));
if profile.pull_on_build { if profile.pull_on_build {
runners.push(git.get_pull_runner()); runners.push(Box::new(git.get_pull_runner()));
} }
} }
@ -36,9 +40,9 @@ pub fn get_build_opencomposite_runners(profile: &Profile, clean_build: bool) ->
}; };
if !Path::new(&build_dir).is_dir() || clean_build { if !Path::new(&build_dir).is_dir() || clean_build {
rm_rf(&build_dir); rm_rf(&build_dir);
runners.push(cmake.get_prepare_runner()); runners.push(Box::new(cmake.get_prepare_runner()));
} }
runners.push(cmake.get_build_runner()); runners.push(Box::new(cmake.get_build_runner()));
runners runners
} }

View file

@ -6,8 +6,8 @@ use crate::{
}; };
use std::{collections::HashMap, path::Path}; use std::{collections::HashMap, path::Path};
pub fn get_build_wivrn_runners(profile: &Profile, clean_build: bool) -> Vec<Runner> { pub fn get_build_wivrn_runners(profile: &Profile, clean_build: bool) -> Vec<Box<dyn Runner>> {
let mut runners: Vec<Runner> = vec![]; let mut runners: Vec<Box<dyn Runner>> = vec![];
let git = Git { let git = Git {
repo: match profile.xrservice_repo.as_ref() { repo: match profile.xrservice_repo.as_ref() {
Some(r) => r.clone(), Some(r) => r.clone(),
@ -15,13 +15,14 @@ pub fn get_build_wivrn_runners(profile: &Profile, clean_build: bool) -> Vec<Runn
}, },
dir: profile.xrservice_path.clone(), dir: profile.xrservice_path.clone(),
}; };
runners.push(Box::new(git.get_override_remote_url_runner()));
if let Some(r) = git.clone_or_pull(profile) { if let Some(r) = git.clone_or_pull(profile) {
runners.push(r); runners.push(Box::new(r));
} }
if let Some(r) = git.get_checkout_ref_runner() { if let Some(r) = git.get_checkout_ref_runner() {
runners.push(r); runners.push(Box::new(r));
if profile.pull_on_build { if profile.pull_on_build {
runners.push(git.get_pull_runner()); runners.push(Box::new(git.get_pull_runner()));
} }
} }
@ -40,10 +41,10 @@ pub fn get_build_wivrn_runners(profile: &Profile, clean_build: bool) -> Vec<Runn
}; };
if !Path::new(&build_dir).is_dir() || clean_build { if !Path::new(&build_dir).is_dir() || clean_build {
rm_rf(&build_dir); rm_rf(&build_dir);
runners.push(cmake.get_prepare_runner()); runners.push(Box::new(cmake.get_prepare_runner()));
} }
runners.push(cmake.get_build_runner()); runners.push(Box::new(cmake.get_build_runner()));
runners.push(cmake.get_install_runner()); runners.push(Box::new(cmake.get_install_runner()));
runners runners
} }

View file

@ -1,4 +1,4 @@
use crate::runner::Runner; use crate::runner::{CmdRunner, Runner};
use nix::{ use nix::{
errno::Errno, errno::Errno,
sys::statvfs::{statvfs, FsFlags}, sys::statvfs::{statvfs, FsFlags},
@ -66,7 +66,7 @@ pub fn set_file_readonly(path_s: &String, readonly: bool) -> Result<(), std::io:
} }
pub fn setcap_cap_sys_nice_eip(file: String) { pub fn setcap_cap_sys_nice_eip(file: String) {
let mut runner = Runner::new( let mut runner = CmdRunner::new(
None, None,
"pkexec".into(), "pkexec".into(),
vec!["setcap".into(), "CAP_SYS_NICE=eip".into(), file], vec!["setcap".into(), "CAP_SYS_NICE=eip".into(), file],

70
src/func_runner.rs Normal file
View file

@ -0,0 +1,70 @@
use std::{
mem,
thread::{self, JoinHandle},
};
use crate::runner::{Runner, RunnerStatus};
#[derive(Debug, Clone, Default)]
pub struct FuncRunnerOut {
pub success: bool,
pub out: Vec<String>,
}
pub struct FuncRunner {
output: Vec<String>,
res: Option<bool>,
thread: Option<JoinHandle<FuncRunnerOut>>,
func: Box<dyn FnOnce() -> FuncRunnerOut + Send + Sync + 'static>,
}
impl FuncRunner {
pub fn new(func: Box<dyn FnOnce() -> FuncRunnerOut + Send + Sync + 'static>) -> Self {
FuncRunner {
output: Vec::new(),
thread: None,
res: None,
func,
}
}
}
impl Runner for FuncRunner {
fn start(&mut self) {
if self.res.is_some() {
panic!("Cannot start a FuncRunner twice!");
}
let f = mem::replace(&mut self.func, Box::new(move || FuncRunnerOut::default()));
self.thread = Some(thread::spawn(move || f()));
}
fn status(&mut self) -> RunnerStatus {
if let Some(res) = self.res {
if res {
return RunnerStatus::Stopped(Some(0));
}
return RunnerStatus::Stopped(Some(1));
}
if let Some(t) = self.thread.take() {
if t.is_finished() {
let out = t.join().expect("Failed to join thread");
self.res = Some(out.success);
self.output = out.out;
return self.status();
}
self.thread = Some(t);
return RunnerStatus::Running;
}
RunnerStatus::Stopped(None)
}
fn consume_output(&mut self) -> String {
self.consume_rows().concat()
}
fn consume_rows(&mut self) -> Vec<String> {
let res = self.output.clone();
self.output.clear();
res
}
}

View file

@ -25,6 +25,7 @@ pub mod downloader;
pub mod env_var_descriptions; pub mod env_var_descriptions;
pub mod file_builders; pub mod file_builders;
pub mod file_utils; pub mod file_utils;
pub mod func_runner;
pub mod log_level; pub mod log_level;
pub mod log_parser; pub mod log_parser;
pub mod paths; pub mod paths;

View file

@ -18,7 +18,14 @@ use std::{
vec, vec,
}; };
pub struct Runner { pub trait Runner {
fn start(&mut self);
fn status(&mut self) -> RunnerStatus;
fn consume_output(&mut self) -> String;
fn consume_rows(&mut self) -> Vec<String>;
}
pub struct CmdRunner {
pub environment: HashMap<String, String>, pub environment: HashMap<String, String>,
pub command: String, pub command: String,
pub args: Vec<String>, pub args: Vec<String>,
@ -67,7 +74,7 @@ macro_rules! logger_thread {
}; };
} }
impl Runner { impl CmdRunner {
pub fn new( pub fn new(
environment: Option<HashMap<String, String>>, environment: Option<HashMap<String, String>>,
command: String, command: String,
@ -125,10 +132,6 @@ impl Runner {
Ok(()) Ok(())
} }
pub fn start(&mut self) {
self.try_start().expect("Failed to execute runner");
}
fn join_threads(&mut self) { fn join_threads(&mut self) {
loop { loop {
match self.threads.pop() { match self.threads.pop() {
@ -163,7 +166,39 @@ impl Runner {
self.join_threads(); self.join_threads();
} }
pub fn status(&mut self) -> RunnerStatus { fn receive_output(&mut self) {
while let Ok(data) = self.receiver.try_recv() {
self.output.push(data);
}
}
fn save_log(path_s: String, log: &[String]) -> Result<(), std::io::Error> {
let mut writer = get_writer(&path_s);
let log_s = log.concat();
writer.write_all(log_s.as_ref())
}
pub fn save_output(&mut self, path: String) -> Result<(), std::io::Error> {
CmdRunner::save_log(path, &self.output)
}
}
impl Runner for CmdRunner {
fn consume_output(&mut self) -> String {
self.receive_output();
let res = self.output.concat();
self.output.clear();
res
}
fn consume_rows(&mut self) -> Vec<String> {
self.receive_output();
let res = self.output.clone();
self.output.clear();
res
}
fn status(&mut self) -> RunnerStatus {
match &mut self.process { match &mut self.process {
None => RunnerStatus::Stopped(None), None => RunnerStatus::Stopped(None),
Some(proc) => match proc.try_wait() { Some(proc) => match proc.try_wait() {
@ -174,42 +209,16 @@ impl Runner {
} }
} }
fn receive_output(&mut self) { fn start(&mut self) {
while let Ok(data) = self.receiver.try_recv() { self.try_start().expect("Failed to execute runner");
self.output.push(data);
}
}
pub fn consume_output(&mut self) -> String {
self.receive_output();
let res = self.output.concat();
self.output.clear();
res
}
pub fn consume_rows(&mut self) -> Vec<String> {
self.receive_output();
let res = self.output.clone();
self.output.clear();
res
}
fn save_log(path_s: String, log: &[String]) -> Result<(), std::io::Error> {
let mut writer = get_writer(&path_s);
let log_s = log.concat();
writer.write_all(log_s.as_ref())
}
pub fn save_output(&mut self, path: String) -> Result<(), std::io::Error> {
Runner::save_log(path, &self.output)
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{CmdRunner, RunnerStatus};
use crate::profile::Profile; use crate::profile::Profile;
use crate::runner::Runner;
use super::{Runner, RunnerStatus};
use core::time; use core::time;
use std::{collections::HashMap, thread::sleep}; use std::{collections::HashMap, thread::sleep};
@ -217,7 +226,7 @@ mod tests {
fn can_run_command_and_read_env() { fn can_run_command_and_read_env() {
let mut env = HashMap::new(); let mut env = HashMap::new();
env.insert("REX2TEST".to_string(), "Lorem ipsum dolor".to_string()); env.insert("REX2TEST".to_string(), "Lorem ipsum dolor".to_string());
let mut runner = Runner::new( let mut runner = CmdRunner::new(
Some(env), Some(env),
"bash".into(), "bash".into(),
vec!["-c".into(), "echo \"REX2TEST: $REX2TEST\"".into()], vec!["-c".into(), "echo \"REX2TEST: $REX2TEST\"".into()],
@ -232,7 +241,7 @@ mod tests {
#[test] #[test]
fn can_save_log() { fn can_save_log() {
let mut runner = Runner::new( let mut runner = CmdRunner::new(
None, None,
"bash".into(), "bash".into(),
vec!["-c".into(), "echo \"Lorem ipsum dolor sit amet\"".into()], vec!["-c".into(), "echo \"Lorem ipsum dolor sit amet\"".into()],
@ -249,7 +258,7 @@ mod tests {
#[test] #[test]
fn can_create_from_profile() { fn can_create_from_profile() {
Runner::xrservice_runner_from_profile(&Profile::load_profile( CmdRunner::xrservice_runner_from_profile(&Profile::load_profile(
&"./test/files/profile.json".to_string(), &"./test/files/profile.json".to_string(),
)); ));
} }

View file

@ -1,9 +1,8 @@
use crate::runner::{Runner, RunnerStatus};
use std::cell::RefCell; use std::cell::RefCell;
use crate::runner::{Runner, RunnerStatus};
pub struct RunnerPipeline { pub struct RunnerPipeline {
runners: Vec<RefCell<Runner>>, runners: Vec<RefCell<Box<dyn Runner>>>,
current_index: usize, current_index: usize,
last_exit_status: Option<i32>, last_exit_status: Option<i32>,
has_started: bool, has_started: bool,
@ -11,8 +10,8 @@ pub struct RunnerPipeline {
} }
impl RunnerPipeline { impl RunnerPipeline {
pub fn new(runners: Vec<Runner>) -> Self { pub fn new(runners: Vec<Box<dyn Runner>>) -> Self {
let mut c_runners: Vec<RefCell<Runner>> = vec![]; let mut c_runners: Vec<RefCell<Box<dyn Runner>>> = vec![];
for runner in runners { for runner in runners {
c_runners.push(RefCell::new(runner)); c_runners.push(RefCell::new(runner));
} }
@ -25,7 +24,7 @@ impl RunnerPipeline {
} }
} }
pub fn get_current_runner(&self) -> Option<&RefCell<Runner>> { pub fn get_current_runner(&self) -> Option<&RefCell<Box<dyn Runner>>> {
self.runners.get(self.current_index) self.runners.get(self.current_index)
} }

View file

@ -32,7 +32,7 @@ use crate::profiles::lighthouse::lighthouse_profile;
use crate::profiles::system_valve_index::system_valve_index_profile; use crate::profiles::system_valve_index::system_valve_index_profile;
use crate::profiles::valve_index::valve_index_profile; use crate::profiles::valve_index::valve_index_profile;
use crate::profiles::wivrn::wivrn_profile; use crate::profiles::wivrn::wivrn_profile;
use crate::runner::{Runner, RunnerStatus}; use crate::runner::{CmdRunner, Runner, RunnerStatus};
use crate::runner_pipeline::RunnerPipeline; use crate::runner_pipeline::RunnerPipeline;
use crate::ui::build_window::BuildWindowMsg; use crate::ui::build_window::BuildWindowMsg;
use crate::ui::debug_view::DebugViewInit; use crate::ui::debug_view::DebugViewInit;
@ -76,7 +76,7 @@ pub struct App {
#[tracker::do_not_track] #[tracker::do_not_track]
config: Config, config: Config,
#[tracker::do_not_track] #[tracker::do_not_track]
xrservice_runner: Option<Runner>, xrservice_runner: Option<CmdRunner>,
#[tracker::do_not_track] #[tracker::do_not_track]
build_pipeline: Option<RunnerPipeline>, build_pipeline: Option<RunnerPipeline>,
#[tracker::do_not_track] #[tracker::do_not_track]
@ -149,7 +149,7 @@ impl App {
XRServiceType::Wivrn => true, // no device from log in wivrn XRServiceType::Wivrn => true, // no device from log in wivrn
}; };
self.debug_view.sender().emit(DebugViewMsg::ClearLog); self.debug_view.sender().emit(DebugViewMsg::ClearLog);
let mut runner = Runner::xrservice_runner_from_profile(&prof); let mut runner = CmdRunner::xrservice_runner_from_profile(&prof);
match runner.try_start() { match runner.try_start() {
Ok(_) => { Ok(_) => {
self.xrservice_runner = Some(runner); self.xrservice_runner = Some(runner);
@ -400,7 +400,7 @@ impl SimpleComponent for App {
Msg::BuildProfile(clean_build) => { Msg::BuildProfile(clean_build) => {
let profile = self.get_selected_profile(); let profile = self.get_selected_profile();
let mut missing_deps = vec![]; let mut missing_deps = vec![];
let mut runners: Vec<Runner> = vec![]; let mut runners: Vec<Box<dyn Runner>> = vec![];
// profile per se can't be built, but we still need opencomp // profile per se can't be built, but we still need opencomp
if profile.can_be_built { if profile.can_be_built {
missing_deps.extend(match profile.xrservice_type { missing_deps.extend(match profile.xrservice_type {
@ -417,7 +417,7 @@ impl SimpleComponent for App {
} }
if profile.features.mercury_enabled { if profile.features.mercury_enabled {
missing_deps.extend(get_missing_mercury_deps()); missing_deps.extend(get_missing_mercury_deps());
runners.push(get_build_mercury_runner(&profile)); runners.push(Box::new(get_build_mercury_runner(&profile)));
} }
runners.extend(match profile.xrservice_type { runners.extend(match profile.xrservice_type {
XRServiceType::Monado => get_build_monado_runners(&profile, clean_build), XRServiceType::Monado => get_build_monado_runners(&profile, clean_build),

View file

@ -5,7 +5,7 @@ use crate::{
downloader::download_file, downloader::download_file,
paths::wivrn_apk_download_path, paths::wivrn_apk_download_path,
profile::{Profile, XRServiceType}, profile::{Profile, XRServiceType},
runner::{Runner, RunnerStatus}, runner::{CmdRunner, Runner, RunnerStatus},
}; };
use gtk::prelude::*; use gtk::prelude::*;
use relm4::{ use relm4::{
@ -31,7 +31,7 @@ pub struct InstallWivrnBox {
#[tracker::do_not_track] #[tracker::do_not_track]
download_thread: Option<JoinHandle<Result<(), reqwest::Error>>>, download_thread: Option<JoinHandle<Result<(), reqwest::Error>>>,
#[tracker::do_not_track] #[tracker::do_not_track]
install_runner: Option<Runner>, install_runner: Option<CmdRunner>,
#[tracker::do_not_track] #[tracker::do_not_track]
root_win: gtk::Window, root_win: gtk::Window,
} }

View file

@ -1,4 +1,7 @@
use crate::{profile::Profile, runner::Runner}; use crate::{
profile::Profile,
runner::{CmdRunner, Runner},
};
use adw::prelude::*; use adw::prelude::*;
use gtk::glib; use gtk::glib;
use relm4::prelude::*; use relm4::prelude::*;
@ -40,7 +43,7 @@ pub struct LibsurviveSetupWindow {
#[tracker::do_not_track] #[tracker::do_not_track]
profile: Option<Profile>, profile: Option<Profile>,
#[tracker::do_not_track] #[tracker::do_not_track]
calibration_runner: Option<Runner>, calibration_runner: Option<CmdRunner>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -54,14 +57,14 @@ pub enum LibsurviveSetupMsg {
} }
impl LibsurviveSetupWindow { impl LibsurviveSetupWindow {
fn create_calibration_runner(&mut self, survive_cli_path: String) -> Runner { fn create_calibration_runner(&mut self, survive_cli_path: String) -> CmdRunner {
let lh_path = self.steam_lighthouse_path.clone(); let lh_path = self.steam_lighthouse_path.clone();
let mut env = HashMap::new(); let mut env = HashMap::new();
env.insert( env.insert(
"LD_LIBRARY_PATH".to_string(), "LD_LIBRARY_PATH".to_string(),
format!("{pfx}/lib", pfx = self.profile.as_ref().unwrap().prefix), format!("{pfx}/lib", pfx = self.profile.as_ref().unwrap().prefix),
); );
Runner::new( CmdRunner::new(
Some(env), Some(env),
survive_cli_path, survive_cli_path,
vec!["--steamvr-calibration".into(), lh_path], vec!["--steamvr-calibration".into(), lh_path],