-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29767a0
commit 9a55ef6
Showing
13 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
...extension/src/main/java/io/ballerina/flowmodelgenerator/extension/InitializerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...n/src/main/java/io/ballerina/flowmodelgenerator/extension/request/CreateFilesRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
39 changes: 39 additions & 0 deletions
39
...src/main/java/io/ballerina/flowmodelgenerator/extension/response/CreateFilesResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...ls-extension/src/test/java/io/ballerina/flowmodelgenerator/extension/InitializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ules/flow-model-generator-ls-extension/src/test/resources/initializer/config/config1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
11 changes: 11 additions & 0 deletions
11
...ules/flow-model-generator-ls-extension/src/test/resources/initializer/config/config2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
...w-model-generator-ls-extension/src/test/resources/initializer/source/proj1/Ballerina.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 5 additions & 0 deletions
5
...es/flow-model-generator-ls-extension/src/test/resources/initializer/source/proj1/main.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import ballerina/io; | ||
|
||
public function main() { | ||
io:println("Hello, World!"); | ||
} |
8 changes: 8 additions & 0 deletions
8
...w-model-generator-ls-extension/src/test/resources/initializer/source/proj2/Ballerina.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Empty file.
5 changes: 5 additions & 0 deletions
5
...es/flow-model-generator-ls-extension/src/test/resources/initializer/source/proj2/main.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import ballerina/io; | ||
|
||
public function main() { | ||
io:println("Hello, World!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters