Skip to content

Commit 4279aab

Browse files
mmarconMassimiliano Marcon
andauthored
feat: VSCODE-323 expose export to Ruby (#391)
* feat: VSCODE-323 expose export to Ruby Also made export to language more prominent in README * test: fixed existing tests and added new one for ruby * chore: increase allowed bundle size Co-authored-by: Massimiliano Marcon <[email protected]>
1 parent cbab069 commit 4279aab

File tree

9 files changed

+94
-7
lines changed

9 files changed

+94
-7
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,24 @@ MongoDB Playgrounds are the most convenient way to prototype and execute CRUD op
2424
- Edit documents returned by your playground.
2525
- Save your playgrounds in your workspace and use them to document how your application interacts with MongoDB.
2626
- Build aggregations quickly with helpful and well-commented stage snippets.
27-
- Export your queries and aggregations to one of the supported languages: Java, Node, C#, and Python 3
2827

2928
![Playgrounds](resources/screenshots/playground.png)
3029

3130
_Make sure you are connected to a server or cluster before using a playground. You can't run a playground and you won't get completions if you are not connected._
3231

32+
#### From Query API to your favorite language
33+
34+
Select queries and aggregations within your Playground files and translate them into your favorite programming language. Supported languages are:
35+
36+
* Java
37+
* Node.js
38+
* C#
39+
* Python 3
40+
* Ruby
41+
42+
![Query Translator](resources/screenshots/query-translator.png)
43+
44+
3345
### Document Editing
3446

3547
MongoDB for VS Code makes it extremely easy to make changes to documents in your collections. You can open documents in an editor tab, edit them and save the changes back to MongoDB.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@
266266
"command": "mdb.exportToNode",
267267
"title": "MongoDB: Export To Node.js"
268268
},
269+
{
270+
"command": "mdb.exportToRuby",
271+
"title": "MongoDB: Export To Ruby"
272+
},
269273
{
270274
"command": "mdb.addConnection",
271275
"title": "Add MongoDB Connection",
612 KB
Loading

scripts/check-vsix-size.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const version = JSON.parse(fs.readFileSync(path.resolve(__dirname, '..', 'packag
66
const vsixFileName = path.resolve(__dirname, '..', `./mongodb-vscode-${version}.vsix`);
77
const size = fs.statSync(vsixFileName).size;
88

9-
const maxSize = 6 * 1000000; // 6 MB
9+
const maxSize = 6.2 * 1000000; // ~6.2 MB
1010

1111
if (size >= maxSize) {
1212
throw new Error(`vsix bundle too big expected max ${maxSize} bytes, got ${size}.`);

src/commands/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum EXTENSION_COMMANDS {
1818
MDB_EXPORT_TO_JAVA = 'mdb.exportToJava',
1919
MDB_EXPORT_TO_CSHARP = 'mdb.exportToCsharp',
2020
MDB_EXPORT_TO_NODE = 'mdb.exportToNode',
21+
MDB_EXPORT_TO_RUBY = 'mdb.exportToRuby',
2122
MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS = 'mdb.changeExportToLanguageAddons',
2223

2324
MDB_OPEN_MONGODB_DOCUMENT_FROM_CODE_LENS = 'mdb.openMongoDBDocumentFromCodeLens',

src/editors/codeActionProvider.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ export default class CodeActionProvider implements vscode.CodeActionProvider {
7171
tooltip: 'Export To Node.js'
7272
};
7373
codeActions.push(exportToJSCommand);
74+
75+
const exportToRubyCommand = new vscode.CodeAction('Export To Ruby', vscode.CodeActionKind.Empty);
76+
exportToRubyCommand.command = {
77+
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_RUBY,
78+
title: 'Export To Ruby',
79+
tooltip: 'Export To Ruby'
80+
};
81+
codeActions.push(exportToRubyCommand);
7482
}
7583

7684
return codeActions;

src/mdbExtensionController.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ export default class MDBExtensionController implements vscode.Disposable {
211211
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_NODE, () =>
212212
this._playgroundController.exportToLanguage(ExportToLanguages.JAVASCRIPT)
213213
);
214+
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_RUBY, () =>
215+
this._playgroundController.exportToLanguage(ExportToLanguages.RUBY)
216+
);
214217
this.registerCommand(EXTENSION_COMMANDS.MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS, (exportToLanguageAddons) =>
215218
this._playgroundController.changeExportToLanguageAddons(exportToLanguageAddons)
216219
);

src/test/suite/editors/codeActionProvider.test.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ suite('Code Action Provider Test Suite', function () {
146146
expect(codeActions).to.exist;
147147

148148
if (codeActions) {
149-
expect(codeActions.length).to.be.equal(5);
149+
expect(codeActions.length).to.be.equal(6);
150150
const actionCommand = codeActions[2].command;
151151

152152
if (actionCommand) {
@@ -178,7 +178,7 @@ suite('Code Action Provider Test Suite', function () {
178178
expect(codeActions).to.exist;
179179

180180
if (codeActions) {
181-
expect(codeActions.length).to.be.equal(5);
181+
expect(codeActions.length).to.be.equal(6);
182182
const actionCommand = codeActions[2].command;
183183

184184
if (actionCommand) {
@@ -227,7 +227,7 @@ suite('Code Action Provider Test Suite', function () {
227227
expect(codeActions).to.exist;
228228

229229
if (codeActions) {
230-
expect(codeActions.length).to.be.equal(5);
230+
expect(codeActions.length).to.be.equal(6);
231231
const actionCommand = codeActions[3].command;
232232

233233
if (actionCommand) {
@@ -280,7 +280,7 @@ suite('Code Action Provider Test Suite', function () {
280280
expect(codeActions).to.exist;
281281

282282
if (codeActions) {
283-
expect(codeActions.length).to.be.equal(5);
283+
expect(codeActions.length).to.be.equal(6);
284284
const actionCommand = codeActions[1].command;
285285

286286
if (actionCommand) {
@@ -312,6 +312,64 @@ suite('Code Action Provider Test Suite', function () {
312312
language: 'python'
313313
};
314314

315+
expect(mdbTestExtension.testExtensionController._playgroundController._playgroundResult).to.be.deep.equal(expectedResult);
316+
}
317+
}
318+
});
319+
test('exports to ruby and includes driver syntax', async () => {
320+
const textFromEditor = "use('db'); db.coll.find({ name: '22' })";
321+
const selection = {
322+
start: { line: 0, character: 24 },
323+
end: { line: 0, character: 38 }
324+
} as vscode.Selection;
325+
const mode = ExportToLanguageMode.QUERY;
326+
const activeTextEditor = { document: { getText: () => textFromEditor } } as vscode.TextEditor;
327+
328+
mdbTestExtension.testExtensionController._playgroundController._selectedText = "{ name: '22' }";
329+
mdbTestExtension.testExtensionController._playgroundController._codeActionProvider.selection = selection;
330+
mdbTestExtension.testExtensionController._playgroundController._codeActionProvider.mode = mode;
331+
mdbTestExtension.testExtensionController._playgroundController._activeTextEditor = activeTextEditor;
332+
333+
const testCodeActionProvider = new CodeActionProvider();
334+
testCodeActionProvider.refresh({ selection, mode });
335+
336+
const codeActions = testCodeActionProvider.provideCodeActions();
337+
338+
expect(codeActions).to.exist;
339+
340+
if (codeActions) {
341+
expect(codeActions.length).to.be.equal(6);
342+
const actionCommand = codeActions[5].command;
343+
344+
if (actionCommand) {
345+
expect(actionCommand.command).to.be.equal('mdb.exportToRuby');
346+
expect(actionCommand.title).to.be.equal('Export To Ruby');
347+
348+
await vscode.commands.executeCommand(actionCommand.command);
349+
350+
let expectedResult: PlaygroundResult = {
351+
namespace: 'DATABASE_NAME.COLLECTION_NAME',
352+
type: null,
353+
content: "{\n 'name' => '22'\n}",
354+
language: 'ruby'
355+
};
356+
expect(mdbTestExtension.testExtensionController._playgroundController._playgroundResult).to.be.deep.equal(expectedResult);
357+
358+
const codeLenses = mdbTestExtension.testExtensionController._playgroundController._exportToLanguageCodeLensProvider.provideCodeLenses();
359+
expect(codeLenses.length).to.be.equal(2);
360+
361+
await vscode.commands.executeCommand('mdb.changeExportToLanguageAddons', {
362+
...mdbTestExtension.testExtensionController._playgroundController._exportToLanguageCodeLensProvider._exportToLanguageAddons,
363+
driverSyntax: true
364+
});
365+
366+
expectedResult = {
367+
namespace: 'db.coll',
368+
type: null,
369+
content: "# Requires the MongoDB Ruby Driver\n# https://docs.mongodb.com/ruby-driver/master/\n\nclient = Mongo::Client.new('mongodb://localhost:27018/?appname=mongodb-vscode+0.0.0-dev.0', :database => 'db')\n\nresult = client.database['coll'].find({\n 'name' => '22'\n})",
370+
language: 'ruby'
371+
};
372+
315373
expect(mdbTestExtension.testExtensionController._playgroundController._playgroundResult).to.be.deep.equal(expectedResult);
316374
}
317375
}

src/types/playgroundType.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export enum ExportToLanguages {
4141
PYTHON = 'python',
4242
JAVA = 'java',
4343
CSHARP = 'csharp',
44-
JAVASCRIPT = 'javascript'
44+
JAVASCRIPT = 'javascript',
45+
RUBY = 'ruby'
4546
}
4647

4748
export enum ExportToLanguageMode {

0 commit comments

Comments
 (0)