Skip to content

Commit

Permalink
Merge pull request ballerina-platform#42730 from gayaldassanayake/all…
Browse files Browse the repository at this point in the history
…ow-empty-proj-for-tools-master

[master] Allow not having source code for projects with build tools
  • Loading branch information
gayaldassanayake authored May 15, 2024
2 parents abfcbf6 + bdf0210 commit 9c45823
Show file tree
Hide file tree
Showing 37 changed files with 446 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import io.ballerina.projects.directory.BuildProject;
import io.ballerina.projects.directory.SingleFileProject;
import io.ballerina.projects.util.ProjectConstants;
import io.ballerina.projects.util.ProjectUtils;
import org.ballerinalang.toml.exceptions.SettingsTomlException;
import org.wso2.ballerinalang.util.RepoUtils;
import picocli.CommandLine;
Expand Down Expand Up @@ -260,14 +259,6 @@ public void execute() {
}
}

// If project is empty
if (ProjectUtils.isProjectEmpty(project)) {
CommandUtil.printError(this.errStream, "package is empty. Please add at least one .bal file.", null,
false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}

// Validate Settings.toml file
try {
RepoUtils.readSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import io.ballerina.projects.directory.BuildProject;
import io.ballerina.projects.directory.SingleFileProject;
import io.ballerina.projects.util.ProjectConstants;
import io.ballerina.projects.util.ProjectUtils;
import org.ballerinalang.toml.exceptions.SettingsTomlException;
import org.wso2.ballerinalang.util.RepoUtils;
import picocli.CommandLine;
Expand Down Expand Up @@ -65,7 +64,7 @@ public class GraphCommand implements BLauncherCmd {
private boolean helpFlag;

@CommandLine.Option(names = {"--offline"}, description = "Print the dependency graph offline without downloading " +
"dependencies.", defaultValue = "false")
"dependencies.")
private boolean offline;

@CommandLine.Option(names = "--sticky", description = "stick to exact versions locked (if exists)",
Expand Down Expand Up @@ -100,14 +99,8 @@ public void execute() {
return;
}

if (ProjectUtils.isProjectEmpty(this.project)) {
printErrorAndExit("package is empty. Please add at least one .bal file.");
return;
}

validateSettingsToml();


TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder()
.addTask(new CleanTargetDirTask(true, false), isSingleFileProject())
.addTask(new RunBuildToolsTask(outStream), isSingleFileProject())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import io.ballerina.projects.internal.PackageDiagnostic;
import io.ballerina.projects.internal.ProjectDiagnosticErrorCode;
import io.ballerina.projects.util.ProjectConstants;
import io.ballerina.projects.util.ProjectUtils;
import io.ballerina.toml.semantic.TomlType;
import io.ballerina.toml.semantic.ast.TomlTableNode;
import io.ballerina.tools.diagnostics.DiagnosticInfo;
Expand Down Expand Up @@ -168,15 +167,6 @@ public void execute() {
return;
}

// If project is empty
if (ProjectUtils.isProjectEmpty(project) && project.currentPackage().compilerPluginToml().isEmpty() &&
project.currentPackage().balToolToml().isEmpty()) {
CommandUtil.printError(this.errStream, "package is empty. Please add at least one .bal file.", null,
false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}

// Check `[package]` section is available when compile
if (project.currentPackage().ballerinaToml().get().tomlDocument().toml().getTable("package")
.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import io.ballerina.projects.directory.BuildProject;
import io.ballerina.projects.directory.SingleFileProject;
import io.ballerina.projects.util.ProjectConstants;
import io.ballerina.projects.util.ProjectUtils;
import picocli.CommandLine;

import java.io.PrintStream;
Expand Down Expand Up @@ -122,12 +121,6 @@ public void execute() {
CommandUtil.exitError(this.exitWhenFinish);
return;
}
if (ProjectUtils.isProjectEmpty(project)) {
CommandUtil.printError(this.errStream, "package is empty. Please add at least one .bal file.",
null, false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
boolean isPackageModified = isProjectUpdated(project);
TaskExecutor taskExecutor = createTaskExecutor(isPackageModified, args, buildOptions,
project.kind() == ProjectKind.SINGLE_FILE_PROJECT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import io.ballerina.projects.directory.SingleFileProject;
import io.ballerina.projects.internal.model.Target;
import io.ballerina.projects.util.ProjectConstants;
import io.ballerina.projects.util.ProjectUtils;
import picocli.CommandLine;

import java.io.IOException;
Expand Down Expand Up @@ -238,12 +237,6 @@ public void execute() {
}
}

// If project is empty
if (ProjectUtils.isProjectEmpty(project)) {
CommandUtil.printError(this.errStream, "package is empty. Please add at least one .bal file.", null, false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
Target target;
try {
if (project.kind().equals(ProjectKind.BUILD_PROJECT)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import io.ballerina.projects.directory.BuildProject;
import io.ballerina.projects.directory.SingleFileProject;
import io.ballerina.projects.util.ProjectConstants;
import io.ballerina.projects.util.ProjectUtils;
import picocli.CommandLine;

import java.io.PrintStream;
Expand Down Expand Up @@ -289,13 +288,6 @@ public void execute() {
}
}

// If project is empty
if (ProjectUtils.isProjectEmpty(project)) {
CommandUtil.printError(this.errStream, "package is empty. Please add at least one .bal file.", null, false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}

// Sets the debug port as a system property, which will be used when setting up debug args before running tests.
if (this.debugPort != null) {
System.setProperty(SYSTEM_PROP_BAL_DEBUG, this.debugPort);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public CompileTask(PrintStream out,

@Override
public void execute(Project project) {
if (ProjectUtils.isProjectEmpty(project) && skipCompilationForBalPack(project)) {
throw createLauncherException("package is empty. Please add at least one .bal file.");
}
this.out.println("Compiling source");

String sourceName;
Expand Down Expand Up @@ -317,4 +320,16 @@ private void addDiagnosticForProvidedPlatformLibs(Project project, List<Diagnost
}
}
}

/**
* If CompileTask is triggered by `bal pack` command, and project does not have CompilerPlugin.toml or BalTool.toml,
* skip the compilation if project is empty. The project should be evaluated for emptiness before calling this.
*
* @param project project instance
* @return true if compilation should be skipped, false otherwise
*/
private boolean skipCompilationForBalPack(Project project) {
return (!compileForBalPack || project.currentPackage().compilerPluginToml().isEmpty() &&
project.currentPackage().balToolToml().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.ballerina.projects.Project;
import io.ballerina.projects.ProjectException;
import io.ballerina.projects.ProjectKind;
import io.ballerina.projects.util.ProjectUtils;
import io.ballerina.tools.diagnostics.Diagnostic;
import org.ballerinalang.central.client.CentralClientConstants;

Expand All @@ -48,7 +49,9 @@ public CreateDependencyGraphTask(PrintStream err, PrintStream out) {

@Override
public void execute(Project project) {

if (ProjectUtils.isProjectEmpty(project)) {
throw createLauncherException("package is empty. Please add at least one .bal file.");
}
System.setProperty(CentralClientConstants.ENABLE_OUTPUT_STREAM, "true");

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import io.ballerina.projects.SemanticVersion;
import io.ballerina.projects.environment.Environment;
import io.ballerina.projects.environment.EnvironmentBuilder;
import io.ballerina.projects.util.BuildToolUtils;
import io.ballerina.projects.util.ProjectUtils;
import org.ballerinalang.test.BCompileUtil;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
Expand All @@ -45,6 +48,7 @@
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.Objects;
import java.util.jar.JarFile;

Expand Down Expand Up @@ -627,11 +631,32 @@ public void testBuildEmptyProjectWithCompilerPlugin() throws IOException {

BuildCommand buildCommand = new BuildCommand(projectPath, printStream, printStream, false);
new CommandLine(buildCommand).parseArgs();
buildCommand.execute();
String buildLog = readOutput(true);
try {
buildCommand.execute();
} catch (BLauncherException e) {
List<String> messages = e.getMessages();
Assert.assertEquals(messages.size(), 1);
Assert.assertEquals(messages.get(0), getOutput("build-empty-project-with-compiler-plugin.txt"));
}
}

Assert.assertEquals(buildLog.replaceAll("\r", ""),
getOutput("build-empty-project-with-compiler-plugin.txt"));
@Test(description = "Build an empty package with code generator build tools")
public void testBuildEmptyProjectWithBuildTools() throws IOException {
BCompileUtil.compileAndCacheBala(testResources.resolve("buildToolResources").resolve("tools")
.resolve("ballerina-generate-file").toString(), testDistCacheDirectory, projectEnvironmentBuilder);
Path projectPath = this.testResources.resolve("emptyProjectWithBuildTool");
replaceDependenciesTomlContent(projectPath, "**INSERT_DISTRIBUTION_VERSION_HERE**",
RepoUtils.getBallerinaShortVersion());
System.setProperty(USER_DIR_PROPERTY, projectPath.toString());
try (MockedStatic<BuildToolUtils> repoUtils = Mockito.mockStatic(
BuildToolUtils.class, Mockito.CALLS_REAL_METHODS)) {
repoUtils.when(BuildToolUtils::getCentralBalaDirPath).thenReturn(testDistCacheDirectory.resolve("bala"));
BuildCommand buildCommand = new BuildCommand(projectPath, printStream, printStream, false);
new CommandLine(buildCommand).parseArgs();
buildCommand.execute();
}
String buildLog = readOutput(true);
Assert.assertEquals(buildLog.replaceAll("\r", ""), getOutput("build-empty-project-with-build-tools.txt"));
}

@Test(description = "Build an empty package with tests only")
Expand Down Expand Up @@ -683,10 +708,13 @@ public void testBuildEmptyNonDefaultModule() throws IOException {

BuildCommand buildCommand = new BuildCommand(projectPath, printStream, printStream, false);
new CommandLine(buildCommand).parseArgs();
buildCommand.execute();
String buildLog = readOutput(true);
Assert.assertEquals(buildLog.replaceAll("\r", ""),
getOutput("build-empty-nondefault-module.txt"));
try {
buildCommand.execute();
} catch (BLauncherException e) {
List<String> messages = e.getMessages();
Assert.assertEquals(messages.size(), 1);
Assert.assertEquals(messages.get(0), getOutput("build-empty-nondefault-module.txt"));
}
}

@Test(description = "Build a package with an invalid user name")
Expand Down Expand Up @@ -803,11 +831,13 @@ public void testBuildEmptyPackage() throws IOException {

BuildCommand buildCommand = new BuildCommand(projectPath, printStream, printStream, false);
new CommandLine(buildCommand).parseArgs();
buildCommand.execute();

String buildLog = readOutput(true);
Assert.assertEquals(buildLog.replaceAll("\r", ""),
getOutput("build-empty-package.txt"));
try {
buildCommand.execute();
} catch (BLauncherException e) {
List<String> messages = e.getMessages();
Assert.assertEquals(messages.size(), 1);
Assert.assertEquals(messages.get(0), getOutput("build-empty-package.txt"));
}
}

@Test(description = "Build an empty package with compiler plugin")
Expand All @@ -817,11 +847,13 @@ public void testBuildEmptyPackageWithCompilerPlugin() throws IOException {

BuildCommand buildCommand = new BuildCommand(projectPath, printStream, printStream, false);
new CommandLine(buildCommand).parseArgs();
buildCommand.execute();

String buildLog = readOutput(true);
Assert.assertEquals(buildLog.replaceAll("\r", ""),
getOutput("build-empty-package.txt"));
try {
buildCommand.execute();
} catch (BLauncherException e) {
List<String> messages = e.getMessages();
Assert.assertEquals(messages.size(), 1);
Assert.assertEquals(messages.get(0), getOutput("build-empty-package.txt"));
}
}

@Test(description = "Build a ballerina project with the flag dump-graph")
Expand Down
Loading

0 comments on commit 9c45823

Please sign in to comment.