Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: use_gitpod() and use_cmakelists() drafts #48

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
33f12f6
use_gitpod draft
benubah Oct 24, 2022
6e9c998
use_cmakelists draft
benubah Oct 24, 2022
70c8fc7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2022
fb9d217
no need to check
benubah Oct 26, 2022
c9f7608
Merge branch 'use-gitpod-cmakelists' of https://github.com/cynkra/cyn…
benubah Oct 26, 2022
745979d
add usethis templates for gitpod and CMakeLists
benubah Oct 26, 2022
3ddfc74
improve use_gitpod() to work with usethis templates
benubah Oct 26, 2022
2218c74
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2022
f9b7b6c
import fs
benubah Oct 26, 2022
77f8efc
improve use_cmakelists to use usethis templates
benubah Oct 26, 2022
6c177cc
Merge branch 'use-gitpod-cmakelists' of https://github.com/cynkra/cyn…
benubah Oct 26, 2022
30cc726
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2022
3d0ebe3
get list of files from R
benubah Oct 26, 2022
e5545bb
Merge branch 'use-gitpod-cmakelists' of https://github.com/cynkra/cyn…
benubah Oct 26, 2022
38fa54f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2022
679f1ab
basic documentation
benubah Oct 26, 2022
486e7a6
update NAMESPACE
benubah Oct 26, 2022
7eb3a12
Merge branch 'use-gitpod-cmakelists' of https://github.com/cynkra/cyn…
benubah Oct 26, 2022
e7516ab
update _pkgdown.yml
benubah Oct 26, 2022
8059079
Tweaks
krlmlr Oct 27, 2022
77fb2e3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2022
d3760e4
Dynamic R home and LinkingTo
krlmlr Dec 5, 2022
62bd3a2
Tweak
krlmlr Dec 6, 2022
c288ba0
Use new template
krlmlr Dec 6, 2022
4225863
Avoid creating Dockerfile by default
krlmlr Dec 6, 2022
dcabffa
Fix CMakeLists template
krlmlr Dec 6, 2022
b2ca34b
Tweak template
krlmlr Feb 20, 2023
8989c53
Install lazytest
krlmlr Feb 20, 2023
7cb5b0d
Merge branch 'main' into use-gitpod-cmakelists
krlmlr Mar 14, 2023
db7fed0
Merge branch 'main' into use-gitpod-cmakelists
krlmlr May 21, 2023
4f87d61
Package-agnostic
krlmlr May 21, 2023
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Imports:
callr,
checkmate,
cli,
fs,
gh,
glue,
jsonlite,
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ export(init_tic)
export(renv_downgrade)
export(renv_install_local)
export(renv_switch_r_version)
export(use_cmakelists)
export(use_gitpod)
importFrom(checkmate,assert_character)
importFrom(cli,cli_alert)
importFrom(fs,dir_create)
importFrom(gh,gh)
importFrom(jsonlite,read_json)
importFrom(lifecycle,deprecated)
importFrom(renv,install)
importFrom(rstudioapi,restartSession)
importFrom(usethis,use_package)
importFrom(usethis,use_template)
importFrom(usethis,use_tidy_description)
importFrom(utils,available.packages)
importFrom(utils,download.file)
Expand Down
61 changes: 61 additions & 0 deletions R/use_cmakelists.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#' Initialize CMakeLists configuration
#'
#' @description
#' Initializes a `CMakeLists.txt` at the current working directory and at the `src/`
#' folder with the necessary configuration information
#'
#' @param project `[character]`\cr
#' The name of the R project
#'
#' @examples
#' \dontrun{
#' use_cmakelists("Your_Project_Name")
#' }
#' @importFrom usethis use_template
#' @importFrom fs dir_create
#' @export
use_cmakelists <- function(project = NULL) {
generator <- "Generated by cynkrathis::use_cmakelists(), do not edit by hand"
deparsed_call <- prepend(capture_call(sys.call()), "# ")

if (is.null(project)) {
project <- desc::desc_get_field("Package")
}

new_cmakelist <- usethis::use_template(
"CMakeLists.txt",
"CMakeLists.txt",
package = "cynkrathis",
data = list(
generator = generator,
call = deparsed_call,
project = project
),
ignore = TRUE
)

fs::dir_create("src")

ext_files <- list.files(path = "src", pattern = "\\.(c|h|cpp)$")
ext_files <- withr::with_collate("C", prepend(sort(ext_files), " "))

deps <- desc::desc_get_deps()
linking_to_deps <- deps$package[deps$type == "LinkingTo"]
linking_to_deps <- withr::with_collate("C", paste0(sort(linking_to_deps), collapse = " "))

new_cmakelist_src <- usethis::use_template(
"CMakeLists-src.txt",
"src/CMakeLists.txt",
package = "cynkrathis",
data = list(
generator = generator,
call = deparsed_call,
project = project,
ext_files = ext_files,
linking_to_deps = linking_to_deps
),
ignore = FALSE
)

invisible(new_cmakelist || new_cmakelist_src)
}
48 changes: 48 additions & 0 deletions R/use_gitpod.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#' Initialize gitpod configuration
#'
#' @description
#' Initializes `.gitpod.Dockerfile` and `.gitpod.yml` at the working directory
#' with the necessary gitpod configuration information
#'
#' @param apt_packages Additional `apt` packages to install in the Docker image.
#' @param user_code User code to configure in `.gitpod.yml`
#'
#' @examples
#' \dontrun{
#' use_gitpod()
#' }
#' @importFrom usethis use_template
#' @export
use_gitpod <- function(apt_packages = NULL, user_code = NULL) {
generator <- "Generated by cynkrathis::use_gitpod(), do not edit by hand"
deparsed_apt_packages <- paste(apt_packages, collapse = " ")
deparsed_user_code <- prepend(user_code, " ")
deparsed_call <- prepend(capture_call(sys.call()), "# ")

new_gitpod_yml <- usethis::use_template(
"gitpod.yml",
".gitpod.yml",
data = list(
generator = generator,
user_code = deparsed_user_code,
call = deparsed_call
),
package = "cynkrathis",
ignore = TRUE
)

invisible(new_gitpod_yml)
}

prepend <- function(x, prefix) {
if (is.null(x)) {
return("")
}
x_vec <- unlist(strsplit(x, "\n"))
x_prefix <- gsub("^ *", prefix, x_vec)
paste0(x_prefix, collapse = "\n")
}

capture_call <- function(call) {
capture.output(print(constructive::construct(call)))
}
31 changes: 31 additions & 0 deletions inst/templates/CMakeLists-src.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# {{{generator}}}
#
# Call:
{{{call}}}

add_library({{{project}}}
{{{ext_files}}}
)

set(R_DEPENDENCIES {{{linking_to_deps}}})

execute_process(COMMAND bash "-c" "Rscript -e 'cat(R.home(\"include\"))'" OUTPUT_VARIABLE R_INCLUDE)
execute_process(COMMAND bash "-c" "Rscript -e 'cat(.libPaths()[[1]])'" OUTPUT_VARIABLE R_LIBDIR)

foreach(PKG_NAME IN LISTS R_DEPENDENCIES)
list(APPEND R_PKG_INCLUDE_LIST "${R_LIBDIR}/${PKG_NAME}/include")
endforeach()
message("${R_PKG_INCLUDE_LIST}")

target_include_directories({{{project}}} PUBLIC
${R_PKG_INCLUDE_LIST}
${R_INCLUDE}
# Included by default
"."
"vendor"
)

get_target_property(include_dirs {{{project}}} INTERFACE_INCLUDE_DIRECTORIES)
message("${include_dirs}")

include(./custom.cmake OPTIONAL)
11 changes: 11 additions & 0 deletions inst/templates/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# {{{generator}}}
#
# Call:
{{{call}}}

cmake_minimum_required(VERSION 3.14)
project({{{project}}} VERSION 0.1.0)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_subdirectory(src)
65 changes: 65 additions & 0 deletions inst/templates/gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# {{{generator}}}
#
# Call:
{{{call}}}

tasks:
- name: dependencies
init: |
# Set up rig
curl -Ls https://github.com/r-lib/rig/releases/download/latest/rig-linux-latest.tar.gz | sudo tar xz -C /usr/local

# Set up R
rig install
rig system add-pak --pak-version devel
rig system make-links
rig system setup-user-lib

# Deps
sudo apt install -y ccache cmake

# Scriptlets, with custom Git config
mv ~/.gitconfig ~/.gitconfig.gitpod
curl -s https://raw.githubusercontent.com/krlmlr/scriptlets/master/bootstrap | sh

## .editorconfig
ln -s ~/.editorconfig ..

## Set up ccache
ln -s /usr/lib/ccache/* ~/bin/

## Work around glitch with non-systemd systems
ln -s $(which true) ~/bin/timedatectl

## Define PATH
# FIXME: Why is this necessary? This doesn't work in Radian.
echo 'export PATH='${HOME}'/bin:${PATH}' >> ~/.bashrc

# Set up Makevars
mkdir -p ~/.R
echo -e "MAKEFLAGS = -j8\nCXXFLAGS = -O0 -g" > ~/.R/Makevars

# Install R packages
echo 'options(repos = "https://packagemanager.rstudio.com/all/__linux__/'$(cat /etc/lsb-release | sed -n '/DISTRIB_CODENAME=/ {s///;p}')'/latest")' >> ~/.Rprofile

## Install devtools and R dependencies
R -q -e 'pak::pak(c("devtools", "languageserver", "styler", "krlmlr/lazytest"); pak::pak()'

# Install radian
sudo pip install radian

# Run custom code
if [ -x tools/gitpod.sh ]; then tools/gitpod.sh; fi

vscode:
extensions:
- ms-vscode.cpptools
# needs reload of VS Code to be operational
- go2sh.cmake-integration-vscode
# buggy -- doesn't reliably interpret include files?
# observed behavior: repeated saving of src/CMakeLists.txt
# changes detaction of include files -- sometimes it works, sometimes it doesn't,
# with no detectable pattern
# - ms-vscode.cmake-tools
- EditorConfig.EditorConfig
- REditorSupport.r
21 changes: 21 additions & 0 deletions man/use_cmakelists.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions man/use_gitpod.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ reference:
- init_tic
- init_gitignore
- init_lintr
- use_gitpod
- use_cmakelists