Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions crates/ark/src/modules/positron/repos.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,34 @@ apply_repo_defaults <- function(
}
options(repos = repos)
}

#' Set the Posit Package Manager repository
#'
#' Sets the Posit Package Manager repository URL for the current session. The
#' URL will be processed to point to a Linux distribution-specific binary URL if
#' applicable.
#'
#' This function only overrides the CRAN repository when Ark has previously set
#' it or when it uses placeholder `"@CRAN@"`.
#'
#' @param url A PPM repository URL. Must be in the form
#' `"https://host/repo/snapshot"`, e.g.,
#' `"https://packagemanager.posit.co/cran/latest"`.
#'
#' @return `NULL`, invisibly.
#'
#' @export
.ps.setPPMRepo <- function(url) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.ps.setPPMRepo <- function(url) {
.ps.set_ppm_repo <- function(url) {

# Use Ark's built-in PPM binary URL detection.
url <- .ps.Call("ps_get_ppm_binary_url", url)

# If Ark has already set the repos option, it should be safe to do so again.
repos <- getOption("repos")
if (isTRUE(attr(repos, "IDE"))) {
repos[["CRAN"]] <- url
Comment on lines +65 to +66
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Maybe add that updating logic to apply_repo_defaults()? E.g. we update CRAN if set to "@CRAN@" or if the IDE attribute is set.

return(options(repos = repos))
}

# Otherwise we defer to the existing application logic.
apply_repo_defaults(c(CRAN = url))
}
24 changes: 24 additions & 0 deletions crates/ark/src/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ use std::io::BufRead;
use std::io::BufReader;
use std::path::PathBuf;

use anyhow::Context;
use harp::exec::RFunction;
use harp::exec::RFunctionExt;
use harp::RObject;
use libr::SEXP;

use crate::modules::ARK_ENVS;

Expand Down Expand Up @@ -346,6 +348,28 @@ fn get_ppm_binary_package_repo(repo_url: Option<url::Url>) -> String {
}
}

#[harp::register]
pub extern "C-unwind" fn ps_get_ppm_binary_url(url: SEXP) -> anyhow::Result<SEXP> {
let url_string = unsafe { RObject::view(url).to::<String>().context("`url` must be a string")? };
if url_string.is_empty() {
return Err(anyhow::anyhow!("Empty Package Manager URL provided"));
}

// Validate the PPM URL structure.
let parsed = url::Url::parse(&url_string).context("Invalid URL format")?;
let segments = parsed
.path_segments()
.ok_or_else(|| anyhow::anyhow!("Package Manager URL must have a path"))?;
if segments.count() != 2 {
return Err(anyhow::anyhow!(
"Package Manager URL must have exactly 2 path segments (e.g., /cran/latest)"
));
}

let final_url = get_ppm_binary_package_repo(Some(parsed));
Ok(RObject::from(final_url).sexp)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down