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

Pick Module.md as the default doc for v2 balas #43642

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -45,6 +45,7 @@
*/
public class BalaProject extends Project {
private final String platform;
private final String balaVersion;

/**
* Loads a BalaProject from the provided bala path.
Expand All @@ -70,6 +71,7 @@ public static BalaProject loadProject(ProjectEnvironmentBuilder environmentBuild
private BalaProject(ProjectEnvironmentBuilder environmentBuilder, Path balaPath, BuildOptions buildOptions) {
super(ProjectKind.BALA_PROJECT, balaPath, environmentBuilder, buildOptions);
this.platform = BalaFiles.readPackageJson(balaPath).getPlatform();
this.balaVersion = BalaFiles.readBalaJson(balaPath).getBala_version();
}

@Override
Expand Down Expand Up @@ -136,6 +138,10 @@ public String platform() {
return platform;
}

public String balaVersion() {
return balaVersion;
}

private boolean isFilePathInProject(Path filepath) {
try {
ProjectPaths.packageRoot(filepath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.ballerina.projects.PlatformLibraryScope;
import io.ballerina.projects.ProjectException;
import io.ballerina.projects.internal.bala.BalToolJson;
import io.ballerina.projects.internal.bala.BalaJson;
import io.ballerina.projects.internal.bala.CompilerPluginJson;
import io.ballerina.projects.internal.bala.DependencyGraphJson;
import io.ballerina.projects.internal.bala.ModuleDependency;
Expand Down Expand Up @@ -66,6 +67,7 @@
import static io.ballerina.projects.internal.ProjectFiles.loadDocuments;
import static io.ballerina.projects.internal.ProjectFiles.loadResources;
import static io.ballerina.projects.util.ProjectConstants.BALA_DOCS_DIR;
import static io.ballerina.projects.util.ProjectConstants.BALA_JSON;
import static io.ballerina.projects.util.ProjectConstants.BAL_TOOL_JSON;
import static io.ballerina.projects.util.ProjectConstants.COMPILER_PLUGIN_DIR;
import static io.ballerina.projects.util.ProjectConstants.COMPILER_PLUGIN_JSON;
Expand Down Expand Up @@ -756,6 +758,41 @@
return packageJson;
}

public static BalaJson readBalaJson(Path balaPath) {
BalaJson balaJson;
if (balaPath.toFile().isDirectory()) {
Path balaJsonPath = balaPath.resolve(BALA_JSON);
if (Files.notExists(balaJsonPath)) {
throw new ProjectException("bala.json does not exists in '" + balaPath + "'");

Check warning on line 766 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java#L766

Added line #L766 was not covered by tests
}
try (BufferedReader bufferedReader = Files.newBufferedReader(balaJsonPath)) {
balaJson = gson.fromJson(bufferedReader, BalaJson.class);
} catch (JsonSyntaxException e) {
throw new ProjectException("Invalid bala.json format in '" + balaPath + "'");
} catch (IOException e) {
throw new ProjectException("Failed to read the bala.json in '" + balaPath + "'");

Check warning on line 773 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java#L770-L773

Added lines #L770 - L773 were not covered by tests
}
} else {
URI zipURI = getZipURI(balaPath);
try (FileSystem zipFileSystem = FileSystems.newFileSystem(zipURI, new HashMap<>())) {
Path balaJsonPath = zipFileSystem.getPath(BALA_JSON);
if (Files.notExists(balaJsonPath)) {
throw new ProjectException("package.json does not exists in '" + balaPath + "'");

Check warning on line 780 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java#L780

Added line #L780 was not covered by tests
}
try (BufferedReader bufferedReader = Files.newBufferedReader(balaJsonPath)) {
balaJson = gson.fromJson(bufferedReader, BalaJson.class);
} catch (JsonSyntaxException e) {
throw new ProjectException("Invalid package.json format in '" + balaPath + "'");
} catch (IOException e) {
throw new ProjectException("Failed to read the package.json in '" + balaPath + "'");

Check warning on line 787 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java#L784-L787

Added lines #L784 - L787 were not covered by tests
}
} catch (IOException e) {
throw new ProjectException("Failed to read bala file:" + balaPath);

Check warning on line 790 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java#L789-L790

Added lines #L789 - L790 were not covered by tests
}
}
return balaJson;
}

private static URI getZipURI(Path balaPath) {
return URI.create("jar:" + balaPath.toAbsolutePath().toUri());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,14 @@ private List<PackageManifest.Module> getModuleEntries(
|| !moduleName.contains(DOT)) { // The invalid module name is already handled
continue;
}
String moduleNamepart = moduleName.substring(packageName.toString().length() + 1);

boolean export = Boolean.TRUE.equals(getBooleanValueFromTomlTableNode(modulesNode, EXPORT));
String description = getStringValueFromTomlTableNode(modulesNode, DESCRIPTION, null);
String modReadme = getStringValueFromTomlTableNode(modulesNode, README, null);
if (modReadme == null) {
Path defaultReadme = modulesRoot.resolve(moduleName).resolve(ProjectConstants.README_MD_FILE_NAME);
Path defaultReadme = modulesRoot.resolve(moduleNamepart)
.resolve(ProjectConstants.README_MD_FILE_NAME);
if (Files.exists(defaultReadme)) {
modReadme = defaultReadme.toString();
}
Expand All @@ -375,7 +377,7 @@ private List<PackageManifest.Module> getModuleEntries(
PackageManifest.Module module = new PackageManifest.Module(moduleName, export,
description, modReadme);
moduleList.add(module);
moduleDirs.remove(moduleName.split("[.]")[1]);
moduleDirs.remove(moduleNamepart);
}
// If there are README.mds in other modules, add them
for (Map.Entry<String, Path> pathEntry : moduleDirs.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import io.ballerina.projects.PackageManifest;
import io.ballerina.projects.PackageReadmeMd;
import io.ballerina.projects.Project;
import io.ballerina.projects.ProjectKind;
import io.ballerina.projects.bala.BalaProject;
import io.ballerina.projects.util.ProjectConstants;
import io.ballerina.projects.util.ProjectUtils;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -560,7 +562,12 @@ public static Map<String, ModuleDoc> generateModuleDocMap(Project project)
if (module.isDefaultModule()) {
moduleName = module.moduleName().packageName().toString();
modulePath = project.sourceRoot();
moduleMdText = project.currentPackage().readmeMd().map(PackageReadmeMd::content).orElse("");
if (project.kind() == ProjectKind.BALA_PROJECT
&& "2.0.0".equals(((BalaProject) project).balaVersion())) {
moduleMdText = module.readmeMd().map(ModuleReadmeMd::content).orElse("");
} else {
moduleMdText = project.currentPackage().readmeMd().map(PackageReadmeMd::content).orElse("");
}
summary = project.currentPackage().manifest().description();
} else {
moduleName = module.moduleName().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void setup() throws IOException {
}

@Test
public void generatingDocsForBalaTest() throws IOException {
public void generatingDocsForBalaV2Test() throws IOException {
Path balaPath = this.resourceDir.resolve("balas/foo-fb-any-1.3.5.bala");

ProjectEnvironmentBuilder defaultBuilder = ProjectEnvironmentBuilder.getDefaultBuilder();
Expand All @@ -57,10 +57,36 @@ public void generatingDocsForBalaTest() throws IOException {

BallerinaDocGenerator.generateAPIDocs(balaProject, this.docsPath.toString(), true);

String sfModuleApiDocsJsonAsString = Files.readString(
this.docsPath.resolve("foo/fb/1.3.5").resolve(BallerinaDocGenerator.API_DOCS_JSON));
Assert.assertTrue(sfModuleApiDocsJsonAsString.contains("\"id\":\"fb\",\"summary\":\"This module " +
"provides an implementation to interact with fb Brokers via fb Consumer and " +
"fb [Ballerina](https://ballerina.io) Producer clients.\\n\""),
"Default module summary is missing");
Assert.assertTrue(sfModuleApiDocsJsonAsString.contains("\"id\":\"fb.world\",\"summary\":" +
"\"Connects the twilio communication services.\\n\""),
"fb.world module summary is missing");
Assert.assertTrue(sfModuleApiDocsJsonAsString.contains("Block"), "Block type is missing");

String sfWorldModuleApiDocsJsonAsString = Files.readString(
this.docsPath.resolve("foo/fb.world/1.3.5").resolve(BallerinaDocGenerator.API_DOCS_JSON));
Assert.assertTrue(sfWorldModuleApiDocsJsonAsString.contains("PersonZ"), "PersonZ class is missing");
}

@Test
public void generatingDocsForBalaV3Test() throws IOException {
Path balaPath = this.resourceDir.resolve("balas/fooV3-fb-any-1.3.5.bala");

ProjectEnvironmentBuilder defaultBuilder = ProjectEnvironmentBuilder.getDefaultBuilder();
defaultBuilder.addCompilationCacheFactory(TempDirCompilationCache::from);
BalaProject balaProject = BalaProject.loadProject(defaultBuilder, balaPath);

BallerinaDocGenerator.generateAPIDocs(balaProject, this.docsPath.toString(), true);

String sfModuleApiDocsJsonAsString = Files.readString(
this.docsPath.resolve("foo/fb/1.3.5").resolve(BallerinaDocGenerator.API_DOCS_JSON));
Assert.assertTrue(sfModuleApiDocsJsonAsString.contains("\"id\":\"fb\",\"summary\":\"Package md content with " +
"character length bigger than 50 characters, in one line in the Package MD file.\\n\""));
"character length bigger than 50 characters, in one line in the Package MD file.\\n\""));
Assert.assertTrue(sfModuleApiDocsJsonAsString.contains("\"id\":\"fb.world\",\"summary\":" +
"\"Connects the twilio communication services.\\n\""),
"fb.world module summary is missing");
Expand Down
Binary file not shown.
Binary file not shown.
Loading