Skip to content

Commit

Permalink
Merge pull request #39 from cedric05/dev
Browse files Browse the repository at this point in the history
release to main
  • Loading branch information
cedric05 authored Apr 3, 2021
2 parents 18f3afe + 4c8ccb5 commit 62c6042
Show file tree
Hide file tree
Showing 15 changed files with 485 additions and 41 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Change Log

## [0.0.9]
- [Announcement] dothttp-runner can be run in remote wsl and containers.
- [**Improvement**] history item label will be bold and will now start showing time it executed for better difference
- [**Improvement**] execute quick pick item, will not start showing urls also.
- [**Improvement**] Configure response directory name.
- [**Bug**] don't set default python3 path (if user adds it, he has to install dothttp-req)
- [**Bug**] postman import is creating duplicate folder, use showOpenDialog rather than, showSaveDialog
- [**Bug**] history execute is not showing file extension/ file syntax
- [**Bug**] history item hove is showing `200 undefined` fixed.

## [0.0.8]
- [New] reuse old tab, when executing httpdef target
- [New] format any dictionary/json in httpdef (select dictionary, do `ctrl+1` click format json)
Expand Down
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dothttp-code",
"displayName": "dothttp-code",
"description": "run dothttp files in vscode natively",
"version": "0.0.8",
"version": "0.0.9",
"publisher": "shivaprasanth",
"repository": {
"url": "https://github.com/cedric05/dothttp-runner"
Expand Down Expand Up @@ -115,12 +115,12 @@
},
{
"command": "dothttpEnvView.enableenv",
"title": "enable env",
"title": "enable environment",
"icon": "$(add)"
},
{
"command": "dothttpEnvView.disableenv",
"title": "disable env",
"title": "disable environment",
"icon": "$(remove)"
},
{
Expand Down Expand Up @@ -155,7 +155,7 @@
},
{
"command": "dothttpEnvView.disableAllEnv",
"title": "disable all env",
"title": "disable all environment",
"icon": "$(close-all)"
},
{
Expand All @@ -170,7 +170,7 @@
},
{
"command": "dothttpEnvView.copyPropertyValue",
"title": "copy env property",
"title": "copy environment value",
"icon": "$(chrome-restore)"
}
],
Expand Down Expand Up @@ -290,9 +290,13 @@
"default": false,
"description": "enable it if reusing old tab while runing requets is preferred"
},
"dothttp.conf.response.savedirectory": {
"type": "string",
"default": "./",
"description": "provide either absolute path or relative path to current file"
},
"dothttp.conf.pythonpath": {
"type": "string",
"default": "/usr/bin/python3",
"description": "python installation location for making requests"
},
"dothttp.conf.runrecent": {
Expand Down Expand Up @@ -374,6 +378,7 @@
"devDependencies": {
"@types/dateformat": "^3.0.1",
"@types/glob": "^7.1.3",
"@types/lodash": "^4.14.168",
"@types/mime-types": "^2.1.0",
"@types/mocha": "^8.0.4",
"@types/node": "^12.20.0",
Expand All @@ -400,4 +405,4 @@
"tingodb": "^0.6.1",
"unzipper": "^0.10.11"
}
}
}
71 changes: 62 additions & 9 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { zip } from 'lodash';
import { platform } from 'os';
import * as vscode from 'vscode';
import { TargetSymbolInfo } from '../lib/client';
import { ApplicationServices } from '../services/global';
import DotHttpEditorView from '../views/editor';
import dateFormat = require('dateformat');
import path = require('path');
import { Configuration } from '../models/config';
import { existsSync, lstatSync } from 'fs'
import { Constants } from '../models/constants';

enum importoptions {
postman = 'postman',
Expand All @@ -29,9 +34,16 @@ export async function importRequests() {
placeHolder: "https://getpostman.com/collections"
});
if (!link) { return }
const folder = await vscode.window.showSaveDialog({
const importUri = await vscode.window.showOpenDialog({
canSelectFolders: true,
canSelectFiles: false,
title: "select folder to import resource",
canSelectMany: false,
openLabel: "select folder to import"

});
if (importUri?.length === 0) { return; }
const folder = importUri![0];
if (!folder?.fsPath) { return }
const directory = folder.fsPath!;
await vscode.workspace.fs.createDirectory(folder);
Expand All @@ -51,14 +63,14 @@ export async function importRequests() {
export async function runFileCommand(...arr: any[]) {
const target = await cacheAndGetTarget(arr);
if (target) {
runHttpFileWithOptions({ curl: false, target: target });
return runHttpFileWithOptions({ curl: false, target: target });
}
}

export async function genCurlCommand(...arr: any[]) {
const target = await cacheAndGetTarget(arr);
if (target) {
runHttpFileWithOptions({ curl: true, target: target });
return runHttpFileWithOptions({ curl: true, target: target });
}
}

Expand All @@ -84,7 +96,9 @@ async function getTargetFromQuickPick(arr: any[]) {
}
}
// otherwise ask for user input
const filename = vscode.window.activeTextEditor?.document.fileName!;
const editor = vscode.window.activeTextEditor!;
const document = editor.document!;
const filename = document.fileName!;
if (ApplicationServices.get().getCconfig().runRecent) {
const storage = ApplicationServices.get().getStorageService();
return storage.getValue(`httpruntarget://${filename}`, '1');
Expand All @@ -93,10 +107,24 @@ async function getTargetFromQuickPick(arr: any[]) {
if (names.error) {
return '1';
}
const option = await vscode.window.showQuickPick((names.names ?? []).map(namer => ({ label: namer.name, target: namer })),
// const selectionDone = false;
// @ts-ignore
const items: vscode.QuickPickItem[] = zip(names.names, names.urls).map(comb => {
const namer = comb[0]!;
return {
label: namer.name,
detail: comb[1]?.url,
target: namer,
/*
picking multiple is not supported by dothttp, picking one is not supported by vscode
so, for now commenting
*/
// picked: !selectionDone && editor.visibleRanges[0].intersection(range) ? true : false,
};
});
const option = await vscode.window.showQuickPick(items,
{
canPickMany: false, ignoreFocusOut: true, onDidSelectItem: function (quickPickItem: { label: string, target: TargetSymbolInfo }) {
const document = vscode.window.activeTextEditor?.document!;
const range = new vscode.Range(
document.positionAt(quickPickItem.target.start),
document.positionAt(quickPickItem.target.end));
Expand All @@ -109,15 +137,18 @@ async function getTargetFromQuickPick(arr: any[]) {
}

export async function runHttpFileWithOptions(options: { curl: boolean, target: string }) {
const config = ApplicationServices.get().getCconfig();

const document = vscode.window.activeTextEditor?.document!;
const filename = document.fileName ?? '';
const filename = document.fileName!;

if (!DotHttpEditorView.isHttpFile(filename) && document.uri.scheme === 'file') {
vscode.window.showInformationMessage('either python path not set correctly!! or not an .dhttp/.http file or file doesn\'t exist ');
return;
}
const date = new Date();
var now = dateFormat(date, 'hh:MM:ss');
if (ApplicationServices.get().getCconfig().reUseOld) {
if (config.reUseOld) {
now = '';
}
vscode.window.withProgress({
Expand All @@ -136,7 +167,7 @@ export async function runHttpFileWithOptions(options: { curl: boolean, target: s
const out = await prom;
addHistory(out, filename, options);
if (!token.isCancellationRequested) {
const fileNameWithInfo = contructFileName(filename, options, out, now);
const fileNameWithInfo = contructFileName(getBaseFileNameToSave(config, filename), options, out, now);
showInUntitledView(fileNameWithInfo.filename, fileNameWithInfo.header, out);
progress.report({ increment: 50, message: 'completed' });
}
Expand All @@ -145,6 +176,28 @@ export async function runHttpFileWithOptions(options: { curl: boolean, target: s
})
}

function getBaseFileNameToSave(config: Configuration, filename: string) {
var sfilename;
if (config.responseSaveDirectory) {
if (path.isAbsolute(config.responseSaveDirectory)) {
// save to absolute directory
sfilename = path.join(config.responseSaveDirectory, path.basename(filename));
} else {
// relatvie to current file's directory
const parentDirectory = path.dirname(filename);
sfilename = path.join(parentDirectory, config.responseSaveDirectory, path.basename(filename));
}
const parDirectory = path.dirname(sfilename);
if (existsSync(parDirectory) && lstatSync(parDirectory).isDirectory()) {
return sfilename
} else {
vscode.window.showErrorMessage(`${Constants.responseDirectory} is set to incorrect value(non existant directory or is a file)`)
return filename;
}
}
return filename;
}

function addHistory(out: any, filename: string, options: { curl: boolean; target: string; }) {
const history = {
url: out.url as string,
Expand Down
15 changes: 8 additions & 7 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,16 @@ export type TargetSymbolInfo = {
};


export type UrlSymbolInfo = {
start: number;
url: string;
method: string;
end: number;
};

export interface DotTttpSymbol {
names?: Array<TargetSymbolInfo>,
urls?: Array<{
start: number,
url: string,
method: string,
end: number

}>,
urls?: Array<UrlSymbolInfo>,
error?: boolean,
error_message?: string,
}
Expand Down
2 changes: 2 additions & 0 deletions src/models/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class Configuration {
configchange = vscode.workspace.onDidChangeConfiguration(() => this.update());
pythonPath!: string;
dothttpPath!: string;
responseSaveDirectory!: string;


private constructor() {
Expand All @@ -92,6 +93,7 @@ export class Configuration {
this.noCookies = vsCodeconfig.get(Constants.nocookie) as boolean
this.pythonPath = vsCodeconfig.get(Constants.pythonPath) as string;
this.dothttpPath = vsCodeconfig.get(Constants.dothttpPath) as string;
this.responseSaveDirectory = vsCodeconfig.get(Constants.responseDirectory) as string;
}

public update() {
Expand Down
3 changes: 2 additions & 1 deletion src/models/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum Constants {
showheaders = "dothttp.conf.showheaders",
runConf = "dothttp.conf.runrecent",
reUseOldTab = "dothttp.conf.run.reuseold",
responseDirectory = "dothttp.conf.response.savedirectory",
// view props

// commands
Expand Down Expand Up @@ -57,7 +58,7 @@ export enum Constants {
propViewEnabled = "dothttpPropViewEnabled",

// download stuff
extensionVersion = "0.0.8",
extensionVersion = "0.0.9",

dothttpVersion = "dothttp.version",

Expand Down
56 changes: 50 additions & 6 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,59 @@
import * as assert from 'assert';

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as path from 'path';
import * as vscode from 'vscode';
import { extensions, Uri } from 'vscode';
const rangeTestData = require('./rangeTestData.json');
const testData = require('./testData.json');

// import * as myExtension from '../../extension';


const directory: string = path.join(__dirname, "..", "..", "..", "test-resources");
const filename = path.join(directory, "multidef.http")
const failFilename = path.join(directory, "fail.http")

suite('Extension Test Suite', () => {
vscode.window.showInformationMessage('Start all tests.');
const fileUri = Uri.file(path.join(filename));
const failFileUri = Uri.file(path.join(failFilename));

test('Sample test', () => {
assert.strictEqual(-1, [1, 2, 3].indexOf(5));
assert.strictEqual(-1, [1, 2, 3].indexOf(0));
suiteSetup(async function () {
await extensions.getExtension('shivaprasanth.dothttp-code')!.activate();
await vscode.window.showTextDocument(fileUri);
});

test("outline should be present", async () => {
const out = await vscode.commands.executeCommand("vscode.executeDocumentSymbolProvider", fileUri) as vscode.SymbolInformation[];
const processed = out.map(a => ({ name: a.name, kind: a.kind, location: { range: a.location.range }, containerName: a.containerName }));
assert.strictEqual(JSON.stringify(processed), JSON.stringify(testData));
})

test("should provide code lens", async () => {
const out = await vscode.commands.executeCommand("vscode.executeCodeLensProvider", failFileUri);
assert.strictEqual(JSON.stringify(out), JSON.stringify(rangeTestData));
})


// test("should execute and open response result", async () => {
// return // skipping
// const args = { target: "first" }
// //close all windows first
// await vscode.window.showTextDocument(failFileUri);
// await Promise.all(vscode.window.visibleTextEditors.map(async editor => {
// editor.hide();
// }));
// await vscode.commands.executeCommand(Constants.runFileCommand, args, args, args);
// if (vscode.window.visibleTextEditors.filter(edtior => edtior.document.uri.scheme === "untitled").length === 1) {
// return;
// }
// assert.fail("not executed")
// })
});


async function sleep(ms: number): Promise<void> {
return new Promise(resolve => {
setTimeout(resolve, ms);
})
};

Loading

0 comments on commit 62c6042

Please sign in to comment.