-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathSpecifySourcePackageNameStep.ts
More file actions
81 lines (76 loc) · 3.6 KB
/
SpecifySourcePackageNameStep.ts
File metadata and controls
81 lines (76 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as vscode from "vscode";
import { IProjectCreationMetadata, IProjectCreationStep, StepResult } from "./types";
import { asyncDebounce, createQuickInputButtons } from "./utils";
export class SpecifySourcePackageNameStep implements IProjectCreationStep {
public static GET_NORMALIZED_PACKAGE_NAME = "getNormalizedPackageName";
public async run(metadata: IProjectCreationMetadata): Promise<StepResult> {
if (!metadata.client) {
return StepResult.STOP;
}
const getNormalizedPackageNameTrigger = asyncDebounce(
metadata.client.getNormalizedPackageName,
500 /** ms */,
metadata.client
);
const disposables: vscode.Disposable[] = [];
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const specifySourcePackageNamePromise = new Promise<StepResult>(async (resolve, _reject) => {
const inputBox = vscode.window.createInputBox();
const defaultName = metadata.sourcePackageName || "";
const normalizedName = await getNormalizedPackageNameTrigger(defaultName);
if (!normalizedName) {
return resolve(StepResult.STOP);
}
inputBox.title = `Create Gradle project: Specify package name (${metadata.steps.length + 1}/${
metadata.totalSteps
})`;
inputBox.prompt = "Input source package name of your project.";
inputBox.placeholder = "e.g. " + normalizedName;
inputBox.value = normalizedName as string;
inputBox.ignoreFocusOut = true;
inputBox.buttons = createQuickInputButtons(metadata);
disposables.push(
inputBox.onDidChangeValue(async () => {
const normalizedName = await getNormalizedPackageNameTrigger(inputBox.value);
if (!normalizedName) {
return;
} else if (normalizedName !== inputBox.value) {
inputBox.validationMessage = `Invalid source package name, suggest name: ${normalizedName}`;
} else {
inputBox.validationMessage = undefined;
}
}),
inputBox.onDidAccept(async () => {
const normalizedName = await metadata.client.getNormalizedPackageName(inputBox.value);
if (!normalizedName) {
return;
} else if (normalizedName !== inputBox.value) {
inputBox.validationMessage = `Invalid source package name, suggest name: ${normalizedName}`;
} else {
metadata.sourcePackageName = inputBox.value;
metadata.nextStep = undefined;
resolve(StepResult.NEXT);
}
}),
inputBox.onDidTriggerButton((item) => {
if (item === vscode.QuickInputButtons.Back) {
resolve(StepResult.PREVIOUS);
}
}),
inputBox.onDidHide(() => {
resolve(StepResult.STOP);
})
);
disposables.push(inputBox);
inputBox.show();
});
try {
return await specifySourcePackageNamePromise;
} finally {
disposables.forEach((d) => d.dispose());
}
}
}
export const specifySourcePackageNameStep = new SpecifySourcePackageNameStep();