diff --git a/.github/workflows/cli-central-publish.yml b/.github/workflows/cli-central-publish.yml new file mode 100644 index 000000000..5432d29cb --- /dev/null +++ b/.github/workflows/cli-central-publish.yml @@ -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 diff --git a/openapi-cli/build.gradle b/openapi-cli/build.gradle index d9660555c..f7b46ef56 100644 --- a/openapi-cli/build.gradle +++ b/openapi-cli/build.gradle @@ -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 { diff --git a/openapi-cli/src/main/java/io/ballerina/openapi/cmd/OpenApiCmd.java b/openapi-cli/src/main/java/io/ballerina/openapi/cmd/TestOpenApiCmd.java similarity index 88% rename from openapi-cli/src/main/java/io/ballerina/openapi/cmd/OpenApiCmd.java rename to openapi-cli/src/main/java/io/ballerina/openapi/cmd/TestOpenApiCmd.java index dee9c84e9..f33ec085a 100644 --- a/openapi-cli/src/main/java/io/ballerina/openapi/cmd/OpenApiCmd.java +++ b/openapi-cli/src/main/java/io/ballerina/openapi/cmd/TestOpenApiCmd.java @@ -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; @@ -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; @@ -121,17 +122,17 @@ public class OpenApiCmd implements BLauncherCmd { @CommandLine.Parameters private List 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; @@ -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; } @@ -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; @@ -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 @@ -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(); diff --git a/openapi-cli/src/main/resources/META-INF/services/io.ballerina.cli.BLauncherCmd b/openapi-cli/src/main/resources/META-INF/services/io.ballerina.cli.BLauncherCmd index 0a90b2b88..07cc9f89d 100644 --- a/openapi-cli/src/main/resources/META-INF/services/io.ballerina.cli.BLauncherCmd +++ b/openapi-cli/src/main/resources/META-INF/services/io.ballerina.cli.BLauncherCmd @@ -1 +1 @@ -io.ballerina.openapi.cmd.OpenApiCmd \ No newline at end of file +io.ballerina.openapi.cmd.TestOpenApiCmd \ No newline at end of file diff --git a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/BallerinaCodeGeneratorLicenseTests.java b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/BallerinaCodeGeneratorLicenseTests.java index 79db8e848..1d40c7da9 100644 --- a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/BallerinaCodeGeneratorLicenseTests.java +++ b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/BallerinaCodeGeneratorLicenseTests.java @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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")) && diff --git a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/BallerinaToOASTests.java b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/BallerinaToOASTests.java index 561583348..9148db62d 100644 --- a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/BallerinaToOASTests.java +++ b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/BallerinaToOASTests.java @@ -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 = ""; @@ -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 = ""; @@ -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 = ""; @@ -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 = ""; @@ -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 = ""; @@ -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 = ""; @@ -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 = ""; @@ -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 = ""; diff --git a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICmdTest.java b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICmdTest.java index 728c4f8f4..ef6aaaf4a 100644 --- a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICmdTest.java +++ b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICmdTest.java @@ -52,7 +52,7 @@ public void setupBallerinaProject() throws IOException { @Test(description = "Test openapi command with help flag") public void testOpenAPICmdHelp() throws IOException { String[] args = {"-h"}; - OpenApiCmd openApiCommand = new OpenApiCmd(printStream, tmpDir, false); + TestOpenApiCmd openApiCommand = new TestOpenApiCmd(printStream, tmpDir, false); new CommandLine(openApiCommand).parseArgs(args); openApiCommand.execute(); String output = readOutput(true); @@ -63,7 +63,7 @@ public void testOpenAPICmdHelp() throws IOException { @Test(description = "Test openapi command with help flag") public void testOpenAPICmdHelpWithBalTools() throws IOException { String[] args = {"help", "openapi"}; - OpenApiCmd openApiCommand = new OpenApiCmd(printStream, tmpDir, false); + TestOpenApiCmd openApiCommand = new TestOpenApiCmd(printStream, tmpDir, false); new CommandLine(openApiCommand).parseArgs(args); openApiCommand.execute(); String output = readOutput(true); @@ -73,7 +73,7 @@ public void testOpenAPICmdHelpWithBalTools() throws IOException { @Test(description = "Test openapi command without help flag") public void testOpenAPICmdHelpWithoutFlag() throws IOException { - OpenApiCmd openApiCommand = new OpenApiCmd(printStream, tmpDir, false); + TestOpenApiCmd openApiCommand = new TestOpenApiCmd(printStream, tmpDir, false); new CommandLine(openApiCommand); openApiCommand.execute(); String output = readOutput(true); @@ -84,7 +84,7 @@ public void testOpenAPICmdHelpWithoutFlag() throws IOException { @Test(description = "Test openapi gen-service without openapi contract file") public void testWithoutOpenApiContract() throws IOException { String[] args = {"--input"}; - OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false); + TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false); new CommandLine(cmd).parseArgs(args); cmd.execute(); String output = readOutput(true); @@ -95,7 +95,7 @@ public void testWithoutOpenApiContract() throws IOException { public void testSuccessfulServiceGeneration() 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(); Path expectedSchemaFile = resourceDir.resolve(Paths.get("expected_gen", "petstore_schema.bal")); @@ -135,7 +135,7 @@ public void testSuccessfulServiceGeneration() throws IOException { public void testSuccessfulTypeBalGeneration() throws IOException { Path petstoreYaml = resourceDir.resolve(Paths.get("petstore_type.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(); Path expectedSchemaFile = resourceDir.resolve(Paths.get("expected_gen", "petstore_schema_type.bal")); @@ -174,7 +174,7 @@ public void testForRemovedUnusedConstraintImport() throws IOException { Path petstoreYaml = resourceDir.resolve(Paths.get("non_constraint_import.yaml")); String[] args = {"--input", petstoreYaml.toString(), "--mode", "client", "--tags", "pets", "-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(); Path expectedSchemaFile = resourceDir.resolve(Paths.get("expected_gen", @@ -209,7 +209,7 @@ public void testWithConstraintImport() throws IOException { Path petstoreYaml = resourceDir.resolve(Paths.get("constraint_import.yaml")); String[] args = {"--input", petstoreYaml.toString(), "--mode", "client", "--tags", "pets", "-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(); Path expectedSchemaFile = resourceDir.resolve(Paths.get("expected_gen", @@ -245,7 +245,7 @@ public void testGenerationWithLicenseHeaders() throws IOException { Path licenseHeader = resourceDir.resolve(Paths.get("license.txt")); String[] args = {"--input", petstoreYaml.toString(), "-o", this.tmpDir.toString(), "--license", licenseHeader.toString()}; - OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false); + TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false); new CommandLine(cmd).parseArgs(args); cmd.execute(); Path expectedSchemaFile = resourceDir.resolve(Paths.get("expected_gen", @@ -287,7 +287,7 @@ public void testGenerationWithLicenseHeadersWithOneNewLine() throws IOException Path licenseHeader = resourceDir.resolve(Paths.get("license_with_new_line.txt")); String[] args = {"--input", petstoreYaml.toString(), "-o", this.tmpDir.toString(), "--license", licenseHeader.toString()}; - OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false); + TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false); new CommandLine(cmd).parseArgs(args); cmd.execute(); Path expectedSchemaFile = resourceDir.resolve(Paths.get("expected_gen", @@ -328,7 +328,7 @@ public void testConnectorGenerationFilteringByTags() throws IOException { Path petstoreYaml = resourceDir.resolve(Paths.get("petstore_tags.yaml")); String[] args = {"--input", petstoreYaml.toString(), "-o", this.tmpDir.toString(), "--tags", "pets,dogs", "--mode", "client", "--client-methods", "remote"}; - OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false); + TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false); new CommandLine(cmd).parseArgs(args); cmd.execute(); Path expectedClientFile = resourceDir.resolve(Paths.get("expected_gen", @@ -370,7 +370,7 @@ public void testGenerationOfTestSuiteWithLicenseHeaders() throws IOException { Path licenseHeader = resourceDir.resolve(Paths.get("license.txt")); String[] args = {"--input", petstoreYaml.toString(), "-o", this.tmpDir.toString(), "--license", licenseHeader.toString(), "--with-tests"}; - OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false); + TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false); new CommandLine(cmd).parseArgs(args); cmd.execute(); Path expectedConfigFilePath = resourceDir.resolve(Paths.get("expected_gen", @@ -414,7 +414,7 @@ public void testInvalidPrefixFile() throws IOException, BallerinaOpenApiExceptio Path licenseHeader = resourceDir.resolve(Paths.get("licence.txt")); String[] args = {"--input", petstoreYaml.toString(), "-o", this.tmpDir.toString(), "--license", licenseHeader.toString()}; - OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false); + TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false); new CommandLine(cmd).parseArgs(args); cmd.execute(); String output = readOutput(true); @@ -427,7 +427,7 @@ public void testClientGenerationWithoutIncludeTestFilesOption() throws IOExcepti Path licenseHeader = resourceDir.resolve(Paths.get("license.txt")); String[] args = {"--input", petstoreYaml.toString(), "-o", this.tmpDir.toString(), "--license", licenseHeader.toString()}; - OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false); + TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false); new CommandLine(cmd).parseArgs(args); cmd.execute(); Path expectedConfigFilePath = resourceDir.resolve(Paths.get("expected_gen", @@ -455,7 +455,7 @@ public void testClientGenerationWithoutIncludeTestFilesOption() throws IOExcepti public void testSuccessfulServiceGenerationForYML() throws IOException { Path petstoreYaml = resourceDir.resolve(Paths.get("petstore.yml")); 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); try { cmd.execute(); @@ -498,7 +498,7 @@ public void testServiceTypeGeneration() throws IOException { Path projectDir = resourceDir.resolve("expected_gen").resolve("ballerina_project"); Path petstoreYaml = resourceDir.resolve(Paths.get("petstore.yaml")); String[] args = {"--input", petstoreYaml.toString(), "-o", projectDir.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(); Path expectedServiceTypeFile = resourceDir.resolve(Paths.get("expected_gen", "pestore_service_type.bal")); @@ -533,7 +533,7 @@ public void testServiceGenerationWithoutDataBinding() throws IOException { Path projectDir = resourceDir.resolve("expected_gen").resolve("ballerina_project"); Path petstoreYaml = resourceDir.resolve(Paths.get("petstore.yaml")); String[] args = {"--input", petstoreYaml.toString(), "-o", projectDir.toString(), "--without-data-binding"}; - OpenApiCmd cmd = new OpenApiCmd(printStream, tmpDir, false); + TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, tmpDir, false); new CommandLine(cmd).parseArgs(args); cmd.execute(); Path expectedService = resourceDir.resolve(Paths.get("expected_gen", "generic_service_petstore.bal")); @@ -577,7 +577,7 @@ public void testServiceGenerationWithoutDataBinding() throws IOException { public void testForYamlContractWithoutOperationID() throws IOException { Path yamlContract = resourceDir.resolve(Paths.get("without_operationID.yaml")); String[] args = {"--input", yamlContract.toString(), "-o", this.tmpDir.toString(), "--mode", "service"}; - 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("without_operationid_service.bal"))) { @@ -634,7 +634,7 @@ private void deleteGeneratedFiles(boolean isConfigGenerated, Path folderPath, bo @Test(description = "getRelative path") public void getRelativePath() { - OpenApiCmd cmd = new OpenApiCmd(); + TestOpenApiCmd cmd = new TestOpenApiCmd(); File resource01 = new File("dir1/test.txt"); String target01 = "dir1/dir2"; File resource02 = new File("dir1/dir2/dir3/test.txt"); diff --git a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICommandTest.java b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICommandTest.java index 35410dfbb..f4ce4f086 100644 --- a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICommandTest.java +++ b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICommandTest.java @@ -77,7 +77,7 @@ public Path getExecuteCommand(String yamlFile, String serviceName) { Path yamlPath = resourceDir.resolve(Paths.get(yamlFile)); String[] args = {"--input", yamlPath.toString(), "--service-name", serviceName, "-o", this.tmpDir.toString(), "--mode", "service"}; - OpenApiCmd cmd = new OpenApiCmd(printStream, this.tmpDir); + TestOpenApiCmd cmd = new TestOpenApiCmd(printStream, this.tmpDir); new CommandLine(cmd).parseArgs(args); String output = ""; try { diff --git a/openapi-cli/src/test/resources/expected_gen/ballerina_project/service_type.bal b/openapi-cli/src/test/resources/expected_gen/ballerina_project/service_type.bal deleted file mode 100644 index e69de29bb..000000000 diff --git a/openapi-integration-tests/src/test/java/io/ballerina/openapi/TestUtil.java b/openapi-integration-tests/src/test/java/io/ballerina/openapi/TestUtil.java index e19590432..af3dc0b31 100644 --- a/openapi-integration-tests/src/test/java/io/ballerina/openapi/TestUtil.java +++ b/openapi-integration-tests/src/test/java/io/ballerina/openapi/TestUtil.java @@ -119,7 +119,7 @@ public static Process executeRun(String distributionName, Path sourceDirectory, */ public static boolean executeOpenAPI(String distributionName, Path sourceDirectory, List args) throws IOException, InterruptedException { - args.add(0, "openapi"); + args.add(0, "test_openapi"); Process process = getProcessBuilderResults(distributionName, sourceDirectory, args); int exitCode = process.waitFor(); logOutput(process.getInputStream()); diff --git a/settings.gradle b/settings.gradle index 6369d1ee0..a572b8068 100644 --- a/settings.gradle +++ b/settings.gradle @@ -28,6 +28,7 @@ include(':openapi-validator') include(':openapi-extension') include(':openapi-extension-tests') include(':openapi-integration-tests') +include(':tool-openapi') //include(':openapi-tests') gradleEnterprise { diff --git a/tool-openapi/.devcontainer.json b/tool-openapi/.devcontainer.json new file mode 100644 index 000000000..86079a308 --- /dev/null +++ b/tool-openapi/.devcontainer.json @@ -0,0 +1,4 @@ +{ + "image": "ballerina/ballerina-devcontainer:2201.6.0", + "extensions": ["WSO2.ballerina"], +} diff --git a/tool-openapi/.gitignore b/tool-openapi/.gitignore new file mode 100644 index 000000000..7512ebe23 --- /dev/null +++ b/tool-openapi/.gitignore @@ -0,0 +1,3 @@ +target +generated +Config.toml diff --git a/tool-openapi/BalTool.toml b/tool-openapi/BalTool.toml new file mode 100644 index 000000000..a37e0f384 --- /dev/null +++ b/tool-openapi/BalTool.toml @@ -0,0 +1,8 @@ +[tool] +id = "test_openapi" + +[[dependency]] +path = "resources/openapi-cli-1.7.0-SNAPSHOT.jar" + +[[dependency]] +path = "resources/libs/*" \ No newline at end of file diff --git a/tool-openapi/Ballerina.toml b/tool-openapi/Ballerina.toml new file mode 100644 index 000000000..9c7252471 --- /dev/null +++ b/tool-openapi/Ballerina.toml @@ -0,0 +1,5 @@ +[package] +org = "aneeshafernando" +name = "test_tool_1" +version = "0.6.0" +distribution = "2201.7.0" diff --git a/tool-openapi/Module.md b/tool-openapi/Module.md new file mode 100644 index 000000000..8a69f5193 --- /dev/null +++ b/tool-openapi/Module.md @@ -0,0 +1,6 @@ +Prints "Hello, World!" with a main function. +[//]: # (above is the module summary) + +# Module Overview +Provides an overview about the module when generating the API documentations. +For example, refer to https://lib.ballerina.io/ballerina/io/latest diff --git a/tool-openapi/Package.md b/tool-openapi/Package.md new file mode 100644 index 000000000..fc8b1b67c --- /dev/null +++ b/tool-openapi/Package.md @@ -0,0 +1,5 @@ +Prints "Hello, World!" with a hello function. +[//]: # (above is the package summary) + +# Package Overview +Prints "Hello, World!" as the output to the command line using a hello function. diff --git a/tool-openapi/build.gradle b/tool-openapi/build.gradle new file mode 100644 index 000000000..c42cd33fb --- /dev/null +++ b/tool-openapi/build.gradle @@ -0,0 +1,93 @@ +import org.apache.tools.ant.taskdefs.condition.Os + +apply from: "$rootDir/gradle/javaProject.gradle" +apply plugin: "java" + +description = "Publish OpenAPI CLI Tools to Central" + +configurations.all { + resolutionStrategy.preferProjectModules() +} + +configurations { + balTools +} + +def targetOpenApiCliJar = file("$project.rootDir/openapi-cli/build/libs/openapi-cli-${project.version}.jar") +def distributionPath = project.buildDir.absolutePath + "/extracted-distribution/jballerina-tools-${ballerinaLangVersion}" +def distributionBinPath = distributionPath + "/bin" + +def needPublishToCentral = false +def needPublishToLocalCentral = false + +task initializeVariables { + if (project.hasProperty("publishToLocalCentral") && (project.findProperty("publishToLocalCentral") == "true")) { + needPublishToLocalCentral = true + } + if (project.hasProperty("publishToCentral") && (project.findProperty("publishToCentral") == "true")) { + needPublishToCentral = true + } +} + + +task copyExtractedDistribution(type: Copy) { + dependsOn project(":openapi-cli").copyStdlibs + from "${project.rootDir}/openapi-cli/build/extracted-distribution" + into "${project.buildDir.absolutePath}/extracted-distribution" +} + +task copyCLIJar(type: Copy) { + dependsOn project(":openapi-cli").build + from targetOpenApiCliJar + into "${project.projectDir}/resources" +} + +task publishToCentral { + dependsOn(copyExtractedDistribution) + dependsOn(copyCLIJar) + doLast { + exec { + workingDir "${project.projectDir}" + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + commandLine 'cmd', '/c', "${distributionBinPath}/bal.bat pack && exit %%ERRORLEVEL%%" + } else { + commandLine 'sh', '-c', "${distributionBinPath}/bal pack" + } + } + if (needPublishToCentral) { + if (project.version.endsWith('-SNAPSHOT') || + project.version.matches(project.ext.timestampedVersionRegex)) { + return + } + if (ballerinaCentralAccessToken != null) { + println("Publishing to the ballerina central..") + exec { + workingDir project.projectDir +// environment "JAVA_OPTS", "-DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true" + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + commandLine 'cmd', '/c', "$distributionBinPath/bal.bat push && exit %%ERRORLEVEL%%" + } else { + commandLine 'sh', '-c', "$distributionBinPath/bal push" + } + } + } else { + throw new InvalidUserDataException("Central Access Token is not present") + } + } else if (needPublishToLocalCentral) { + println("Publishing to the ballerina local central repository..") + exec { + workingDir project.projectDir +// environment "JAVA_OPTS", "-DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true" + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + commandLine 'cmd', '/c', "$distributionBinPath/bal.bat push && exit %%ERRORLEVEL%% --repository=local" + } else { + commandLine 'sh', '-c', "$distributionBinPath/bal push --repository=local" + } + } + } + } +} + +//publishToCentral.dependsOn ":openapi-cli:build" + +// Add any additional tasks or configurations you need \ No newline at end of file diff --git a/tool-openapi/tool_openapi.bal b/tool-openapi/tool_openapi.bal new file mode 100644 index 000000000..7a151019e --- /dev/null +++ b/tool-openapi/tool_openapi.bal @@ -0,0 +1,10 @@ +# Returns the string `Hello` with the input string name. +# +# + name - name as a string +# + return - "Hello, " with the input string name +public function hello(string name) returns string { + if !(name is "") { + return "Hello, " + name; + } + return "Hello, World!"; +} \ No newline at end of file