|
| 1 | +import path = require('path'); |
| 2 | +import * as fs from 'fs-extra'; |
| 3 | +import * as vscode from 'vscode'; |
| 4 | + |
| 5 | +import { LocatorServiceFactory, AbstractLocatorService } from './locator'; |
| 6 | +import { ExecutableStatusItem, ExecutableQuickPick } from './ui'; |
| 7 | +import { RExecutableService, WorkspaceExecutableEvent } from './service'; |
| 8 | +import { extensionContext } from '../extension'; |
| 9 | +import { spawnAsync } from '../util'; |
| 10 | +import { RExecutable, VirtualRExecutable } from './executable'; |
| 11 | + |
| 12 | +// super class that manages relevant sub classes |
| 13 | +export class RExecutableManager { |
| 14 | + private retrievalService: AbstractLocatorService; |
| 15 | + private statusBar: ExecutableStatusItem; |
| 16 | + private quickPick: ExecutableQuickPick; |
| 17 | + private executableService: RExecutableService; |
| 18 | + |
| 19 | + constructor() { |
| 20 | + this.retrievalService = LocatorServiceFactory.getLocator(); |
| 21 | + this.retrievalService.refreshPaths(); |
| 22 | + this.executableService = new RExecutableService(); |
| 23 | + this.statusBar = new ExecutableStatusItem(this.executableService); |
| 24 | + this.quickPick = new ExecutableQuickPick(this.executableService, this.retrievalService); |
| 25 | + |
| 26 | + extensionContext.subscriptions.push( |
| 27 | + this.onDidChangeActiveExecutable(() => { |
| 28 | + this.reload(); |
| 29 | + }), |
| 30 | + vscode.window.onDidChangeActiveTextEditor((e: vscode.TextEditor) => { |
| 31 | + if (e?.document) { |
| 32 | + this.reload(); |
| 33 | + } |
| 34 | + }), |
| 35 | + this.executableService, |
| 36 | + this.statusBar, |
| 37 | + this.quickPick |
| 38 | + ); |
| 39 | + this.reload(); |
| 40 | + } |
| 41 | + |
| 42 | + public get activeExecutable(): RExecutable { |
| 43 | + return this.executableService.activeExecutable; |
| 44 | + } |
| 45 | + |
| 46 | + public get executableQuickPick(): ExecutableQuickPick { |
| 47 | + return this.quickPick; |
| 48 | + } |
| 49 | + |
| 50 | + public get executableStatusItem(): ExecutableStatusItem { |
| 51 | + return this.statusBar; |
| 52 | + } |
| 53 | + |
| 54 | + public get onDidChangeActiveExecutable(): vscode.Event<RExecutable> { |
| 55 | + return this.executableService.onDidChangeActiveExecutable; |
| 56 | + } |
| 57 | + |
| 58 | + public get onDidChangeWorkspaceExecutable(): vscode.Event<WorkspaceExecutableEvent> { |
| 59 | + return this.executableService.onDidChangeWorkspaceExecutable; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @description |
| 64 | + * Orders a refresh of the executable manager, causing a refresh of the language status bar item and |
| 65 | + * activates a conda environment if present. |
| 66 | + * @memberof RExecutableManager |
| 67 | + */ |
| 68 | + public reload(): void { |
| 69 | + this.statusBar.refresh(); |
| 70 | + const loading = this.activateEnvironment(); |
| 71 | + void this.statusBar.busy(loading); |
| 72 | + } |
| 73 | + |
| 74 | + private async activateEnvironment(): Promise<unknown> { |
| 75 | + const opts = { |
| 76 | + env: { |
| 77 | + ...process.env |
| 78 | + }, |
| 79 | + }; |
| 80 | + if (this.activeExecutable instanceof VirtualRExecutable && opts.env.CONDA_DEFAULT_ENV !== this.activeExecutable.name) { |
| 81 | + return spawnAsync( |
| 82 | + 'conda', // hard coded for now |
| 83 | + this.activeExecutable.activationCommand, |
| 84 | + opts, |
| 85 | + undefined |
| 86 | + ); |
| 87 | + } else { |
| 88 | + return Promise.resolve(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +/** |
| 96 | + * Is the folder of a given executable a valid R installation? |
| 97 | + * |
| 98 | + * A path is valid if the folder contains the R executable and an Rcmd file. |
| 99 | + * @param execPath |
| 100 | + * @returns boolean |
| 101 | + */ |
| 102 | +export function validateRFolder(execPath: string): boolean { |
| 103 | + const basename = process.platform === 'win32' ? 'R.exe' : 'R'; |
| 104 | + const scriptPath = path.normalize(`${execPath}/../Rcmd`); |
| 105 | + return fs.existsSync(execPath) && path.basename(basename) && fs.existsSync(scriptPath); |
| 106 | +} |
0 commit comments