-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `r_command()` for executing commands over `R` and `R.bat` * Rework `r_command()` for readability
- Loading branch information
1 parent
e06f5fa
commit e07efa9
Showing
9 changed files
with
94 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// command.rs | ||
// | ||
// Copyright (C) 2024 Posit Software, PBC. All rights reserved. | ||
// | ||
// | ||
|
||
use std::io; | ||
use std::process::Command; | ||
use std::process::Output; | ||
|
||
use crate::sys::command::COMMAND_R_LOCATIONS; | ||
|
||
/// Execute a `Command` for R, trying multiple locations where R might exist | ||
/// | ||
/// - For unix, this look at `R` | ||
/// - For Windows, this looks at `R` (`R.exe`) and `R.bat` (for rig compatibility) | ||
/// | ||
/// Returns the `Ok()` value of the first success, or the `Err()` value of the | ||
/// last failure if all locations fail. | ||
pub fn r_command<F>(build: F) -> io::Result<Output> | ||
where | ||
F: Fn(&mut Command), | ||
{ | ||
assert!(COMMAND_R_LOCATIONS.len() > 0); | ||
|
||
let mut out = None; | ||
|
||
for program in COMMAND_R_LOCATIONS.iter() { | ||
// Build the `Command` from the user's function | ||
let mut command = Command::new(program); | ||
build(&mut command); | ||
|
||
// Run it, waiting on it to finish. | ||
// Store it as `out` no matter what. If all locations fail | ||
// we end up returning the last failure. | ||
let result = command.output(); | ||
let ok = result.is_ok(); | ||
out = Some(result); | ||
|
||
if ok { | ||
// We had a successful command, don't try any more | ||
break; | ||
} | ||
} | ||
|
||
// SAFETY: The `assert!` above ensures at least 1 program location is provided | ||
out.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
* | ||
*/ | ||
|
||
pub mod command; | ||
pub mod library; | ||
pub mod line_ending; | ||
pub mod polled_events; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* command.rs | ||
* | ||
* Copyright (C) 2024 Posit Software, PBC. All rights reserved. | ||
* | ||
*/ | ||
|
||
/// Locations on the `PATH` to look for R when using `Command::new()` | ||
pub(crate) const COMMAND_R_LOCATIONS: [&str; 1] = ["R"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
* | ||
*/ | ||
|
||
pub mod command; | ||
pub mod library; | ||
pub mod line_ending; | ||
mod locale; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* command.rs | ||
* | ||
* Copyright (C) 2024 Posit Software, PBC. All rights reserved. | ||
* | ||
*/ | ||
|
||
/// Locations on the `PATH` to look for R when using `Command::new()` | ||
pub(crate) const COMMAND_R_LOCATIONS: [&str; 2] = ["R", "R.bat"]; |