Skip to content

Commit

Permalink
mix create project modes
Browse files Browse the repository at this point in the history
  • Loading branch information
CsCherrYY committed Sep 19, 2022
1 parent d097db8 commit dbc521a
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 57 deletions.
8 changes: 1 addition & 7 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
"workspaceContains:settings.gradle",
"workspaceContains:build.gradle.kts",
"workspaceContains:settings.gradle.kts",
"onCommand:gradle.createProject",
"onCommand:gradle.createProjectAdvanced"
"onCommand:gradle.createProject"
],
"main": "./dist/index.js",
"contributes": {
Expand Down Expand Up @@ -328,11 +327,6 @@
"category": "Gradle",
"title": "Create a Gradle Java Project..."
},
{
"command": "gradle.createProjectAdvanced",
"category": "Gradle",
"title": "Create a Gradle Java Project... (Advanced)"
},
{
"command": "gradle.runTasks",
"category": "Gradle",
Expand Down
3 changes: 1 addition & 2 deletions extension/src/commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import { GradleTaskProvider } from "../tasks";
import { isJavaExtEnabled } from "../util/javaExtension";
import { GradleDaemonsTreeDataProvider, GradleTasksTreeDataProvider, RecentTasksTreeDataProvider } from "../views";
import { Command } from "./Command";
import { COMMAND_CREATE_PROJECT, COMMAND_CREATE_PROJECT_ADVANCED, CreateProjectCommand } from "./CreateProjectCommand";
import { COMMAND_CREATE_PROJECT, CreateProjectCommand } from "./CreateProjectCommand";
import { HideStoppedDaemonsCommand, HIDE_STOPPED_DAEMONS } from "./HideStoppedDaemonsCommand";
import { COMMAND_RELOAD_JAVA_PROJECT, ReloadJavaProjectsCommand } from "./ReloadJavaProjectsCommand";
import { COMMAND_RUN_TASKS, RunTasksCommand } from "./RunTasksCommand";
Expand Down Expand Up @@ -186,7 +186,6 @@ export class Commands {
this.registerCommand(SHOW_STOPPED_DAEMONS, new ShowStoppedDaemonsCommand(this.gradleDaemonsTreeDataProvider));
this.registerCommand(HIDE_STOPPED_DAEMONS, new HideStoppedDaemonsCommand(this.gradleDaemonsTreeDataProvider));
this.registerCommand(COMMAND_CREATE_PROJECT, new CreateProjectCommand(this.client), [false]);
this.registerCommand(COMMAND_CREATE_PROJECT_ADVANCED, new CreateProjectCommand(this.client), [true]);
this.registerCommand(COMMAND_RUN_TASKS, new RunTasksCommand(this.gradleTaskProvider));
if (isJavaExtEnabled()) {
this.registerCommand(COMMAND_RELOAD_JAVA_PROJECT, new ReloadJavaProjectsCommand());
Expand Down
10 changes: 6 additions & 4 deletions extension/src/commands/CreateProjectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { IProjectCreationMetadata, IProjectCreationStep, ProjectType, StepResult
import { Command } from "./Command";

export const COMMAND_CREATE_PROJECT = "gradle.createProject";
export const COMMAND_CREATE_PROJECT_ADVANCED = "gradle.createProjectAdvanced";

export class CreateProjectCommand extends Command {
constructor(private client: GradleClient) {
Expand All @@ -31,7 +30,7 @@ export class CreateProjectCommand extends Command {
canSelectFolders: true,
canSelectMany: false,
});
const isAdvanced = params[0] as boolean;
const isAdvanced = false;
if (targetFolderUri) {
const metadata: IProjectCreationMetadata = {
isAdvanced: isAdvanced,
Expand All @@ -42,7 +41,7 @@ export class CreateProjectCommand extends Command {
projectName: path.basename(targetFolderUri[0].fsPath),
sourcePackageName: await this.client.getNormalizedPackageName(path.basename(targetFolderUri[0].fsPath)),
steps: [],
nextStep: isAdvanced ? selectProjectTypeStep : selectScriptDSLStep,
nextStep: selectScriptDSLStep,
client: this.client,
};
const success = await this.runSteps(metadata);
Expand Down Expand Up @@ -73,6 +72,10 @@ export class CreateProjectCommand extends Command {
}
step = metadata.steps.pop();
break;
case StepResult.RESTART:
metadata.steps = [];
step = selectProjectTypeStep;
break;
case StepResult.STOP:
return false; // user cancellation
default:
Expand All @@ -82,7 +85,6 @@ export class CreateProjectCommand extends Command {
return true;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
private async createProject(metadata: IProjectCreationMetadata): Promise<void> {
const cancellationKey = getRunTaskCommandCancellationKey(metadata.targetFolder, "init");
const args: string[] = ["init"];
Expand Down
5 changes: 2 additions & 3 deletions extension/src/createProject/SelectProjectTypeStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as vscode from "vscode";
import { selectScriptDSLStep } from "./SelectScriptDSLStep";
import { IProjectCreationMetadata, IProjectCreationStep, ProjectType, StepResult } from "./types";
import { updateTotalSteps } from "./utils";

export class SelectProjectTypeStep implements IProjectCreationStep {
public async run(metadata: IProjectCreationMetadata): Promise<StepResult> {
Expand All @@ -25,19 +26,17 @@ export class SelectProjectTypeStep implements IProjectCreationStep {
switch (selectedType.label) {
case "application":
metadata.projectType = ProjectType.JAVA_APPLICATION;
metadata.totalSteps = 5;
break;
case "library":
metadata.projectType = ProjectType.JAVA_LIBRARY;
metadata.totalSteps = 5;
break;
case "Gradle plugin":
metadata.projectType = ProjectType.JAVA_GRADLE_PLUGIN;
metadata.totalSteps = 4; // when creating gradle plugin, we shouldn't specify test framework
break;
default:
resolve(StepResult.STOP);
}
updateTotalSteps(metadata);
metadata.steps.push(selectProjectTypeStep);
metadata.nextStep = selectScriptDSLStep;
resolve(StepResult.NEXT);
Expand Down
21 changes: 11 additions & 10 deletions extension/src/createProject/SelectScriptDSLStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as vscode from "vscode";
import { selectTestFrameworkStep } from "./SelectTestFrameworkStep";
import { specifyProjectNameStep } from "./SpecifyProjectNameStep";
import { IProjectCreationMetadata, IProjectCreationStep, ProjectType, StepResult } from "./types";
import { createQuickInputButtons, switchToAdvancedLabel, updateTotalSteps } from "./utils";

export class SelectScriptDSLStep implements IProjectCreationStep {
public async run(metadata: IProjectCreationMetadata): Promise<StepResult> {
Expand All @@ -19,16 +20,7 @@ export class SelectScriptDSLStep implements IProjectCreationStep {
pickBox.matchOnDescription = true;
pickBox.ignoreFocusOut = true;
pickBox.items = this.getScriptDSLPickItems();
if (metadata.steps.length) {
pickBox.buttons = [vscode.QuickInputButtons.Back];
disposables.push(
pickBox.onDidTriggerButton((item) => {
if (item === vscode.QuickInputButtons.Back) {
resolve(StepResult.PREVIOUS);
}
})
);
}
pickBox.buttons = createQuickInputButtons(metadata);
disposables.push(
pickBox.onDidAccept(() => {
const selectedScriptDSL = pickBox.selectedItems[0];
Expand All @@ -52,6 +44,15 @@ export class SelectScriptDSLStep implements IProjectCreationStep {
resolve(StepResult.NEXT);
}
}),
pickBox.onDidTriggerButton((item) => {
if (item === vscode.QuickInputButtons.Back) {
resolve(StepResult.PREVIOUS);
} else if (item.tooltip === switchToAdvancedLabel) {
metadata.isAdvanced = true;
updateTotalSteps(metadata);
resolve(StepResult.RESTART);
}
}),
pickBox.onDidHide(() => {
resolve(StepResult.STOP);
})
Expand Down
17 changes: 7 additions & 10 deletions extension/src/createProject/SelectTestFrameworkStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as vscode from "vscode";
import { specifyProjectNameStep } from "./SpecifyProjectNameStep";
import { IProjectCreationMetadata, IProjectCreationStep, StepResult, TestFramework } from "./types";
import { createQuickInputButtons } from "./utils";

export class SelectTestFrameworkStep implements IProjectCreationStep {
public async run(metadata: IProjectCreationMetadata): Promise<StepResult> {
Expand All @@ -18,16 +19,7 @@ export class SelectTestFrameworkStep implements IProjectCreationStep {
pickBox.matchOnDescription = true;
pickBox.ignoreFocusOut = true;
pickBox.items = this.getTestFrameworkPickItems();
if (metadata.steps.length) {
pickBox.buttons = [vscode.QuickInputButtons.Back];
disposables.push(
pickBox.onDidTriggerButton((item) => {
if (item === vscode.QuickInputButtons.Back) {
resolve(StepResult.PREVIOUS);
}
})
);
}
pickBox.buttons = createQuickInputButtons(metadata);
disposables.push(
pickBox.onDidAccept(() => {
const selectedTestFramework = pickBox.selectedItems[0];
Expand All @@ -54,6 +46,11 @@ export class SelectTestFrameworkStep implements IProjectCreationStep {
resolve(StepResult.NEXT);
}
}),
pickBox.onDidTriggerButton((item) => {
if (item === vscode.QuickInputButtons.Back) {
resolve(StepResult.PREVIOUS);
}
}),
pickBox.onDidHide(() => {
resolve(StepResult.STOP);
})
Expand Down
21 changes: 11 additions & 10 deletions extension/src/createProject/SpecifyProjectNameStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as vscode from "vscode";
import { specifySourcePackageNameStep } from "./SpecifySourcePackageNameStep";
import { IProjectCreationMetadata, IProjectCreationStep, StepResult } from "./types";
import { createQuickInputButtons, switchToAdvancedLabel, updateTotalSteps } from "./utils";

export class SpecifyProjectNameStep implements IProjectCreationStep {
public async run(metadata: IProjectCreationMetadata): Promise<StepResult> {
Expand All @@ -21,16 +22,7 @@ export class SpecifyProjectNameStep implements IProjectCreationStep {
const validationMessage: string | undefined = this.isValidProjectName(metadata.projectName);
inputBox.enabled = validationMessage === undefined;
inputBox.validationMessage = validationMessage;
if (metadata.steps.length) {
inputBox.buttons = [vscode.QuickInputButtons.Back];
disposables.push(
inputBox.onDidTriggerButton((item) => {
if (item === vscode.QuickInputButtons.Back) {
resolve(StepResult.PREVIOUS);
}
})
);
}
inputBox.buttons = createQuickInputButtons(metadata);
disposables.push(
inputBox.onDidChangeValue(() => {
const validationMessage: string | undefined = this.isValidProjectName(inputBox.value);
Expand All @@ -43,6 +35,15 @@ export class SpecifyProjectNameStep implements IProjectCreationStep {
metadata.nextStep = !metadata.isAdvanced ? undefined : specifySourcePackageNameStep;
resolve(StepResult.NEXT);
}),
inputBox.onDidTriggerButton((item) => {
if (item === vscode.QuickInputButtons.Back) {
resolve(StepResult.PREVIOUS);
} else if (item.tooltip === switchToAdvancedLabel) {
metadata.isAdvanced = true;
updateTotalSteps(metadata);
resolve(StepResult.RESTART);
}
}),
inputBox.onDidHide(() => {
resolve(StepResult.STOP);
})
Expand Down
18 changes: 7 additions & 11 deletions extension/src/createProject/SpecifySourcePackageNameStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import * as vscode from "vscode";
import { IProjectCreationMetadata, IProjectCreationStep, StepResult } from "./types";
import { asyncDebounce } from "./utils";
import { asyncDebounce, createQuickInputButtons } from "./utils";

export class SpecifySourcePackageNameStep implements IProjectCreationStep {
public static GET_NORMALIZED_PACKAGE_NAME = "getNormalizedPackageName";
Expand Down Expand Up @@ -34,16 +34,7 @@ export class SpecifySourcePackageNameStep implements IProjectCreationStep {
inputBox.value = normalizedName as string;
inputBox.ignoreFocusOut = true;
inputBox.enabled = true;
if (metadata.steps.length) {
inputBox.buttons = [vscode.QuickInputButtons.Back];
disposables.push(
inputBox.onDidTriggerButton((item) => {
if (item === vscode.QuickInputButtons.Back) {
resolve(StepResult.PREVIOUS);
}
})
);
}
inputBox.buttons = createQuickInputButtons(metadata);
disposables.push(
inputBox.onDidChangeValue(async () => {
const normalizedName = await getNormalizedPackageNameTrigger(inputBox.value);
Expand All @@ -67,6 +58,11 @@ export class SpecifySourcePackageNameStep implements IProjectCreationStep {
resolve(StepResult.NEXT);
}
}),
inputBox.onDidTriggerButton((item) => {
if (item === vscode.QuickInputButtons.Back) {
resolve(StepResult.PREVIOUS);
}
}),
inputBox.onDidHide(() => {
resolve(StepResult.STOP);
})
Expand Down
2 changes: 2 additions & 0 deletions extension/src/createProject/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export enum StepResult {
NEXT,
STOP,
PREVIOUS,
// used for switching mode and restart all steps
RESTART,
}

export enum ProjectType {
Expand Down
29 changes: 29 additions & 0 deletions extension/src/createProject/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

/* eslint-disable @typescript-eslint/no-explicit-any */
import { debounce } from "lodash";
import { QuickInputButton, QuickInputButtons, ThemeIcon } from "vscode";
import { IProjectCreationMetadata, ProjectType } from "./types";

export const switchToAdvancedLabel = "Switch to advanced mode...";

export function asyncDebounce(func: any, wait: any, bind: any) {
const debounced = debounce(async (resolve, reject, bindSelf, args) => {
Expand All @@ -22,3 +26,28 @@ export function asyncDebounce(func: any, wait: any, bind: any) {

return returnFunc;
}

export function updateTotalSteps(metadata: IProjectCreationMetadata): void {
if (!metadata.isAdvanced) {
metadata.totalSteps = 2;
} else if (metadata.projectType === ProjectType.JAVA_GRADLE_PLUGIN) {
// when creating gradle plugin, we shouldn't specify test framework
metadata.totalSteps = 4;
} else {
metadata.totalSteps = 5;
}
}

export function createQuickInputButtons(metadata: IProjectCreationMetadata): QuickInputButton[] {
const buttons: QuickInputButton[] = [];
if (metadata.steps.length) {
buttons.push(QuickInputButtons.Back);
}
if (!metadata.isAdvanced) {
buttons.push({
iconPath: new ThemeIcon("settings-gear"),
tooltip: switchToAdvancedLabel,
});
}
return buttons;
}

0 comments on commit dbc521a

Please sign in to comment.