-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deprecate(rules-engine): actionHandlers will become protected in v13 (#…
…2518) ## Proposed change It's not a good practice to expose a `Set`. I added a method `registerActionHandlers` to replace the usage of `actionHandlers.add(...)`. It will be future proof if we want to do something else when an action handler is registered.
- Loading branch information
Showing
8 changed files
with
178 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
100 changes: 100 additions & 0 deletions
100
packages/@o3r/rules-engine/schematics/ng-update/v11.6/use-register-action-handlers.spec.ts
This file contains 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,100 @@ | ||
import { callRule, type SchematicContext, Tree } from '@angular-devkit/schematics'; | ||
import { lastValueFrom } from 'rxjs'; | ||
import { useRegisterActionHandlers } from './use-register-action-handlers'; | ||
|
||
let initialTree: Tree; | ||
let tree: Tree; | ||
const untouchedFile = 'untouchedFile.ts'; | ||
const fileWithActionHandlerAdd = 'fileWithActionHandlerAdd.ts'; | ||
const fileWithActionHandlerDelete = 'fileWithActionHandlerDelete.ts'; | ||
const multiplePresenceFile = 'multiplePresence.ts'; | ||
|
||
describe('useRegisterActionHandlers', () => { | ||
beforeEach(() => { | ||
initialTree = Tree.empty(); | ||
}); | ||
|
||
it('should not touch files without references', async () => { | ||
initialTree.create(untouchedFile, 'export {};'); | ||
tree = await lastValueFrom(callRule(useRegisterActionHandlers, initialTree, {} as SchematicContext)); | ||
expect(tree.readText(untouchedFile)).toBe(initialTree.readText(untouchedFile)); | ||
}); | ||
|
||
it('should change file with a reference to actionHandlers.add', async () => { | ||
initialTree.create(fileWithActionHandlerAdd, ` | ||
import {inject, runInInjectionContext} from '@angular/core'; | ||
import {RulesEngineRunnerService} from '@o3r/rules-engine'; | ||
import {appConfig} from './app/app.config'; | ||
import {AppComponent} from './app/app.component'; | ||
import {PopupActionHandler} from './services/popup-action-handler'; | ||
bootstrapApplication(AppComponent, appConfig) | ||
.then((m) => { | ||
runInInjectionContext(m.injector, () => { | ||
inject(RulesEngineRunnerService); | ||
ruleEngine.actionHandlers.add(inject(PopupActionHandler)); | ||
}); | ||
}) | ||
// eslint-disable-next-line no-console | ||
.catch(err => console.error(err)); | ||
`); | ||
tree = await lastValueFrom(callRule(useRegisterActionHandlers, initialTree, {} as SchematicContext)); | ||
expect(tree.readText(fileWithActionHandlerAdd)).not.toContain('actionHandlers.add'); | ||
expect(tree.readText(fileWithActionHandlerAdd)).toContain('ruleEngine.registerActionHandlers(inject(PopupActionHandler))'); | ||
}); | ||
|
||
it('should change file with a reference to actionHandlers.delete', async () => { | ||
initialTree.create(fileWithActionHandlerDelete, ` | ||
import {inject, runInInjectionContext} from '@angular/core'; | ||
import {RulesEngineRunnerService} from '@o3r/rules-engine'; | ||
import {appConfig} from './app/app.config'; | ||
import {AppComponent} from './app/app.component'; | ||
import {PopupActionHandler} from './services/popup-action-handler'; | ||
bootstrapApplication(AppComponent, appConfig) | ||
.then((m) => { | ||
runInInjectionContext(m.injector, () => { | ||
inject(RulesEngineRunnerService); | ||
ruleEngine.actionHandlers.delete(inject(PopupActionHandler)); | ||
}); | ||
}) | ||
// eslint-disable-next-line no-console | ||
.catch(err => console.error(err)); | ||
`); | ||
tree = await lastValueFrom(callRule(useRegisterActionHandlers, initialTree, {} as SchematicContext)); | ||
expect(tree.readText(fileWithActionHandlerDelete)).not.toContain('actionHandlers.delete'); | ||
expect(tree.readText(fileWithActionHandlerDelete)).toContain('ruleEngine.unregisterActionHandlers(inject(PopupActionHandler))'); | ||
}); | ||
|
||
it('should change file with multiple reference', async () => { | ||
initialTree.create(multiplePresenceFile, ` | ||
import {inject, runInInjectionContext} from '@angular/core'; | ||
import {RulesEngineRunnerService} from '@o3r/rules-engine'; | ||
import {ConfigurationRulesEngineActionHandler} from '@o3r/configuration/rules-engine'; | ||
import {appConfig} from './app/app.config'; | ||
import {AppComponent} from './app/app.component'; | ||
import {PopupActionHandler} from './services/popup-action-handler'; | ||
bootstrapApplication(AppComponent, appConfig) | ||
.then((m) => { | ||
runInInjectionContext(m.injector, () => { | ||
inject(RulesEngineRunnerService); | ||
ruleEngine.actionHandlers.add(inject(PopupActionHandler)); | ||
ruleEngine.actionHandlers.add(inject(ConfigurationRulesEngineActionHandler)); | ||
ruleEngine.actionHandlers.delete(inject(PopupActionHandler)); | ||
ruleEngine.actionHandlers.delete(inject(ConfigurationRulesEngineActionHandler)); | ||
}); | ||
}) | ||
// eslint-disable-next-line no-console | ||
.catch(err => console.error(err)); | ||
`); | ||
tree = await lastValueFrom(callRule(useRegisterActionHandlers, initialTree, {} as SchematicContext)); | ||
const text = tree.readText(multiplePresenceFile); | ||
expect(text).not.toContain('actionHandlers.add'); | ||
expect(tree.readText(multiplePresenceFile)).toContain('ruleEngine.registerActionHandlers(inject(PopupActionHandler))'); | ||
expect(tree.readText(multiplePresenceFile)).toContain('ruleEngine.registerActionHandlers(inject(ConfigurationRulesEngineActionHandler))'); | ||
expect(text).not.toContain('actionHandlers.delete'); | ||
expect(tree.readText(multiplePresenceFile)).toContain('ruleEngine.unregisterActionHandlers(inject(PopupActionHandler))'); | ||
expect(tree.readText(multiplePresenceFile)).toContain('ruleEngine.unregisterActionHandlers(inject(ConfigurationRulesEngineActionHandler))'); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/@o3r/rules-engine/schematics/ng-update/v11.6/use-register-action-handlers.ts
This file contains 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,20 @@ | ||
import type { Rule, Tree } from '@angular-devkit/schematics'; | ||
import { findFilesInTree } from '@o3r/schematics'; | ||
|
||
/** | ||
* Replace `actionHandlers.add` or `actionHandlers.delete` by `registerActionHandlers` or `unregisterActionHandlers` in file | ||
* @param tree | ||
*/ | ||
export const useRegisterActionHandlers: Rule = (tree: Tree) => { | ||
const files = findFilesInTree(tree.root, (file) => /\.actionHandlers\.(add|delete)/.test(tree.readText(file))); | ||
files.forEach(({ content, path }) => { | ||
tree.overwrite( | ||
path, | ||
content | ||
.toString() | ||
.replaceAll(/\.actionHandlers\.add/g, '.registerActionHandlers') | ||
.replaceAll(/\.actionHandlers\.delete/g, '.unregisterActionHandlers') | ||
); | ||
}); | ||
return tree; | ||
}; |
This file contains 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