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

Add bal tool support for OpenAPI tool #1446

Closed
Closed
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
69 changes: 69 additions & 0 deletions .github/workflows/cli-central-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Publish OpenAPI CLI tools to the Ballerina central

on:
workflow_dispatch:
inputs:
environment:
type: choice
description: Select environment
required: true
options:
- CENTRAL
- DEV CENTRAL
- STAGE CENTRAL

jobs:
publish-release:
runs-on: ubuntu-latest
if: github.repository_owner == 'ballerina-platform'
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: 11
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
env:
packageUser: ${{ github.actor }}
packagePAT: ${{ secrets.GITHUB_TOKEN }}
run: ./gradlew build -x check -x test
- name: Ballerina Central Push
if: ${{ github.event.inputs.environment == 'CENTRAL' }}
env:
BALLERINA_DEV_CENTRAL: false
BALLERINA_STAGE_CENTRAL: false
BALLERINA_CENTRAL_ACCESS_TOKEN: ${{ secrets.BALLERINA_CENTRAL_ACCESS_TOKEN }}
packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
GITHUB_TOKEN: ${{ secrets.BALLERINA_BOT_TOKEN }}
run: |
./gradlew clean build :tool-openapi:publishToCentral -PpublishToCentral=true

- name: Ballerina Central Dev Push
if: ${{ github.event.inputs.environment == 'DEV CENTRAL' }}
env:
BALLERINA_DEV_CENTRAL: true
BALLERINA_STAGE_CENTRAL: false
BALLERINA_CENTRAL_ACCESS_TOKEN: ${{ secrets.BALLERINA_CENTRAL_DEV_ACCESS_TOKEN }}
packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
GITHUB_TOKEN: ${{ secrets.BALLERINA_BOT_TOKEN }}
run: |
sed -i 's/version=\(.*\)-SNAPSHOT/version=\1/g' gradle.properties
./gradlew clean build :tool-openapi:publishToCentral -PpublishToCentral=true

- name: Ballerina Central Stage Push
if: ${{ github.event.inputs.environment == 'STAGE CENTRAL' }}
env:
BALLERINA_DEV_CENTRAL: false
BALLERINA_STAGE_CENTRAL: true
BALLERINA_CENTRAL_ACCESS_TOKEN: ${{ secrets.BALLERINA_CENTRAL_STAGE_ACCESS_TOKEN }}
packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
GITHUB_TOKEN: ${{ secrets.BALLERINA_BOT_TOKEN }}
run: |
sed -i 's/version=\(.*\)-SNAPSHOT/version=\1/g' gradle.properties
./gradlew clean build :tool-openapi:publishToCentral -PpublishToCentral=true
6 changes: 6 additions & 0 deletions openapi-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,14 @@ dependencies {
}
}

task copyDependencies(type: Copy) {
from configurations.runtimeClasspath
into '../tool-openapi/resources/libs'
}

def bDistribution = file("$project.buildDir/extracted-distribution/jballerina-tools-${ballerinaLangVersion}")
task jBallerinaPack {
dependsOn(copyDependencies)
doLast {
configurations.balTools.resolvedConfiguration.resolvedArtifacts.each { artifact ->
copy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.ballerina.openapi.cmd;

import io.ballerina.cli.BLauncherCmd;
import io.ballerina.cli.launcher.LauncherUtils;
import io.ballerina.openapi.converter.diagnostic.DiagnosticMessages;
import io.ballerina.openapi.converter.diagnostic.ExceptionDiagnostic;
import io.ballerina.openapi.converter.diagnostic.IncompatibleResourceDiagnostic;
Expand Down Expand Up @@ -57,11 +58,11 @@
* generation.
*/
@CommandLine.Command(
name = "openapi",
name = "test_openapi",
description = "Generate the Ballerina sources for a given OpenAPI definition and vice versa."
)
public class OpenApiCmd implements BLauncherCmd {
private static final String CMD_NAME = "openapi";
public class TestOpenApiCmd implements BLauncherCmd {
private static final String CMD_NAME = "test_openapi";
private PrintStream outStream;
private Path executionPath = Paths.get(System.getProperty("user.dir"));
private Path targetOutputPath;
Expand Down Expand Up @@ -121,17 +122,17 @@ public class OpenApiCmd implements BLauncherCmd {
@CommandLine.Parameters
private List<String> argList;

public OpenApiCmd() {
public TestOpenApiCmd() {
this.outStream = System.err;
this.executionPath = Paths.get(System.getProperty("user.dir"));
this.exitWhenFinish = true;
}

public OpenApiCmd(PrintStream outStream, Path executionDir) {
new OpenApiCmd(outStream, executionDir, true);
public TestOpenApiCmd(PrintStream outStream, Path executionDir) {
new TestOpenApiCmd(outStream, executionDir, true);
}

public OpenApiCmd(PrintStream outStream, Path executionDir, boolean exitWhenFinish) {
public TestOpenApiCmd(PrintStream outStream, Path executionDir, boolean exitWhenFinish) {
this.outStream = outStream;
this.executionPath = executionDir;
this.exitWhenFinish = exitWhenFinish;
Expand All @@ -140,7 +141,7 @@ public OpenApiCmd(PrintStream outStream, Path executionDir, boolean exitWhenFini
public void execute() {

if (helpFlag) {
String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(getName());
String commandUsageInfo = getCommandUsageInfo(getName());
outStream.println(commandUsageInfo);
return;
}
Expand Down Expand Up @@ -212,7 +213,8 @@ public void execute() {
exitError(this.exitWhenFinish);
}
} else {
String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(getName());
// String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(getName());
String commandUsageInfo = getCommandUsageInfo(getName());
outStream.println(commandUsageInfo);
exitError(this.exitWhenFinish);
return;
Expand All @@ -223,6 +225,56 @@ public void execute() {
}
}

private static String getCommandUsageInfo(String commandName) {
String fileName = "ballerina-openapi.help";
try {
return readFileAsString(fileName);
} catch (IOException var3) {
throw LauncherUtils.createUsageExceptionWithHelp(
"usage info not available for command: " + commandName);
}
}
private static String readFileAsString(String path) throws IOException {
Class<?> openApiCmdClass = TestOpenApiCmd.class; // Replace `YourClass` with the actual class name
ClassLoader classLoader = openApiCmdClass.getClassLoader();
InputStream is = classLoader.getResourceAsStream(path);
InputStreamReader inputStreamREader = null;
BufferedReader br = null;
StringBuilder sb = new StringBuilder();

try {
inputStreamREader = new InputStreamReader(is, StandardCharsets.UTF_8);
br = new BufferedReader(inputStreamREader);
String content = br.readLine();
if (content == null) {
String var6 = sb.toString();
return var6;
}

sb.append(content);

while ((content = br.readLine()) != null) {
sb.append('\n').append(content);
}
} finally {
if (inputStreamREader != null) {
try {
inputStreamREader.close();
} catch (IOException var18) {
}
}

if (br != null) {
try {
br.close();
} catch (IOException var17) {
}
}

}
return sb.toString();
}

/**
* This util method to generate openApi contract based on the given service ballerina file.
* @param fileName input resource file
Expand Down Expand Up @@ -425,7 +477,9 @@ public String getName() {

@Override
public void printLongDesc(StringBuilder out) {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("ballerina-openapi.help");
Class<?> clazz = TestOpenApiCmd.class;
ClassLoader classLoader = clazz.getClassLoader();
InputStream inputStream = classLoader.getSystemResourceAsStream("ballerina-openapi.help");
try (InputStreamReader inputStreamREader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(inputStreamREader)) {
String content = br.readLine();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.ballerina.openapi.cmd.OpenApiCmd
io.ballerina.openapi.cmd.TestOpenApiCmd
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void setupBallerinaProject() throws IOException {
public void testClientGeneration() throws IOException {
Path petstoreYaml = resourceDir.resolve(Paths.get("petstore.yaml"));
String[] args = {"--input", petstoreYaml.toString(), "-o", this.tmpDir.toString(), "--mode", "client"};
OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false);
TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false);
new CommandLine(cmd).parseArgs(args);
cmd.execute();

Expand All @@ -72,7 +72,7 @@ public void testClientGeneration() throws IOException {
public void testServiceTypeGeneration() throws IOException {
Path petstoreYaml = resourceDir.resolve(Paths.get("petstore.yaml"));
String[] args = {"--input", petstoreYaml.toString(), "-o", this.tmpDir.toString(), "--with-service-type"};
OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false);
TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false);
new CommandLine(cmd).parseArgs(args);
cmd.execute();

Expand All @@ -99,7 +99,7 @@ public void testServiceGeneration() throws IOException {
Path petstoreYaml = resourceDir.resolve(Paths.get("petstore.yaml"));
String[] args = {"--input", petstoreYaml.toString(), "-o", this.tmpDir.toString(), "--with-service-type",
"--mode", "service"};
OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false);
TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false);
new CommandLine(cmd).parseArgs(args);
cmd.execute();

Expand All @@ -123,7 +123,7 @@ public void testServiceGeneration() throws IOException {
public void testBothClientServiceGeneration() throws IOException {
Path petstoreYaml = resourceDir.resolve(Paths.get("petstore.yaml"));
String[] args = {"--input", petstoreYaml.toString(), "-o", this.tmpDir.toString()};
OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false);
TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false);
new CommandLine(cmd).parseArgs(args);
cmd.execute();

Expand Down Expand Up @@ -151,7 +151,7 @@ public void testUserGivenLicenseHeader() throws IOException {
Path licenseHeader = resourceDir.resolve(Paths.get("expected_gen/licenses/license.txt"));
String[] args = {"--input", petstoreYaml.toString(), "-o", this.tmpDir.toString(), "--license",
licenseHeader.toString(), "--with-service-type"};
OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false);
TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false);
new CommandLine(cmd).parseArgs(args);
cmd.execute();
if (Files.exists(this.tmpDir.resolve("client.bal")) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setupBallerinaProject() throws IOException {
public void testBallerinaToOpenAPIGeneration() {
Path filePath = resourceDir.resolve(Paths.get("cmd/ballerina-to-openapi/ballerina-file.bal"));
String[] args = {"--input", filePath.toString(), "-o", this.tmpDir.toString()};
OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false);
TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false);
new CommandLine(cmd).parseArgs(args);

String output = "";
Expand All @@ -65,7 +65,7 @@ public void testBallerinaToOpenAPIGeneration() {
public void testDefaultMethod() {
Path filePath = resourceDir.resolve(Paths.get("cmd/ballerina-to-openapi/default_method.bal"));
String[] args = {"--input", filePath.toString(), "-o", this.tmpDir.toString()};
OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false);
TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false);
new CommandLine(cmd).parseArgs(args);

String output = "";
Expand All @@ -85,7 +85,7 @@ public void testDefaultMethod() {
public void testDefaultMethod02() {
Path filePath = resourceDir.resolve(Paths.get("cmd/ballerina-to-openapi/default_method_02.bal"));
String[] args = {"--input", filePath.toString(), "-o", this.tmpDir.toString()};
OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false);
TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false);
new CommandLine(cmd).parseArgs(args);

String output = "";
Expand All @@ -104,7 +104,7 @@ public void testDefaultMethod02() {
public void testHttpResponse() {
Path filePath = resourceDir.resolve(Paths.get("cmd/ballerina-to-openapi/http_response.bal"));
String[] args = {"--input", filePath.toString(), "-o", this.tmpDir.toString()};
OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false);
TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false);
new CommandLine(cmd).parseArgs(args);

String output = "";
Expand All @@ -128,7 +128,7 @@ public void testHttpResponse() {
public void testHttpRequest() {
Path filePath = resourceDir.resolve(Paths.get("cmd/ballerina-to-openapi/http_request.bal"));
String[] args = {"--input", filePath.toString(), "-o", this.tmpDir.toString()};
OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false);
TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false);
new CommandLine(cmd).parseArgs(args);

String output = "";
Expand All @@ -146,7 +146,7 @@ public void testHttpRequest() {
public void openapiAnnotationWithContract() {
Path filePath = resourceDir.resolve(Paths.get("cmd/ballerina-to-openapi/project_1/service.bal"));
String[] args = {"--input", filePath.toString(), "-o", this.tmpDir.toString()};
OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false);
TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false);
new CommandLine(cmd).parseArgs(args);

String output = "";
Expand All @@ -170,7 +170,7 @@ public void openapiAnnotationWithContract() {
public void openapiAnnotationWithOutContract() {
Path filePath = resourceDir.resolve(Paths.get("cmd/ballerina-to-openapi/project_2/service.bal"));
String[] args = {"--input", filePath.toString(), "-o", this.tmpDir.toString()};
OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false);
TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false);
new CommandLine(cmd).parseArgs(args);

String output = "";
Expand All @@ -193,7 +193,7 @@ public void openapiAnnotationWithOutContract() {
public void openapiAnnotationWithoutFields() {
Path filePath = resourceDir.resolve(Paths.get("cmd/ballerina-to-openapi/project_3/service.bal"));
String[] args = {"--input", filePath.toString(), "-o", this.tmpDir.toString()};
OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false);
TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false);
new CommandLine(cmd).parseArgs(args);

String output = "";
Expand Down
Loading
Loading