Skip to content

Commit

Permalink
Merge pull request #311 from r-lib/feature/builder
Browse files Browse the repository at this point in the history
Builder: GH releases + GHCR helper functions
  • Loading branch information
gaborcsardi authored Jul 29, 2024
2 parents 945abab + a5801b5 commit 2ce273c
Show file tree
Hide file tree
Showing 17 changed files with 2,703 additions and 18 deletions.
8 changes: 8 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Suggests:
debugme,
fansi,
fs,
gh,
gitcreds,
glue,
htmlwidgets,
mockery,
Expand All @@ -54,6 +56,12 @@ Suggests:
testthat (>= 3.2.0),
tibble,
webfakes (>= 1.1.5.9000),
withr (>= 2.1.1),
Remotes:
r-lib/pkgcache
Config/Needs/builder:
gh,
pkgsearch,
withr (>= 2.1.1)
Config/Needs/coverage:
r-lib/asciicast,
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export(as_pkg_dependencies)
export(current_config)
export(current_r_platform)
export(default_platforms)
export(ghr)
export(ghrepo)
export(install_package_plan)
export(is_valid_package_name)
export(lib_status)
Expand All @@ -41,6 +43,7 @@ export(new_pkg_installation_plan)
export(new_pkg_installation_proposal)
export(parse_pkg_ref)
export(parse_pkg_refs)
export(pkg_build)
export(pkg_dep_types)
export(pkg_dep_types_hard)
export(pkg_dep_types_soft)
Expand All @@ -50,6 +53,7 @@ export(pkg_installation_plan)
export(pkg_installation_proposal)
export(pkg_name_check)
export(pkg_rx)
export(repo)
export(sysreqs_check_installed)
export(sysreqs_db_list)
export(sysreqs_db_match)
Expand Down
86 changes: 86 additions & 0 deletions R/builder.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

#' Create a binary package from an installed package
#'
#' The built package will be in the current working directory.
#'
#' This function is currently experimental.
#'
#' @param pkg Package name.
#' @param library Library path.
#' @param flavor Platform flavor. Defaults to the `PKG_BUILD_FLAVOR`
#' environment variable. If not `NULL` or an empty string, then it is
#' appended to the platform string with a dash.
#' @param build_number An integer number that is added to the file name,
#' after the version number, to be able to have multiple builds for the
#' same package version.
#' @return Path to the built package.
#'
#' @export
#' @keywords internal

pkg_build <- function(pkg, library = .libPaths()[1],
flavor = Sys.getenv("PKG_BUILD_FLAVOR"),
build_number = 1L) {
pkgdir <- file.path(library, pkg)
if (!dir.exists(pkgdir)) {
throw(pkg_error(
"Cannot find package {.pkg {pkg}} in library at {.path {library}}."
))
}
platform <- pkgcache::current_r_platform()
if (nzchar(flavor %||% "")) {
platform <- paste0(platform, "-", flavor)
}
meta <- c(
RemoteBuildPlatform = platform,
GraphicsAPIVersion = pkgcache::get_graphics_api_version(),
InternalsId = pkgcache::get_internals_id()
)
add_metadata(pkgdir, meta)
dsc <- desc::desc(file = pkgdir)
version <- dsc$get_field("Version")
rversion <- get_minor_r_version(getRversion())

sys <- sysname()
if (sys == "windows") {
install_md5_sums(pkg)
fn <- paste0(
pkg, "_", version, "_",
"b", build_number, "_",
"R", rversion,
if (nzchar(flavor %||% "")) paste0("_", flavor),
".zip"
)
zip::zip(fn, pkgdir, mode = "cherry-pick")

} else {
ext <- if (sys == "mac") ".tgz" else ".tar.gz"
fn <- paste0(
pkg, "_", version, "_",
"b", build_number, "_",
"R", rversion, "_",
platform,
ext
)
ffn <- file.path(normalizePath("."), fn)
old <- getwd()
on.exit(setwd(old), add = TRUE)
setwd(dirname(pkgdir))
utils::tar(ffn, pkg, compression = "gzip", compression_level = 9)
}

fn
}

install_md5_sums <- function(pkgdir) {
old <- getwd()
on.exit(setwd(old), add = TRUE)

setwd(pkgdir)
fns <- setdiff(dir(".", recursive = TRUE), "MD5")
md5 <- cli::hash_file_md5(fns)
writeLines(
paste0(md5, " *", fns),
"MD5"
)
}
Loading

0 comments on commit 2ce273c

Please sign in to comment.