Skip to content

Commit

Permalink
refactor to consume v2.2 metadata (#145)
Browse files Browse the repository at this point in the history
Signed-off-by: Yan Zhang <[email protected]>
  • Loading branch information
Eskibear authored Jul 31, 2020
1 parent 310c363 commit 2afe19f
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 263 deletions.
11 changes: 4 additions & 7 deletions src/DependencyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// Licensed under the MIT license.

import { QuickPickItem } from "vscode";
import { IDependency, ServiceManager } from "./model";
import { IDependency, serviceManager } from "./model";
import { readFileFromExtensionRoot, writeFileToExtensionRoot } from "./Utils";

const PLACEHOLDER: string = "";
const HINT_CONFIRM: string = "Press <Enter> to continue.";
const DEPENDENCIES_HISTORY_FILENAME: string = ".last_used_dependencies";

class DependencyManager {
export class DependencyManager {

public lastselected: string = null;
public dependencies: IDependency[] = [];
Expand All @@ -30,9 +30,9 @@ class DependencyManager {
this.lastselected = idList;
}

public async getQuickPickItems(manager: ServiceManager, bootVersion: string, options?: { hasLastSelected: boolean }): Promise<Array<QuickPickItem & IDependenciesItem>> {
public async getQuickPickItems(serviceUrl: string, bootVersion: string, options?: { hasLastSelected: boolean }): Promise<Array<QuickPickItem & IDependenciesItem>> {
if (this.dependencies.length === 0) {
await this.initialize(await manager.getAvailableDependencies(bootVersion));
await this.initialize(await serviceManager.getAvailableDependencies(serviceUrl, bootVersion));
}
const ret: Array<QuickPickItem & IDependenciesItem> = [];
if (this.selectedIds.length === 0) {
Expand Down Expand Up @@ -97,6 +97,3 @@ class DependencyManager {
}

export interface IDependenciesItem { itemType: string; id: string; }

// tslint:disable-next-line:export-name
export const dependencyManager: DependencyManager = new DependencyManager();
10 changes: 5 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import * as vscode from "vscode";
import {
dispose as disposeTelemetryWrapper,
initializeFromJsonFile,
instrumentOperation,
instrumentOperation
} from "vscode-extension-telemetry-wrapper";
import { EditStartersHandler, GenerateProjectHandler } from "./handler";
import { GenerateProjectHandler } from "./handler";
import { getTargetPomXml, loadPackageInfo } from "./Utils";

export async function activate(context: vscode.ExtensionContext): Promise<void> {
Expand Down Expand Up @@ -38,11 +38,11 @@ async function initializeExtension(_operationId: string, context: vscode.Extensi
}
}));

context.subscriptions.push(instrumentAndRegisterCommand("spring.initializr.editStarters", async (operationId: string, entry?: vscode.Uri) => {
context.subscriptions.push(instrumentAndRegisterCommand("spring.initializr.editStarters", async (_oid: string, entry?: vscode.Uri) => {
throw new Error("Not implemented");
const targetFile: vscode.Uri = entry || await getTargetPomXml();
if (targetFile) {
await vscode.window.showTextDocument(targetFile);
await new EditStartersHandler().run(operationId, targetFile);
// await vscode.window.showTextDocument(targetFile);
} else {
vscode.window.showInformationMessage("No pom.xml found in the workspace.");
}
Expand Down
182 changes: 0 additions & 182 deletions src/handler/EditStartersHandler.ts

This file was deleted.

22 changes: 10 additions & 12 deletions src/handler/GenerateProjectHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import * as fse from "fs-extra";
import * as path from "path";
import * as vscode from "vscode";
import { instrumentOperationStep, sendInfo } from "vscode-extension-telemetry-wrapper";
import { dependencyManager, IDependenciesItem } from "../DependencyManager";
import { DependencyManager, IDependenciesItem } from "../DependencyManager";
import { OperationCanceledError } from "../Errors";
import { IValue, ServiceManager } from "../model";
import { IValue, serviceManager } from "../model";
import { artifactIdValidation, downloadFile, groupIdValidation } from "../Utils";
import { getFromInputBox, openDialogForFolder } from "../Utils/VSCodeUI";
import { BaseHandler } from "./BaseHandler";
Expand All @@ -24,7 +24,6 @@ export class GenerateProjectHandler extends BaseHandler {
private bootVersion: string;
private dependencies: IDependenciesItem;
private outputUri: vscode.Uri;
private manager: ServiceManager;

constructor(projectType: "maven-project" | "gradle-project") {
super();
Expand All @@ -40,7 +39,6 @@ export class GenerateProjectHandler extends BaseHandler {
// Step: service URL
this.serviceUrl = await instrumentOperationStep(operationId, "serviceUrl", specifyServiceUrl)();
if (this.serviceUrl === undefined) { throw new OperationCanceledError("Service URL not specified."); }
this.manager = new ServiceManager(this.serviceUrl);

// Step: language
this.language = await instrumentOperationStep(operationId, "Language", specifyLanguage)();
Expand All @@ -59,12 +57,12 @@ export class GenerateProjectHandler extends BaseHandler {
if (this.packaging === undefined) { throw new OperationCanceledError("Packaging not specified."); }

// Step: bootVersion
this.bootVersion = await instrumentOperationStep(operationId, "BootVersion", specifyBootVersion)(this.manager);
this.bootVersion = await instrumentOperationStep(operationId, "BootVersion", specifyBootVersion)(this.serviceUrl);
if (this.bootVersion === undefined) { throw new OperationCanceledError("BootVersion not specified."); }
sendInfo(operationId, { bootVersion: this.bootVersion });

// Step: Dependencies
this.dependencies = await instrumentOperationStep(operationId, "Dependencies", specifyDependencies)(this.manager, this.bootVersion);
this.dependencies = await instrumentOperationStep(operationId, "Dependencies", specifyDependencies)(this.serviceUrl, this.bootVersion);
sendInfo(operationId, { depsType: this.dependencies.itemType, dependencies: this.dependencies.id });

// Step: Choose target folder
Expand All @@ -74,8 +72,6 @@ export class GenerateProjectHandler extends BaseHandler {
// Step: Download & Unzip
await instrumentOperationStep(operationId, "DownloadUnzip", downloadAndUnzip)(this.downloadUrl, this.outputUri.fsPath);

dependencyManager.updateLastUsedDependencies(this.dependencies);

// Open in new window
const hasOpenFolder: boolean = (vscode.workspace.workspaceFolders !== undefined);
const candidates: string[] = [
Expand Down Expand Up @@ -147,20 +143,21 @@ async function specifyPackaging(): Promise<string> {
return packaging && packaging.toLowerCase();
}

async function specifyBootVersion(manager: ServiceManager): Promise<string> {
async function specifyBootVersion(serviceUrl: string): Promise<string> {
const bootVersion: { value: IValue, label: string } = await vscode.window.showQuickPick<{ value: IValue, label: string }>(
// @ts-ignore
manager.getBootVersions().then(versions => versions.map(v => ({ value: v, label: v.name }))),
serviceManager.getBootVersions(serviceUrl).then(versions => versions.map(v => ({ value: v, label: v.name }))),
{ ignoreFocusOut: true, placeHolder: "Specify Spring Boot version." }
);
return bootVersion && bootVersion.value && bootVersion.value.id;
}

async function specifyDependencies(manager: ServiceManager, bootVersion: string): Promise<IDependenciesItem> {
async function specifyDependencies(serviceUrl: string, bootVersion: string): Promise<IDependenciesItem> {
const dependencyManager = new DependencyManager();
let current: IDependenciesItem = null;
do {
current = await vscode.window.showQuickPick(
dependencyManager.getQuickPickItems(manager, bootVersion, { hasLastSelected: true }),
dependencyManager.getQuickPickItems(serviceUrl, bootVersion, { hasLastSelected: true }),
{ ignoreFocusOut: true, placeHolder: "Search for dependencies.", matchOnDetail: true, matchOnDescription: true },
);
if (current && current.itemType === "dependency") {
Expand All @@ -170,6 +167,7 @@ async function specifyDependencies(manager: ServiceManager, bootVersion: string)
if (!current) {
throw new OperationCanceledError("Canceled on dependency seletion.");
}
dependencyManager.updateLastUsedDependencies(this.dependencies);
return current;
}

Expand Down
3 changes: 1 addition & 2 deletions src/handler/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import { EditStartersHandler } from "./EditStartersHandler";
import { GenerateProjectHandler } from "./GenerateProjectHandler";

export { EditStartersHandler, GenerateProjectHandler };
export { GenerateProjectHandler };
44 changes: 44 additions & 0 deletions src/model/Metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

/**
* See https://docs.spring.io/initializr/docs/current/reference/html/#api-guide
*/
export interface Metadata {
bootVersion: Category<BootVersion>;
dependencies: Category<DependencyGroup>;
packaging: Category<Packaging>;
javaVersion: Category<JavaVersion>;
language: Category<Language>;
type: Category<ProjectType>;
}

interface Nameable {
name: string;
}

interface Identifiable extends Nameable {
id: string;
}

interface Category<T extends Nameable> {
default?: string;
values: T[];
}

interface ProjectType extends Identifiable {
action: string;
}

type BootVersion = Identifiable;
type Packaging = Identifiable;
type JavaVersion = Identifiable;
type Language = Identifiable;

export interface DependencyGroup extends Category<Dependency>, Nameable {
}

export interface Dependency extends Identifiable {
description?: string;
versionRange?: string;
}
Loading

0 comments on commit 2afe19f

Please sign in to comment.