Refactor vscode-mssql sql bindings logic to sql bindings ext (#18725)

* wip for refactor of mssql to sql-bindings

* remove STS dependency

* work to bring function over and setup with vscodeMsql APIs

* copy typings from vscode-mssql
This commit is contained in:
Vasu Bhog
2022-03-14 13:07:27 -07:00
committed by GitHub
parent a86301312c
commit 2d1ffeb47c
12 changed files with 627 additions and 81 deletions

View File

@@ -6,7 +6,7 @@
import type * as azdataType from 'azdata';
import * as vscode from 'vscode';
import * as vscodeMssql from 'vscode-mssql';
import * as fse from 'fs-extra';
import * as fs from 'fs';
import * as path from 'path';
import * as glob from 'fast-glob';
import * as cp from 'child_process';
@@ -16,6 +16,13 @@ export interface ValidationResult {
validated: boolean
}
export interface IPackageInfo {
name: string;
fullName: string;
version: string;
aiKey: string;
}
/**
* Consolidates on the error message string
*/
@@ -40,13 +47,6 @@ export async function getVscodeMssqlApi(): Promise<vscodeMssql.IExtension> {
return ext.activate();
}
export interface IPackageInfo {
name: string;
fullName: string;
version: string;
aiKey: string;
}
// Try to load the azdata API - but gracefully handle the failure in case we're running
// in a context where the API doesn't exist (such as VS Code)
let azdataApi: typeof azdataType | undefined = undefined;
@@ -68,14 +68,6 @@ export function getAzdataApi(): typeof azdataType | undefined {
return azdataApi;
}
export async function createFolderIfNotExist(folderPath: string): Promise<void> {
try {
await fse.mkdir(folderPath);
} catch {
// Ignore if failed
}
}
export async function executeCommand(command: string, cwd?: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
cp.exec(command, { maxBuffer: 500 * 1024, cwd: cwd }, (error: Error | null, stdout: string, stderr: string) => {
@@ -109,6 +101,75 @@ export async function getAllProjectsInFolder(folder: vscode.Uri, projectExtensio
return (await glob(projFilter)).map(p => vscode.Uri.file(path.resolve(p)));
}
/**
* Format a string. Behaves like C#'s string.Format() function.
*/
export function formatString(str: string, ...args: any[]): string {
// This is based on code originally from https://github.com/Microsoft/vscode/blob/master/src/vs/nls.js
// License: https://github.com/Microsoft/vscode/blob/master/LICENSE.txt
let result: string;
if (args.length === 0) {
result = str;
} else {
result = str.replace(/\{(\d+)\}/g, (match, rest) => {
let index = rest[0];
return typeof args[index] !== 'undefined' ? args[index] : match;
});
}
return result;
}
/**
* Generates a quoted full name for the object
* @param schema of the object
* @param objectName object chosen by the user
* @returns the quoted and escaped full name of the specified schema and object
*/
export function generateQuotedFullName(schema: string, objectName: string): string {
return `[${escapeClosingBrackets(schema)}].[${escapeClosingBrackets(objectName)}]`;
}
/**
* Returns a promise that will reject after the specified timeout
* @param errorMessage error message to be returned in the rejection
* @param ms timeout in milliseconds. Default is 10 seconds
* @returns a promise that rejects after the specified timeout
*/
export function timeoutPromise(errorMessage: string, ms: number = 10000): Promise<string> {
return new Promise((_, reject) => {
setTimeout(() => {
reject(new Error(errorMessage));
}, ms);
});
}
/**
* Gets a unique file name
* Increment the file name by adding 1 to function name if the file already exists
* Undefined if the filename suffix count becomes greater than 1024
* @param folderPath selected project folder path
* @param fileName base filename to use
* @returns a promise with the unique file name, or undefined
*/
export async function getUniqueFileName(folderPath: string, fileName: string): Promise<string | undefined> {
let count: number = 0;
const maxCount: number = 1024;
let uniqueFileName = fileName;
while (count < maxCount) {
if (!fs.existsSync(path.join(folderPath, uniqueFileName + '.cs'))) {
return uniqueFileName;
}
count += 1;
uniqueFileName = fileName + count.toString();
}
return undefined;
}
export function escapeClosingBrackets(str: string): string {
return str.replace(']', ']]');
}
/**
* Gets the package info for the extension based on where the extension is installed
* @returns the package info object