Refactoring readProjFile() (#17637)

* move reading project parts to different helper functions

* cleanup

* remove comment

* addressing comments
This commit is contained in:
Kim Santiago
2021-11-12 15:02:52 -08:00
committed by GitHub
parent 4538103e60
commit bf1cc057be

View File

@@ -122,6 +122,24 @@ export class Project implements ISqlProject {
// check if this is a new msbuild sdk style project // check if this is a new msbuild sdk style project
this._isMsbuildSdkStyleProject = this.CheckForMsbuildSdkStyleProject(); this._isMsbuildSdkStyleProject = this.CheckForMsbuildSdkStyleProject();
// get files and folders
this._files = await this.readFilesInProject();
this.files.push(...await this.readFolders());
this._preDeployScripts = this.readPreDeployScripts();
this._postDeployScripts = this.readPostDeployScripts();
this._noneDeployScripts = this.readNoneDeployScripts();
this._databaseReferences = this.readDatabaseReferences();
this._importedTargets = this.readImportedTargets();
// find all SQLCMD variables to include
try {
this._sqlCmdVariables = utils.readSqlCmdVariables(this.projFileXmlDoc, false);
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.sqlCmdVariables, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
// get projectGUID // get projectGUID
try { try {
this._projectGuid = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ProjectGuid)[0].childNodes[0].nodeValue!; this._projectGuid = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ProjectGuid)[0].childNodes[0].nodeValue!;
@@ -129,24 +147,32 @@ export class Project implements ISqlProject {
void window.showErrorMessage(constants.errorReadingProject(constants.ProjectGuid, this.projectFilePath)); void window.showErrorMessage(constants.errorReadingProject(constants.ProjectGuid, this.projectFilePath));
console.error(utils.getErrorMessage(e)); console.error(utils.getErrorMessage(e));
} }
// glob style getting files and folders for new msbuild sdk style projects
if (this._isMsbuildSdkStyleProject) {
const files = await utils.getSqlFilesInFolder(this.projectFolderPath, true);
files.forEach(f => {
this._files.push(this.createFileProjectEntry(utils.trimUri(Uri.file(this.projectFilePath), Uri.file(f)), EntryType.File));
});
const folders = await utils.getFoldersInFolder(this.projectFolderPath, true);
folders.forEach(f => {
this._files.push(this.createFileProjectEntry(utils.trimUri(Uri.file(this.projectFilePath), Uri.file(f)), EntryType.Folder));
});
} }
for (let ig = 0; ig < this.projFileXmlDoc.documentElement.getElementsByTagName(constants.ItemGroup).length; ig++) { /**
const itemGroup = this.projFileXmlDoc.documentElement.getElementsByTagName(constants.ItemGroup)[ig]; * Gets all the files specified by <Build Inlude="..."> and removes all the files specified by <Build Remove="...">
* and all files included by the default glob of the folder of the sqlproj if it's an msbuild sdk style project
*/
private async readFilesInProject(): Promise<FileProjectEntry[]> {
const filesSet: Set<string> = new Set();
const entriesWithType: { relativePath: string, typeAttribute: string }[] = [];
// find all folders and files to include that are specified in the project file // default glob include pattern for msbuild sdk style projects
if (this._isMsbuildSdkStyleProject) {
try {
const globFiles = await utils.getSqlFilesInFolder(this.projectFolderPath, true);
globFiles.forEach(f => {
filesSet.add(utils.convertSlashesForSqlProj(utils.trimUri(Uri.file(this.projectFilePath), Uri.file(f))));
});
} catch (e) {
console.error(utils.getErrorMessage(e));
}
}
for (let ig = 0; ig < this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup).length; ig++) {
const itemGroup = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup)[ig];
// find all files to include that are specified in the project file
try { try {
const buildElements = itemGroup.getElementsByTagName(constants.Build); const buildElements = itemGroup.getElementsByTagName(constants.Build);
@@ -163,14 +189,15 @@ export class Project implements ISqlProject {
const globFiles = await utils.globWithPattern(fullPath); const globFiles = await utils.globWithPattern(fullPath);
globFiles.forEach(gf => { globFiles.forEach(gf => {
const newFileRelativePath = utils.convertSlashesForSqlProj(utils.trimUri(Uri.file(this.projectFilePath), Uri.file(gf))); const newFileRelativePath = utils.convertSlashesForSqlProj(utils.trimUri(Uri.file(this.projectFilePath), Uri.file(gf)));
if (!this._files.find(f => f.relativePath === newFileRelativePath)) { filesSet.add(newFileRelativePath);
this._files.push(this.createFileProjectEntry(utils.trimUri(Uri.file(this.projectFilePath), Uri.file(gf)), EntryType.File));
}
}); });
} else { } else {
// only add file if it wasn't already added filesSet.add(relativePath);
if (!this._files.find(f => f.relativePath === relativePath)) {
this._files.push(this.createFileProjectEntry(relativePath, EntryType.File, buildElements[b].getAttribute(constants.Type)!)); // Right now only used for external streaming jobs
const typeAttribute = buildElements[b].getAttribute(constants.Type)!;
if (typeAttribute) {
entriesWithType.push({ relativePath, typeAttribute });
} }
} }
} }
@@ -188,10 +215,7 @@ export class Project implements ISqlProject {
const globRemoveFiles = await utils.globWithPattern(fullPath); const globRemoveFiles = await utils.globWithPattern(fullPath);
globRemoveFiles.forEach(gf => { globRemoveFiles.forEach(gf => {
const removeFileRelativePath = utils.convertSlashesForSqlProj(utils.trimUri(Uri.file(this.projectFilePath), Uri.file(gf))); const removeFileRelativePath = utils.convertSlashesForSqlProj(utils.trimUri(Uri.file(this.projectFilePath), Uri.file(gf)));
filesSet.delete(removeFileRelativePath);
if (this._files.find(f => f.relativePath === removeFileRelativePath)) {
this._files = this._files.filter(f => f.relativePath !== removeFileRelativePath);
}
}); });
} }
} }
@@ -200,56 +224,114 @@ export class Project implements ISqlProject {
void window.showErrorMessage(constants.errorReadingProject(constants.BuildElements, this.projectFilePath)); void window.showErrorMessage(constants.errorReadingProject(constants.BuildElements, this.projectFilePath));
console.error(utils.getErrorMessage(e)); console.error(utils.getErrorMessage(e));
} }
}
// create a FileProjectEntry for each file
const fileEntries: FileProjectEntry[] = [];
filesSet.forEach(f => {
const typeEntry = entriesWithType.find(e => e.relativePath === f);
fileEntries.push(this.createFileProjectEntry(f, EntryType.File, typeEntry ? typeEntry.typeAttribute : undefined));
});
return fileEntries;
}
private async readFolders(): Promise<FileProjectEntry[]> {
const folderEntries: FileProjectEntry[] = [];
// glob style getting folders for new msbuild sdk style projects
if (this._isMsbuildSdkStyleProject) {
const folders = await utils.getFoldersInFolder(this.projectFolderPath, true);
folders.forEach(f => {
folderEntries.push(this.createFileProjectEntry(utils.trimUri(Uri.file(this.projectFilePath), Uri.file(f)), EntryType.Folder));
});
}
// get any folders listed in the project file
for (let ig = 0; ig < this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup).length; ig++) {
const itemGroup = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup)[ig];
try { try {
const folderElements = itemGroup.getElementsByTagName(constants.Folder); const folderElements = itemGroup.getElementsByTagName(constants.Folder);
for (let f = 0; f < folderElements.length; f++) { for (let f = 0; f < folderElements.length; f++) {
const relativePath = folderElements[f].getAttribute(constants.Include)!; const relativePath = folderElements[f].getAttribute(constants.Include)!;
// don't add Properties folder since it isn't supported for now and don't add if the folder was already added // don't add Properties folder since it isn't supported for now and don't add if the folder was already added
if (relativePath !== constants.Properties && !this._files.find(f => f.relativePath === utils.trimChars(relativePath, '\\'))) { if (relativePath !== constants.Properties && !folderEntries.find(f => f.relativePath === utils.trimChars(relativePath, '\\'))) {
this._files.push(this.createFileProjectEntry(relativePath, EntryType.Folder)); folderEntries.push(this.createFileProjectEntry(relativePath, EntryType.Folder));
} }
} }
} catch (e) { } catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.Folder, this.projectFilePath)); void window.showErrorMessage(constants.errorReadingProject(constants.Folder, this.projectFilePath));
console.error(utils.getErrorMessage(e)); console.error(utils.getErrorMessage(e));
} }
}
return folderEntries;
}
private readPreDeployScripts(): FileProjectEntry[] {
const preDeployScripts: FileProjectEntry[] = [];
// find all pre-deployment scripts to include // find all pre-deployment scripts to include
let preDeployScriptCount: number = 0; let preDeployScriptCount: number = 0;
for (let ig = 0; ig < this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup).length; ig++) {
const itemGroup = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup)[ig];
try { try {
const preDeploy = itemGroup.getElementsByTagName(constants.PreDeploy); const preDeploy = itemGroup.getElementsByTagName(constants.PreDeploy);
for (let pre = 0; pre < preDeploy.length; pre++) { for (let pre = 0; pre < preDeploy.length; pre++) {
this._preDeployScripts.push(this.createFileProjectEntry(preDeploy[pre].getAttribute(constants.Include)!, EntryType.File)); preDeployScripts.push(this.createFileProjectEntry(preDeploy[pre].getAttribute(constants.Include)!, EntryType.File));
preDeployScriptCount++; preDeployScriptCount++;
} }
} catch (e) { } catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.PreDeployElements, this.projectFilePath)); void window.showErrorMessage(constants.errorReadingProject(constants.PreDeployElements, this.projectFilePath));
console.error(utils.getErrorMessage(e)); console.error(utils.getErrorMessage(e));
} }
}
if (preDeployScriptCount > 1) {
void window.showWarningMessage(constants.prePostDeployCount, constants.okString);
}
return preDeployScripts;
}
private readPostDeployScripts(): FileProjectEntry[] {
const postDeployScripts: FileProjectEntry[] = [];
// find all post-deployment scripts to include // find all post-deployment scripts to include
let postDeployScriptCount: number = 0; let postDeployScriptCount: number = 0;
for (let ig = 0; ig < this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup).length; ig++) {
const itemGroup = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup)[ig];
try { try {
const postDeploy = itemGroup.getElementsByTagName(constants.PostDeploy); const postDeploy = itemGroup.getElementsByTagName(constants.PostDeploy);
for (let post = 0; post < postDeploy.length; post++) { for (let post = 0; post < postDeploy.length; post++) {
this._postDeployScripts.push(this.createFileProjectEntry(postDeploy[post].getAttribute(constants.Include)!, EntryType.File)); postDeployScripts.push(this.createFileProjectEntry(postDeploy[post].getAttribute(constants.Include)!, EntryType.File));
postDeployScriptCount++; postDeployScriptCount++;
} }
} catch (e) { } catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.PostDeployElements, this.projectFilePath)); void window.showErrorMessage(constants.errorReadingProject(constants.PostDeployElements, this.projectFilePath));
console.error(utils.getErrorMessage(e)); console.error(utils.getErrorMessage(e));
} }
}
if (preDeployScriptCount > 1 || postDeployScriptCount > 1) { if (postDeployScriptCount > 1) {
void window.showWarningMessage(constants.prePostDeployCount, constants.okString); void window.showWarningMessage(constants.prePostDeployCount, constants.okString);
} }
return postDeployScripts;
}
private readNoneDeployScripts(): FileProjectEntry[] {
const noneDeployScripts: FileProjectEntry[] = [];
for (let ig = 0; ig < this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup).length; ig++) {
const itemGroup = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup)[ig];
// find all none-deployment scripts to include // find all none-deployment scripts to include
try { try {
const noneItems = itemGroup.getElementsByTagName(constants.None); const noneItems = itemGroup.getElementsByTagName(constants.None);
for (let n = 0; n < noneItems.length; n++) { for (let n = 0; n < noneItems.length; n++) {
this._noneDeployScripts.push(this.createFileProjectEntry(noneItems[n].getAttribute(constants.Include)!, EntryType.File)); noneDeployScripts.push(this.createFileProjectEntry(noneItems[n].getAttribute(constants.Include)!, EntryType.File));
} }
} catch (e) { } catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.NoneElements, this.projectFilePath)); void window.showErrorMessage(constants.errorReadingProject(constants.NoneElements, this.projectFilePath));
@@ -257,27 +339,13 @@ export class Project implements ISqlProject {
} }
} }
// find all import statements to include return noneDeployScripts;
try {
const importElements = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.Import);
for (let i = 0; i < importElements.length; i++) {
const importTarget = importElements[i];
this._importedTargets.push(importTarget.getAttribute(constants.Project)!);
}
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.ImportElements, this.projectFilePath));
console.error(utils.getErrorMessage(e));
} }
// find all SQLCMD variables to include private readDatabaseReferences(): IDatabaseReferenceProjectEntry[] {
try { const databaseReferenceEntries: IDatabaseReferenceProjectEntry[] = [];
this._sqlCmdVariables = utils.readSqlCmdVariables(this.projFileXmlDoc, false);
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.sqlCmdVariables, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
// find all database references to include // database(system db and dacpac) references
const references = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ArtifactReference); const references = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ArtifactReference);
for (let r = 0; r < references.length; r++) { for (let r = 0; r < references.length; r++) {
try { try {
@@ -295,13 +363,13 @@ export class Project implements ISqlProject {
const path = utils.convertSlashesForSqlProj(this.getSystemDacpacUri(`${name}.dacpac`).fsPath); const path = utils.convertSlashesForSqlProj(this.getSystemDacpacUri(`${name}.dacpac`).fsPath);
if (path.includes(filepath)) { if (path.includes(filepath)) {
this._databaseReferences.push(new SystemDatabaseReferenceProjectEntry( databaseReferenceEntries.push(new SystemDatabaseReferenceProjectEntry(
Uri.file(filepath), Uri.file(filepath),
this.getSystemDacpacSsdtUri(`${name}.dacpac`), this.getSystemDacpacSsdtUri(`${name}.dacpac`),
name, name,
suppressMissingDependencies)); suppressMissingDependencies));
} else { } else {
this._databaseReferences.push(new DacpacReferenceProjectEntry({ databaseReferenceEntries.push(new DacpacReferenceProjectEntry({
dacpacFileLocation: Uri.file(utils.getPlatformSafeFileEntryPath(filepath)), dacpacFileLocation: Uri.file(utils.getPlatformSafeFileEntryPath(filepath)),
databaseName: name, databaseName: name,
suppressMissingDependenciesErrors: suppressMissingDependencies suppressMissingDependenciesErrors: suppressMissingDependencies
@@ -314,7 +382,7 @@ export class Project implements ISqlProject {
} }
} }
// find project references // project references
const projectReferences = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ProjectReference); const projectReferences = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ProjectReference);
for (let r = 0; r < projectReferences.length; r++) { for (let r = 0; r < projectReferences.length; r++) {
try { try {
@@ -335,7 +403,7 @@ export class Project implements ISqlProject {
const suppressMissingDependenciesErrorNode = projectReferences[r].getElementsByTagName(constants.SuppressMissingDependenciesErrors); const suppressMissingDependenciesErrorNode = projectReferences[r].getElementsByTagName(constants.SuppressMissingDependenciesErrors);
const suppressMissingDependencies = suppressMissingDependenciesErrorNode.length === 1 ? (suppressMissingDependenciesErrorNode[0].childNodes[0].nodeValue === constants.True) : false; const suppressMissingDependencies = suppressMissingDependenciesErrorNode.length === 1 ? (suppressMissingDependenciesErrorNode[0].childNodes[0].nodeValue === constants.True) : false;
this._databaseReferences.push(new SqlProjectReferenceProjectEntry({ databaseReferenceEntries.push(new SqlProjectReferenceProjectEntry({
projectRelativePath: Uri.file(utils.getPlatformSafeFileEntryPath(filepath)), projectRelativePath: Uri.file(utils.getPlatformSafeFileEntryPath(filepath)),
projectName: name, projectName: name,
projectGuid: '', // don't care when just reading project as a reference projectGuid: '', // don't care when just reading project as a reference
@@ -346,6 +414,26 @@ export class Project implements ISqlProject {
console.error(utils.getErrorMessage(e)); console.error(utils.getErrorMessage(e));
} }
} }
return databaseReferenceEntries;
}
private readImportedTargets(): string[] {
const imports: string[] = [];
// find all import statements to include
try {
const importElements = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.Import);
for (let i = 0; i < importElements.length; i++) {
const importTarget = importElements[i];
imports.push(importTarget.getAttribute(constants.Project)!);
}
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.ImportElements, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
return imports;
} }
private resetProject(): void { private resetProject(): void {