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

Add startup argument and flowr config file entry for changing the R shell's R executable #718

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
7 changes: 6 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ export interface FlowrConfigOptions extends MergeableRecord {
* Whether source calls should be ignored, causing {@link processSourceCall}'s behavior to be skipped
*/
ignoreSourceCalls: boolean
/**
* The path to the R executable to use. If this is undefined, {@link DEFAULT_R_PATH} will be used.
*/
rPath: string | undefined
}

export const defaultConfigOptions: FlowrConfigOptions = {
ignoreSourceCalls: false
ignoreSourceCalls: false,
rPath: undefined
}
export const defaultConfigFile = 'flowr.json'

Expand Down
13 changes: 10 additions & 3 deletions src/flowr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Otherwise, it will start a REPL that can call these scripts and return their results repeatedly.
*/
import { log, LogLevel } from './util/log'
import type { RShellOptions } from './r-bridge'
import { RShell } from './r-bridge'
import type { OptionDefinition } from 'command-line-usage'
import commandLineUsage from 'command-line-usage'
Expand Down Expand Up @@ -37,7 +38,8 @@ export const optionDefinitions: OptionDefinition[] = [
{ name: 'execute', alias: 'e', type: String, description: 'Execute the given command and exit. Use a semicolon ";" to separate multiple commands.', typeLabel: '{underline command}', multiple: false },
{ name: 'no-ansi', type: Boolean, description: 'Disable ansi-escape-sequences in the output. Useful, if you want to redirect the output to a file.' },
{ name: 'script', alias: 's', type: String, description: `The sub-script to run (${scriptsText})`, multiple: false, defaultOption: true, typeLabel: '{underline files}', defaultValue: undefined },
{ name: 'config-file', type: String, description: 'The name of the configuration file to use', multiple: false }
{ name: 'config-file', type: String, description: 'The name of the configuration file to use', multiple: false },
{ name: 'r-path', type: String, description: 'The path to the R executable to use. Defaults to your PATH.', multiple: false }
]

export interface FlowrCliOptions {
Expand All @@ -51,6 +53,7 @@ export interface FlowrCliOptions {
execute: string | undefined
script: string | undefined
'config-file': string
'r-path': string | undefined
}

export const optionHelp = [
Expand Down Expand Up @@ -88,14 +91,18 @@ setConfigFile(undefined, options['config-file'] ?? defaultConfigFile, true)

function retrieveShell(): RShell {
// we keep an active shell session to allow other parse investigations :)
return new RShell({
let config: Partial<RShellOptions> = {
revive: 'always',
onRevive: (code, signal) => {
const signalText = signal == null ? '' : ` and signal ${signal}`
console.log(formatter.format(`R process exited with code ${code}${signalText}. Restarting...`, { color: Colors.Magenta, effect: ColorEffect.Foreground }))
console.log(italic(`If you want to exit, press either Ctrl+C twice, or enter ${bold(':quit')}`))
},
})
}
if(options['r-path']) {
config = { ...config, pathToRExecutable: options['r-path'] }
Ellpeck marked this conversation as resolved.
Show resolved Hide resolved
}
return new RShell(config)
}

async function mainRepl() {
Expand Down
4 changes: 3 additions & 1 deletion src/r-bridge/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { SemVer } from 'semver'
import semver from 'semver/preload'
import { getPlatform } from '../util/os'
import fs from 'fs'
import { getConfig } from '../config'

export type OutputStreamSelector = 'stdout' | 'stderr' | 'both';

Expand Down Expand Up @@ -89,8 +90,9 @@ export interface RShellOptions extends RShellSessionOptions {
readonly sessionName: string
}

export const DEFAULT_R_PATH = getPlatform() === 'windows' ? 'R.exe' : 'R'
export const DEFAULT_R_SHELL_EXEC_OPTIONS: RShellExecutionOptions = {
pathToRExecutable: getPlatform() === 'windows' ? 'R.exe' : 'R',
pathToRExecutable: getConfig().rPath ?? DEFAULT_R_PATH,
commandLineOptions: ['--vanilla', '--quiet', '--no-echo', '--no-save'],
cwd: process.cwd(),
env: process.env,
Expand Down
Loading