Refactor structures for cordova command palette feature (#953)

* refactor extension command

* ajust test case according to the command refactor

---------

Co-authored-by: Ezio Li <v-yukl@microsoft.com>
This commit is contained in:
lexie011 2024-02-22 15:57:27 +08:00 коммит произвёл GitHub
Родитель 65d84450b7
Коммит 3737fde1bb
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
13 изменённых файлов: 216 добавлений и 128 удалений

Просмотреть файл

@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import * as vscode from "vscode";
import * as nls from "vscode-nls";
import { SimulateOptions } from "cordova-simulate";
import { ProjectsStorage } from "../projectsStorage";
import { CordovaWorkspaceManager } from "../cordovaWorkspaceManager";
import { TelemetryHelper } from "../../utils/telemetryHelper";
nls.config({
messageFormat: nls.MessageFormat.bundle,
bundleFormat: nls.BundleFormat.standalone,
})();
const localize = nls.loadMessageBundle();
export function selectProject(): Promise<CordovaWorkspaceManager> {
const keys = Object.keys(ProjectsStorage.projectsCache);
if (keys.length > 1) {
return new Promise((resolve, reject) => {
vscode.window.showQuickPick(keys).then(selected => {
if (selected) {
resolve(ProjectsStorage.projectsCache[selected]);
}
}, reject);
});
} else if (keys.length === 1) {
return Promise.resolve(ProjectsStorage.projectsCache[keys[0]]);
}
return Promise.reject(
new Error(localize("NoCordovaProjectIsFound", "No Cordova project is found")),
);
}
export function commandWrapper(fn, args) {
return selectProject().then(project => {
return fn(project.workspaceRoot.uri.fsPath, ...args);
});
}
/* Launches a simulate command and records telemetry for it */
export function launchSimulateCommand(
cordovaProjectRoot: string,
options: SimulateOptions,
): Promise<void> {
return TelemetryHelper.generate("simulateCommand", generator => {
return TelemetryHelper.determineProjectTypes(cordovaProjectRoot).then(projectType => {
generator.add(
"simulateOptions",
{
platform: options.platform,
target: options.target,
livereload: options.livereload,
forceprepare: options.forceprepare,
corsproxy: options.corsproxy,
},
false,
);
generator.add(
"projectType",
TelemetryHelper.prepareProjectTypesTelemetry(projectType),
false,
);
// visibleTextEditors is null proof (returns empty array if no editors visible)
generator.add(
"visibleTextEditorsCount",
vscode.window.visibleTextEditors.length,
false,
);
return projectType;
});
}).then(projectType => {
const uri = vscode.Uri.file(cordovaProjectRoot);
const workspaceFolder = <vscode.WorkspaceFolder>vscode.workspace.getWorkspaceFolder(uri);
return ProjectsStorage.getFolder(workspaceFolder).pluginSimulator.simulate(
cordovaProjectRoot,
options,
projectType,
);
});
}

Просмотреть файл

@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
export * from "./restart";
export * from "./cordovaRun";
export * from "./cordovaBuild";
export * from "./cordovaPrepare";
export * from "./ionicBuild";
export * from "./ionicPrepare";
export * from "./ionicRun";
export * from "./simulateAndroid";
export * from "./simulateIos";

Просмотреть файл

@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper";
import { commandWrapper } from "./commandUtil";
export class CordovaBuild {
static codeName = "cordova.build";
static createHandler = () =>
commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["build"]);
}

Просмотреть файл

@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper";
import { commandWrapper } from "./commandUtil";
export class CordovaPrepare {
static codeName = "cordova.prepare";
static createHandler = () =>
commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["prepare"]);
}

Просмотреть файл

@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper";
import { commandWrapper } from "./commandUtil";
export class CordovaRun {
static codeName = "cordova.run";
static createHandler = () =>
commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["run"]);
}

Просмотреть файл

@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper";
import { commandWrapper } from "./commandUtil";
export class IonicBuild {
static codeName = "ionic.build";
static createHandler = () =>
commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["build", true]);
}

Просмотреть файл

@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper";
import { commandWrapper } from "./commandUtil";
export class IonicPrepare {
static codeName = "ionic.prepare";
static createHandler = () =>
commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["prepare", true]);
}

Просмотреть файл

@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper";
import { commandWrapper } from "./commandUtil";
export class IonicRun {
static codeName = "ionic.run";
static createHandler = () =>
commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["run", true]);
}

Просмотреть файл

@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import { CordovaCommandHelper } from "../../utils/cordovaCommandHelper";
import { commandWrapper } from "./commandUtil";
import { CordovaSessionManager } from "../cordovaSessionManager";
export class Restart {
static codeName = "cordova.restart";
static createHandler = () =>
commandWrapper(CordovaCommandHelper.restartCordovaDebugging, [new CordovaSessionManager()]);
}

Просмотреть файл

@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import * as vscode from "vscode";
import { selectProject, launchSimulateCommand } from "./commandUtil";
export class SimulateAndroid {
static codeName = "cordova.simulate.android";
static createHandler = () => {
return selectProject().then(project => {
return launchSimulateCommand(project.workspaceRoot.uri.fsPath, {
dir: project.workspaceRoot.uri.fsPath,
target: "chrome",
platform: "android",
lang: vscode.env.language,
});
});
};
}

Просмотреть файл

@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import * as vscode from "vscode";
import { selectProject, launchSimulateCommand } from "./commandUtil";
export class SimulateIos {
static codeName = "cordova.simulate.ios";
static createHandler = () => {
return selectProject().then(project => {
return launchSimulateCommand(project.workspaceRoot.uri.fsPath, {
dir: project.workspaceRoot.uri.fsPath,
target: "chrome",
platform: "ios",
lang: vscode.env.language,
});
});
};
}

Просмотреть файл

@ -3,12 +3,10 @@
import * as fs from "fs";
import * as path from "path";
import { SimulateOptions } from "cordova-simulate";
import * as vscode from "vscode";
import * as semver from "semver";
import * as nls from "vscode-nls";
import { CordovaProjectHelper } from "../utils/cordovaProjectHelper";
import { CordovaCommandHelper } from "../utils/cordovaCommandHelper";
import { Telemetry } from "../utils/telemetry";
import { TelemetryHelper } from "../utils/telemetryHelper";
import { TsdHelper } from "../utils/tsdHelper";
@ -71,7 +69,7 @@ export function activate(context: vscode.ExtensionContext): void {
vscode.workspace.workspaceFolders;
if (workspaceFolders) {
registerCordovaCommands(cordovaFactory);
registerCordovaCommands();
workspaceFolders.forEach((folder: vscode.WorkspaceFolder) => {
onFolderAdded(folder);
});
@ -443,130 +441,13 @@ function updatePluginTypeDefinitions(cordovaProjectRoot: string): void {
});
}
/* Launches a simulate command and records telemetry for it */
function launchSimulateCommand(
cordovaProjectRoot: string,
options: SimulateOptions,
): Promise<void> {
return TelemetryHelper.generate("simulateCommand", generator => {
return TelemetryHelper.determineProjectTypes(cordovaProjectRoot).then(projectType => {
generator.add(
"simulateOptions",
{
platform: options.platform,
target: options.target,
livereload: options.livereload,
forceprepare: options.forceprepare,
corsproxy: options.corsproxy,
},
false,
);
generator.add(
"projectType",
TelemetryHelper.prepareProjectTypesTelemetry(projectType),
false,
);
// visibleTextEditors is null proof (returns empty array if no editors visible)
generator.add(
"visibleTextEditorsCount",
vscode.window.visibleTextEditors.length,
false,
);
return projectType;
});
}).then(projectType => {
const uri = vscode.Uri.file(cordovaProjectRoot);
const workspaceFolder = <vscode.WorkspaceFolder>vscode.workspace.getWorkspaceFolder(uri);
return ProjectsStorage.getFolder(workspaceFolder).pluginSimulator.simulate(
cordovaProjectRoot,
options,
projectType,
);
async function registerCordovaCommands() {
const commands = await import("./commands/commands");
Object.values(commands).forEach(it => {
EXTENSION_CONTEXT.subscriptions.push(register(it));
});
}
function registerCordovaCommands(cordovaSessionManager: CordovaSessionManager): void {
EXTENSION_CONTEXT.subscriptions.push(
vscode.commands.registerCommand("cordova.restart", () =>
commandWrapper(CordovaCommandHelper.restartCordovaDebugging, [cordovaSessionManager]),
),
);
EXTENSION_CONTEXT.subscriptions.push(
vscode.commands.registerCommand("cordova.prepare", () =>
commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["prepare"]),
),
);
EXTENSION_CONTEXT.subscriptions.push(
vscode.commands.registerCommand("cordova.build", () =>
commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["build"]),
),
);
EXTENSION_CONTEXT.subscriptions.push(
vscode.commands.registerCommand("cordova.run", () =>
commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["run"]),
),
);
EXTENSION_CONTEXT.subscriptions.push(
vscode.commands.registerCommand("ionic.prepare", () =>
commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["prepare", true]),
),
);
EXTENSION_CONTEXT.subscriptions.push(
vscode.commands.registerCommand("ionic.build", () =>
commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["build", true]),
),
);
EXTENSION_CONTEXT.subscriptions.push(
vscode.commands.registerCommand("ionic.run", () =>
commandWrapper(CordovaCommandHelper.executeCordovaCommand, ["run", true]),
),
);
EXTENSION_CONTEXT.subscriptions.push(
vscode.commands.registerCommand("cordova.simulate.android", () => {
return selectProject().then(project => {
return launchSimulateCommand(project.workspaceRoot.uri.fsPath, {
dir: project.workspaceRoot.uri.fsPath,
target: "chrome",
platform: "android",
lang: vscode.env.language,
});
});
}),
);
EXTENSION_CONTEXT.subscriptions.push(
vscode.commands.registerCommand("cordova.simulate.ios", () => {
return selectProject().then(project => {
return launchSimulateCommand(project.workspaceRoot.uri.fsPath, {
dir: project.workspaceRoot.uri.fsPath,
target: "chrome",
platform: "ios",
lang: vscode.env.language,
});
});
}),
);
}
function selectProject(): Promise<CordovaWorkspaceManager> {
const keys = Object.keys(ProjectsStorage.projectsCache);
if (keys.length > 1) {
return new Promise((resolve, reject) => {
vscode.window.showQuickPick(keys).then(selected => {
if (selected) {
resolve(ProjectsStorage.projectsCache[selected]);
}
}, reject);
});
} else if (keys.length === 1) {
return Promise.resolve(ProjectsStorage.projectsCache[keys[0]]);
}
return Promise.reject(
new Error(localize("NoCordovaProjectIsFound", "No Cordova project is found")),
);
}
function commandWrapper(fn, args) {
return selectProject().then(project => {
return fn(project.workspaceRoot.uri.fsPath, ...args);
});
function register(it) {
return vscode.commands.registerCommand(it.codeName, it.createHandler);
}

Просмотреть файл

@ -28,9 +28,9 @@ suite("extensionContext", () => {
});
assert.deepStrictEqual(cordovaCommandsAvailable, [
"cordova.restart",
"cordova.prepare",
"cordova.build",
"cordova.run",
"cordova.build",
"cordova.prepare",
"cordova.simulate.android",
"cordova.simulate.ios",
]);