-
Notifications
You must be signed in to change notification settings - Fork 11.9k
refactor(@angular/cli): implement experimental unified run_target facade and strategy dispatcher #33215
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
Open
clydin
wants to merge
1
commit into
angular:main
Choose a base branch
from
clydin:feat/mcp-run-target-facade
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
refactor(@angular/cli): implement experimental unified run_target facade and strategy dispatcher #33215
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/angular/cli/src/commands/mcp/tools/run-target/generic-target-strategy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { getCommandErrorLogs } from '../../utils'; | ||
| import type { McpToolContext } from '../tool-registry'; | ||
| import type { TargetStrategy } from './strategy'; | ||
| import type { RunTargetOutput, StrategyExecutionContext } from './types'; | ||
|
|
||
| const BUILT_IN_COMMANDS = new Set([ | ||
| 'build', | ||
| 'test', | ||
| 'e2e', | ||
| 'serve', | ||
| 'deploy', | ||
| 'extract-i18n', | ||
| 'lint', | ||
| ]); | ||
|
|
||
| export class GenericTargetStrategy implements TargetStrategy { | ||
| canHandle(target: string, builder?: string): boolean { | ||
| return true; // Universal fallback strategy | ||
| } | ||
|
|
||
| async execute( | ||
| input: StrategyExecutionContext, | ||
| context: McpToolContext, | ||
| ): Promise<RunTargetOutput> { | ||
| if (input.target === 'serve' || input.options?.['watch'] === true) { | ||
| throw new Error( | ||
| `Watch mode execution (serve target or watch option) is not yet supported by 'run_target'. ` + | ||
| `Please use the legacy 'devserver.start' / 'devserver.wait_for_build' tools instead.`, | ||
| ); | ||
| } | ||
|
|
||
| const args: string[] = []; | ||
| if (BUILT_IN_COMMANDS.has(input.target)) { | ||
| args.push(input.target, input.projectName); | ||
| } else { | ||
| args.push('run', `${input.projectName}:${input.target}`); | ||
| } | ||
|
|
||
| if (input.configuration) { | ||
| args.push('-c', input.configuration); | ||
| } | ||
|
|
||
| let options = input.options; | ||
| if (input.target === 'test') { | ||
| options = { | ||
| ...options, | ||
| watch: false, | ||
| }; | ||
| } | ||
|
|
||
| if (options) { | ||
| for (const [key, value] of Object.entries(options)) { | ||
| if (!/^[a-zA-Z0-9-_]+$/.test(key)) { | ||
| throw new Error( | ||
| `Invalid option key: '${key}'. Option keys must be alphanumeric, hyphens, or underscores.`, | ||
| ); | ||
| } | ||
|
|
||
| if (typeof value === 'boolean') { | ||
| args.push(value ? `--${key}` : `--no-${key}`); | ||
| } else if (Array.isArray(value)) { | ||
| for (const item of value) { | ||
| args.push(`--${key}=${item}`); | ||
| } | ||
| } else if (value !== null && value !== undefined) { | ||
| args.push(`--${key}=${value}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let status: 'success' | 'failure' = 'success'; | ||
| let logs: string[]; | ||
|
|
||
| try { | ||
| const result = await context.host.executeNgCommand(args, { cwd: input.workspacePath }); | ||
| logs = result.logs; | ||
| } catch (e) { | ||
| status = 'failure'; | ||
| logs = getCommandErrorLogs(e); | ||
| } | ||
|
|
||
| return { status, logs }; | ||
| } | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
packages/angular/cli/src/commands/mcp/tools/run-target/run-target.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { createStructuredContentOutput } from '../../utils'; | ||
| import { resolveWorkspaceAndProject } from '../../workspace-utils'; | ||
| import { type McpToolContext, declareTool } from '../tool-registry'; | ||
| import { GenericTargetStrategy } from './generic-target-strategy'; | ||
| import type { TargetStrategy } from './strategy'; | ||
| import { type RunTargetInput, runTargetInputSchema, runTargetOutputSchema } from './types'; | ||
|
|
||
| const FALLBACK_STRATEGY = new GenericTargetStrategy(); | ||
| const STRATEGIES: TargetStrategy[] = []; | ||
|
|
||
| export async function runTarget(input: RunTargetInput, context: McpToolContext) { | ||
| const { workspace, workspacePath, projectName } = await resolveWorkspaceAndProject({ | ||
| host: context.host, | ||
| server: context.server, | ||
| workspacePathInput: input.workspace, | ||
| projectNameInput: input.project, | ||
| mcpWorkspace: context.workspace, | ||
| }); | ||
|
|
||
| const targetDefinition = workspace.projects.get(projectName)?.targets.get(input.target); | ||
| const builder = targetDefinition?.builder; | ||
|
|
||
| const strategy = STRATEGIES.find((s) => s.canHandle(input.target, builder)) ?? FALLBACK_STRATEGY; | ||
|
|
||
| const result = await strategy.execute( | ||
| { | ||
| workspacePath, | ||
| projectName, | ||
| target: input.target, | ||
| configuration: input.configuration, | ||
| options: input.options, | ||
| }, | ||
| context, | ||
| ); | ||
|
|
||
| return createStructuredContentOutput(result); | ||
| } | ||
|
|
||
| export const RUN_TARGET_TOOL = declareTool({ | ||
| name: 'run_target', | ||
| title: 'Run Project Target', | ||
| description: ` | ||
| <Purpose> | ||
| Executes a configured target (such as build, test, lint, e2e) for an Angular project. | ||
| This is the single, unified interface for executing all project tasks natively. | ||
| </Purpose> | ||
| <Use Cases> | ||
| * Building an application or library. | ||
| * Running unit tests, E2E tests, or linters. | ||
| * Deploying or running custom workspace targets discovered via 'list_projects'. | ||
| </Use Cases> | ||
| <Operational Notes> | ||
| * Mandatory Discovery: You MUST discover available project targets by calling 'list_projects' first. | ||
| * Watch mode (serve target or watch options) is NOT yet supported in this version of run_target. | ||
| You MUST use the legacy 'devserver.*' tools for background server lifecycles. | ||
| </Operational Notes>`, | ||
| isReadOnly: false, | ||
| isLocalOnly: true, | ||
| inputSchema: runTargetInputSchema.shape, | ||
| outputSchema: runTargetOutputSchema.shape, | ||
| factory: (context) => (input) => runTarget(input, context), | ||
| }); |
148 changes: 148 additions & 0 deletions
148
packages/angular/cli/src/commands/mcp/tools/run-target/run-target_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { CommandError } from '../../host'; | ||
| import type { MockHost } from '../../testing/mock-host'; | ||
| import { | ||
| type MockMcpToolContext, | ||
| addProjectToWorkspace, | ||
| createMockContext, | ||
| } from '../../testing/test-utils'; | ||
| import { runTarget } from './run-target'; | ||
|
|
||
| describe('Run Target Tool', () => { | ||
| let mockHost: MockHost; | ||
| let mockContext: MockMcpToolContext; | ||
|
|
||
| beforeEach(() => { | ||
| const mock = createMockContext(); | ||
| mockHost = mock.host; | ||
| mockContext = mock.context; | ||
| addProjectToWorkspace(mock.projects, 'my-app'); | ||
| }); | ||
|
|
||
| it('should construct the command correctly with target and default project', async () => { | ||
| mockContext.workspace.extensions['defaultProject'] = 'my-app'; | ||
| await runTarget({ target: 'build' }, mockContext); | ||
| expect(mockHost.executeNgCommand).toHaveBeenCalledWith(['build', 'my-app'], { cwd: '/test' }); | ||
| }); | ||
|
|
||
| it('should construct the command correctly with a specified project', async () => { | ||
| addProjectToWorkspace(mockContext.workspace.projects, 'my-lib'); | ||
| await runTarget({ project: 'my-lib', target: 'lint' }, mockContext); | ||
| expect(mockHost.executeNgCommand).toHaveBeenCalledWith(['lint', 'my-lib'], { cwd: '/test' }); | ||
| }); | ||
|
|
||
| it('should construct the command correctly with configuration', async () => { | ||
| mockContext.workspace.extensions['defaultProject'] = 'my-app'; | ||
| await runTarget({ target: 'build', configuration: 'production' }, mockContext); | ||
| expect(mockHost.executeNgCommand).toHaveBeenCalledWith( | ||
| ['build', 'my-app', '-c', 'production'], | ||
| { | ||
| cwd: '/test', | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| it('should route custom targets via ng run command syntax', async () => { | ||
| mockContext.workspace.extensions['defaultProject'] = 'my-app'; | ||
| await runTarget({ target: 'storybook', configuration: 'docs' }, mockContext); | ||
| expect(mockHost.executeNgCommand).toHaveBeenCalledWith( | ||
| ['run', 'my-app:storybook', '-c', 'docs'], | ||
| { cwd: '/test' }, | ||
| ); | ||
| }); | ||
|
|
||
| it('should map boolean options correctly to CLI flags', async () => { | ||
| mockContext.workspace.extensions['defaultProject'] = 'my-app'; | ||
| await runTarget({ target: 'lint', options: { fix: true, quiet: false } }, mockContext); | ||
| expect(mockHost.executeNgCommand).toHaveBeenCalledWith( | ||
| ['lint', 'my-app', '--fix', '--no-quiet'], | ||
| { cwd: '/test' }, | ||
| ); | ||
| }); | ||
|
|
||
| it('should map string and number options correctly to CLI flags and auto-inject no-watch', async () => { | ||
| mockContext.workspace.extensions['defaultProject'] = 'my-app'; | ||
| await runTarget( | ||
| { target: 'test', options: { browsers: 'ChromeHeadless', timeout: 5000 } }, | ||
| mockContext, | ||
| ); | ||
| expect(mockHost.executeNgCommand).toHaveBeenCalledWith( | ||
| ['test', 'my-app', '--browsers=ChromeHeadless', '--timeout=5000', '--no-watch'], | ||
| { cwd: '/test' }, | ||
| ); | ||
| }); | ||
|
|
||
| it('should map array options correctly as multiple occurrences of the flag', async () => { | ||
| mockContext.workspace.extensions['defaultProject'] = 'my-app'; | ||
| await runTarget({ target: 'lint', options: { include: ['a', 'b'] } }, mockContext); | ||
| expect(mockHost.executeNgCommand).toHaveBeenCalledWith( | ||
| ['lint', 'my-app', '--include=a', '--include=b'], | ||
| { cwd: '/test' }, | ||
| ); | ||
| }); | ||
|
|
||
| it('should automatically inject no-watch for test target even if no options provided', async () => { | ||
| mockContext.workspace.extensions['defaultProject'] = 'my-app'; | ||
| await runTarget({ target: 'test' }, mockContext); | ||
| expect(mockHost.executeNgCommand).toHaveBeenCalledWith(['test', 'my-app', '--no-watch'], { | ||
| cwd: '/test', | ||
| }); | ||
| }); | ||
|
|
||
| it('should throw an error if option key is malformed (contains whitespace/special chars)', async () => { | ||
| mockContext.workspace.extensions['defaultProject'] = 'my-app'; | ||
| await expectAsync( | ||
| runTarget({ target: 'lint', options: { 'fix --danger': true } }, mockContext), | ||
| ).toBeRejectedWithError(/Invalid option key: 'fix --danger'/); | ||
| }); | ||
|
|
||
| it('should handle a successful execution and return logs', async () => { | ||
| const executionLogs = ['Linting complete', 'All rules passed!']; | ||
| mockHost.executeNgCommand.and.resolveTo({ | ||
| logs: executionLogs, | ||
| }); | ||
|
|
||
| const { structuredContent } = await runTarget( | ||
| { project: 'my-app', target: 'lint' }, | ||
| mockContext, | ||
| ); | ||
|
|
||
| expect(structuredContent.status).toBe('success'); | ||
| expect(structuredContent.logs).toEqual(executionLogs); | ||
| }); | ||
|
|
||
| it('should handle a failed execution and capture command errors', async () => { | ||
| const executionLogs = ['Error: Rule violation found.']; | ||
| const error = new CommandError('Lint failed', executionLogs, 1); | ||
| mockHost.executeNgCommand.and.rejectWith(error); | ||
|
|
||
| const { structuredContent } = await runTarget( | ||
| { project: 'my-app', target: 'lint' }, | ||
| mockContext, | ||
| ); | ||
|
|
||
| expect(structuredContent.status).toBe('failure'); | ||
| expect(structuredContent.logs).toEqual([...executionLogs, 'Lint failed']); | ||
| }); | ||
|
|
||
| it('should throw an error if attempting to run the serve target', async () => { | ||
| mockContext.workspace.extensions['defaultProject'] = 'my-app'; | ||
| await expectAsync(runTarget({ target: 'serve' }, mockContext)).toBeRejectedWithError( | ||
| /Watch mode execution.*is not yet supported/, | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw an error if attempting to run a target with watch option true', async () => { | ||
| mockContext.workspace.extensions['defaultProject'] = 'my-app'; | ||
| await expectAsync( | ||
| runTarget({ target: 'build', options: { watch: true } }, mockContext), | ||
| ).toBeRejectedWithError(/Watch mode execution.*is not yet supported/); | ||
| }); | ||
| }); |
18 changes: 18 additions & 0 deletions
18
packages/angular/cli/src/commands/mcp/tools/run-target/strategy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import type { McpToolContext } from '../tool-registry'; | ||
| import type { RunTargetOutput, StrategyExecutionContext } from './types'; | ||
|
|
||
| export interface TargetStrategy { | ||
| /** Whether this strategy is responsible for handling the given target/builder */ | ||
| canHandle(target: string, builder?: string): boolean; | ||
|
|
||
| /** Executes the target using this strategy */ | ||
| execute(input: StrategyExecutionContext, context: McpToolContext): Promise<RunTargetOutput>; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.