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(acl): remove forRoot #1690

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions packages/acl/docs/getting-started.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ Install `@delon/acl`:
npm i -S @delon/acl
```

Import `DelonACLModule` module:
If you use Standalone, there is no need to import the `DelonACLModule` module, otherwise:

```typescript
import { DelonACLModule } from '@delon/acl';

@NgModule({
imports: [
DelonACLModule.forRoot()
DelonACLModule
]
})
export class AppModule { }
Expand Down
4 changes: 2 additions & 2 deletions packages/acl/docs/getting-started.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ ACL 全称叫访问控制列表(Access Control List),是一种非常简单
npm i -S @delon/acl
```

导入 `DelonACLModule` 模块:
若使用 Standalone 无需要额外导入 `DelonACLModule` 模块,否则

```typescript
import { DelonACLModule } from '@delon/acl';

@NgModule({
imports: [
DelonACLModule.forRoot()
DelonACLModule
]
})
export class AppModule { }
Expand Down
2 changes: 1 addition & 1 deletion packages/acl/src/acl-guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('acl: guard', () => {
data: { guard: { role: ['admin'] } } as ACLGuardData
}
]),
DelonACLModule.forRoot()
DelonACLModule
]
});
srv = TestBed.inject<ACLGuardService>(ACLGuardService);
Expand Down
2 changes: 1 addition & 1 deletion packages/acl/src/acl-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Observable, of, map, tap } from 'rxjs';
import { ACLService } from './acl.service';
import type { ACLCanType, ACLGuardData } from './acl.type';

@Injectable()
@Injectable({ providedIn: 'root' })
export class ACLGuardService {
constructor(
private srv: ACLService,
Expand Down
2 changes: 1 addition & 1 deletion packages/acl/src/acl-if.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('acl-if: directive', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestComponent],
imports: [DelonACLModule.forRoot()],
imports: [DelonACLModule],
providers: [{ provide: ComponentFixtureAutoDetect, useValue: true }]
});
fixture = TestBed.createComponent(TestComponent);
Expand Down
3 changes: 2 additions & 1 deletion packages/acl/src/acl-if.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { ACLCanType } from './acl.type';

@Directive({
selector: '[aclIf]',
exportAs: 'aclIf'
exportAs: 'aclIf',
standalone: true
})
export class ACLIfDirective implements OnDestroy {
static ngAcceptInputType_except: boolean | string | undefined | null;
Expand Down
2 changes: 1 addition & 1 deletion packages/acl/src/acl.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('acl: directive', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestComponent],
imports: [DelonACLModule.forRoot()],
imports: [DelonACLModule],
providers: [{ provide: ComponentFixtureAutoDetect, useValue: true }]
});
fixture = TestBed.createComponent(TestComponent);
Expand Down
3 changes: 2 additions & 1 deletion packages/acl/src/acl.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { ACLCanType } from './acl.type';

@Directive({
selector: '[acl]',
exportAs: 'acl'
exportAs: 'acl',
standalone: true
})
export class ACLDirective implements OnDestroy {
private _value!: ACLCanType;
Expand Down
16 changes: 3 additions & 13 deletions packages/acl/src/acl.module.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import { CommonModule } from '@angular/common';
import { ModuleWithProviders, NgModule } from '@angular/core';
import { NgModule } from '@angular/core';

import { ACLGuardService } from './acl-guard';
import { ACLIfDirective } from './acl-if.directive';
import { ACLDirective } from './acl.directive';
import { ACLService } from './acl.service';

const COMPONENTS = [ACLDirective, ACLIfDirective];

@NgModule({
imports: [CommonModule],
declarations: COMPONENTS,
imports: [CommonModule, ...COMPONENTS],
exports: COMPONENTS
})
export class DelonACLModule {
static forRoot(): ModuleWithProviders<DelonACLModule> {
return {
ngModule: DelonACLModule,
providers: [ACLService, ACLGuardService]
};
}
}
export class DelonACLModule {}
4 changes: 1 addition & 3 deletions packages/acl/src/acl.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import { ACLCanType, ACLType } from './acl.type';

/**
* ACL 控制服务,[在线文档](https://ng-alain.com/acl)
*
* 务必在根目录注册 `DelonACLModule.forRoot()` 才能使用服务
*/
@Injectable()
@Injectable({ providedIn: 'root' })
export class ACLService {
private options: AlainACLConfig;
private roles: string[] = [];
Expand Down
2 changes: 1 addition & 1 deletion packages/form/spec/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('form: component', () => {
options = { acl: false, i18n: false, ...options };
const imports = [NoopAnimationsModule, DelonFormModule.forRoot(), AlainThemeModule];
if (options.acl) {
imports.push(DelonACLModule.forRoot());
imports.push(DelonACLModule);
}
TestBed.configureTestingModule({
imports,
Expand Down
3 changes: 2 additions & 1 deletion schematics/ng-update/upgrade-rules/v17/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Schematic: ng-update: v17Rule', () => {
expect(content).toContain(`, UploadWidgetModule`);
});

it('#removeAlainThemeModuleForRoot', async () => {
it('#removeForRoot', async () => {
const globalConfigPath = '/projects/foo/src/app/global-config.module.ts';
tree.create(
globalConfigPath,
Expand All @@ -62,6 +62,7 @@ describe('Schematic: ng-update: v17Rule', () => {
await runMigration();
const content = tree.readContent(globalConfigPath);
expect(content).not.toContain(`AlainThemeModule.forRoot()`);
expect(content).not.toContain(`DelonACLModule.forRoot()`);
});

it('#replaceProvideAlainConfig', async () => {
Expand Down
10 changes: 2 additions & 8 deletions schematics/ng-update/upgrade-rules/v17/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { chain, Rule, SchematicContext, Tree } from '@angular-devkit/schematics'
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';

import { autoRegisterFormWidgets } from './autoRegisterFormWidgets';
import { removeAlainThemeModuleForRoot } from './removeAlainThemeModuleForRoot';
import { removeForRoot } from './removeForRoot';
import { replaceProvideConfig } from './replaceProvideConfig';
import { logFinished, logInfo, logWarn } from '../../../utils';
import { UpgradeMainVersions } from '../../../utils/versions';
Expand Down Expand Up @@ -31,12 +31,6 @@ export function v17Rule(): Rule {
return async (tree: Tree, context: SchematicContext) => {
UpgradeMainVersions(tree);
logInfo(context, `Upgrade dependency version number`);
return chain([
removeAlainThemeModuleForRoot(),
autoRegisterFormWidgets(),
replaceProvideConfig(),
qr(),
finished()
]);
return chain([removeForRoot(), autoRegisterFormWidgets(), replaceProvideConfig(), qr(), finished()]);
};
}

This file was deleted.

53 changes: 53 additions & 0 deletions schematics/ng-update/upgrade-rules/v17/removeForRoot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';

import { DEFAULT_WORKSPACE_PATH, logInfo, readJSON } from '../../../utils';

export function removeForRoot(): Rule {
return (tree: Tree, context: SchematicContext) => {
const angularJson = readJSON(tree, DEFAULT_WORKSPACE_PATH);
const projectNames = Object.keys(angularJson.projects);
for (const name of projectNames) {
const sourceRoot = angularJson.projects[name].sourceRoot;
removeAlainThemeForRoot(tree, name, sourceRoot, context);
removeAlainThemeForChild(tree, name, sourceRoot, context);
removeDelonACLModuleForRoot(tree, name, sourceRoot, context);
}
};
}

function removeAlainThemeForRoot(tree: Tree, name: string, sourceRoot: string, context: SchematicContext): void {
const modulePath = `${sourceRoot}/app/global-config.module.ts`;
if (!tree.exists(modulePath)) return;

const forRoot = 'AlainThemeModule.forRoot()';
const content = tree.readText(modulePath);
tree.overwrite(modulePath, content.replace(/AlainThemeModule\.forRoot\(\),?/g, ''));

logInfo(context, `Remove ${forRoot} in ${name} project`);
}

function removeAlainThemeForChild(tree: Tree, name: string, _: string, context: SchematicContext): void {
const forChild = 'AlainThemeModule.forChild()';

tree.visit((path, entry) => {
if (!entry || !path.endsWith('.ts')) return;

const content = tree.readText(path);
if (!content.includes(forChild)) return;

tree.overwrite(path, content.replace(forChild, 'AlainThemeModule'));
});

logInfo(context, `Remove ${forChild} in ${name} project`);
}

function removeDelonACLModuleForRoot(tree: Tree, name: string, sourceRoot: string, context: SchematicContext): void {
const modulePath = `${sourceRoot}/app/global-config.module.ts`;
if (!tree.exists(modulePath)) return;

const forRoot = 'DelonACLModule.forRoot()';
const content = tree.readText(modulePath);
tree.overwrite(modulePath, content.replace(/DelonACLModule\.forRoot\(\),?/g, ''));

logInfo(context, `Remove ${forRoot} in ${name} project`);
}
2 changes: 1 addition & 1 deletion src/app/core/code/files/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ imports: [
AlainThemeModule,
DemoDelonABCModule,
DemoDelonChartModule,
DelonACLModule.forRoot(),
DelonACLModule,
DelonCacheModule,
DelonAuthModule,
DelonFormModule.forRoot(),
Expand Down
5 changes: 2 additions & 3 deletions src/app/core/code/files/global-config.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import { AlainConfig, provideAlainConfig, AlainConfigService } from '@delon/util
// Please refer to: https://ng-alain.com/docs/global-config
// #region NG-ALAIN Config

import { DelonACLModule } from '@delon/acl';
import * as MOCKDATA from '../../_mock';

const alainConfig: AlainConfig = { };

const alainModules = [DelonACLModule.forRoot(), DelonMockModule.forRoot({ data: MOCKDATA })];
const alainProvides = [provideAlainConfig(alainConfig)];
const alainModules = [DelonMockModule.forRoot({ data: MOCKDATA })];
const alainProvides = [{ provide: ALAIN_CONFIG, useValue: alainConfig }];

// #region reuse-tab

Expand Down
3 changes: 1 addition & 2 deletions src/app/global-config.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable import/order */
import { ModuleWithProviders, NgModule } from '@angular/core';

import { DelonACLModule } from '@delon/acl';
import { DelonMockModule } from '@delon/mock';
import { AlainConfig, provideAlainConfig } from '@delon/util/config';

Expand Down Expand Up @@ -72,7 +71,7 @@ const zorroProvides = [provideNzConfig(ngZorroConfig)];
// #endregion

@NgModule({
imports: [DelonACLModule.forRoot(), DelonMockModule.forRoot({ data: MOCKDATA })]
imports: [DelonMockModule.forRoot({ data: MOCKDATA })]
})
export class GlobalConfigModule {
static forRoot(): ModuleWithProviders<GlobalConfigModule> {
Expand Down