Skip to content

Commit a1b9887

Browse files
committed
added tasks for generating HTML, OpenAPI and TypeScript
1 parent eb2b908 commit a1b9887

File tree

5 files changed

+531
-1
lines changed

5 files changed

+531
-1
lines changed

ramler-gradle-plugin/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,22 @@
1313
<name>OPS4J Ramler Gradle Plugin</name>
1414

1515
<dependencies>
16+
<dependency>
17+
<groupId>org.ops4j.ramler</groupId>
18+
<artifactId>ramler-html</artifactId>
19+
</dependency>
1620
<dependency>
1721
<groupId>org.ops4j.ramler</groupId>
1822
<artifactId>ramler-java</artifactId>
1923
</dependency>
24+
<dependency>
25+
<groupId>org.ops4j.ramler</groupId>
26+
<artifactId>ramler-openapi</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.ops4j.ramler</groupId>
30+
<artifactId>ramler-typescript</artifactId>
31+
</dependency>
2032
<dependency>
2133
<groupId>name.remal.gradle-api</groupId>
2234
<artifactId>gradle-api</artifactId>
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright 2019 OPS4J Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
* implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.ops4j.ramler.gradle;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
23+
import org.gradle.api.DefaultTask;
24+
import org.gradle.api.GradleException;
25+
import org.gradle.api.tasks.Input;
26+
import org.gradle.api.tasks.Optional;
27+
import org.gradle.api.tasks.OutputDirectory;
28+
import org.gradle.api.tasks.TaskAction;
29+
import org.ops4j.ramler.common.exc.RamlerException;
30+
import org.ops4j.ramler.html.HtmlConfiguration;
31+
import org.ops4j.ramler.html.HtmlGenerator;
32+
33+
public class RamlerHtmlGenerator extends DefaultTask {
34+
35+
/** RAML specification file, relative to <code>${project.basedir}</code>. */
36+
private String model;
37+
38+
/**
39+
* Output directory for generated sources.
40+
*/
41+
private String outputDir;
42+
43+
/**
44+
* Directory with web resources to be used instead of the built-in resources.
45+
*/
46+
private String webResourceDir;
47+
48+
/**
49+
* Directory with Trimou templates which take precedence over the built-in templates. The entry
50+
* template is named {@code api.trimou.html}.
51+
*/
52+
private String templateDir;
53+
54+
/**
55+
* Gets the model.
56+
*
57+
* @return the model
58+
*/
59+
@Input
60+
public String getModel() {
61+
return model;
62+
}
63+
64+
/**
65+
* Sets the model.
66+
*
67+
* @param model
68+
* the model to set
69+
*/
70+
public void setModel(String model) {
71+
this.model = model;
72+
}
73+
74+
/**
75+
* Gets the outputDir.
76+
*
77+
* @return the outputDir
78+
*/
79+
@Input
80+
@OutputDirectory
81+
@Optional
82+
public String getOutputDir() {
83+
return outputDir;
84+
}
85+
86+
/**
87+
* Sets the outputDir.
88+
*
89+
* @param outputDir
90+
* the outputDir to set
91+
*/
92+
public void setOutputDir(String outputDir) {
93+
this.outputDir = outputDir;
94+
}
95+
96+
/**
97+
* Gets the webResourceDir.
98+
*
99+
* @return the webResourceDir
100+
*/
101+
@Input
102+
@Optional
103+
public String getWebResourceDir() {
104+
return webResourceDir;
105+
}
106+
107+
/**
108+
* Sets the webResourceDir.
109+
*
110+
* @param webResourceDir
111+
* the webResourceDir to set
112+
*/
113+
public void setWebResourceDir(String webResourceDir) {
114+
this.webResourceDir = webResourceDir;
115+
}
116+
117+
/**
118+
* Gets the templateDir.
119+
*
120+
* @return the templateDir
121+
*/
122+
@Input
123+
@Optional
124+
public String getTemplateDir() {
125+
return templateDir;
126+
}
127+
128+
/**
129+
* Sets the templateDir.
130+
*
131+
* @param templateDir
132+
* the templateDir to set
133+
*/
134+
public void setTemplateDir(String templateDir) {
135+
this.templateDir = templateDir;
136+
}
137+
138+
@TaskAction
139+
public void generate() {
140+
getLogger().info("Generating TypeScript sources from {}", model);
141+
String sourceFile = new File(getProject().getProjectDir(), model).getPath();
142+
String outputDir = java.util.Optional.ofNullable(getOutputDir())
143+
.orElse(new File(getProject().getBuildDir(), "ramler/html").getPath());
144+
HtmlConfiguration config = new HtmlConfiguration();
145+
config.setSourceFile(sourceFile);
146+
config.setTargetDir(outputDir);
147+
config.setTemplateDir(templateDir);
148+
config.setWebResourceDir(webResourceDir);
149+
150+
try {
151+
HtmlGenerator generator = new HtmlGenerator(config);
152+
generator.generate();
153+
}
154+
catch (RamlerException | IOException exc) {
155+
throw new GradleException("HTML generation failed", exc);
156+
}
157+
}
158+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright 2019 OPS4J Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
* implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.ops4j.ramler.gradle;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
23+
import org.gradle.api.DefaultTask;
24+
import org.gradle.api.GradleException;
25+
import org.gradle.api.tasks.Input;
26+
import org.gradle.api.tasks.Optional;
27+
import org.gradle.api.tasks.OutputDirectory;
28+
import org.gradle.api.tasks.TaskAction;
29+
import org.ops4j.ramler.common.exc.RamlerException;
30+
import org.ops4j.ramler.openapi.OpenApiConfiguration;
31+
import org.ops4j.ramler.openapi.OpenApiGenerator;
32+
33+
public class RamlerOpenApiGenerator extends DefaultTask {
34+
35+
/** RAML specification file, relative to <code>${project.basedir}</code>. */
36+
private String model;
37+
38+
/**
39+
* Output directory for generated sources.
40+
*/
41+
private String outputDir;
42+
43+
/**
44+
* Generate YAML output.
45+
*/
46+
private boolean yaml = true;
47+
48+
/**
49+
* Generate JSON output.
50+
*/
51+
private boolean json;
52+
53+
/**
54+
* Gets the model.
55+
*
56+
* @return the model
57+
*/
58+
@Input
59+
public String getModel() {
60+
return model;
61+
}
62+
63+
/**
64+
* Sets the model.
65+
*
66+
* @param model
67+
* the model to set
68+
*/
69+
public void setModel(String model) {
70+
this.model = model;
71+
}
72+
73+
/**
74+
* Gets the outputDir.
75+
*
76+
* @return the outputDir
77+
*/
78+
@Input
79+
@OutputDirectory
80+
@Optional
81+
public String getOutputDir() {
82+
return outputDir;
83+
}
84+
85+
/**
86+
* Sets the outputDir.
87+
*
88+
* @param outputDir
89+
* the outputDir to set
90+
*/
91+
public void setOutputDir(String outputDir) {
92+
this.outputDir = outputDir;
93+
}
94+
95+
/**
96+
* Gets the yaml.
97+
*
98+
* @return the yaml
99+
*/
100+
public boolean isYaml() {
101+
return yaml;
102+
}
103+
104+
/**
105+
* Sets the yaml.
106+
*
107+
* @param yaml
108+
* the yaml to set
109+
*/
110+
public void setYaml(boolean yaml) {
111+
this.yaml = yaml;
112+
}
113+
114+
/**
115+
* Gets the json.
116+
*
117+
* @return the json
118+
*/
119+
public boolean isJson() {
120+
return json;
121+
}
122+
123+
/**
124+
* Sets the json.
125+
*
126+
* @param json
127+
* the json to set
128+
*/
129+
public void setJson(boolean json) {
130+
this.json = json;
131+
}
132+
133+
@TaskAction
134+
public void generate() {
135+
getLogger().info("Generating OpenAPI from {}", model);
136+
String sourceFile = new File(getProject().getProjectDir(), model).getPath();
137+
String outputDir = java.util.Optional.ofNullable(getOutputDir())
138+
.orElse(new File(getProject().getBuildDir(), "ramler/openapi").getPath());
139+
OpenApiConfiguration config = new OpenApiConfiguration();
140+
config.setSourceFile(sourceFile);
141+
config.setTargetDir(new File(outputDir));
142+
config.setGenerateJson(json);
143+
config.setGenerateYaml(yaml);
144+
145+
146+
try {
147+
OpenApiGenerator generator = new OpenApiGenerator(config);
148+
generator.generate();
149+
}
150+
catch (RamlerException | IOException exc) {
151+
throw new GradleException("OpenAPI generation failed", exc);
152+
}
153+
}
154+
}

0 commit comments

Comments
 (0)