Skip to content

Commit

Permalink
Make rig resolve more flexible
Browse files Browse the repository at this point in the history
Add `--platform` and `--arch` arguments.
Allow installing from URL on all platforms.
  • Loading branch information
gaborcsardi committed Aug 22, 2023
1 parent ea5d4a1 commit a1ac482
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 65 deletions.
18 changes: 18 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,26 @@ pub fn rig_app() -> Command {
.long("json")
.num_args(0)
.required(false),
)
.arg(
Arg::new("platform")
.help("Use this platform, instead of auto-detecting it.")
.long("platform")
.required(false)
);

#[cfg(any(target_os = "windows", target_os = "linux"))]
{
cmd_resolve = cmd_resolve.arg(
Arg::new("arch")
.help("Use this architecture, instead of auto-detecting it.")
.short('a')
.long("arch")
.required(false)
.value_parser(clap::value_parser!(String))
);
}

#[cfg(target_os = "macos")]
{
cmd_resolve = cmd_resolve.arg(
Expand Down
50 changes: 42 additions & 8 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,19 @@ pub fn sc_rstudio(args: &ArgMatches) -> Result<(), Box<dyn Error>> {

// -- rig avilable --------------------------------------------------------

fn get_platform(args: &ArgMatches)
-> Result<String, Box<dyn Error>> {
let platform: Option<String> = args.get_one::<String>("platform").cloned();
pub fn get_platform(args: &ArgMatches)
-> Result<String, Box<dyn Error>> {

// rig add doe snot have a -platform argument
let platform = args.try_get_one::<&str>("platform");

let platform: Option<&str> = match platform {
Ok(x) => x.copied(),
Err(_) => None
};

if let Some(x) = platform {
return Ok(x);
return Ok(x.to_string());
}

#[allow(unused_mut)]
Expand All @@ -259,12 +266,39 @@ fn get_platform(args: &ArgMatches)
Ok(os)
}

fn get_arch(platform: &str, args: &ArgMatches) -> String {
pub fn get_arch(platform: &str, args: &ArgMatches) -> String {
#[allow(unused_mut)]

let arch: String = match args.get_one::<String>("arch").cloned() {
Some(x) => x,
None => env::consts::ARCH.to_string()
// For rig add we don't have --arch, except on macOS
let arch = args.try_get_one::<String>("arch");
debug!("specifies arch: {:?}.", arch);
let arch: Option<String> = match arch {
Ok(x) => x.cloned(),
Err(_) => None
};

// For Windows, the default is x86_64
let arch = match arch {
Some(x) => {
match args.value_source("arch") {
Some(y) => {
if y == clap::parser::ValueSource::DefaultValue &&
platform == "windows"{
"x86_64".to_string()
} else {
x
}
},
None => x
}
},
None => {
if platform == "windows" {
"x86_64".to_string()
} else {
env::consts::ARCH.to_string()
}
}
};

// Prefer 'arm64' on macos, but 'aarch64' on linux
Expand Down
15 changes: 1 addition & 14 deletions src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ use clap::ArgMatches;
use simple_error::*;
use simplelog::{trace,debug, info, warn};

use crate::resolve::resolve_versions;
use crate::rversion::*;

use crate::alias::*;
use crate::common::*;
use crate::download::*;
use crate::escalate::*;
use crate::library::*;
use crate::resolve::get_resolve;
use crate::run::*;
use crate::utils::*;

Expand Down Expand Up @@ -365,19 +365,6 @@ fn version_from_link(pb: PathBuf) -> Option<String> {
s
}

pub fn get_resolve(args: &ArgMatches) -> Result<Rversion, Box<dyn Error>> {
let str = args.get_one::<String>("str").unwrap();
let eps = vec![str.to_string()];
let me = detect_linux_old()?;
let version = resolve_versions(
eps,
"linux".to_string(),
std::env::consts::ARCH.to_string(),
Some(me)
)?;
Ok(version[0].to_owned())
}

pub fn sc_get_list() -> Result<Vec<String>, Box<dyn Error>> {
let mut vers = Vec::new();
if !Path::new(R_ROOT).exists() {
Expand Down
19 changes: 1 addition & 18 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::common::*;
use crate::download::*;
use crate::escalate::*;
use crate::library::*;
use crate::resolve::resolve_versions;
use crate::resolve::get_resolve;
use crate::rversion::*;
use crate::run::*;
use crate::utils::*;
Expand Down Expand Up @@ -702,23 +702,6 @@ pub fn sc_system_forget() -> Result<(), Box<dyn Error>> {
Ok(())
}

pub fn get_resolve(args: &ArgMatches) -> Result<Rversion, Box<dyn Error>> {
let str: &String = args.get_one("str").unwrap();
let arch: &String = args.get_one("arch").unwrap();

if str.len() > 8 && (&str[..7] == "http://" || &str[..8] == "https://") {
Ok(Rversion {
version: None,
url: Some(str.to_string()),
arch: None,
})
} else {
let eps = vec![str.to_string()];
let version = resolve_versions(eps, "macos".to_string(), arch.to_string(), None)?;
Ok(version[0].to_owned())
}
}

pub fn sc_system_no_openmp(args: &ArgMatches) -> Result<(), Box<dyn Error>> {
escalate("updating R compiler configuration")?;
let vers = args.get_many::<String>("version");
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ mod linux;
#[cfg(target_os = "linux")]
use linux::*;

use resolve::*;

mod alias;
mod library;
mod common;
Expand Down
43 changes: 26 additions & 17 deletions src/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
use futures::future;
use std::error::Error;

use clap::ArgMatches;
use simple_error::bail;

use crate::common::*;
use crate::download::*;
use crate::rversion::*;
use crate::utils::*;

const API_URI: &str = "https://api.r-hub.io/rversions/resolve/";

pub fn get_resolve(args: &ArgMatches) -> Result<Rversion, Box<dyn Error>> {
let platform = get_platform(args)?;
let arch = get_arch(&platform, args);
let str: &String = args.get_one("str").unwrap();
let eps = vec![str.to_string()];

if str.len() > 8 && (&str[..7] == "http://" || &str[..8] == "https://") {
Ok(Rversion {
version: None,
url: Some(str.to_string()),
arch: None,
})
} else {
Ok(resolve_versions(eps, &platform, &arch)?[0].to_owned())
}
}

#[tokio::main]
pub async fn resolve_versions(
vers: Vec<String>,
os: String,
arch: String,
linux: Option<LinuxVersion>,
platform: &str,
arch: &str
) -> Result<Vec<Rversion>, Box<dyn Error>> {
let client = reqwest::Client::new();
let client = &client;
let os = &os;
let arch = &arch;
let out: Vec<Result<Rversion, Box<dyn Error>>> =
future::join_all(vers.into_iter().map(move |ver| {
let linux = linux.clone();
async move {
resolve_version(client, &ver, os, arch, linux).await
resolve_version(client, &ver, platform, arch).await
}
}))
.await;
Expand All @@ -43,17 +58,11 @@ pub async fn resolve_versions(

async fn resolve_version(
client: &reqwest::Client,
ver: &String,
os: &String,
arch: &String,
linux: Option<LinuxVersion>,
ver: &str,
platform: &str,
arch: &str
) -> Result<Rversion, Box<dyn Error>> {
let mut url = API_URI.to_string() + ver + "/" + os;

if os == "linux" {
let linux = linux.unwrap();
url = url + "-" + &linux.distro + "-" + &linux.version;
}
let mut url = API_URI.to_string() + ver + "/" + platform;

if arch != "default" {
url = url + "/" + arch;
Expand Down
9 changes: 1 addition & 8 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::common::*;
use crate::download::*;
use crate::escalate::*;
use crate::library::*;
use crate::resolve::resolve_versions;
use crate::resolve::get_resolve;
use crate::rversion::*;
use crate::run::*;
use crate::utils::*;
Expand Down Expand Up @@ -585,13 +585,6 @@ pub fn sc_system_detect_platform(_args: &ArgMatches, _mainargs: &ArgMatches)
Ok(())
}

pub fn get_resolve(args: &ArgMatches) -> Result<Rversion, Box<dyn Error>> {
let str = args.get_one::<String>("str").unwrap();
let eps = vec![str.to_string()];
let version = resolve_versions(eps, "win".to_string(), "default".to_string(), None)?;
Ok(version[0].to_owned())
}

// ------------------------------------------------------------------------

pub fn sc_get_list() -> Result<Vec<String>, Box<dyn Error>> {
Expand Down

0 comments on commit a1ac482

Please sign in to comment.