@@ -40,6 +40,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
4040 public static final String WITH_INTERFACES = "withInterfaces" ;
4141 public static final String NG_VERSION = "ngVersion" ;
4242 public static final String NG_PACKAGR = "useNgPackagr" ;
43+ public static final String PROVIDED_IN_ROOT ="providedInRoot" ;
4344
4445 protected String npmName = null ;
4546 protected String npmVersion = "1.0.0" ;
@@ -55,6 +56,7 @@ public TypeScriptAngularClientCodegen() {
5556 this .cliOptions .add (new CliOption (SNAPSHOT , "When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm" , SchemaTypeUtil .BOOLEAN_TYPE ).defaultValue (Boolean .FALSE .toString ()));
5657 this .cliOptions .add (new CliOption (WITH_INTERFACES , "Setting this property to true will generate interfaces next to the default class implementations." , SchemaTypeUtil .BOOLEAN_TYPE ).defaultValue (Boolean .FALSE .toString ()));
5758 this .cliOptions .add (new CliOption (NG_VERSION , "The version of Angular. Default is '4.3'" ));
59+ this .cliOptions .add (new CliOption (PROVIDED_IN_ROOT , "Use this property to provide Injectables in root (it is only valid in angular version greater or equal to 6.0.0)." , SchemaTypeUtil .BOOLEAN_TYPE ).defaultValue (Boolean .FALSE .toString ()));
5860 }
5961
6062 @ Override
@@ -106,26 +108,116 @@ public void processOpts() {
106108 supportingFiles .add (new SupportingFile ("npmignore" , "" , ".npmignore" ));
107109 supportingFiles .add (new SupportingFile ("git_push.sh.mustache" , "" , "git_push.sh" ));
108110
109- // determine NG version
110- SemVer ngVersion ;
111- if (additionalProperties .containsKey (NG_VERSION )) {
112- ngVersion = new SemVer (additionalProperties .get (NG_VERSION ).toString ());
111+ SemVer ngVersion = determineNgVersion ();
112+
113+ additionalProperties .put (NG_VERSION , ngVersion );
114+
115+ // for Angular 2 AOT support we will use good-old ngc,
116+ // Angular Package format wasn't invented at this time and building was much more easier
117+ if (!ngVersion .atLeast ("4.0.0" )) {
118+ LOGGER .warn ("Please update your legacy Angular " + ngVersion + " project to benefit from 'Angular Package Format' support." );
119+ additionalProperties .put (NG_PACKAGR , false );
113120 } else {
114- ngVersion = new SemVer ("6.0.0" );
115- LOGGER .info ("generating code for Angular {} ..." , ngVersion );
116- LOGGER .info (" (you can select the angular version by setting the additionalProperty ngVersion)" );
121+ additionalProperties .put (NG_PACKAGR , true );
122+ }
123+
124+ // Set the typescript version compatible to the Angular version
125+ if (ngVersion .atLeast ("8.0.0" )) {
126+ additionalProperties .put ("tsVersion" , ">=3.4.0 <3.6.0" );
127+ } else if (ngVersion .atLeast ("7.0.0" )) {
128+ additionalProperties .put ("tsVersion" , ">=3.1.1 <3.2.0" );
129+ } else if (ngVersion .atLeast ("6.0.0" )) {
130+ additionalProperties .put ("tsVersion" , ">=2.7.2 and <2.10.0" );
131+ } else if (ngVersion .atLeast ("5.0.0" )) {
132+ additionalProperties .put ("tsVersion" , ">=2.1.5 <2.7.0" );
133+ } else {
134+ // Angular v2-v4 requires typescript ">=2.1.5 <2.8"
135+ additionalProperties .put ("tsVersion" , ">=2.1.5 <2.8.0" );
136+ }
137+
138+ // Set the rxJS version compatible to the Angular version
139+ if (ngVersion .atLeast ("8.0.0" )) {
140+ additionalProperties .put ("rxjsVersion" , "6.5.0" );
141+ additionalProperties .put ("useRxJS6" , true );
142+ } else if (ngVersion .atLeast ("7.0.0" )) {
143+ additionalProperties .put ("rxjsVersion" , "6.3.0" );
144+ additionalProperties .put ("useRxJS6" , true );
145+ } else if (ngVersion .atLeast ("6.0.0" )) {
146+ additionalProperties .put ("rxjsVersion" , "6.1.0" );
147+ additionalProperties .put ("useRxJS6" , true );
148+ } else {
149+ // Angular prior to v6
150+ additionalProperties .put ("rxjsVersion" , "5.4.0" );
117151 }
118- additionalProperties .put (NG_VERSION , ngVersion );
119- additionalProperties .put (NG_PACKAGR , ngVersion .atLeast ("4.0.0" ));
120- additionalProperties .put ("useRxJS6" , ngVersion .atLeast ("6.0.0" ));
121- additionalProperties .put ("injectionToken" , ngVersion .atLeast ("4.0.0" ) ? "InjectionToken" : "OpaqueToken" );
122- additionalProperties .put ("injectionTokenTyped" , ngVersion .atLeast ("4.0.0" ));
123- additionalProperties .put ("useHttpClient" , ngVersion .atLeast ("4.3.0" ));
124- additionalProperties .put ("useHttpClientPackage" , ngVersion .atLeast ("4.3.0" ) && !ngVersion .atLeast ("8.0.0" ));
125152 if (!ngVersion .atLeast ("4.3.0" )) {
126153 supportingFiles .add (new SupportingFile ("rxjs-operators.mustache" , getIndexDirectory (), "rxjs-operators.ts" ));
127154 }
128155
156+ // for Angular 2 AOT support we will use good-old ngc,
157+ // Angular Package format wasn't invented at this time and building was much more easier
158+ if (!ngVersion .atLeast ("4.0.0" )) {
159+ LOGGER .warn ("Please update your legacy Angular " + ngVersion + " project to benefit from 'Angular Package Format' support." );
160+ additionalProperties .put ("useNgPackagr" , false );
161+ } else {
162+ additionalProperties .put ("useNgPackagr" , true );
163+ supportingFiles .add (new SupportingFile ("ng-package.mustache" , getIndexDirectory (), "ng-package.json" ));
164+ }
165+
166+ // Libraries generated with v1.x of ng-packagr will ship with AoT metadata in v3, which is intended for Angular v4.
167+ // Libraries generated with v2.x of ng-packagr will ship with AoT metadata in v4, which is intended for Angular v5 (and Angular v6).
168+ additionalProperties .put ("useOldNgPackagr" , !ngVersion .atLeast ("5.0.0" ));
169+
170+ // Specific ng-packagr configuration
171+ if (ngVersion .atLeast ("8.0.0" )) {
172+ additionalProperties .put ("ngPackagrVersion" , "5.4.0" );
173+ additionalProperties .put ("tsickleVersion" , "0.35.0" );
174+ } else if (ngVersion .atLeast ("7.0.0" )) {
175+ // compatible versions with typescript version
176+ additionalProperties .put ("ngPackagrVersion" , "5.1.0" );
177+ additionalProperties .put ("tsickleVersion" , "0.34.0" );
178+ } else if (ngVersion .atLeast ("6.0.0" )) {
179+ // compatible versions with typescript version
180+ additionalProperties .put ("ngPackagrVersion" , "3.0.6" );
181+ additionalProperties .put ("tsickleVersion" , "0.32.1" );
182+ } else if (ngVersion .atLeast ("5.0.0" )) {
183+ // compatible versions with typescript version
184+ additionalProperties .put ("ngPackagrVersion" , "2.4.5" );
185+ additionalProperties .put ("tsickleVersion" , "0.27.5" );
186+ } else {
187+ // Angular versions prior to v5
188+ additionalProperties .put ("ngPackagrVersion" , "1.6.0" );
189+ }
190+
191+ // set zone.js version
192+ if (ngVersion .atLeast ("8.0.0" )) {
193+ additionalProperties .put ("zonejsVersion" , "0.9.1" );
194+ } else if (ngVersion .atLeast ("5.0.0" )) {
195+ // compatible versions to Angular 5+
196+ additionalProperties .put ("zonejsVersion" , "0.8.26" );
197+ } else {
198+ // Angular versions prior to v5
199+ additionalProperties .put ("zonejsVersion" , "0.7.6" );
200+ }
201+
202+ // set http client usage
203+ if (ngVersion .atLeast ("8.0.0" )) {
204+ additionalProperties .put ("useHttpClient" , true );
205+ additionalProperties .put ("useHttpClientPackage" , false );
206+ } else if (ngVersion .atLeast ("4.3.0" )) {
207+ additionalProperties .put ("useHttpClient" , true );
208+ additionalProperties .put ("useHttpClientPackage" , true );
209+ } else {
210+ additionalProperties .put ("useHttpClient" , false );
211+ additionalProperties .put ("useHttpClientPackage" , false );
212+ }
213+
214+ if (additionalProperties .containsKey (PROVIDED_IN_ROOT ) && !ngVersion .atLeast ("6.0.0" )) {
215+ additionalProperties .put (PROVIDED_IN_ROOT ,false );
216+ }
217+
218+ additionalProperties .put ("injectionToken" , ngVersion .atLeast ("4.0.0" ) ? "InjectionToken" : "OpaqueToken" );
219+ additionalProperties .put ("injectionTokenTyped" , ngVersion .atLeast ("4.0.0" ));
220+
129221 if (additionalProperties .containsKey (NPM_NAME )) {
130222 addNpmPackageGeneration ();
131223 }
@@ -139,6 +231,18 @@ public void processOpts() {
139231
140232 }
141233
234+ private SemVer determineNgVersion () {
235+ SemVer ngVersion ;
236+ if (additionalProperties .containsKey (NG_VERSION )) {
237+ ngVersion = new SemVer (additionalProperties .get (NG_VERSION ).toString ());
238+ } else {
239+ ngVersion = new SemVer ("8.0.0" );
240+ LOGGER .info ("generating code for Angular {} ..." , ngVersion );
241+ LOGGER .info (" (you can select the angular version by setting the additionalProperty ngVersion)" );
242+ }
243+ return ngVersion ;
244+ }
245+
142246 private void addNpmPackageGeneration () {
143247 if (additionalProperties .containsKey (NPM_NAME )) {
144248 this .setNpmName (additionalProperties .get (NPM_NAME ).toString ());
0 commit comments