Skip to content

Commit

Permalink
Add service to initialize tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
KavinduZoysa committed Feb 26, 2025
1 parent 29767a0 commit 9a55ef6
Show file tree
Hide file tree
Showing 13 changed files with 289 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com)
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.flowmodelgenerator.extension;

import com.google.gson.Gson;
import io.ballerina.flowmodelgenerator.extension.request.CreateFilesRequest;
import io.ballerina.flowmodelgenerator.extension.response.CreateFilesResponse;
import org.ballerinalang.annotation.JavaSPIService;
import org.ballerinalang.langserver.commons.service.spi.ExtendedLanguageServerService;
import org.ballerinalang.langserver.commons.workspace.WorkspaceManager;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest;
import org.eclipse.lsp4j.jsonrpc.services.JsonSegment;
import org.eclipse.lsp4j.services.LanguageServer;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@JavaSPIService("org.ballerinalang.langserver.commons.service.spi.ExtendedLanguageServerService")
@JsonSegment("initializer")
public class InitializerService implements ExtendedLanguageServerService {

private static final String[] FILES = new String[]{"config.bal", "connections.bal", "data_mappings.bal",
"functions.bal", "agents.bal", "types.bal"};

@Override
public void init(LanguageServer langServer, WorkspaceManager workspaceManager) {
}

@JsonRequest
public CompletableFuture<CreateFilesResponse> createFiles(CreateFilesRequest request) {
return CompletableFuture.supplyAsync(() -> {
CreateFilesResponse response = new CreateFilesResponse();
try {
Path projectPath = Path.of(request.projectPath());
Map<Path, List<TextEdit>> textEditsMap = new HashMap<>();
List<String> createdFiles = new ArrayList<>();
for (String file : FILES) {
Path filePath = projectPath.resolve(file);
if (!Files.exists(filePath)) {
Files.createFile(filePath);
createdFiles.add(file);
}
}
response.setFiles(new Gson().toJsonTree(createdFiles).getAsJsonArray());
} catch (Throwable e) {
response.setError(e);
}
return response;
});
}

@Override
public Class<?> getRemoteInterface() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com)
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.flowmodelgenerator.extension.request;

/**
* A request to retrieve components of a module such as connections, types..
*
* @param projectPath path of the project
*/
public record CreateFilesRequest(String projectPath) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com)
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.flowmodelgenerator.extension.response;

import com.google.gson.JsonArray;

/**
* Represents the response for new files.
*
* @since 2.0.0
*/
public class CreateFilesResponse extends AbstractFlowModelResponse {

private JsonArray files;

public JsonArray getFiles() {
return files;
}

public void setFiles(JsonArray files) {
this.files = files;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ io.ballerina.flowmodelgenerator.extension.TypesManagerService
io.ballerina.flowmodelgenerator.extension.OpenAPIService
io.ballerina.flowmodelgenerator.extension.JsonToRecordConverterService
io.ballerina.flowmodelgenerator.extension.XMLToRecordConverterService
io.ballerina.flowmodelgenerator.extension.InitializerService
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com)
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.flowmodelgenerator.extension;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import io.ballerina.flowmodelgenerator.extension.request.CreateFilesRequest;
import io.ballerina.modelgenerator.commons.AbstractLSTest;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Tests for the flow model source generator service.
*
* @since 2.0.0
*/
public class InitializerTest extends AbstractLSTest {

@Override
@Test(dataProvider = "data-provider")
public void test(Path config) throws IOException {
Path configJsonPath = configDir.resolve(config);
TestConfig testConfig = gson.fromJson(Files.newBufferedReader(configJsonPath), TestConfig.class);

CreateFilesRequest request =
new CreateFilesRequest(sourceDir.resolve(testConfig.source()).toAbsolutePath().toString());
JsonArray filesCreated = getResponse(request).getAsJsonArray("files");

for (JsonElement file : filesCreated) {
Files.delete(sourceDir.resolve(testConfig.source()).resolve(file.getAsString()));
}

if (!filesCreated.equals(testConfig.files())) {
TestConfig updatedConfig =
new TestConfig(testConfig.source(), testConfig.description(), filesCreated);
updateConfig(configJsonPath, updatedConfig);
Assert.fail(String.format("Failed test: '%s' (%s)", testConfig.description(), configJsonPath));
}
}

@Override
protected String getResourceDir() {
return "initializer";
}

@Override
protected Class<? extends AbstractLSTest> clazz() {
return InitializerTest.class;
}

@Override
protected String getApiName() {
return "createFiles";
}

@Override
protected String getServiceName() {
return "initializer";
}

/**
* Represents the test configuration for the source generator test.
*
* @param source The source file name
* @param description The description of the test
* @param files The file names that are created
*/
private record TestConfig(String source, String description, JsonArray files) {

public String description() {
return description == null ? "" : description;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"source": "proj1",
"description": "Sample diagram node",
"files": [
"config.bal",
"connections.bal",
"data_mappings.bal",
"functions.bal",
"agents.bal",
"types.bal"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"source": "proj2",
"description": "Sample diagram node",
"files": [
"config.bal",
"connections.bal",
"data_mappings.bal",
"functions.bal",
"types.bal"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
org = "org"
name = "proj1"
version = "0.1.0"
distribution = "2201.11.0"

[build-options]
observabilityIncluded = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ballerina/io;

public function main() {
io:println("Hello, World!");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
org = "org"
name = "proj2"
version = "0.1.0"
distribution = "2201.11.0"

[build-options]
observabilityIncluded = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ballerina/io;

public function main() {
io:println("Hello, World!");
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ under the License.
<class name="io.ballerina.flowmodelgenerator.extension.typesmanager.GetGraphqlTypeTest"/>
<class name="io.ballerina.flowmodelgenerator.extension.typesmanager.CreateGraphqlClassTypeTest"/>
<class name="io.ballerina.flowmodelgenerator.extension.typesmanager.RecordConfigTest"/>
<class name="io.ballerina.flowmodelgenerator.extension.InitializerTest"/>
</classes>
</test>
</suite>

0 comments on commit 9a55ef6

Please sign in to comment.