Skip to content

Commit

Permalink
feat: Nx Move and Remove Project in Context Menu (#1256)
Browse files Browse the repository at this point in the history
* WIP: checking approach for Jon's thoughts

* revert minor noise

* fixing merge conflicts

* adding remove, version checks, removing for angular cli projects
  • Loading branch information
ZackDeRose authored Apr 20, 2022
1 parent 4e5999c commit ebc2a9c
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 11 deletions.
1 change: 1 addition & 0 deletions apps/vscode/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export async function activate(c: ExtensionContext) {
cliTaskProvider,
runTargetTreeView,
contextMenuUri,
generator: runTargetTreeItem.generator,
});
}
);
Expand Down
20 changes: 20 additions & 0 deletions apps/vscode/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@
"command": "nx.run.fileexplorer",
"group": "2_workspace"
},
{
"when": "isNxWorkspace && config.nxConsole.enableGenerateFromContextMenu",
"command": "nx.move.fileexplorer",
"group": "2_workspace"
},
{
"when": "isNxWorkspace && config.nxConsole.enableGenerateFromContextMenu",
"command": "nx.remove.fileexplorer",
"group": "2_workspace"
},
{
"when": "isNxWorkspace && explorerResourceIsFolder && resourcePath in nxAppsDir && config.nxConsole.enableGenerateFromContextMenu && nx.hasApplicationGenerators",
"command": "nx.generate.ui.app.fileexplorer",
Expand Down Expand Up @@ -563,6 +573,16 @@
"title": "Nx generate...",
"command": "nx.generate.ui.fileexplorer"
},
{
"category": "Nx",
"title": "Move Nx Project...",
"command": "nx.move.fileexplorer"
},
{
"category": "Nx",
"title": "Remove Nx Project...",
"command": "nx.move.fileexplorer"
},
{
"category": "Nx",
"title": "Nx run",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export class RunTargetTreeItem extends TreeItem {
readonly configurationFilePath: string,
readonly route: string,
readonly extensionPath: string,
readonly generatorType?: GeneratorType
readonly generatorType?: GeneratorType,
readonly generator?: string
) {
super(route, TreeItemCollapsibleState.None);
}
Expand Down
1 change: 1 addition & 0 deletions libs/vscode/nx-workspace/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './lib/workspace-codelens-provider';
export * from './lib/verify-workspace';
export * from './lib/get-nx-config';
export * from './lib/get-nx-workspace-config';
export * from './lib/nx-version';
80 changes: 75 additions & 5 deletions libs/vscode/tasks/src/lib/cli-task-commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { commands, ExtensionContext, window, Uri } from 'vscode';

import { verifyWorkspace } from '@nx-console/vscode/nx-workspace';
import { nxVersion, verifyWorkspace } from '@nx-console/vscode/nx-workspace';
import { verifyBuilderDefinition } from '@nx-console/vscode/verify';
import { RunTargetTreeItem } from '@nx-console/vscode/nx-run-target-view';
import { CliTaskProvider } from './cli-task-provider';
Expand All @@ -9,6 +9,7 @@ import { selectFlags } from './select-flags';
import { GeneratorType, Option, OptionType } from '@nx-console/schema';
import { WorkspaceJsonConfiguration } from '@nrwl/devkit';
import { selectGenerator } from './select-generator';
import { getGenerators } from '@nx-console/server';

const CLI_COMMAND_LIST = [
'build',
Expand Down Expand Up @@ -67,6 +68,73 @@ export function registerCliTaskCommands(
selectCliCommandAndPromptForFlags('run', await getCliProjectFromUri(uri))
);

/**
* move and remove were release in patch 8.11
*/
const version = nxVersion();
if (version && version >= 8) {
commands.registerCommand(`${cli}.move.fileexplorer`, async (uri: Uri) => {
/**
* Bit of a hack - always runs angular/move if it is installed.
*
* As of the date of implementation, no issues with running this angular generator
* on non-angular projects. BUT THIS MIGHT CHANGE IN THE FUTURE.
*
* Also, future may hold other framework specific move/remove generators - this
* solution won't work when that happens.
*/
const getCorrectMoveGenerator = async () => {
const workspacePath = cliTaskProvider.getWorkspacePath();
const generators = await getGenerators(workspacePath);
return generators.find(
(generator) => generator.name === '@nrwl/angular:move'
)
? '@nrwl/angular:move'
: '@nrwl/workspace:move';
};
const generator = await getCorrectMoveGenerator();
selectCliCommandAndShowUi(
'generate',
context.extensionPath,
uri,
GeneratorType.Other,
generator
);
});

commands.registerCommand(
`${cli}.remove.fileexplorer`,
async (uri: Uri) => {
/**
* Bit of a hack - always runs angular/remove if it is installed.
*
* As of the date of implementation, no issues with running this angular generator
* on non-angular projects. BUT THIS MIGHT CHANGE IN THE FUTURE.
*
* Also, future may hold other framework specific move/remove generators - this
* solution won't work when that happens.
*/
const getCorrectRemoveGenerator = async () => {
const workspacePath = cliTaskProvider.getWorkspacePath();
const generators = await getGenerators(workspacePath);
return generators.find(
(generator) => generator.name === '@nrwl/angular:remove'
)
? '@nrwl/angular:remove'
: '@nrwl/workspace:remove';
};
const generator = await getCorrectRemoveGenerator();
selectCliCommandAndShowUi(
'generate',
context.extensionPath,
uri,
GeneratorType.Other,
generator
);
}
);
}

commands.registerCommand(`${cli}.generate`, () =>
selectGeneratorAndPromptForFlags()
);
Expand Down Expand Up @@ -137,7 +205,8 @@ async function selectCliCommandAndShowUi(
command: string,
extensionPath: string,
uri?: Uri,
generatorType?: GeneratorType
generatorType?: GeneratorType,
generator?: string
) {
const workspacePath = cliTaskProvider.getWorkspacePath();
if (!workspacePath) {
Expand All @@ -153,9 +222,10 @@ async function selectCliCommandAndShowUi(
}
const workspaceTreeItem = new RunTargetTreeItem(
configurationFilePath,
`${command[0].toUpperCase()}${command.slice(1)}`,
command,
extensionPath,
generatorType
generatorType,
generator
);

commands.executeCommand(
Expand Down Expand Up @@ -320,7 +390,7 @@ export async function selectCliProject(

if (!items.length) {
window.showInformationMessage(
`No projects have an target command for ${command}`
`No projects have a target command for ${command}`
);

return;
Expand Down
11 changes: 9 additions & 2 deletions libs/vscode/tasks/src/lib/select-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export async function getGeneratorOptions(
export async function selectGenerator(
workspacePath: string,
workspaceType: 'nx' | 'ng',
generatorType?: GeneratorType
generatorType?: GeneratorType,
generator?: { collection: string; name: string }
): Promise<TaskExecutionSchema | undefined> {
interface GenerateQuickPickItem extends QuickPickItem {
collectionName: string;
Expand Down Expand Up @@ -105,7 +106,13 @@ export async function selectGenerator(
}

if (generators) {
const selection = await window.showQuickPick(generatorsQuickPicks);
const selection = generator
? generatorsQuickPicks.find(
(quickPick) =>
quickPick.generator.collection === generator.collection &&
quickPick.generator.name === generator.name
)
: await window.showQuickPick(generatorsQuickPicks);
if (selection) {
const options =
selection.generator.options ||
Expand Down
11 changes: 9 additions & 2 deletions libs/vscode/webview/src/lib/get-task-execution-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export async function getTaskExecutionSchema(
cliTaskProvider: CliTaskProvider,
command = 'run',
contextMenuUri?: Uri,
generatorType?: GeneratorType
generatorType?: GeneratorType,
incomingGenerator?: string
): Promise<TaskExecutionSchema | void> {
try {
if (!cliTaskProvider.getWorkspacePath()) {
Expand Down Expand Up @@ -89,7 +90,13 @@ export async function getTaskExecutionSchema(
const generator = await selectGenerator(
cliTaskProvider.getWorkspacePath(),
workspaceType,
generatorType
generatorType,
incomingGenerator
? {
collection: incomingGenerator.split(':')[0],
name: incomingGenerator.split(':')[1],
}
: undefined
);

if (!generator) {
Expand Down
5 changes: 4 additions & 1 deletion libs/vscode/webview/src/lib/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface RevealWebViewPanelConfig {
cliTaskProvider: CliTaskProvider;
runTargetTreeView: TreeView<RunTargetTreeItem>;
contextMenuUri?: Uri;
generator?: string;
}

export async function revealWebViewPanel({
Expand All @@ -34,13 +35,15 @@ export async function revealWebViewPanel({
runTargetTreeItem,
runTargetTreeView,
contextMenuUri,
generator,
}: RevealWebViewPanelConfig) {
const { label, generatorType } = runTargetTreeItem;
const schema = await getTaskExecutionSchema(
cliTaskProvider,
label,
contextMenuUri,
generatorType
generatorType,
generator
);

if (!schema) {
Expand Down

0 comments on commit ebc2a9c

Please sign in to comment.