Skip to content

Commit

Permalink
Merge pull request #42203 from ShammiL/tool-task-commands
Browse files Browse the repository at this point in the history
Add build tool support to bal run, pack, test, graph, doc and profile commands
  • Loading branch information
ShammiL authored Feb 29, 2024
2 parents 9732ac9 + 6045e91 commit a949e6d
Show file tree
Hide file tree
Showing 37 changed files with 409 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ public void execute() {
TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder()
// clean the target directory(projects only)
.addTask(new CleanTargetDirTask(isPackageModified, buildOptions.enableCache()), isSingleFileBuild)
// Run build tools
.addTask(new RunBallerinaPreBuildToolsTask(outStream))
// resolve maven dependencies in Ballerina.toml
.addTask(new ResolveMavenDependenciesTask(outStream))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.ballerina.cli.task.CreateDocsTask;
import io.ballerina.cli.task.CreateTargetDirTask;
import io.ballerina.cli.task.ResolveMavenDependenciesTask;
import io.ballerina.cli.task.RunBallerinaPreBuildToolsTask;
import io.ballerina.projects.BuildOptions;
import io.ballerina.projects.Project;
import io.ballerina.projects.ProjectEnvironmentBuilder;
Expand Down Expand Up @@ -181,6 +182,7 @@ public void execute() {

TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder()
.addTask(new CreateTargetDirTask()) // create target directory.
.addTask(new RunBallerinaPreBuildToolsTask(outStream)) // run build tools
.addTask(new ResolveMavenDependenciesTask(outStream)) // resolve maven dependencies in Ballerina.toml
.addTask(new CompileTask(outStream, errStream)) // compile the modules
.addTask(new CreateDocsTask(outStream, outputPath)) // creates API documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.ballerina.cli.task.CleanTargetDirTask;
import io.ballerina.cli.task.CreateDependencyGraphTask;
import io.ballerina.cli.task.ResolveMavenDependenciesTask;
import io.ballerina.cli.task.RunBallerinaPreBuildToolsTask;
import io.ballerina.cli.utils.FileUtils;
import io.ballerina.projects.BuildOptions;
import io.ballerina.projects.Project;
Expand Down Expand Up @@ -109,6 +110,7 @@ public void execute() {

TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder()
.addTask(new CleanTargetDirTask(true, false), isSingleFileProject())
.addTask(new RunBallerinaPreBuildToolsTask(outStream))
.addTask(new ResolveMavenDependenciesTask(outStream))
.addTask(new CreateDependencyGraphTask(outStream, errStream))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.ballerina.cli.task.CreateBalaTask;
import io.ballerina.cli.task.DumpBuildTimeTask;
import io.ballerina.cli.task.ResolveMavenDependenciesTask;
import io.ballerina.cli.task.RunBallerinaPreBuildToolsTask;
import io.ballerina.cli.utils.BuildTime;
import io.ballerina.cli.utils.FileUtils;
import io.ballerina.projects.BuildOptions;
Expand Down Expand Up @@ -247,6 +248,7 @@ public void execute() {

TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder()
.addTask(new CleanTargetDirTask(isPackageModified, buildOptions.enableCache()), isSingleFileBuild)
.addTask(new RunBallerinaPreBuildToolsTask(outStream))
.addTask(new ResolveMavenDependenciesTask(outStream))
.addTask(new CompileTask(outStream, errStream, true, isPackageModified, buildOptions.enableCache()))
.addTask(new CreateBalaTask(outStream))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.ballerina.cli.task.CreateExecutableTask;
import io.ballerina.cli.task.DumpBuildTimeTask;
import io.ballerina.cli.task.ResolveMavenDependenciesTask;
import io.ballerina.cli.task.RunBallerinaPreBuildToolsTask;
import io.ballerina.cli.task.RunProfilerTask;
import io.ballerina.cli.utils.FileUtils;
import io.ballerina.projects.BuildOptions;
Expand Down Expand Up @@ -194,6 +195,7 @@ private TaskExecutor createTaskExecutor(boolean isPackageModified, String[] args
boolean isSingleFileBuild) {
return new TaskExecutor.TaskBuilder()
.addTask(new CleanTargetDirTask(isPackageModified, buildOptions.enableCache()), isSingleFileBuild)
.addTask(new RunBallerinaPreBuildToolsTask(outStream))
.addTask(new ResolveMavenDependenciesTask(outStream))
.addTask(new CompileTask(outStream, errStream, false, isPackageModified,
buildOptions.enableCache()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.ballerina.cli.task.CompileTask;
import io.ballerina.cli.task.DumpBuildTimeTask;
import io.ballerina.cli.task.ResolveMavenDependenciesTask;
import io.ballerina.cli.task.RunBallerinaPreBuildToolsTask;
import io.ballerina.cli.task.RunExecutableTask;
import io.ballerina.cli.utils.BuildTime;
import io.ballerina.cli.utils.FileUtils;
Expand Down Expand Up @@ -233,6 +234,8 @@ public void execute() {
TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder()
// clean target dir for projects
.addTask(new CleanTargetDirTask(isPackageModified, buildOptions.enableCache()), isSingleFileBuild)
// Run build tools
.addTask(new RunBallerinaPreBuildToolsTask(outStream))
// resolve maven dependencies in Ballerina.toml
.addTask(new ResolveMavenDependenciesTask(outStream))
// compile the modules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.ballerina.cli.task.CompileTask;
import io.ballerina.cli.task.DumpBuildTimeTask;
import io.ballerina.cli.task.ResolveMavenDependenciesTask;
import io.ballerina.cli.task.RunBallerinaPreBuildToolsTask;
import io.ballerina.cli.task.RunNativeImageTestTask;
import io.ballerina.cli.task.RunTestsTask;
import io.ballerina.cli.utils.BuildTime;
Expand Down Expand Up @@ -341,6 +342,7 @@ public void execute() {

TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder()
.addTask(new CleanTargetCacheDirTask(), isSingleFile) // clean the target cache dir(projects only)
.addTask(new RunBallerinaPreBuildToolsTask(outStream)) // run build tools
.addTask(new ResolveMavenDependenciesTask(outStream)) // resolve maven dependencies in Ballerina.toml
// compile the modules
.addTask(new CompileTask(outStream, errStream, false, isPackageModified, buildOptions.enableCache()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.nio.file.Paths;
import java.util.Objects;

import static io.ballerina.cli.cmd.CommandOutputUtils.getOutput;

/**
* Doc command tests.
*
Expand Down Expand Up @@ -81,4 +83,22 @@ public void testDocCommandWithCustomTarget() throws IOException {
Files.delete(customTargetPath.resolve("apidocs").resolve("foo").resolve("winery").resolve("0.1.0")
.resolve("index.html"));
}

@Test(description = "Test doc command on a ballerina project with build tool execution.")
public void testDocCommandWithBuildTool() throws IOException {
Path projectPath = this.testResources.resolve("doc_project_with_build_tool");
System.setProperty("user.dir", projectPath.toString());
DocCommand docCommand = new DocCommand(this.printStream, this.printStream, false);
docCommand.execute();

String buildLog = readOutput(true);
Assert.assertEquals(buildLog.replaceAll("\r", ""),
getOutput("doc-with-build-tool.txt"));

Assert.assertTrue(Files.exists(this.testResources.resolve("doc_project_with_build_tool").resolve("target")
.resolve("apidocs").resolve("foo").resolve("winery").resolve("0.1.0").resolve("index.html")));

Files.delete(this.testResources.resolve("doc_project_with_build_tool").resolve("target")
.resolve("apidocs").resolve("foo").resolve("winery").resolve("0.1.0").resolve("index.html"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@
*/
public class GraphCommandTest extends BaseCommandTest {
private Path testResources;
private Path projectsWithDependencyConflicts;

@BeforeClass
public void setup() throws IOException {
super.setup();
this.testResources = super.tmpDir.resolve("build-test-resources");
projectsWithDependencyConflicts = this.testResources.resolve("projectsWithDependencyConflicts")
Path projectsWithDependencyConflicts = this.testResources.resolve("projectsWithDependencyConflicts")
.resolve("package_p");
try {
copyTestResourcesToTmpDir();
Expand Down Expand Up @@ -105,6 +104,19 @@ public void testPrintGraphForBalProjectWithNoDependencies() throws IOException {
Assert.assertEquals(actualLog, expectedLog);
}

@Test(description = "Print the dependency graph of a ballerina project with build tools")
public void testPrintGraphForBalProjectWithBuildTools() throws IOException {
Path balProjectPath = this.testResources.resolve("proper-build-tool");

GraphCommand graphCommand = new GraphCommand(balProjectPath, printStream, printStream, false);
new CommandLine(graphCommand).parseArgs(balProjectPath.toString());
graphCommand.execute();

String actualLog = readFormattedOutput();
String expectedLog = CommandOutputUtils.getOutput("graph-with-build-tool.txt");
Assert.assertEquals(actualLog, expectedLog);
}

@Test(description = "Print the dependency graph of a valid ballerina project with dependencies")
public void testPrintGraphForBalProjectWithDependencies() throws IOException {
Path balProjectPath = this.testResources.resolve("projectsForDumpGraph").resolve("package_a");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static io.ballerina.cli.cmd.CommandOutputUtils.replaceDependenciesTomlContent;
import static io.ballerina.projects.util.ProjectConstants.DEPENDENCIES_TOML;
import static io.ballerina.projects.util.ProjectConstants.RESOURCE_DIR_NAME;
import static io.ballerina.projects.util.ProjectConstants.USER_DIR_PROPERTY;

/**
* Pack command tests.
Expand Down Expand Up @@ -251,6 +252,20 @@ public void testPackageWithTestOnlyJavaImports() throws IOException {
.toFile().exists());
}

@Test(description = "Pack a project with a build tool execution")
public void testPackProjectWithBuildTool() throws IOException {
Path projectPath = this.testResources.resolve("proper-build-tool");
System.setProperty(USER_DIR_PROPERTY, projectPath.toString());
PackCommand packCommand = new PackCommand(projectPath, printStream, printStream, false, true);
new CommandLine(packCommand).parseArgs();
packCommand.execute();
String buildLog = readOutput(true);
Assert.assertEquals(buildLog.replaceAll("\r", ""),
getOutput("pack-project-with-build-tool.txt"));
Assert.assertTrue(projectPath.resolve("target").resolve("bala").resolve("foo-winery-any-0.1.0.bala")
.toFile().exists());
}

@Test(description = "Pack a package with an empty Dependencies.toml")
public void testPackageWithEmptyDependenciesToml() throws IOException {
Path projectPath = this.testResources.resolve("validProjectWithDependenciesToml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ public void testRunBalProjectWithProfileFlag() throws IOException {
ProjectUtils.deleteDirectory(projectPath.resolve("target"));
}

@Test(description = "Profile a ballerina project with build tools")
public void testRunBalProjectWithProfileFlagWithBuildTools() throws IOException {
Path projectPath = this.testResources.resolve("projectForProfile").resolve("package_b");
System.setProperty("user.dir", projectPath.toString());

java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
System.setOut(new java.io.PrintStream(out));

ProfileCommand profileCommand = new ProfileCommand(projectPath, printStream, false);
profileCommand.execute();
String buildLog = readOutput(true).replaceAll("\r", "").strip();
Assert.assertEquals(buildLog, getOutput("profile-project-with-build-tool.txt"));
Path htmlPath = projectPath.resolve("target").resolve("profiler").resolve("ProfilerReport.html");
Assert.assertTrue(htmlPath.toFile().exists());
try {
String htmlContent = Files.readString(htmlPath);
Assert.assertTrue(htmlContent.contains("foo/package_b/0/main.main"));
Assert.assertTrue(htmlContent.contains("foo/package_b/0/$_init.$moduleInit"));
} catch (IOException e) {
Assert.fail("Error reading html file");
}
ProjectUtils.deleteDirectory(projectPath.resolve("target"));
}

@Test(description = "Test profile command with help")
public void testProfileCommandAndHelp() throws IOException {
String[] args = {"--help"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static io.ballerina.cli.cmd.CommandOutputUtils.getOutput;
import static io.ballerina.projects.util.ProjectConstants.DIST_CACHE_DIRECTORY;
import static io.ballerina.projects.util.ProjectConstants.USER_DIR_PROPERTY;

/**
* Run command tests.
Expand Down Expand Up @@ -167,6 +168,18 @@ public void testRunValidBalProjectFromProjectDir() throws IOException {
Files.delete(tempFile);
}

@Test(description = "Run a project with a build tool execution")
public void testRunProjectWithBuildTool() throws IOException {
Path projectPath = this.testResources.resolve("proper-build-tool");
System.setProperty(USER_DIR_PROPERTY, projectPath.toString());
RunCommand runCommand = new RunCommand(projectPath, printStream, false);
new CommandLine(runCommand).parseArgs();
runCommand.execute();
String buildLog = readOutput(true);
Assert.assertEquals(buildLog.replaceAll("\r", ""),
getOutput("run-project-with-build-tool.txt"));
}

@Test(description = "Run a valid ballerina project with invalid argument")
public void testRunCommandWithInvalidArg() {
Path projectPath = this.testResources.resolve("validRunProject");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import static io.ballerina.projects.util.ProjectConstants.DIST_CACHE_DIRECTORY;
import static io.ballerina.projects.util.ProjectConstants.RESOURCE_DIR_NAME;
import static io.ballerina.projects.util.ProjectConstants.TARGET_DIR_NAME;
import static io.ballerina.projects.util.ProjectConstants.USER_DIR_PROPERTY;

/**
* Test command tests.
Expand Down Expand Up @@ -184,6 +185,18 @@ public void testTestBalProjectFromADifferentDirectory() throws IOException {
Assert.assertEquals(buildLog.replaceAll("\r", ""), getOutput("test-project.txt"));
}

@Test(description = "Test a project with a build tool execution")
public void testTestProjectWithBuildTool() throws IOException {
Path projectPath = this.testResources.resolve("proper-build-tool-with-tests");
System.setProperty(USER_DIR_PROPERTY, projectPath.toString());
TestCommand testCommand = new TestCommand(projectPath, printStream, printStream, false);
new CommandLine(testCommand).parseArgs();
testCommand.execute();
String buildLog = readOutput(true);
Assert.assertEquals(buildLog.replaceAll("\r", ""),
getOutput("test-project-with-build-tool.txt"));
}

@Test(description = "Test the heap dump generation for a project with an OOM error")
public void testHeapDumpGenerationForOOM() {
Path projectPath = this.testResources.resolve("oom-project");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

Executing Build Tools
openapi(generate-delivery-client)

Compiling source
foo/winery:0.1.0
Generating API Documentation
Saved to: apidocs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Executing Build Tools
openapi(generate-delivery-client)


digraph "foo/winery:0.1.0" {
node [shape=record]
"foo/winery" [label="<0.1.0> foo/winery:0.1.0"];

// Edges

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

Executing Build Tools
openapi(generate-delivery-client)

Compiling source
foo/winery:0.1.0

Creating bala
target/bala/foo-winery-any-0.1.0.bala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Executing Build Tools
openapi(generate-delivery-client)

Compiling source
foo/package_b:0.1.0

Generating executable
target/bin/package_b.jar
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

Executing Build Tools
openapi(generate-delivery-client)

Compiling source
foo/winery:0.1.0

Running executable

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

Executing Build Tools
openapi(generate-delivery-client)

Compiling source
foo/winery:0.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

Executing Build Tools
openapi(generate-delivery-client)

Compiling source
foo/winery:0.1.0
Generating API Documentation
Saved to: apidocs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Executing Build Tools
openapi(generate-delivery-client)


digraph "foo/winery:0.1.0" {
node [shape=record]
"foo/winery" [label="<0.1.0> foo/winery:0.1.0"];

// Edges

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

Executing Build Tools
openapi(generate-delivery-client)

Compiling source
foo/winery:0.1.0

Creating bala
target/bala/foo-winery-any-0.1.0.bala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Executing Build Tools
openapi(generate-delivery-client)

Compiling source
foo/package_b:0.1.0

Generating executable
target/bin/package_b.jar
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

Executing Build Tools
openapi(generate-delivery-client)

Compiling source
foo/winery:0.1.0

Running executable

Loading

0 comments on commit a949e6d

Please sign in to comment.