From 1a9d75f09d47e50330456cb28e70234d3412469d Mon Sep 17 00:00:00 2001 From: eitsupi <50911393+eitsupi@users.noreply.github.com> Date: Tue, 5 Sep 2023 22:52:30 +0900 Subject: [PATCH] New Feature: R history (#193) Close #190 --- .github/workflows/test.yaml | 3 ++ src/r-history/NOTES.md | 6 +++ src/r-history/devcontainer-feature.json | 24 ++++++++++++ src/r-history/install.sh | 35 +++++++++++++++++ src/r-history/oncreate.sh | 50 +++++++++++++++++++++++++ test/r-history/test.sh | 12 ++++++ 6 files changed, 130 insertions(+) create mode 100644 src/r-history/NOTES.md create mode 100644 src/r-history/devcontainer-feature.json create mode 100755 src/r-history/install.sh create mode 100755 src/r-history/oncreate.sh create mode 100755 test/r-history/test.sh diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 4df6d01..c0b8c56 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -29,6 +29,7 @@ jobs: pandoc: ./**/pandoc/** quarto-cli: ./**/quarto-cli/** r-apt: ./**/r-apt/** + r-history: ./**/r-history/** r-packages: ./**/r-packages/** r-rig: ./**/r-rig/** renv-cache: ./**/renv-cache/** @@ -89,6 +90,7 @@ jobs: - pandoc - quarto-cli - r-apt + - r-history - r-packages - r-rig - renv-cache @@ -119,6 +121,7 @@ jobs: - pandoc - quarto-cli - r-apt + - r-history - r-packages - r-rig - renv-cache diff --git a/src/r-history/NOTES.md b/src/r-history/NOTES.md new file mode 100644 index 0000000..3079bbe --- /dev/null +++ b/src/r-history/NOTES.md @@ -0,0 +1,6 @@ + + +## Acknowledgements + +This Feature is inspired by +[`ghcr.io/stuartleeks/dev-container-features/shell-history`](https://github.com/stuartleeks/dev-container-features/tree/main/src/shell-history). diff --git a/src/r-history/devcontainer-feature.json b/src/r-history/devcontainer-feature.json new file mode 100644 index 0000000..3748946 --- /dev/null +++ b/src/r-history/devcontainer-feature.json @@ -0,0 +1,24 @@ +{ + "name": "R history", + "id": "r-history", + "version": "0.1.0", + "description": "Preserve R terminal history across Dev Container instances. Supports Radian and RStudio Server integrated terminal.", + "documentationURL": "https://github.com/rocker-org/devcontainer-features/tree/main/src/r-history", + "options": {}, + "containerEnv": { + "R_HISTFILE": "/dc/r-history/.Rhistory" + }, + "mounts": [ + { + "source": "${devcontainerId}-r-history", + "target": "/dc/r-history", + "type": "volume" + } + ], + "onCreateCommand": { + "r-history-setup": "/usr/local/share/rocker-devcontainer-features/r-history/scripts/oncreate.sh" + }, + "installsAfter": [ + "ghcr.io/devcontainers/features/common-utils" + ] +} diff --git a/src/r-history/install.sh b/src/r-history/install.sh new file mode 100755 index 0000000..7b40063 --- /dev/null +++ b/src/r-history/install.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +USERNAME=${USERNAME:-${_REMOTE_USER}} + +LIFECYCLE_SCRIPTS_DIR="/usr/local/share/rocker-devcontainer-features/r-history/scripts" + +set -e + +create_cache_dir() { + if [ -d "$1" ]; then + echo "Cache directory $1 already exists. Skip creation..." + else + echo "Create cache directory $1..." + mkdir -p "$1" + fi + + if [ -z "$2" ]; then + echo "No username provided. Skip chown..." + else + echo "Change owner of $1 to $2..." + chown -R "$2:$2" "$1" + fi +} + +export DEBIAN_FRONTEND=noninteractive + +create_cache_dir "/dc/r-history" "${USERNAME}" + +# Set Lifecycle scripts +if [ -f oncreate.sh ]; then + mkdir -p "${LIFECYCLE_SCRIPTS_DIR}" + cp oncreate.sh "${LIFECYCLE_SCRIPTS_DIR}/oncreate.sh" +fi + +echo "Done!" diff --git a/src/r-history/oncreate.sh b/src/r-history/oncreate.sh new file mode 100755 index 0000000..7709267 --- /dev/null +++ b/src/r-history/oncreate.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +set -e + +fix_permissions() { + local dir + dir="${1}" + + if [ ! -w "${dir}" ]; then + echo "Fixing permissions of '${dir}'..." + sudo chown -R "$(id -u):$(id -g)" "${dir}" + echo "Done!" + else + echo "Permissions of '${dir}' are OK!" + fi +} + +set_renviron() { + local renviron_file + + if [ -n "${R_ENVIRON}" ]; then + renviron_file="${R_ENVIRON}" + else + renviron_file="${HOME}/.Renviron" + fi + + echo "Updating '${renviron_file}'..." + echo "R_HISTFILE=${R_HISTFILE}" >>"${renviron_file}" + echo "Done!" +} + +set_radian_history() { + local radian_profile_dir + + if [ -n "${XDG_CONFIG_HOME}" ]; then + radian_profile_dir="${XDG_CONFIG_HOME}/radian" + else + radian_profile_dir="${HOME}/.config/radian" + fi + + mkdir -p "${radian_profile_dir}" + + echo "Updating '${radian_profile_dir}/profile'..." + echo 'options(radian.global_history_file = "/dc/r-history/.radian_history")' >>"${radian_profile_dir}/profile" + echo "Done!" +} + +fix_permissions "/dc/r-history" +set_renviron +set_radian_history diff --git a/test/r-history/test.sh b/test/r-history/test.sh new file mode 100755 index 0000000..953d8bb --- /dev/null +++ b/test/r-history/test.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +set -e + +# Optional: Import test library bundled with the devcontainer CLI +source dev-container-features-test-lib + +# Feature-specific tests +check "cache dir permission" bash -c "test -w /dc/r-history/" + +# Report result +reportResults