-
Notifications
You must be signed in to change notification settings - Fork 11.9k
feat(@angular-devkit/schematics): add schematics to generate ai context files. #30763
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
JeanMeche
wants to merge
1
commit into
angular:main
Choose a base branch
from
JeanMeche:generate-ai-file
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
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
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
54 changes: 54 additions & 0 deletions
54
packages/schematics/angular/ai-config/files/__rulesName__.template
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,54 @@ | ||
<% if (frontmatter) { %><%= frontmatter %> | ||
|
||
<% } %>You are an expert in TypeScript, Angular, and scalable web application development. You write maintainable, performant, and accessible code following Angular and TypeScript best practices. | ||
|
||
## TypeScript Best Practices | ||
|
||
- Use strict type checking | ||
- Prefer type inference when the type is obvious | ||
- Avoid the `any` type; use `unknown` when type is uncertain | ||
|
||
## Angular Best Practices | ||
|
||
- Always use standalone components over NgModules | ||
- Must NOT set `standalone: true` inside Angular decorators. It's the default. | ||
- Use signals for state management | ||
- Implement lazy loading for feature routes | ||
- Do NOT use the `@HostBinding` and `@HostListener` decorators. Put host bindings inside the `host` object of the `@Component` or `@Directive` decorator instead | ||
- Use `NgOptimizedImage` for all static images. | ||
- `NgOptimizedImage` does not work for inline base64 images. | ||
|
||
## Components | ||
|
||
- Keep components small and focused on a single responsibility | ||
- Use `input()` and `output()` functions instead of decorators | ||
- Use `computed()` for derived state | ||
- Set `changeDetection: ChangeDetectionStrategy.OnPush` in `@Component` decorator | ||
- Prefer inline templates for small components | ||
- Prefer Reactive forms instead of Template-driven ones | ||
- Do NOT use `ngClass`, use `class` bindings instead | ||
- DO NOT use `ngStyle`, use `style` bindings instead | ||
|
||
## State Management | ||
|
||
- Use signals for local component state | ||
- Use `computed()` for derived state | ||
- Keep state transformations pure and predictable | ||
- Do NOT use `mutate` on signals, use `update` or `set` instead | ||
|
||
## Templates | ||
|
||
- Keep templates simple and avoid complex logic | ||
- Use native control flow (`@if`, `@for`, `@switch`) instead of `*ngIf`, `*ngFor`, `*ngSwitch` | ||
- Use the async pipe to handle observables | ||
|
||
## Services | ||
|
||
- Design services around a single responsibility | ||
- Use the `providedIn: 'root'` option for singleton services | ||
- Use the `inject()` function instead of constructor injection | ||
|
||
## Common pitfalls | ||
|
||
- Control flow (`@if`): | ||
- You cannot use `as` expressions in `@else if (...)`. E.g. invalid code: `@else if (bla(); as x)`. |
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,91 @@ | ||
/** | ||
* @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 { | ||
Rule, | ||
SchematicsException, | ||
apply, | ||
applyTemplates, | ||
chain, | ||
filter, | ||
mergeWith, | ||
move, | ||
strings, | ||
url, | ||
} from '@angular-devkit/schematics'; | ||
import { posix as path } from 'node:path'; | ||
import { getWorkspace as readWorkspace } from '../utility/workspace'; | ||
import { Schema as ConfigOptions, Tool } from './schema'; | ||
|
||
const geminiFile: ContextFileInfo = { rulesName: 'GEMINI.md', directory: '.gemini' }; | ||
const copilotFile: ContextFileInfo = { | ||
rulesName: 'copilot-instructions.md', | ||
directory: '.github', | ||
}; | ||
const claudeFile: ContextFileInfo = { rulesName: 'CLAUDE.md', directory: '.claude' }; | ||
const windsurfFile: ContextFileInfo = { | ||
rulesName: 'guidelines.md', | ||
directory: path.join('.windsurf', 'rules'), | ||
}; | ||
|
||
// Cursor file is a bit different, it has a front matter section. | ||
const cursorFile: ContextFileInfo = { | ||
rulesName: 'cursor.mdc', | ||
directory: path.join('.cursor', 'rules'), | ||
frontmatter: `---\ncontext: true\npriority: high\nscope: project\n---`, | ||
}; | ||
|
||
const AI_TOOLS = { | ||
'gemini': geminiFile, | ||
'claude': claudeFile, | ||
'copilot': copilotFile, | ||
'cursor': cursorFile, | ||
'windsurf': windsurfFile, | ||
}; | ||
|
||
export default function (options: ConfigOptions): Rule { | ||
return addAiContextFile(options); | ||
} | ||
|
||
interface ContextFileInfo { | ||
rulesName: string; | ||
directory: string; | ||
frontmatter?: string; | ||
} | ||
|
||
function addAiContextFile(options: ConfigOptions): Rule { | ||
const selectedTools: Tool[] = options.tool; | ||
const files: ContextFileInfo[] = selectedTools.map( | ||
(selectedTool: keyof typeof AI_TOOLS) => AI_TOOLS[selectedTool], | ||
); | ||
|
||
return async (host) => { | ||
const workspace = await readWorkspace(host); | ||
const project = workspace.projects.get(options.project); | ||
if (!project) { | ||
throw new SchematicsException(`Project name "${options.project}" doesn't not exist.`); | ||
} | ||
|
||
const rules = files.map(({ rulesName, directory, frontmatter }) => | ||
mergeWith( | ||
apply(url('./files'), [ | ||
// Keep only the single source template | ||
filter((p) => p.endsWith('__rulesName__.template')), | ||
applyTemplates({ | ||
...strings, | ||
rulesName, | ||
frontmatter: frontmatter ?? '', | ||
}), | ||
move(directory), | ||
]), | ||
), | ||
); | ||
|
||
return chain(rules); | ||
}; | ||
} |
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,97 @@ | ||
/** | ||
* @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 { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
import { Schema as ApplicationOptions } from '../application/schema'; | ||
import { Schema as WorkspaceOptions } from '../workspace/schema'; | ||
import { Schema as ConfigOptions, Tool as ConfigTool } from './schema'; | ||
|
||
describe('Ai Config Schematic', () => { | ||
const schematicRunner = new SchematicTestRunner( | ||
'@schematics/angular', | ||
require.resolve('../collection.json'), | ||
); | ||
|
||
const workspaceOptions: WorkspaceOptions = { | ||
name: 'workspace', | ||
newProjectRoot: 'projects', | ||
version: '15.0.0', | ||
}; | ||
|
||
const defaultAppOptions: ApplicationOptions = { | ||
name: 'foo', | ||
inlineStyle: true, | ||
inlineTemplate: true, | ||
routing: false, | ||
skipPackageJson: false, | ||
}; | ||
|
||
let applicationTree: UnitTestTree; | ||
function runConfigSchematic(tool: ConfigTool[]): Promise<UnitTestTree> { | ||
return schematicRunner.runSchematic<ConfigOptions>( | ||
'ai-config', | ||
{ | ||
project: 'foo', | ||
tool, | ||
}, | ||
applicationTree, | ||
); | ||
} | ||
|
||
beforeEach(async () => { | ||
const workspaceTree = await schematicRunner.runSchematic('workspace', workspaceOptions); | ||
applicationTree = await schematicRunner.runSchematic( | ||
'application', | ||
defaultAppOptions, | ||
workspaceTree, | ||
); | ||
}); | ||
|
||
it('should create a GEMINI.MD file', async () => { | ||
const tree = await runConfigSchematic([ConfigTool.Gemini]); | ||
expect(tree.readContent('.gemini/GEMINI.md')).toMatch(/^You are an expert in TypeScript/); | ||
}); | ||
|
||
it('should create a copilot-instructions.md file', async () => { | ||
const tree = await runConfigSchematic([ConfigTool.Copilot]); | ||
expect(tree.readContent('.github/copilot-instructions.md')).toContain( | ||
'You are an expert in TypeScript', | ||
); | ||
}); | ||
|
||
it('should create a cursor file', async () => { | ||
const tree = await runConfigSchematic([ConfigTool.Cursor]); | ||
const cursorFile = tree.readContent('.cursor/rules/cursor.mdc'); | ||
expect(cursorFile).toContain('You are an expert in TypeScript'); | ||
expect(cursorFile).toContain('context: true'); | ||
expect(cursorFile).toContain('---\n\nYou are an expert in TypeScript'); | ||
}); | ||
|
||
it('should create a windsurf file', async () => { | ||
const tree = await runConfigSchematic([ConfigTool.Windsurf]); | ||
expect(tree.readContent('.windsurf/rules/guidelines.md')).toContain( | ||
'You are an expert in TypeScript', | ||
); | ||
}); | ||
|
||
it('should create a claude file', async () => { | ||
const tree = await runConfigSchematic([ConfigTool.Claude]); | ||
expect(tree.readContent('.claude/CLAUDE.md')).toContain('You are an expert in TypeScript'); | ||
}); | ||
|
||
it('should create multiple files when multiple tools are selected', async () => { | ||
const tree = await runConfigSchematic([ | ||
ConfigTool.Gemini, | ||
ConfigTool.Copilot, | ||
ConfigTool.Cursor, | ||
]); | ||
expect(tree.readContent('.gemini/GEMINI.md').length).toBeGreaterThan(0); | ||
expect(tree.readContent('.github/copilot-instructions.md').length).toBeGreaterThan(0); | ||
expect(tree.readContent('.cursor/rules/cursor.mdc').length).toBeGreaterThan(0); | ||
}); | ||
}); |
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,29 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"$id": "SchematicsAngularConfig", | ||
"title": "Angular Config File Options Schema", | ||
"type": "object", | ||
"additionalProperties": false, | ||
"description": "Generates configuration files for your project. These files control various aspects of your project's build process, testing, and browser compatibility. This schematic helps you create or update essential configuration files with ease.", | ||
"properties": { | ||
"project": { | ||
"type": "string", | ||
"description": "The name of the project where the configuration file should be created or updated.", | ||
"$default": { | ||
"$source": "projectName" | ||
} | ||
}, | ||
"tool": { | ||
"type": "array", | ||
"uniqueItems": true, | ||
"x-prompt": "Which AI tool would you like to configure?", | ||
"description": "The AI tool for which the configuration file is being generated. This can include tools like Gemini, Copilot, Claude, Cursor, or Windsurf.", | ||
"minItems": 1, | ||
"items": { | ||
"type": "string", | ||
"enum": ["gemini", "copilot", "claude", "cursor", "windsurf"] | ||
} | ||
} | ||
}, | ||
"required": ["project", "tool"] | ||
} |
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
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
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
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 |
---|---|---|
|
@@ -144,6 +144,16 @@ | |
"x-prompt": "Do you want to create a 'zoneless' application without zone.js (Developer Preview)?", | ||
"type": "boolean", | ||
"default": false | ||
}, | ||
"aiConfig": { | ||
"type": "array", | ||
"default": [], | ||
"uniqueItems": true, | ||
"description": "Create an AI configuration file for the project. This file is used to improve the outputs of AI tools by following the best practices.", | ||
"items": { | ||
"type": "string", | ||
"enum": ["gemini", "copilot", "claude", "cursor", "windsurf"] | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: Do we need |
||
} | ||
}, | ||
"required": ["name", "version"] | ||
|
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.