From 782e026ad96337c0c8c3f27c1fcdab82e164b0a7 Mon Sep 17 00:00:00 2001 From: azinneera Date: Fri, 22 Nov 2024 11:56:37 +0530 Subject: [PATCH] Pick Module.md as the default doc for v2 balas --- .../ballerina/projects/bala/BalaProject.java | 6 +++ .../projects/internal/BalaFiles.java | 37 ++++++++++++++++++ .../projects/internal/ManifestBuilder.java | 6 ++- .../docgen/docs/BallerinaDocGenerator.java | 9 ++++- .../docgen/tests/GenDocsForBalaTest.java | 30 +++++++++++++- .../balas/fooNewFormat-fb-any-1.3.5.bala | Bin 5022 -> 0 bytes .../resources/balas/fooV3-fb-any-1.3.5.bala | Bin 0 -> 7346 bytes 7 files changed, 83 insertions(+), 5 deletions(-) delete mode 100644 misc/docerina/src/test/resources/balas/fooNewFormat-fb-any-1.3.5.bala create mode 100644 misc/docerina/src/test/resources/balas/fooV3-fb-any-1.3.5.bala diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/bala/BalaProject.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/bala/BalaProject.java index 282079a6df1c..6a805952e810 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/bala/BalaProject.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/bala/BalaProject.java @@ -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. @@ -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 @@ -136,6 +138,10 @@ public String platform() { return platform; } + public String balaVersion() { + return balaVersion; + } + private boolean isFilePathInProject(Path filepath) { try { ProjectPaths.packageRoot(filepath); diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java index 0d1253c81b14..6ffd7410a181 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java @@ -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; @@ -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; @@ -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()); } diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/ManifestBuilder.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/ManifestBuilder.java index 39399a550de6..19dff0a5129c 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/ManifestBuilder.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/ManifestBuilder.java @@ -358,12 +358,14 @@ private List 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(); } @@ -375,7 +377,7 @@ private List 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 pathEntry : moduleDirs.entrySet()) { diff --git a/misc/docerina/src/main/java/org/ballerinalang/docgen/docs/BallerinaDocGenerator.java b/misc/docerina/src/main/java/org/ballerinalang/docgen/docs/BallerinaDocGenerator.java index 4327093d9420..e9837e732f1b 100644 --- a/misc/docerina/src/main/java/org/ballerinalang/docgen/docs/BallerinaDocGenerator.java +++ b/misc/docerina/src/main/java/org/ballerinalang/docgen/docs/BallerinaDocGenerator.java @@ -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; @@ -560,7 +562,12 @@ public static Map 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(); diff --git a/misc/docerina/src/test/java/org/ballerinalang/docgen/tests/GenDocsForBalaTest.java b/misc/docerina/src/test/java/org/ballerinalang/docgen/tests/GenDocsForBalaTest.java index 6408bb4d54dd..6db97c879b8b 100644 --- a/misc/docerina/src/test/java/org/ballerinalang/docgen/tests/GenDocsForBalaTest.java +++ b/misc/docerina/src/test/java/org/ballerinalang/docgen/tests/GenDocsForBalaTest.java @@ -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(); @@ -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"); diff --git a/misc/docerina/src/test/resources/balas/fooNewFormat-fb-any-1.3.5.bala b/misc/docerina/src/test/resources/balas/fooNewFormat-fb-any-1.3.5.bala deleted file mode 100644 index f5ce8c39df9d9c735d64c76313772d1f6f13558d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5022 zcmWIWW@Zs#0D((2cZ0wTD8b60z>t!kT&y1&!pp#}`ter6FS$DjpMbcuf}4SnGx%nxjIjK;izJIs{HU_UzsY&-xTqA&PR9X_mVb6sjj>2Oa2Ls5o z_fLaZ>w$io3&fIGO!Ec1Q7<|Z>_n)Vv<9Wvly_`c^*ka)zcy~&jfJUJbTzCB43 zk4$~L?7q6J#QPdnwz@sa6Zpkon%Tfz!Ne#RKhtn?_=5p7ri%G7K;kk>}yt z#4=x0?4f>_lXCr|-qr6|mtOz)F^kD54MJYA-t^8ESKCC{d6JXx+Ovi#*zG0~^uqS{ZKmh5Cec(V#E zDTrZlQF(q*P6{MJ2#64oAV6lWxH#*C6e9zJ8WRJ9JWexFJ&DKgwG(aq4;u)yegDIy z>~fW-Z*`wXC@YV_hJ#^>Pb}JtFCFcxdCu@(Zab$uH?z>})cx}3=bpd%SX;=gRM?q8 z-7xg(!#4$UCt7_~2s5%@TRm~2e(%H-^^~_8I#15NCggZ1;j2ejT$0oof!#g}_Q&1Z zGUt-?yg&PV&34q8M~HcRwO?}0{Ze&_fW*GX1s{07b6hEG-uR?c?#!Y~;fqUcV{*)I zr!9^?RiN_wpwMxpZc9sxgBNm=jenOEUcGwrq1aQs?OKeDsR6HRj6YCgHu((Jh}j71lgUs?ggyf35Z=-3xhYI>q|mOG9<# z&v{h^6wfFslqhmYXH5>^$<*S0ula@a z-YjP4WC3?C0|R=2#sPFM>17$ZDMCo5q$QD;MUDPA&ktv0V8{cO!qQmH17``nq{JL> zd4?x<)&_0#118+NpGB9adAhDlNa(UEanj+~sL(RumBsWc4{lAZUC4B${&?A&ZCf(; z9yxcn`un@PPe1fzH;aizCTws^-pUkPtFSzCTDoOSW0H1DcEuO@y9~O9&ssIIYYXnl z9d*$7;g*&&Kjiwlj<1~`%e=NYs%5?mwzxi5X~m3`F8S**zR6WDMAQY-lvh}6$effT>8`$= z{VuDUnaQ@tX*@ycQ}Wr(4|h14>IU8EN|Le6ndPp3e7;9spxNakvH!$hM_g*rdzGU7 z>D$^v({@e_U1-1Nd#}MKHhT@H@b*L10lS*_wluXGe|US>*#FV0iZgG@ss`2ZAUT>milCMEfJXIlT#%fTge* zM|t{f98JGeNWYL|XH;tbS|6C?jDeUF7+t_}B~dS{I6se2>9YD+P(Xl}*BQ?qP}P6# z%=zF=K>7n>z6PK zpY}fE%gU{$c;r-2cbDh9vS3Tn^OlXCI9sC{1ll&7K=(@tbheO{a!DL3WW zg}p#enK3dj@G!_Q6hP}fNN930FbC~7Ne=*GNIUy0P#L(1?%RKw>yUwf>-Rd>lefa6 z1rN>-D0t|!#AkMj`>YS7mwQYx<7w@^#5Okusu_`Dpw3+Wz zi2aMeg>H`@{oj+OQn;b7J$9-H>$3e${aa->&K0}6Ds}6b(^~rGCuKx;^hLBQ&PZ?K zD}9i7m2b*X4oSylZ&kOJZEXMcC|+ZW$ma9n0p5&EBFwmZ8o=-Xg9DBripYKk!btQY z7S!{Afdh`tFe8btyAeiX>xm$Y?n5`4sD25|bnHD7nCV*@?_e<j^nLHGq**8yh9mPU499>!q_s+aIIRgk@ety=)I?SSJUi_;EN4-sh@r~{7PI7V2O!U7FG;+x6{pI~pv!VEv)D8kA> zLc10gy_ V2Y9oxfwT#O8uh?bk--Dv0RY-BavuNy diff --git a/misc/docerina/src/test/resources/balas/fooV3-fb-any-1.3.5.bala b/misc/docerina/src/test/resources/balas/fooV3-fb-any-1.3.5.bala new file mode 100644 index 0000000000000000000000000000000000000000..1865b6fd0f4460a95e1453c24ef631d0b342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