From bc85f52dd45852b86dcecabe5ff6cc68f2f0711e Mon Sep 17 00:00:00 2001 From: Kim Santiago <31145923+kisantia@users.noreply.github.com> Date: Tue, 28 Feb 2023 14:31:49 -0800 Subject: [PATCH] Update sql project file move to use DacFx api (#22064) * update move to use STS api * swap rename to use move function * add try catch * remove debug statement * get original path from node --- .../src/common/constants.ts | 1 + .../src/controllers/projectController.ts | 54 ++++++++++++------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/extensions/sql-database-projects/src/common/constants.ts b/extensions/sql-database-projects/src/common/constants.ts index 303e0fb5e3..a808825843 100644 --- a/extensions/sql-database-projects/src/common/constants.ts +++ b/extensions/sql-database-projects/src/common/constants.ts @@ -664,3 +664,4 @@ export const movingFilesBetweenProjectsNotSupported = localize('movingFilesBetwe export function errorMovingFile(source: string, destination: string, error: string) { return localize('errorMovingFile', "Error when moving file from {0} to {1}. Error: {2}", source, destination, error); } export function moveConfirmationPrompt(source: string, destination: string) { return localize('moveConfirmationPrompt', "Are you sure you want to move {0} to {1}?", source, destination); } export const move = localize('Move', "Move"); +export function errorRenamingFile(source: string, destination: string, error: string) { return localize('errorRenamingFile', "Error when renaming file from {0} to {1}. Error: {2}", source, destination, error); } diff --git a/extensions/sql-database-projects/src/controllers/projectController.ts b/extensions/sql-database-projects/src/controllers/projectController.ts index 7754a4a4f1..688abb54ac 100644 --- a/extensions/sql-database-projects/src/controllers/projectController.ts +++ b/extensions/sql-database-projects/src/controllers/projectController.ts @@ -14,7 +14,6 @@ import * as vscode from 'vscode'; import type * as azdataType from 'azdata'; import * as dataworkspace from 'dataworkspace'; import type * as mssqlVscode from 'vscode-mssql'; -import * as fse from 'fs-extra'; import { promises as fs } from 'fs'; import { PublishDatabaseDialog } from '../dialogs/publishDatabaseDialog'; @@ -857,17 +856,12 @@ export class ProjectsController { } const newFilePath = path.join(path.dirname(utils.getPlatformSafeFileEntryPath(file?.relativePath!)), `${newFileName}.sql`); - const sqlProjectsService = await utils.getSqlProjectsService(); - if (node instanceof SqlObjectFileNode) { - await sqlProjectsService.moveSqlObjectScript(project.projectFilePath, newFilePath, file!.relativePath) - } else if (node instanceof PreDeployNode) { - await sqlProjectsService.movePreDeploymentScript(project.projectFilePath, newFilePath, file!.relativePath) - } else if (node instanceof PostDeployNode) { - await sqlProjectsService.movePostDeploymentScript(project.projectFilePath, newFilePath, file!.relativePath) + try { + await this.move(node, node.projectFileUri.fsPath, newFilePath); + } catch (e) { + void vscode.window.showErrorMessage(constants.errorRenamingFile(file?.relativePath!, newFilePath, utils.getErrorMessage(e))); } - // TODO add support for renaming none scripts after those are added in STS - // TODO add support for renaming publish profiles when support is added in DacFx this.refreshProjectsTree(context); } @@ -1864,10 +1858,10 @@ export class ProjectsController { let folderPath; // target is the root of project, which is the .sqlproj if (target.element.projectFileUri.fsPath === target.element.fileSystemUri.fsPath) { - folderPath = path.dirname(target.element.projectFileUri.fsPath!); + folderPath = path.basename(path.dirname(target.element.projectFileUri.fsPath!)); } else { // target is another file or folder - folderPath = target.element.fileSystemUri.fsPath.endsWith(constants.sqlFileExtension) ? path.dirname(target.element.fileSystemUri.fsPath) : target.element.fileSystemUri.fsPath; + folderPath = target.element.relativeProjectUri.fsPath.endsWith(constants.sqlFileExtension) ? path.dirname(target.element.relativeProjectUri.fsPath) : target.element.relativeProjectUri.fsPath; } const newPath = path.join(folderPath!, sourceFileNode.friendlyName); @@ -1884,16 +1878,40 @@ export class ProjectsController { // Move the file try { - const project = await Project.openProject(projectUri.fsPath); - - // TODO: swap out with DacFx projects apis - currently moving pre/post deploy scripts don't work, but they should work after the swap - await fse.move(sourceFileNode.fileSystemUri.fsPath, newPath!); - await project.exclude(project.files.find(f => f.fsUri.fsPath === sourceFileNode.fileSystemUri.fsPath)!); - await project.addExistingItem(newPath!); + await this.move(sourceFileNode, projectUri.fsPath, newPath); } catch (e) { void vscode.window.showErrorMessage(constants.errorMovingFile(sourceFileNode.fileSystemUri.fsPath, newPath, utils.getErrorMessage(e))); } } + + /** + * Moves a file to a different location + * @param node Node being moved + * @param projectFilePath Full file path to .sqlproj + * @param destinationRelativePath path of the destination, relative to .sqlproj + */ + private async move(node: BaseProjectTreeItem, projectFilePath: string, destinationRelativePath: string): Promise { + // trim off the project folder at the beginning of the relative path stored in the tree + const projectRelativeUri = vscode.Uri.file(path.basename(projectFilePath, constants.sqlprojExtension)); + const originalRelativePath = utils.trimUri(projectRelativeUri, node.relativeProjectUri); + destinationRelativePath = utils.trimUri(projectRelativeUri, vscode.Uri.file(destinationRelativePath)); + + if (originalRelativePath === destinationRelativePath) { + return; + } + + const sqlProjectsService = await utils.getSqlProjectsService(); + + if (node instanceof SqlObjectFileNode) { + await sqlProjectsService.moveSqlObjectScript(projectFilePath, destinationRelativePath, originalRelativePath) + } else if (node instanceof PreDeployNode) { + await sqlProjectsService.movePreDeploymentScript(projectFilePath, destinationRelativePath, originalRelativePath) + } else if (node instanceof PostDeployNode) { + await sqlProjectsService.movePostDeploymentScript(projectFilePath, destinationRelativePath, originalRelativePath) + } + // TODO add support for renaming none scripts after those are added in STS + // TODO add support for renaming publish profiles when support is added in DacFx + } } export interface NewProjectParams {