Skip to content

Commit

Permalink
Migrate Module.md to README.md in langlibs
Browse files Browse the repository at this point in the history
  • Loading branch information
azinneera committed Oct 20, 2024
1 parent 81270be commit eaee8df
Show file tree
Hide file tree
Showing 25 changed files with 70 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -105,7 +106,7 @@ public Path write(Path balaPath) {
this.packageContext.packageName().value(),
this.packageContext.packageVersion().value().toString(),
this.target);
// Create the archive over write if exists
// Create the archive overwrite if exists
try (ZipOutputStream balaOutputStream = new ZipOutputStream(
new FileOutputStream(String.valueOf(balaPath.resolve(balaName))))) {
// Now lets put stuff in
Expand All @@ -129,8 +130,7 @@ private void populateBalaArchive(ZipOutputStream balaOutputStream)

addBalaJson(balaOutputStream);
addPackageDoc(balaOutputStream,
this.packageContext.project().sourceRoot(),
this.packageContext.packageName().toString());
this.packageContext.packageManifest());
addPackageSource(balaOutputStream);
addResources(balaOutputStream);
addIncludes(balaOutputStream);
Expand Down Expand Up @@ -183,6 +183,8 @@ private void addPackageJson(ZipOutputStream balaOutputStream, Optional<JsonArray
// Set graalvmCompatibility property in package.json
setGraalVMCompatibilityProperty(packageJson, packageManifest);

setDocs(packageJson, packageManifest);

// Remove fields with empty values from `package.json`
Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(Collection.class, new JsonCollectionsAdaptor())
.registerTypeHierarchyAdapter(String.class, new JsonStringsAdaptor()).setPrettyPrinting().create();
Expand All @@ -195,6 +197,27 @@ private void addPackageJson(ZipOutputStream balaOutputStream, Optional<JsonArray
}
}

private void setDocs(PackageJson packageJson, PackageManifest packageManifest) {
if (packageManifest.readme() == null) {
// ballerinai packages
return;
}
Map<String, String> docs = new HashMap<>();
String packageDocPathPrefix = BALA_DOCS_DIR + UNIX_FILE_SEPARATOR;
docs.put(packageManifest.descriptor().name().toString(),
packageDocPathPrefix + Paths.get(packageManifest.readme()).getFileName());
Map<String, PackageManifest.Modules> modules = packageManifest.modules();
for (Map.Entry<String, PackageManifest.Modules> module : modules.entrySet()) {
if (module.getValue().readme().isEmpty()) {
continue;
}
String moduleDoc = packageDocPathPrefix + MODULES_ROOT + UNIX_FILE_SEPARATOR + module.getKey() +
UNIX_FILE_SEPARATOR + Paths.get(module.getValue().readme().get()).getFileName();
docs.put(module.getValue().name(), moduleDoc);
}
packageJson.setDocs(docs);
}

private void setGraalVMCompatibilityProperty(PackageJson packageJson, PackageManifest packageManifest) {
Map<String, PackageManifest.Platform> platforms = packageManifest.platforms();
Boolean allPlatformDepsGraalvmCompatible = isAllPlatformDepsGraalvmCompatible(packageManifest.platforms());
Expand Down Expand Up @@ -253,20 +276,18 @@ private String otherPlatformGraalvmCompatibleVerified(String target,

// TODO when iterating and adding source files should create source files from Package sources

private void addPackageDoc(ZipOutputStream balaOutputStream, Path packageSourceDir, String pkgName)
private void addPackageDoc(ZipOutputStream balaOutputStream, PackageManifest packageManifest)
throws IOException {
final String packageMdFileName = "Package.md";
final String moduleMdFileName = "Module.md";

Path packageMd = packageSourceDir.resolve(packageMdFileName);
if (packageManifest.readme() == null) {
return;
}
Path pkgReadme = Paths.get(packageManifest.readme());
Path docsDirInBala = Path.of(BALA_DOCS_DIR);

// If `Package.md` exists, create the docs directory & add `Package.md`
if (packageMd.toFile().exists()) {
Path packageMdInBala = docsDirInBala.resolve(packageMdFileName);
putZipEntry(balaOutputStream, packageMdInBala,
new FileInputStream(String.valueOf(packageMd)));
}
Path packageMdInBala = docsDirInBala.resolve(pkgReadme.getFileName());
putZipEntry(balaOutputStream, packageMdInBala,
new FileInputStream(pkgReadme.toString()));

// If `icon` mentioned in the Ballerina.toml, add it to docs directory
String icon = this.packageContext.packageManifest().icon();
Expand All @@ -276,35 +297,16 @@ private void addPackageDoc(ZipOutputStream balaOutputStream, Path packageSourceD
putZipEntry(balaOutputStream, iconInBala, new FileInputStream(String.valueOf(iconPath)));
}

// If `Module.md` of default module exists, create `docs/modules` directory & add `Module.md`
Path defaultModuleMd = packageSourceDir.resolve(moduleMdFileName);
Path modulesDirInBalaDocs = docsDirInBala.resolve(MODULES_ROOT);

if (defaultModuleMd.toFile().exists()) {
Path defaultModuleMdInBalaDocs = modulesDirInBalaDocs.resolve(pkgName).resolve(moduleMdFileName);
putZipEntry(balaOutputStream, defaultModuleMdInBalaDocs,
new FileInputStream(String.valueOf(defaultModuleMd)));
}

// Add other module docs
File modulesSourceDir = new File(String.valueOf(packageSourceDir.resolve(MODULES_ROOT)));
File[] directoryListing = modulesSourceDir.listFiles();

if (directoryListing != null) {
for (File moduleDir : directoryListing) {
if (moduleDir.isDirectory()) {
// Get `Module.md` path
Path otherModuleMd = packageSourceDir.resolve(MODULES_ROOT).resolve(moduleDir.getName())
.resolve(moduleMdFileName);
// Create `package.module` folder, if `Module.md` path exists
if (otherModuleMd.toFile().exists()) {
Path otherModuleMdInBalaDocs = modulesDirInBalaDocs.resolve(pkgName + "." + moduleDir.getName())
.resolve(moduleMdFileName);
putZipEntry(balaOutputStream, otherModuleMdInBalaDocs,
new FileInputStream(String.valueOf(otherModuleMd)));
}
}
for (Map.Entry<String, PackageManifest.Modules> module : packageManifest.modules().entrySet()) {
if (module.getValue().readme().isEmpty()) {
continue;
}
Path otherReadmeMdInBalaDocs = modulesDirInBalaDocs.resolve(module.getKey())
.resolve(Paths.get(module.getValue().readme().get()).getFileName());
putZipEntry(balaOutputStream, otherReadmeMdInBalaDocs,
new FileInputStream(module.getValue().readme().get()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import static io.ballerina.projects.internal.ManifestUtils.getBooleanFromTomlTableNode;
import static io.ballerina.projects.internal.ManifestUtils.getBuildToolTomlValueType;
import static io.ballerina.projects.internal.ManifestUtils.getStringFromTomlTableNode;
import static io.ballerina.projects.util.ProjectConstants.DOT;
import static io.ballerina.projects.util.ProjectUtils.defaultName;
import static io.ballerina.projects.util.ProjectUtils.defaultOrg;
import static io.ballerina.projects.util.ProjectUtils.defaultVersion;
Expand Down Expand Up @@ -226,7 +227,7 @@ private PackageManifest parseAsPackageManifest() {

String customReadmeVal = getStringValueFromTomlTableNode(pkgNode, README, null);
readme = validateAndGetReadmePath(pkgNode, customReadmeVal);
moduleEntries = getModuleEntries(pkgNode, customReadmeVal);
moduleEntries = getModuleEntries(pkgNode, customReadmeVal, packageDescriptor.name());
}
}

Expand Down Expand Up @@ -269,7 +270,7 @@ localRepoDependencies, otherEntries, diagnostics(), license, authors, keywords,
}

private Map<String, PackageManifest.Modules> getModuleEntries(
TomlTableNode pkgNode, String customReadmeVal) {
TomlTableNode pkgNode, String customReadmeVal, PackageName packageName) {

Map<String, PackageManifest.Modules> moduleMap = new HashMap<>();
Path modulesRoot = this.projectPath.resolve(ProjectConstants.MODULES_ROOT);
Expand All @@ -296,9 +297,9 @@ private Map<String, PackageManifest.Modules> getModuleEntries(
modReadme = modReadmePath.toString();
}
PackageManifest.Modules module = new PackageManifest.Modules(
moduleDir.getFileName().toString(), false,
packageName + DOT + moduleDir.getFileName().toString(), false,
Optional.empty(), Optional.ofNullable(modReadme));
moduleMap.put(moduleDir.getFileName().toString(), module);
moduleMap.put(packageName + DOT + moduleDir.getFileName().toString(), module);
}
return moduleMap;
}
Expand All @@ -314,9 +315,9 @@ private Map<String, PackageManifest.Modules> getModuleEntries(
modReadme = modReadmePath.toString();
}
PackageManifest.Modules module = new PackageManifest.Modules(
moduleDir.getFileName().toString(), false,
packageName + DOT + moduleDir.getFileName().toString(), false,
Optional.empty(), Optional.ofNullable(modReadme));
moduleMap.put(moduleDir.getFileName().toString(), module);
moduleMap.put(packageName + DOT + moduleDir.getFileName().toString(), module);
}
return moduleMap;
}
Expand All @@ -331,7 +332,14 @@ private Map<String, PackageManifest.Modules> getModuleEntries(
// TODO: report diagnostic - default module not allowed
continue;
}
if (Files.notExists(this.projectPath.resolve(ProjectConstants.MODULES_ROOT).resolve(moduleName))) {

// String[] moduleNameParts = moduleName.split(packageName + DOT);
// if (moduleNameParts.length < 2) {
// // TODO: report diagnostic - invalid module provided. module not found
// continue;
// }
if (Files.notExists(this.projectPath.resolve(ProjectConstants.MODULES_ROOT)
.resolve(moduleName))) {
// TODO: report diagnostic - invalid module provided. module not found
continue;
}
Expand All @@ -348,9 +356,9 @@ private Map<String, PackageManifest.Modules> getModuleEntries(
modReadme = this.projectPath.resolve(modReadme).toString();
}
}
PackageManifest.Modules module = new PackageManifest.Modules(moduleName, export,
PackageManifest.Modules module = new PackageManifest.Modules(packageName + DOT + moduleName, export,
Optional.ofNullable(description), Optional.ofNullable(modReadme));
moduleMap.put(moduleName, module);
moduleMap.put(packageName + DOT + moduleName, module);
}
}
return moduleMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.gson.JsonArray;

import java.util.List;
import java.util.Map;

/**
* {@code PackageJson} Model for Package JSON file.
Expand Down Expand Up @@ -57,6 +58,9 @@ public class PackageJson {
private boolean template; //?
private String template_version; //?

//Docs
private Map<String, String> docs;

public PackageJson(String organization, String name, String version) {
this.organization = organization;
this.name = name;
Expand Down Expand Up @@ -221,4 +225,12 @@ public String getTemplateVersion() {
public void setTemplateVersion(String template_version) {
this.template_version = template_version;
}

public Map<String, String> getDocs() {
return docs;
}

public void setDocs(Map<String, String> docs) {
this.docs = docs;
}
}
9 changes: 0 additions & 9 deletions langlib/jballerina.java/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.array/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.boolean/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.decimal/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.error/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.float/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.function/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.future/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.int/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.map/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.object/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.query/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.regexp/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.runtime/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.stream/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.string/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.table/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.transaction/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.typedesc/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.value/src/main/ballerina/Module.md

This file was deleted.

3 changes: 0 additions & 3 deletions langlib/lang.xml/src/main/ballerina/Module.md

This file was deleted.

0 comments on commit eaee8df

Please sign in to comment.