Skip to content

Commit 9a6a265

Browse files
JohannesHoppewing328
authored andcommitted
[typescript-angular] AOT-compatible API client (via ng-packagr and ngc) (#7984)
* typescript-angular: uses ng-packagr for the build see #6722 see #6735 * this should make everybody happy: Angular 2/4/5 AOT support - uses ngc when targeting Angular 2 (as seen in #6735) - uses ng-packagr 1 when targeting Angular 4 - uses ng-packagr 2 when targeting Angular 5 * removes bogus import * cleans / updates Petstore samples, adds a new sample for Angular 5 * typo in README * fixes broken travis build. adds pom.xml files again This reverts commit 471d248. * makes usage of `dist` more clear and i feel generally better when `npm run build` is called explicitly. - for ng-packagr 2 is doesn't matter, since the final package.json does not have any scripts - for old ng-packagr 1 it matters, scripts are copied to the final package.json which breaks installation via `npm install {{npmName}} --save` (it runs `npm run build` again) * typescript-angular: small improvements as suggested by @macjohnny * angular-typescript: updated petstore samples, 3rd try
1 parent f673531 commit 9a6a265

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2022
-2138
lines changed

bin/typescript-angular-petstore-all.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ java $JAVA_OPTS -jar $executable $ags
4646
echo "Typescript Petstore API client (v4.3 { Adding HttpClientModule over HttpModule })"
4747
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l typescript-angular -c bin/typescript-petstore-npm.json -o samples/client/petstore/typescript-angular-v4.3/npm --additional-properties ngVersion=4.3"
4848
java $JAVA_OPTS -jar $executable $ags
49+
50+
echo "Typescript Petstore API client (v5 { Uses latest version of ng-packagr })"
51+
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l typescript-angular -c bin/typescript-petstore-npm.json -o samples/client/petstore/typescript-angular-v5/npm --additional-properties ngVersion=5"
52+
java $JAVA_OPTS -jar $executable $ags
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l typescript-angular -c bin/typescript-petstore-npm.json -o samples/client/petstore/typescript-angular-v5/npm --additional-properties ngVersion=5"
30+
31+
java $JAVA_OPTS -jar $executable $ags

bin/windows/typescript-angular-petstore-all.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ call .\bin\windows\typescript-angular-v2-interfaces.bat
33
call .\bin\windows\typescript-angular-v2.bat
44
call .\bin\windows\typescript-angular-v4-with-npm.bat
55
call .\bin\windows\typescript-angular-v4.3-with-npm.bat
6+
call .\bin\windows\typescript-angular-v5-with-npm.bat
67

78

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar
2+
3+
If Not Exist %executable% (
4+
mvn clean package
5+
)
6+
7+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
8+
set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -c bin\typescript-petstore-npm.json -l typescript-angular -o samples\client\petstore\typescript-angular-v5\npm --additional-properties ngVersion=5
9+
10+
java %JAVA_OPTS% -jar %executable% %ags%

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public TypeScriptAngularClientCodegen() {
4949
apiPackage = "api";
5050
modelPackage = "model";
5151

52-
this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package"));
53-
this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package"));
52+
this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package." +
53+
" Required to generate a full angular package"));
54+
this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package. Default is '1.0.0'"));
5455
this.cliOptions.add(new CliOption(NPM_REPOSITORY,
5556
"Use this property to set an url your private npmRepo in the package.json"));
5657
this.cliOptions.add(new CliOption(SNAPSHOT,
@@ -78,7 +79,7 @@ public String getName() {
7879

7980
@Override
8081
public String getHelp() {
81-
return "Generates a TypeScript Angular (2.x or 4.x) client library.";
82+
return "Generates a TypeScript Angular (2.x - 5.x) client library.";
8283
}
8384

8485
@Override
@@ -97,8 +98,18 @@ public void processOpts() {
9798
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
9899
supportingFiles.add(new SupportingFile("README.mustache", getIndexDirectory(), "README.md"));
99100

101+
// determine NG version
102+
SemVer ngVersion;
103+
if (additionalProperties.containsKey(NG_VERSION)) {
104+
ngVersion = new SemVer(additionalProperties.get(NG_VERSION).toString());
105+
} else {
106+
ngVersion = new SemVer("4.3.0");
107+
LOGGER.info("generating code for Angular {} ...", ngVersion);
108+
LOGGER.info(" (you can select the angular version by setting the additionalProperty ngVersion)");
109+
}
110+
100111
if (additionalProperties.containsKey(NPM_NAME)) {
101-
addNpmPackageGeneration();
112+
addNpmPackageGeneration(ngVersion);
102113
}
103114

104115
if (additionalProperties.containsKey(WITH_INTERFACES)) {
@@ -112,15 +123,6 @@ public void processOpts() {
112123
taggedUnions = Boolean.parseBoolean(additionalProperties.get(TAGGED_UNIONS).toString());
113124
}
114125

115-
// determine NG version
116-
SemVer ngVersion;
117-
if (additionalProperties.containsKey(NG_VERSION)) {
118-
ngVersion = new SemVer(additionalProperties.get(NG_VERSION).toString());
119-
} else {
120-
ngVersion = new SemVer("4.3.0");
121-
LOGGER.info("generating code for Angular {} ...", ngVersion);
122-
LOGGER.info(" (you can select the angular version by setting the additionalProperty ngVersion)");
123-
}
124126
additionalProperties.put(NG_VERSION, ngVersion);
125127
additionalProperties.put("injectionToken", ngVersion.atLeast("4.0.0") ? "InjectionToken" : "OpaqueToken");
126128
additionalProperties.put("injectionTokenTyped", ngVersion.atLeast("4.0.0"));
@@ -130,7 +132,8 @@ public void processOpts() {
130132
}
131133
}
132134

133-
private void addNpmPackageGeneration() {
135+
private void addNpmPackageGeneration(SemVer ngVersion) {
136+
134137
if (additionalProperties.containsKey(NPM_NAME)) {
135138
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
136139
}
@@ -149,6 +152,20 @@ private void addNpmPackageGeneration() {
149152
this.setNpmRepository(additionalProperties.get(NPM_REPOSITORY).toString());
150153
}
151154

155+
// for Angular 2 AOT support we will use good-old ngc,
156+
// Angular Package format wasn't invented at this time and building was much more easier
157+
if (!ngVersion.atLeast("4.0.0")) {
158+
LOGGER.warn("Please update your legacy Angular " + ngVersion + " project to benefit from 'Angular Package Format' support.");
159+
additionalProperties.put("useNgPackagr", false);
160+
} else {
161+
additionalProperties.put("useNgPackagr", true);
162+
supportingFiles.add(new SupportingFile("ng-package.mustache", getIndexDirectory(), "ng-package.json"));
163+
}
164+
165+
// Libraries generated with v1.x of ng-packagr will ship with AoT metadata in v3, which is intended for Angular v4.
166+
// Libraries generated with v2.x of ng-packagr will ship with AoT metadata in v4, which is intended for Angular v5 (and Angular v6).
167+
additionalProperties.put("useOldNgPackagr", !ngVersion.atLeast("5.0.0"));
168+
152169
//Files for building our lib
153170
supportingFiles.add(new SupportingFile("package.mustache", getIndexDirectory(), "package.json"));
154171
supportingFiles.add(new SupportingFile("typings.mustache", getIndexDirectory(), "typings.json"));

modules/swagger-codegen/src/main/resources/typescript-angular/README.mustache

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,66 @@
22

33
### Building
44

5-
To build an compile the typescript sources to javascript use:
5+
To install the required dependencies and to build the typescript sources run:
66
```
77
npm install
88
npm run build
99
```
1010

1111
### publishing
1212

13+
{{#useNgPackagr}}
14+
First build the package than run ```npm publish dist``` (don't forget to specify the `dist` folder!)
15+
{{/useNgPackagr}}
16+
{{^useNgPackagr}}
1317
First build the package than run ```npm publish```
18+
{{/useNgPackagr}}
1419

1520
### consuming
1621

17-
navigate to the folder of your consuming project and run one of next commando's.
22+
Navigate to the folder of your consuming project and run one of next commands.
1823

1924
_published:_
2025

2126
```
2227
npm install {{npmName}}@{{npmVersion}} --save
2328
```
2429

25-
_unPublished (not recommended):_
30+
_without publishing (not recommended):_
2631

2732
```
33+
{{#useNgPackagr}}
34+
npm install PATH_TO_GENERATED_PACKAGE/dist --save
35+
{{/useNgPackagr}}
36+
{{^useNgPackagr}}
2837
npm install PATH_TO_GENERATED_PACKAGE --save
38+
{{/useNgPackagr}}
2939
```
3040

3141
_using `npm link`:_
3242

43+
{{#useNgPackagr}}
44+
In PATH_TO_GENERATED_PACKAGE/dist:
45+
{{/useNgPackagr}}
46+
{{^useNgPackagr}}
3347
In PATH_TO_GENERATED_PACKAGE:
48+
{{/useNgPackagr}}
3449
```
3550
npm link
3651
```
3752

3853
In your project:
3954
```
40-
npm link {{npmName}}@{{npmVersion}}
55+
npm link {{npmName}}
4156
```
4257

58+
__Note for Windows users:__ The Angular CLI has troubles to use linked npm packages.
59+
Please refer to this issue https://github.com/angular/angular-cli/issues/8284 for a solution / workaround.
60+
Published packages are not effected by this issue.
61+
62+
63+
#### General usage
64+
4365
In your Angular project:
4466

4567

modules/swagger-codegen/src/main/resources/typescript-angular/configuration.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export class Configuration {
2828
* Select the correct content-type to use for a request.
2929
* Uses {@link Configuration#isJsonMime} to determine the correct content-type.
3030
* If no content type is found return the first found type if the contentTypes is not empty
31-
* @param {string[]} contentTypes - the array of content types that are available for selection
32-
* @returns {string} the selected content-type or <code>undefined</code> if no selection could be made.
31+
* @param contentTypes - the array of content types that are available for selection
32+
* @returns the selected content-type or <code>undefined</code> if no selection could be made.
3333
*/
3434
public selectHeaderContentType (contentTypes: string[]): string | undefined {
3535
if (contentTypes.length == 0) {
@@ -47,8 +47,8 @@ export class Configuration {
4747
* Select the correct accept content-type to use for a request.
4848
* Uses {@link Configuration#isJsonMime} to determine the correct accept content-type.
4949
* If no content type is found return the first found type if the contentTypes is not empty
50-
* @param {string[]} accepts - the array of content types that are available for selection.
51-
* @returns {string} the selected content-type or <code>undefined</code> if no selection could be made.
50+
* @param accepts - the array of content types that are available for selection.
51+
* @returns the selected content-type or <code>undefined</code> if no selection could be made.
5252
*/
5353
public selectHeaderAccept(accepts: string[]): string | undefined {
5454
if (accepts.length == 0) {
@@ -69,8 +69,8 @@ export class Configuration {
6969
* application/json; charset=UTF8
7070
* APPLICATION/JSON
7171
* application/vnd.company+json
72-
* @param {string} mime - MIME (Multipurpose Internet Mail Extensions)
73-
* @return {boolean} True if the given MIME is JSON, false otherwise.
72+
* @param mime - MIME (Multipurpose Internet Mail Extensions)
73+
* @return True if the given MIME is JSON, false otherwise.
7474
*/
7575
public isJsonMime(mime: string): boolean {
7676
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "./node_modules/ng-packagr/ng-package.schema.json",
3+
"lib": {
4+
"entryFile": "index.ts"
5+
}
6+
}

modules/swagger-codegen/src/main/resources/typescript-angular/package.mustache

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@
77
"swagger-client"
88
],
99
"license": "Unlicense",
10+
{{#useNgPackagr}}
11+
"scripts": {
12+
"build": "ng-packagr -p ng-package.json"
13+
},
14+
{{/useNgPackagr}}
15+
{{^useNgPackagr}}
1016
"main": "dist/index.js",
17+
"module": "dist/index.js",
1118
"typings": "dist/index.d.ts",
1219
"scripts": {
13-
"build": "tsc --outDir dist/",
20+
"build": "ngc",
1421
"postinstall": "npm run build"
1522
},
23+
{{/useNgPackagr}}
1624
"peerDependencies": {
1725
"@angular/core": "^{{ngVersion}}",
1826
"@angular/http": "^{{ngVersion}}",
@@ -24,11 +32,13 @@
2432
"zone.js": "^0.7.6"
2533
},
2634
"devDependencies": {
35+
"@angular/compiler-cli": "^{{ngVersion}}",
2736
"@angular/core": "^{{ngVersion}}",
2837
"@angular/http": "^{{ngVersion}}",
2938
"@angular/common": "^{{ngVersion}}",
3039
"@angular/compiler": "^{{ngVersion}}",
31-
"@angular/platform-browser": "^{{ngVersion}}",
40+
"@angular/platform-browser": "^{{ngVersion}}",{{#useNgPackagr}}
41+
"ng-packagr": {{#useOldNgPackagr}}"^1.6.0"{{/useOldNgPackagr}}{{^useOldNgPackagr}}"^2.4.1"{{/useOldNgPackagr}},{{/useNgPackagr}}
3242
"reflect-metadata": "^0.1.3",
3343
"rxjs": "^5.4.0",
3444
"zone.js": "^0.7.6",

modules/swagger-codegen/src/main/resources/typescript-angular/tsconfig.mustache

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@
2121
"filesGlob": [
2222
"./model/*.ts",
2323
"./api/*.ts"
24-
]
24+
]{{^useNgPackagr}},
25+
"angularCompilerOptions": {
26+
"genDir": "dist",
27+
"skipTemplateCodegen": true
28+
}{{/useNgPackagr}}
2529
}

0 commit comments

Comments
 (0)