Skip to content

Commit dc9c9da

Browse files
committed
feat: prompt to install bundled sess package when creating R terminal
1 parent 9fd8639 commit dc9c9da

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

R/install_sess.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local({
2+
args <- commandArgs(trailingOnly = TRUE)
3+
if (length(args) < 2) {
4+
stop("Missing arguments: pkg_path and repo")
5+
}
6+
pkg_path <- args[1]
7+
repo <- args[2]
8+
9+
if (!file.exists(file.path(pkg_path, "DESCRIPTION"))) {
10+
stop(paste("DESCRIPTION file not found in", pkg_path))
11+
}
12+
13+
desc <- read.dcf(file.path(pkg_path, "DESCRIPTION"))
14+
deps <- if ("Imports" %in% colnames(desc)) desc[, "Imports"] else ""
15+
deps <- unlist(strsplit(deps, ","))
16+
deps <- gsub("\\s*\\(.*\\)", "", deps)
17+
deps <- trimws(deps)
18+
# Filter out base packages and already installed packages
19+
deps <- deps[nzchar(deps)]
20+
installed <- rownames(installed.packages())
21+
base_pkgs <- rownames(installed.packages(priority = "base"))
22+
deps <- deps[!deps %in% base_pkgs & !deps %in% installed]
23+
24+
if (length(deps) > 0) {
25+
message("Installing dependencies: ", paste(deps, collapse = ", "))
26+
install.packages(deps, repos = repo)
27+
}
28+
29+
message("Installing sess package from: ", pkg_path)
30+
install.packages(pkg_path, repos = NULL, type = "source")
31+
})

src/rTerminal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export async function makeTerminalOptions(): Promise<vscode.TerminalOptions> {
155155

156156
export async function createRTerm(preserveshow?: boolean): Promise<boolean> {
157157
const termOptions = await makeTerminalOptions();
158+
void util.promptToInstallSessPackage(termOptions.cwd);
158159
const termPath = termOptions.shellPath;
159160
if(!termPath){
160161
void vscode.window.showErrorMessage('Could not find R path. Please check r.rterm and r.rpath setting.');

src/util.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,45 @@ export async function promptToInstallRPackage(name: string, section: string, cwd
588588
});
589589
}
590590

591+
/**
592+
* Prompt to install the bundled "sess" package
593+
*/
594+
export async function promptToInstallSessPackage(cwd?: string | vscode.Uri): Promise<void> {
595+
const _config = config();
596+
const sessionWatcher = _config.get<boolean>('sessionWatcher');
597+
if (!sessionWatcher) {
598+
return;
599+
}
600+
const sessVersion = await getRPackageVersion('sess', cwd instanceof vscode.Uri ? cwd.fsPath : cwd);
601+
if (sessVersion) {
602+
return;
603+
}
604+
605+
const sessPath = extensionContext.asAbsolutePath('sess').replace(/\\/g, '/');
606+
const installSessScript = extensionContext.asAbsolutePath(path.join('R', 'install_sess.R')).replace(/\\/g, '/');
607+
const installMsg = 'R package "sess" (shipped with vscode-R) is required for the session watcher to work. Do you want to install it?';
608+
await vscode.window.showErrorMessage(installMsg, 'Yes', 'No')
609+
.then(async function (select) {
610+
if (select === 'Yes') {
611+
const rPath = await getRpath();
612+
if (!rPath) {
613+
void vscode.window.showErrorMessage('R path not set', 'OK');
614+
return;
615+
}
616+
const repo = await getCranUrl('', cwd instanceof vscode.Uri ? cwd.fsPath : cwd);
617+
const args = [
618+
'--silent',
619+
'--no-echo',
620+
'--no-save',
621+
'--no-restore',
622+
'-f', installSessScript,
623+
'--args', sessPath, repo
624+
];
625+
void executeAsTask('Install "sess" package', rPath, args, true);
626+
}
627+
});
628+
}
629+
591630
/**
592631
* Create temporary directory. Will avoid name clashes. Caller must delete directory after use.
593632
*

0 commit comments

Comments
 (0)