Update remove file for sdk style sql projects (#17688)

* add support for removing file in new style project

* fix test

* only load files, not whole project when checking if a <Build Remove> needs to be added

* merge changes

* fixes after merge
This commit is contained in:
Kim Santiago
2021-11-22 14:45:43 -10:00
committed by GitHub
parent 4c191d4acc
commit 33f01054c0
3 changed files with 76 additions and 4 deletions

View File

@@ -899,7 +899,7 @@ export class Project implements ISqlProject {
itemGroup.appendChild(newFileNode);
}
private removeFileFromProjFile(path: string): void {
private async removeFileFromProjFile(path: string): Promise<void> {
const fileNodes = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.Build);
const preDeployNodes = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.PreDeploy);
const postDeployNodes = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.PostDeploy);
@@ -907,14 +907,40 @@ export class Project implements ISqlProject {
const nodes = [fileNodes, preDeployNodes, postDeployNodes, noneNodes];
let deleted = false;
// remove the <Build Include="..."> entry if there is one
for (let i = 0; i < nodes.length; i++) {
deleted = this.removeNode(path, nodes[i]);
if (deleted) {
return;
// still might need to add a <Build Remove="..."> node if this is an sdk style project
if (this.isSdkStyleProject) {
break;
} else {
return;
}
}
}
// if it's an sdk style project, we'll need to add a <Build Remove="..."> entry to remove this file if it's
// still included by a glob
if (this.isSdkStyleProject) {
// write any changes from removing an include node and get the current files included in the project
if (deleted) {
await this.serializeToProjFile(this.projFileXmlDoc);
}
const currentFiles = await this.readFilesInProject();
// only add a node to exclude the file if it's still included by a glob
if (currentFiles.find(f => f.relativePath === utils.convertSlashesForSqlProj(path))) {
const removeFileNode = this.projFileXmlDoc!.createElement(constants.Build);
removeFileNode.setAttribute(constants.Remove, utils.convertSlashesForSqlProj(path));
this.findOrCreateItemGroup(constants.Build).appendChild(removeFileNode);
}
return;
}
throw new Error(constants.unableToFindObject(path, constants.fileObject));
}
@@ -1233,7 +1259,7 @@ export class Project implements ISqlProject {
for (const entry of entries) {
switch (entry.type) {
case EntryType.File:
this.removeFileFromProjFile((<FileProjectEntry>entry).relativePath);
await this.removeFileFromProjFile((<FileProjectEntry>entry).relativePath);
break;
case EntryType.Folder:
this.removeFolderFromProjFile((<FileProjectEntry>entry).relativePath);
@@ -1260,6 +1286,9 @@ export class Project implements ISqlProject {
}); // TODO: replace <any>
await fs.writeFile(this._projectFilePath, xml);
// update projFileXmlDoc since the file was updated
this.projFileXmlDoc = new xmldom.DOMParser().parseFromString(xml);
}
/**