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

R package build / check / etc as tasks provided by extension #735

Merged
merged 9 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 19 additions & 1 deletion extensions/positron-r/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,25 @@
"when": "!virtualWorkspace"
}
]
}
},
"taskDefinitions": [
{
"type": "rPackageBuild",
"when": "isRPackage"
},
{
"type": "rPackageLoad",
"when": "isRPackage"
},
{
"type": "rPackageTest",
"when": "isRPackage"
},
{
"type": "rPackageCheck",
"when": "isRPackage"
}
]
},
"scripts": {
"vscode:prepublish": "yarn run compile",
Expand Down
4 changes: 4 additions & 0 deletions extensions/positron-r/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as vscode from 'vscode';
import { registerCommands } from './commands';
import { adaptJupyterKernel } from './kernel';
import { initializeLogging, trace, traceOutputChannel } from './logging';
import { providePackageTasks } from './tasks';

function activateKernel(context: vscode.ExtensionContext) {

Expand Down Expand Up @@ -59,5 +60,8 @@ export function activate(context: vscode.ExtensionContext) {
// Register commands.
registerCommands(context);

// Provide tasks.
providePackageTasks(context);

}

70 changes: 70 additions & 0 deletions extensions/positron-r/src/tasks.ts
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);
Copy link
Contributor Author

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.


const allPackageTasks: PackageTask[] = [
{ 'type': 'rPackageLoad', 'name': 'Load package', 'shellExecution': 'R -e "devtools::load_all()"' },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new executeCode() API is now available if you want to use it for tasks that should execute in the current R session. (I think that's at least load_all?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:

After this PR is merged, in the short term, I will:

The custom execution for tasks is just a bit more complicated and I wanted to approach it separately, unless you feel strongly.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
};