Skip to content

Commit

Permalink
Merge pull request #43642 from azinneera/prj_api_readme_newMaster
Browse files Browse the repository at this point in the history
Pick Module.md as the default doc for v2 balas
  • Loading branch information
azinneera authored Feb 2, 2025
2 parents a67872d + 782e026 commit 39dadd4
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 5 deletions.
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 @@ public static PackageJson readPackageJson(Path balaPath) {
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 + "'");
}
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 + "'");
}
} 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 + "'");
}
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 + "'");
}
} catch (IOException e) {
throw new ProjectException("Failed to read bala file:" + balaPath);
}
}
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.

0 comments on commit 39dadd4

Please sign in to comment.