Adding SQL Edge project template (#13558)

* Checkpoint

* removing flag for not creating subfolder

* Adding Edge template

* Removing janky map function

* Adding templates for additional objects

* Updating tests, fixing bug

* Added Edge project icon

* Updating strings to Drew-approved text

* Cleaning up template scripts and Edge project template names
This commit is contained in:
Benjin Dubishar
2020-12-03 10:33:31 -08:00
committed by GitHub
parent 08735c9434
commit fde031be48
15 changed files with 230 additions and 67 deletions

View File

@@ -15,6 +15,9 @@ export const script: string = 'script';
export const table: string = 'table';
export const view: string = 'view';
export const storedProcedure: string = 'storedProcedure';
export const dataSource: string = 'dataSource';
export const fileFormat: string = 'fileFormat';
export const externalStream: string = 'externalStream';
export const externalStreamingJob: string = 'externalStreamingJob';
export const folder: string = 'folder';
@@ -25,12 +28,12 @@ export const postDeployScript: string = 'postDeployScript';
let scriptTypeMap: Record<string, ProjectScriptType> = {};
export function projectScriptTypeMap(): Record<string, ProjectScriptType> {
export function get(key: string): ProjectScriptType {
if (Object.keys(scriptTypeMap).length === 0) {
throw new Error('Templates must be loaded from file before attempting to use.');
}
return scriptTypeMap;
return scriptTypeMap[key.toLocaleLowerCase()];
}
let scriptTypes: ProjectScriptType[] = [];
@@ -44,6 +47,8 @@ export function projectScriptTypes(): ProjectScriptType[] {
}
export async function loadTemplates(templateFolderPath: string) {
reset();
await Promise.all([
Promise.resolve(newSqlProjectTemplate = await loadTemplate(templateFolderPath, 'newSqlProjectTemplate.xml')),
loadObjectTypeInfo(script, constants.scriptFriendlyName, templateFolderPath, 'newTsqlScriptTemplate.sql'),
@@ -52,11 +57,14 @@ export async function loadTemplates(templateFolderPath: string) {
loadObjectTypeInfo(storedProcedure, constants.storedProcedureFriendlyName, templateFolderPath, 'newTsqlStoredProcedureTemplate.sql'),
loadObjectTypeInfo(preDeployScript, constants.preDeployScriptFriendlyName, templateFolderPath, 'newTsqlPreDeployScriptTemplate.sql'),
loadObjectTypeInfo(postDeployScript, constants.postDeployScriptFriendlyName, templateFolderPath, 'newTsqlPostDeployScriptTemplate.sql'),
loadObjectTypeInfo(dataSource, constants.dataSourceFriendlyName, templateFolderPath, 'newTsqlDataSourceTemplate.sql'),
loadObjectTypeInfo(fileFormat, constants.fileFormatFriendlyName, templateFolderPath, 'newTsqlFileFormatTemplate.sql'),
loadObjectTypeInfo(externalStream, constants.externalStreamFriendlyName, templateFolderPath, 'newTsqlExternalStreamTemplate.sql'),
loadObjectTypeInfo(externalStreamingJob, constants.externalStreamingJobFriendlyName, templateFolderPath, 'newTsqlExternalStreamingJobTemplate.sql')
]);
for (const scriptType of scriptTypes) {
if (Object.keys(projectScriptTypeMap).find(s => s === scriptType.type.toLocaleLowerCase() || s === scriptType.friendlyName.toLocaleLowerCase())) {
if (Object.keys(scriptTypeMap).find(s => s === scriptType.type.toLocaleLowerCase() || s === scriptType.friendlyName.toLocaleLowerCase())) {
throw new Error(`Script type map already contains ${scriptType.type} or its friendlyName.`);
}
@@ -65,9 +73,27 @@ export async function loadTemplates(templateFolderPath: string) {
}
}
async function loadObjectTypeInfo(key: string, friendlyName: string, templateFolderPath: string, fileName: string) {
export function macroExpansion(template: string, macroDict: Record<string, string>): string {
const macroIndicator = '@@';
let output = template;
for (const macro in macroDict) {
// check if value contains the macroIndicator, which could break expansion for successive macros
if (macroDict[macro].includes(macroIndicator)) {
throw new Error(`Macro value ${macroDict[macro]} is invalid because it contains ${macroIndicator}`);
}
output = output.replace(new RegExp(macroIndicator + macro + macroIndicator, 'g'), macroDict[macro]);
}
return output;
}
async function loadObjectTypeInfo(key: string, friendlyName: string, templateFolderPath: string, fileName: string): Promise<string> {
const template = await loadTemplate(templateFolderPath, fileName);
scriptTypes.push(new ProjectScriptType(key, friendlyName, template));
return key;
}
async function loadTemplate(templateFolderPath: string, fileName: string): Promise<string> {