Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Nx Move and Remove Project in Context Menu #1256

Merged
merged 5 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to use isAngularWorkspace here.

Suggested change
"when": "!isNxWorkspace && config.nxConsole.enableGenerateFromContextMenu",
"when": "isAngularWorkspace && config.nxConsole.enableGenerateFromContextMenu",

"command": "ng.move.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 @@ -559,6 +569,16 @@
"title": "Nx generate...",
"command": "nx.generate.ui.fileexplorer"
},
{
"category": "Nx",
"title": "Move Nx Project...",
"command": "nx.move.fileexplorer"
},
{
"category": "ng",
"title": "Move Nx Project...",
"command": "ng.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
34 changes: 29 additions & 5 deletions libs/vscode/tasks/src/lib/cli-task-commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { commands, ExtensionContext, window, Uri } from 'vscode';
import {
commands,
ExtensionContext,
window,
Uri,
Task,
TaskScope,
tasks,
} from 'vscode';

import { verifyWorkspace } from '@nx-console/vscode/nx-workspace';
import { verifyBuilderDefinition } from '@nx-console/vscode/verify';
Expand Down Expand Up @@ -67,6 +75,20 @@ export function registerCliTaskCommands(
selectCliCommandAndPromptForFlags('run', await getCliProjectFromUri(uri))
);

commands.registerCommand(`${cli}.move.fileexplorer`, async (uri: Uri) => {
const getCorrectMoveGenerator = (uri: Uri) => '@nrwl/workspace:move';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably find out the minimum version of Nx that introduced this generator and have that logic here. We have this function here that could be exported and used outside that project:

export function nxVersion(): number | null {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh, hadn't considered that. Good point.

For versions younger than we had the move/remove generators, we should probably remove this option from the context menu, right?

Copy link
Member Author

@ZackDeRose ZackDeRose Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar note: for when the user right-clicks on a node in the root, my solution would open the webview for the @nrwl/workspace:move generator; without a project set.

I think this is problematic, since we should be using the @nrwl/angular:move generator if the project is ends up being angular :\

// ^ needs to determine if the @nrwl/angular:move is in the list of
// generators for the cli and use it instead if present.
const generator = getCorrectMoveGenerator(uri);
selectCliCommandAndShowUi(
'generate',
context.extensionPath,
uri,
GeneratorType.Other,
generator
);
});

commands.registerCommand(`${cli}.generate`, () =>
selectGeneratorAndPromptForFlags()
);
Expand Down Expand Up @@ -137,7 +159,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 +176,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 @@ -319,7 +343,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