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

feat: Add cli command to generate cfast representation for cobol prog… #2377

Merged
merged 1 commit into from
Jul 10, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@
/**
* The Cli class represents a Command Line Interface (CLI) for interacting with the application.
*/
@CommandLine.Command(description = "COBOL Analysis CLI tools.", mixinStandardHelpOptions = true, scope = CommandLine.ScopeType.INHERIT, subcommands = {ListSources.class, ListCopybooks.class, CliAnalysis.class})
@CommandLine.Command(description = "COBOL Analysis CLI tools.", mixinStandardHelpOptions = true, scope = CommandLine.ScopeType.INHERIT,
subcommands = {
ListSources.class,
ListCopybooks.class,
CliAnalysis.class,
CliCFAST.class
})
@Slf4j
public class Cli implements Callable<Integer> {
ProcessorGroupsResolver processorGroupsResolver;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*
*/
package org.eclipse.lsp.cobol.cli.command;

import com.google.common.io.Files;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.eclipse.lsp.cobol.cfg.CFASTBuilder;
import org.eclipse.lsp.cobol.cli.di.CliModule;
import org.eclipse.lsp.cobol.common.dialects.CobolLanguageId;
import org.eclipse.lsp.cobol.common.model.tree.Node;
import org.eclipse.lsp.cobol.common.pipeline.StageResult;
import org.eclipse.lsp.cobol.dialects.ibm.ProcessingResult;
import picocli.CommandLine;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.Callable;

/**
* Generates CFAST representation of the COBOL program in the defined folder
*/
@CommandLine.Command(name = "cfast", description = "generate cfast from cobol source")
public class CliCFAST implements Callable<Integer> {

@CommandLine.ParentCommand
private Cli parent;

@CommandLine.Option(
description = "Path to the source folder.",
names = {"-sf", "--source_folder"})
private Path workspace;

@Override
public Integer call() throws Exception {
try {
if (Objects.nonNull(workspace)) {
Injector diCtx = Guice.createInjector(new CliModule());
CFASTBuilder builder = diCtx.getInstance(CFASTBuilder.class);

File[] paths = workspace.toFile().listFiles();
if (paths == null) {
throw new Exception("Cannot find folder: " + workspace.toFile().getAbsolutePath());
}

Gson gson = new GsonBuilder().setPrettyPrinting().create();

Arrays.stream(paths)
.filter(CliCFAST::isCobolFile)
.forEach(file -> generateCFAST(file, builder, gson, diCtx));
}
} catch (Exception e) {
System.out.println("Failed to generate CFAST: " + e.getMessage());
return 1;
}

return 0;
}

private void generateCFAST(File file, CFASTBuilder builder, Gson gson, Injector diCtx) {
try {
Cli.Result analysisResult = parent.runAnalysis(file.getCanonicalFile(), CobolLanguageId.COBOL, diCtx, true);
StageResult<ProcessingResult> result = (StageResult<ProcessingResult>) analysisResult.pipelineResult.getLastStageResult();
Node rootNode = result.getData().getRootNode();

String json = gson.toJson(builder.build(rootNode).getControlFlowAST());

try (FileWriter writer = new FileWriter(getCFASTFileName(file.toPath()))) {
writer.write(json);
writer.flush();
}

} catch (IOException e) {
System.out.println("Error processing file: " + file.getAbsolutePath() + " \n" + e.getMessage());
}
}

private static boolean isCobolFile(File file) {
return "cbl".equals(Files.getFileExtension(file.getAbsolutePath()));
}

private static String getCFASTFileName(Path file) {
String fileName = file.toAbsolutePath().toString();
return fileName.substring(0, fileName.length() - ".cbl".length()) + ".cfast.json";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class CliTest {
+ " list_copybooks list copybooks"
+ System.lineSeparator()
+ " analysis analyse cobol source"
+ System.lineSeparator()
+ " cfast generate cfast from cobol source"
+ System.lineSeparator();

@Test
Expand Down Expand Up @@ -73,9 +75,10 @@ void testCli_srcNull() {
void testCliCommands() {
CommandLine commandLine = new CommandLine(new Cli());
Set<String> commandList = commandLine.getSubcommands().keySet();
assertEquals(3, commandList.size());
assertEquals(4, commandList.size());
assertTrue(commandList.contains("analysis"));
assertTrue(commandList.contains("list_copybooks"));
assertTrue(commandList.contains("list_sources"));
assertTrue(commandList.contains("cfast"));
}
}
Loading