-
Notifications
You must be signed in to change notification settings - Fork 91
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
R package build / check / etc as tasks provided by extension #735
Changes from all commits
bf48a61
48d35a1
f5cbc4c
e47a579
79a4ff0
1a0d633
bc67532
d18a6cd
18805af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (C) 2023 Posit Software, PBC. All rights reserved. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
|
||
export async function providePackageTasks(_context: vscode.ExtensionContext): Promise<void> { | ||
|
||
const isRPackage = await detectRPackage(); | ||
vscode.commands.executeCommand('setContext', 'isRPackage', isRPackage); | ||
|
||
const allPackageTasks: PackageTask[] = [ | ||
{ 'type': 'rPackageLoad', 'name': 'Load package', 'shellExecution': 'R -e "devtools::load_all()"' }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to do these two things (includes switching execution, yes) in immediate followup PRs after this one, if that is OK:
The custom execution for tasks is just a bit more complicated and I wanted to approach it separately, unless you feel strongly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely fine to handle this in a followup! |
||
{ 'type': 'rPackageBuild', 'name': 'Build package', 'shellExecution': 'R -e "devtools::build()"' }, | ||
{ 'type': 'rPackageTest', 'name': 'Test package', 'shellExecution': 'R -e "devtools::test()"' }, | ||
{ 'type': 'rPackageCheck', 'name': 'Check package', 'shellExecution': 'R -e "devtools::check()"' }, | ||
]; | ||
|
||
for (const packageTask of allPackageTasks) { | ||
registerRPackageTaskProvider(packageTask); | ||
} | ||
|
||
} | ||
|
||
async function detectRPackage(): Promise<boolean> { | ||
if (vscode.workspace.workspaceFolders !== undefined) { | ||
const folderUri = vscode.workspace.workspaceFolders[0].uri; | ||
const fileUri = vscode.Uri.joinPath(folderUri, 'DESCRIPTION'); | ||
try { | ||
const bytes = await vscode.workspace.fs.readFile(fileUri); | ||
const descriptionText = Buffer.from(bytes).toString('utf8'); | ||
const descriptionLines = descriptionText.split(/(\r?\n)/); | ||
const descStartsWithPackage = descriptionLines[0].startsWith('Package:'); | ||
const typeLines = descriptionLines.filter(line => line.startsWith('Type:')); | ||
const typeIsPackage = typeLines.length === 0 || typeLines[0].includes('Package'); | ||
return descStartsWithPackage && typeIsPackage; | ||
} catch { } | ||
} | ||
return false; | ||
} | ||
|
||
function registerRPackageTaskProvider(packageTask: PackageTask): vscode.Disposable { | ||
const task = rPackageTask(packageTask); | ||
const taskProvider = vscode.tasks.registerTaskProvider(packageTask.type, { | ||
provideTasks: () => { | ||
return [task]; | ||
}, | ||
resolveTask(_task: vscode.Task): vscode.Task | undefined { | ||
return undefined; | ||
} | ||
}); | ||
return (taskProvider); | ||
} | ||
|
||
function rPackageTask(packageTask: PackageTask): vscode.Task { | ||
return new vscode.Task( | ||
{ type: packageTask.type }, | ||
vscode.TaskScope.Workspace, | ||
packageTask.name, | ||
'R', | ||
new vscode.ShellExecution(packageTask.shellExecution), | ||
[] | ||
); | ||
} | ||
|
||
type PackageTask = { | ||
type: string; | ||
name: string; | ||
shellExecution: string; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I put this here instead of
commands.ts
because it is executing the "setContext" command, not registering a command for users to have.