mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-16 17:22:29 -05:00
This change introduces new boolean flag for `addToProject`, `addScriptItem` and `addFolderItem` APIs that allows to skip files/folders if they are already included in the project file. Original behavior was to throw an error if duplicate entry is encountered, so I kept that as a default (new flag is not set). I started by fixing the original behavior, as it was not always working as expected. In our extension that relies on `addToProject` API we've hit an issue where duplicate items were added and no errors were thrown. There was a test for this scenario, but unfortunately the test had few bugs in it as well, so I addressed that first. First issue with the test code was missing `await` on the `testUtils.shouldThrowSpecificError` calls, so test was not actually testing anything. After adding missing keywords, I hit an issue where exception was not thrown, but this turned out to be different issue, compared to what we were hitting. In the test code, it was using the very first folder from the generate list of test entires. This folder wass actually the root of the project (where sqlproj file is located), and `addToProject` API had a special case to ignore the root. This means neither first call nor second call was actually adding anything to the project and no errors were produced. I fixed this problem by using next available folder in the generated files list. After addressing the test code I could not reproduce the issue that we were seeing with duplicate entires being added, everything was working as expected and errors were thrown. I started adding more tests that better resemble our production scenario - add files in subfolders, add files to existing project, rather than a new one. Finally I was able to reproduce the problem in tests when adding a file in a subfolder to an existing project. After investigation this turned out to be an issue with mismatch in how `relativePath` is maintained within the `FileProjectEntry`. When loading an existing project, `relativePath` is populated based on the value of the `Include` attribute of the `Build` item. This attribute is normalized to Windows-style path, using `\`, so for nested file you will have `folder\file.sql`. When adding new item to the project, one could pass either Windows or Unix-style path (`folder/file.sql`), so the path comparison between loaded Windows-style path and newly added Unix-style path was failing, resulting in them being treated as different items. In fact, `addToProject` API that we were using was relying on `Uri` helpers to extract relative path, thus the path was forced to be Unix-style and path was never the same as the loaded one. After this discovery I added a dedicated test to validate the round-trip of the `relativePath` for serialized and desirialized project file. In order to address this problem, I updated the factory method `createFileProjectEntry` to always run `utils.convertSlashesForSqlProj` on the relative path to ensure we have Windows-style path stored in there. I also optimized the helper code slightly to not do split/join, if there are no split points in the input string, which should eliminate unnecessary array instantiation. It is worth mentioning that I had to normalize the input relative paths in the `addScriptItem` and `addFolderItem` APIs, because there is no guarantee that they will be Windows-style when we try to compare them to `relativePath` of the existing project items. Finally I was able to add a simply flag and update the condition to return existing record, if duplicates were allowed. I also updated typings file for the extension and added tests to cover this scenario.
127 lines
4.0 KiB
TypeScript
127 lines
4.0 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
declare module 'sqldbproj' {
|
|
import * as vscode from 'vscode';
|
|
export const enum extension {
|
|
name = 'Microsoft.sql-database-projects'
|
|
}
|
|
|
|
/**
|
|
* sql database projects extension
|
|
*/
|
|
export interface IExtension {
|
|
/**
|
|
* Create a project
|
|
* @param name name of the project
|
|
* @param location the parent directory
|
|
* @param projectTypeId the ID of the project/template
|
|
* @returns Uri of the newly created project file
|
|
*/
|
|
createProject(name: string, location: vscode.Uri, projectTypeId: string): Promise<vscode.Uri>;
|
|
|
|
/**
|
|
* Opens and loads a .sqlproj file
|
|
*/
|
|
openProject(projectFilePath: string): Promise<ISqlProject>;
|
|
}
|
|
|
|
export interface ISqlProject {
|
|
/**
|
|
* Reads the project setting and contents from the file
|
|
*/
|
|
readProjFile(): Promise<void>;
|
|
|
|
/**
|
|
* Adds the list of sql files and directories to the project, and saves the project file
|
|
* @param list list of files and folder Uris. Files and folders must already exist. No files or folders will be added if any do not exist.
|
|
* @param doNotThrowOnDuplicate Flag that indicates whether duplicate entries should be ignored or throw an error.
|
|
*/
|
|
addToProject(list: vscode.Uri[], doNotThrowOnDuplicate?: boolean): Promise<void>;
|
|
|
|
/**
|
|
* Adds a folder to the project, and saves the project file
|
|
* @param relativeFolderPath Relative path of the folder
|
|
* @param doNotThrowOnDuplicate
|
|
* Flag that indicates whether duplicate entries should be ignored or throw an error. If flag is set to `true` and
|
|
* item already exists in the project file, then existing entry will be returned.
|
|
*/
|
|
addFolderItem(relativeFolderPath: string, doNotThrowOnDuplicate?: boolean): Promise<IFileProjectEntry>;
|
|
|
|
/**
|
|
* Writes a file to disk if contents are provided, adds that file to the project, and writes it to disk
|
|
* @param relativeFilePath Relative path of the file
|
|
* @param contents Contents to be written to the new file
|
|
* @param itemType Type of the project entry to add. This maps to the build action for the item.
|
|
* @param doNotThrowOnDuplicate
|
|
* Flag that indicates whether duplicate entries should be ignored or throw an error. If flag is set to `true` and
|
|
* item already exists in the project file, then existing entry will be returned.
|
|
*/
|
|
addScriptItem(relativeFilePath: string, contents?: string, itemType?: string, doNotThrowOnDuplicate?: boolean): Promise<IFileProjectEntry>;
|
|
|
|
/**
|
|
* Adds a SQLCMD variable to the project
|
|
* @param name name of the variable
|
|
* @param defaultValue
|
|
*/
|
|
addSqlCmdVariable(name: string, defaultValue: string): Promise<void>;
|
|
|
|
/**
|
|
* Excludes entry from project by removing it from the project file
|
|
* @param entry
|
|
*/
|
|
exclude(entry: IFileProjectEntry): Promise<void>;
|
|
|
|
/**
|
|
* Deletes file or folder and removes it from the project file
|
|
* @param entry
|
|
*/
|
|
deleteFileFolder(entry: IFileProjectEntry): Promise<void>;
|
|
|
|
/**
|
|
* returns the sql version the project is targeting
|
|
*/
|
|
getProjectTargetVersion(): string;
|
|
|
|
/**
|
|
* Path where dacpac is output to after a successful build
|
|
*/
|
|
readonly dacpacOutputPath: string;
|
|
|
|
/**
|
|
* Path to folder containing the project file
|
|
*/
|
|
readonly projectFolderPath: string;
|
|
|
|
/**
|
|
* Project file path
|
|
*/
|
|
readonly projectFilePath: string;
|
|
|
|
/**
|
|
* Project file name
|
|
*/
|
|
readonly projectFileName: string;
|
|
|
|
/**
|
|
* Files and folders that are included in the project
|
|
*/
|
|
readonly files: IFileProjectEntry[];
|
|
|
|
/**
|
|
* SqlCmd variables and their values
|
|
*/
|
|
readonly sqlCmdVariables: Record<string, string>;
|
|
}
|
|
|
|
/**
|
|
* Represents an entry in a project file
|
|
*/
|
|
export interface IFileProjectEntry {
|
|
fsUri: vscode.Uri;
|
|
relativePath: string;
|
|
}
|
|
}
|