Skip to content

Commit

Permalink
New Feature: R history (#193)
Browse files Browse the repository at this point in the history
Close #190
  • Loading branch information
eitsupi authored Sep 5, 2023
1 parent aec69b8 commit 1a9d75f
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/**
Expand Down Expand Up @@ -89,6 +90,7 @@ jobs:
- pandoc
- quarto-cli
- r-apt
- r-history
- r-packages
- r-rig
- renv-cache
Expand Down Expand Up @@ -119,6 +121,7 @@ jobs:
- pandoc
- quarto-cli
- r-apt
- r-history
- r-packages
- r-rig
- renv-cache
Expand Down
6 changes: 6 additions & 0 deletions src/r-history/NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- markdownlint-disable MD041 -->

## 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).
24 changes: 24 additions & 0 deletions src/r-history/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
35 changes: 35 additions & 0 deletions src/r-history/install.sh
Original file line number Diff line number Diff line change
@@ -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!"
50 changes: 50 additions & 0 deletions src/r-history/oncreate.sh
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions test/r-history/test.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1a9d75f

Please sign in to comment.