Add default name to promptAddItem (#19439)

* Add default name to promptAddItem

* Change to addItemPrompt
This commit is contained in:
Charles Gagnon
2022-05-19 15:10:34 -07:00
committed by GitHub
parent 4c008059df
commit 887d218342
4 changed files with 27 additions and 15 deletions

View File

@@ -40,7 +40,7 @@ import { DashboardData, PublishData, Status } from '../models/dashboardData/dash
import { getPublishDatabaseSettings, launchPublishTargetOption } from '../dialogs/publishDatabaseQuickpick';
import { launchCreateAzureServerQuickPick, launchPublishToDockerContainerQuickpick } from '../dialogs/deployDatabaseQuickpick';
import { DeployService } from '../models/deploy/deployService';
import { GenerateProjectFromOpenApiSpecOptions, ISqlProject, ItemType, SqlTargetPlatform } from 'sqldbproj';
import { AddItemOptions, GenerateProjectFromOpenApiSpecOptions, ISqlProject, ItemType, SqlTargetPlatform } from 'sqldbproj';
import { AutorestHelper } from '../tools/autorestHelper';
import { createNewProjectFromDatabaseWithQuickpick } from '../dialogs/createProjectFromDatabaseQuickpick';
import { addDatabaseReferenceQuickpick } from '../dialogs/addDatabaseReferenceQuickpick';
@@ -675,10 +675,11 @@ export class ProjectsController {
}
public async addItemPromptFromNode(treeNode: dataworkspace.WorkspaceTreeItem, itemTypeName?: string): Promise<void> {
await this.addItemPrompt(this.getProjectFromContext(treeNode), this.getRelativePath(treeNode.element), itemTypeName, treeNode.treeDataProvider as SqlDatabaseProjectTreeViewProvider);
await this.addItemPrompt(this.getProjectFromContext(treeNode), this.getRelativePath(treeNode.element), { itemType: itemTypeName }, treeNode.treeDataProvider as SqlDatabaseProjectTreeViewProvider);
}
public async addItemPrompt(project: ISqlProject, relativePath: string, itemTypeName?: string, treeDataProvider?: SqlDatabaseProjectTreeViewProvider): Promise<void> {
public async addItemPrompt(project: ISqlProject, relativePath: string, options?: AddItemOptions, treeDataProvider?: SqlDatabaseProjectTreeViewProvider): Promise<void> {
let itemTypeName = options?.itemType;
if (!itemTypeName) {
const items: vscode.QuickPickItem[] = [];
@@ -697,7 +698,7 @@ export class ProjectsController {
const itemType = templates.get(itemTypeName);
const absolutePathToParent = path.join(project.projectFolderPath, relativePath);
let itemObjectName = await this.promptForNewObjectName(itemType, project, absolutePathToParent, constants.sqlFileExtension);
let itemObjectName = await this.promptForNewObjectName(itemType, project, absolutePathToParent, constants.sqlFileExtension, options?.defaultName);
itemObjectName = itemObjectName?.trim();
@@ -1340,8 +1341,8 @@ export class ProjectsController {
}
}
private async promptForNewObjectName(itemType: templates.ProjectScriptType, _project: ISqlProject, folderPath: string, fileExtension?: string): Promise<string | undefined> {
const suggestedName = itemType.friendlyName.replace(/\s+/g, '');
private async promptForNewObjectName(itemType: templates.ProjectScriptType, _project: ISqlProject, folderPath: string, fileExtension?: string, defaultName?: string): Promise<string | undefined> {
const suggestedName = defaultName ?? itemType.friendlyName.replace(/\s+/g, '');
let counter: number = 0;
do {

View File

@@ -104,8 +104,8 @@ export class SqlDatabaseProjectProvider implements dataworkspace.IProjectProvide
return Project.openProject(projectFilePath);
}
public promptAddItem(project: sqldbproj.ISqlProject, relativeFilePath: string, itemType?: string): Promise<void> {
return this.projectController.addItemPrompt(project, relativeFilePath, itemType);
public addItemPrompt(project: sqldbproj.ISqlProject, relativeFilePath: string, options?: sqldbproj.AddItemOptions): Promise<void> {
return this.projectController.addItemPrompt(project, relativeFilePath, options);
}
/**

View File

@@ -59,12 +59,23 @@ declare module 'sqldbproj' {
* Prompts the user to add a new item to the specified project
* @param project The project to add the item to
* @param relativeFilePath The relative path in the project where the item should be added
* @param itemType Optional - The type of item to add. If not specified the user will choose from a list of available types
* @param options The additional options to use
*/
promptAddItem(project: ISqlProject, relativeFilePath: string, itemType?: string): Promise<void>;
addItemPrompt(project: ISqlProject, relativeFilePath: string, options?: AddItemOptions): Promise<void>;
}
export interface AddItemOptions {
/**
* The type of item to add. If not specified the user will choose from a list of available types
*/
itemType?: string,
/**
* The default name to display in the name prompt
*/
defaultName?: string
}
/**
* The type of an item in a SQL Project
*/

View File

@@ -96,7 +96,7 @@ describe('ProjectsController', function (): void {
const project = new Project('FakePath');
should(project.files.length).equal(0);
await projController.addItemPrompt(new Project('FakePath'), '', ItemType.script);
await projController.addItemPrompt(new Project('FakePath'), '', { itemType: ItemType.script });
should(project.files.length).equal(0, 'Expected to return without throwing an exception or adding a file when an empty/undefined name is provided.');
should(showErrorMessageSpy.notCalled).be.true('showErrorMessage should not have been called');
showInputBoxStub.restore();
@@ -112,9 +112,9 @@ describe('ProjectsController', function (): void {
const project = await testUtils.createTestProject(baselines.newProjectFileBaseline);
should(project.files.length).equal(0, 'There should be no files');
await projController.addItemPrompt(project, '', ItemType.script);
await projController.addItemPrompt(project, '', { itemType: ItemType.script });
should(project.files.length).equal(1, 'File should be successfully added');
await projController.addItemPrompt(project, '', ItemType.script);
await projController.addItemPrompt(project, '', { itemType: ItemType.script });
const msg = constants.fileAlreadyExists(tableName);
should(spy.calledOnce).be.true('showErrorMessage should have been called exactly once');
should(spy.calledWith(msg)).be.true(`showErrorMessage not called with expected message '${msg}' Actual '${spy.getCall(0).args[0]}'`);
@@ -327,13 +327,13 @@ describe('ProjectsController', function (): void {
sinon.stub(vscode.window, 'showInputBox').resolves(preDeployScriptName);
should(project.preDeployScripts.length).equal(0, 'There should be no pre deploy scripts');
await projController.addItemPrompt(project, '', ItemType.preDeployScript);
await projController.addItemPrompt(project, '', { itemType: ItemType.preDeployScript });
should(project.preDeployScripts.length).equal(1, `Pre deploy script should be successfully added. ${project.preDeployScripts.length}, ${project.files.length}`);
sinon.restore();
sinon.stub(vscode.window, 'showInputBox').resolves(postDeployScriptName);
should(project.postDeployScripts.length).equal(0, 'There should be no post deploy scripts');
await projController.addItemPrompt(project, '', ItemType.postDeployScript);
await projController.addItemPrompt(project, '', { itemType: ItemType.postDeployScript });
should(project.postDeployScripts.length).equal(1, 'Post deploy script should be successfully added');
});