假设我已经有一些由生成器生成的文件,并且想要创建一些子生成器,它们根据某些内容的模板将内容插入到这些文件中。
目标是创建一个由 3 层组成的多层架构的生成器(对于用 typescript 编写的 Angular2 应用程序):
- 应用层
- metier层和
- 业务代理层
对于每一层,主生成器必须生成构成它的所有文件:模块文件,接口文件,...在此过程中生成的主要 3 个文件如下所示:
hero.application.ts:
import { Injectable } from '@angular/core';
import { IHeroApplicatif } from './hero.applicatif.interface';
import { HeroMetier } from '../metier/hero.metier';
@Injectable()
export class HeroApplicatif implements IHeroApplicatif {
constructor(private heroMetier: HeroMetier) {}
}
hero.metier.ts:
import { Injectable } from '@angular/core';
import { IHeroMetier } from './hero.metier.interface';
import { HeroBusinessDelegate } from '../business-delegate/hero.business-delegate';
@Injectable()
export class HeroMetier implements IHeroMetier {
constructor(private heroBusinessDelegate: HeroBusinessDelegate) {}
}
hero.business-delegate.ts:
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { IHeroBusinessDelegate } from './hero.business-delegate.interface';
@Injectable()
export class HeroBusinessDelegate implements IHeroBusinessDelegate {
constructor(private http: Http) {}
}
基于模板生成这些文件不会造成问题。但我想要提示用户输入方法名称的子生成器,它是返回类型和参数,因此子生成器必须修改每个先前生成的文件以插入代码,默认情况下,对于每一层,将调用传递给下一层。
假设子生成器提示用户输入一个名为 getHero 的方法,这 3 个文件的内容必须修改如下:
hero.application.ts:
import { Injectable } from '@angular/core';
import { IHeroApplicatif } from './hero.applicatif.interface';
import { HeroMetier } from '../metier/hero.metier';
@Injectable()
export class HeroApplicatif implements IHeroApplicatif {
constructor(private heroMetier: HeroMetier) {}
getHero(id:number): Promise<any> {
return this.heroMetier.getHero(id);
}
}
hero.metier.ts:
import { Injectable } from '@angular/core';
import { IHeroMetier } from './hero.metier.interface';
import { HeroBusinessDelegate } from '../business-delegate/hero.business-delegate';
@Injectable()
export class HeroMetier implements IHeroMetier {
constructor(private heroBusinessDelegate: HeroBusinessDelegate) {}
getHero(id:number): Promise<any> {
return this.heroBusinessDelegate.getHero(id);
}
}
hero.business-delegate.ts:
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { IHeroBusinessDelegate } from './hero.business-delegate.interface';
@Injectable()
export class HeroBusinessDelegate implements IHeroBusinessDelegate {
constructor(private http: Http) {}
getHero(id:number): Promise<any> {
return this.http.get(...).toPromise();
}
}
最简单、最安全、最新的方法是什么?