Search in sources :

Example 71 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class BallerinaDocGenerator method generatePackageDocsFromBallerina.

/**
 * Generates {@link BLangPackage} objects for each Ballerina package from the given ballerina files.
 *
 * @param sourceRoot    points to the folder relative to which package path is given
 * @param packagePath   a {@link Path} object pointing either to a ballerina file or a folder with ballerina files.
 * @param packageFilter comma separated list of package names/patterns to be filtered from the documentation.
 * @param isNative      whether the given packages are native or not.
 * @return a map of {@link BLangPackage} objects. Key - Ballerina package name Value - {@link BLangPackage}
 */
protected static Map<String, BLangPackage> generatePackageDocsFromBallerina(String sourceRoot, Path packagePath, String packageFilter, boolean isNative) throws IOException {
    final List<Path> packagePaths = new ArrayList<>();
    if (Files.isDirectory(packagePath)) {
        BallerinaSubPackageVisitor subPackageVisitor = new BallerinaSubPackageVisitor(packagePath, packagePaths);
        Files.walkFileTree(packagePath, subPackageVisitor);
    } else {
        packagePaths.add(packagePath);
    }
    BallerinaDocDataHolder dataHolder = BallerinaDocDataHolder.getInstance();
    if (!isNative) {
        // This is necessary to be true in order to Ballerina to work properly
        System.setProperty("skipNatives", "true");
    }
    BLangPackage bLangPackage;
    for (Path path : packagePaths) {
        CompilerContext context = new CompilerContext();
        CompilerOptions options = CompilerOptions.getInstance(context);
        options.put(CompilerOptionName.PROJECT_DIR, sourceRoot);
        options.put(CompilerOptionName.COMPILER_PHASE, CompilerPhase.DESUGAR.toString());
        options.put(CompilerOptionName.PRESERVE_WHITESPACE, "false");
        Compiler compiler = Compiler.getInstance(context);
        // TODO: Remove this and the related constants once these are properly handled in the core
        if (BAL_BUILTIN.equals(path) || BAL_BUILTIN_CORE.equals(path)) {
            bLangPackage = loadBuiltInPackage(context);
        } else {
            // compile the given file
            bLangPackage = compiler.compile(getPackageNameFromPath(path));
        }
        if (bLangPackage == null) {
            out.println(String.format("docerina: invalid Ballerina package: %s", packagePath));
        } else {
            String packageName = bLangPackage.symbol.pkgID.name.value;
            if (isFilteredPackage(packageName, packageFilter)) {
                if (BallerinaDocUtils.isDebugEnabled()) {
                    out.println("Package " + packageName + " excluded");
                }
                continue;
            }
            dataHolder.getPackageMap().put(packageName, bLangPackage);
        }
    }
    return dataHolder.getPackageMap();
}
Also used : Path(java.nio.file.Path) Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions)

Example 72 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class BallerinaDocGenerator method loadBuiltInPackage.

private static BLangPackage loadBuiltInPackage(CompilerContext context) {
    SymbolTable symbolTable = SymbolTable.getInstance(context);
    // Load built-in packages.
    BLangPackage builtInPkg = getBuiltInPackage(context);
    symbolTable.builtInPackageSymbol = builtInPkg.symbol;
    return builtInPkg;
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable)

Example 73 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class HtmlDocTest method testFunctions.

@Test(description = "Functions in a package should be shown in the constructs")
public void testFunctions() throws Exception {
    BLangPackage bLangPackage = createPackage("package x.y; public function hello(string name) returns (string){}");
    Page page = generatePage(bLangPackage);
    Assert.assertEquals(page.constructs.size(), 1);
    Assert.assertEquals(page.constructs.get(0).name, "hello");
    Assert.assertTrue(page.constructs.get(0) instanceof FunctionDoc, "Invalid documentable type");
    FunctionDoc functionDoc = (FunctionDoc) page.constructs.get(0);
    Assert.assertEquals(functionDoc.parameters.get(0).toString(), "string name", "Invalid parameter string value");
    Assert.assertEquals(functionDoc.returnParams.get(0).toString(), "string", "Invalid return type");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) FunctionDoc(org.ballerinalang.docgen.model.FunctionDoc) Page(org.ballerinalang.docgen.model.Page) Test(org.testng.annotations.Test)

Example 74 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class HtmlDocTest method testPrivateConstructsInPackage.

@Test(description = "Private constructs should not appear at all.")
public void testPrivateConstructsInPackage() {
    BLangPackage bLangPackage = createPackage("package x.y; " + "function hello(){}" + "enum Direction { IN,OUT}" + "enum Money { USD,LKR}" + "annotation ParameterInfo;" + "annotation ReturnInfo;" + "int total = 98;" + "string content = \"Name\";" + "struct Message {}" + "struct Response {}");
    Page page = generatePage(bLangPackage);
    Assert.assertEquals(page.constructs.size(), 0);
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Page(org.ballerinalang.docgen.model.Page) Test(org.testng.annotations.Test)

Example 75 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class HtmlDocTest method testEnumPropertiesExtracted.

@Test(description = "Enum properties should be available via construct")
public void testEnumPropertiesExtracted() throws Exception {
    BLangPackage bLangPackage = createPackage("package x.y; " + "@Description { value:\"The direction of the parameter\"}\n" + "@Field { value:\"IN: IN parameters are used to send values to stored procedures\"}\n" + "@Field { value:\"OUT: OUT parameters are used to get values from stored procedures\"}\n" + "public enum Direction { IN,OUT}");
    EnumDoc enumDoc = Generator.createDocForNode(bLangPackage.getEnums().get(0));
    Assert.assertEquals(enumDoc.name, "Direction", "Enum name should be extracted");
    Assert.assertEquals(enumDoc.description, "The direction of the parameter", "Description of the " + "enum should be extracted");
    // Enumerators inside the enum
    Assert.assertEquals(enumDoc.enumerators.get(0).name, "IN", "Enumerator name should be extracted");
    Assert.assertEquals(enumDoc.enumerators.get(0).description, "IN parameters are used to send values to " + "stored procedures", "Description of the enumerator should be extracted");
}
Also used : EnumDoc(org.ballerinalang.docgen.model.EnumDoc) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Test(org.testng.annotations.Test)

Aggregations

BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)78 Test (org.testng.annotations.Test)29 ArrayList (java.util.ArrayList)28 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)19 Page (org.ballerinalang.docgen.model.Page)18 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)15 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)15 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)14 IOException (java.io.IOException)13 List (java.util.List)13 Path (java.nio.file.Path)12 Compiler (org.wso2.ballerinalang.compiler.Compiler)12 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)12 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)11 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)10 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)10 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)9 BLangAction (org.wso2.ballerinalang.compiler.tree.BLangAction)9 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)9