Search in sources :

Example 91 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class BCompileUtil method compileAndGetPackage.

/**
 * Compile and return the compiled package node.
 *
 * @param sourceFilePath Path to source package/file
 * @return compiled package node
 */
public static BLangPackage compileAndGetPackage(String sourceFilePath) {
    Path sourcePath = Paths.get(sourceFilePath);
    String packageName = sourcePath.getFileName().toString();
    Path sourceRoot = resourceDir.resolve(sourcePath.getParent());
    CompilerContext context = new CompilerContext();
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, resourceDir.resolve(sourceRoot).toString());
    options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString());
    options.put(PRESERVE_WHITESPACE, "false");
    CompileResult comResult = new CompileResult();
    // catch errors
    DiagnosticListener listener = comResult::addDiagnostic;
    context.put(DiagnosticListener.class, listener);
    // compile
    Compiler compiler = Compiler.getInstance(context);
    return compiler.compile(packageName);
}
Also used : Path(java.nio.file.Path) Compiler(org.wso2.ballerinalang.compiler.Compiler) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener)

Example 92 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class BallerinaDocGenerator method generateApiDocs.

/**
 * API to generate Ballerina API documentation.
 *
 * @param output        path to the output directory where the API documentation will be written to.
 * @param packageFilter comma separated list of package names to be filtered from the documentation.
 * @param isNative      whether the given packages are native or not.
 * @param sources       either the path to the directories where Ballerina source files reside or a
 *                      path to a Ballerina file which does not belong to a package.
 */
public static void generateApiDocs(String output, String packageFilter, boolean isNative, String... sources) {
    out.println("docerina: API documentation generation for sources - " + Arrays.toString(sources));
    for (String source : sources) {
        source = source.trim();
        try {
            Map<String, BLangPackage> docsMap;
            if (source.endsWith(".bal")) {
                Path sourceFilePath = Paths.get(source);
                Path parentDir = sourceFilePath.getParent();
                Path fileName = sourceFilePath.getFileName();
                if (fileName == null) {
                    log.warn("Skipping the source generation for invalid path: " + sourceFilePath);
                    continue;
                }
                if (parentDir == null) {
                    parentDir = Paths.get(".");
                }
                docsMap = generatePackageDocsFromBallerina(parentDir.toString(), fileName, packageFilter, isNative);
            } else {
                Path dirPath = Paths.get(source);
                // TODO fix this properly
                // TODO Temporary fix that creates .ballerina to create project structure
                Path projectFolder = dirPath.resolve(ProjectDirConstants.DOT_BALLERINA_DIR_NAME);
                Files.createDirectory(projectFolder);
                Path sourceRootPath = LauncherUtils.getSourceRootPath(dirPath.toString());
                docsMap = generatePackageDocsFromBallerina(sourceRootPath.toString(), dirPath, packageFilter, isNative);
            }
            if (docsMap.size() == 0) {
                out.println("docerina: no package definitions found!");
                return;
            }
            if (BallerinaDocUtils.isDebugEnabled()) {
                out.println("Generating HTML API documentation...");
            }
            String userDir = System.getProperty("user.dir");
            // If output directory is empty
            if (output == null) {
                output = System.getProperty(BallerinaDocConstants.HTML_OUTPUT_PATH_KEY, userDir + File.separator + "api-docs" + File.separator + "html");
            }
            // Create output directories
            Files.createDirectories(Paths.get(output));
            // Sort packages by package path
            List<BLangPackage> packageList = new ArrayList<>(docsMap.values());
            packageList.sort(Comparator.comparing(pkg -> pkg.packageID.toString()));
            // Iterate over the packages to generate the pages
            List<String> packageNames = new ArrayList<>(docsMap.keySet());
            // Sort the package names
            Collections.sort(packageNames);
            List<Link> packageNameList = PackageName.convertList(packageNames);
            if (packageNames.contains("ballerina.builtin")) {
                StaticCaption primitivesLinkName = new StaticCaption(BallerinaDocConstants.PRIMITIVE_TYPES_PAGE_NAME);
                packageNameList.add(0, new Link(primitivesLinkName, BallerinaDocConstants.PRIMITIVE_TYPES_PAGE_HREF, false));
            }
            // Generate pages for the packages
            String packageTemplateName = System.getProperty(BallerinaDocConstants.PACKAGE_TEMPLATE_NAME_KEY, "page");
            for (BLangPackage bLangPackage : packageList) {
                // Sort functions, connectors, structs, type mappers and annotationDefs
                bLangPackage.getFunctions().sort(Comparator.comparing(f -> (f.getReceiver() == null ? "" : f.getReceiver().getName()) + f.getName().getValue()));
                bLangPackage.getConnectors().sort(Comparator.comparing(c -> c.getName().getValue()));
                bLangPackage.getStructs().sort(Comparator.comparing(s -> s.getName().getValue()));
                bLangPackage.getAnnotations().sort(Comparator.comparing(a -> a.getName().getValue()));
                bLangPackage.getEnums().sort(Comparator.comparing(a -> a.getName().getValue()));
                // Sort connector actions
                if ((bLangPackage.getConnectors() != null) && (bLangPackage.getConnectors().size() > 0)) {
                    bLangPackage.getConnectors().forEach(connector -> connector.getActions().sort(Comparator.comparing(a -> a.getName().getValue())));
                }
                String packagePath = refinePackagePath(bLangPackage);
                Page page = Generator.generatePage(bLangPackage, packageNameList);
                String filePath = output + File.separator + packagePath + HTML;
                Writer.writeHtmlDocument(page, packageTemplateName, filePath);
                if ("ballerina.builtin".equals(packagePath)) {
                    Page primitivesPage = Generator.generatePageForPrimitives(bLangPackage, packageNameList);
                    String primitivesFilePath = output + File.separator + "primitive-types" + HTML;
                    Writer.writeHtmlDocument(primitivesPage, packageTemplateName, primitivesFilePath);
                }
            }
            // Generate the index file with the list of all packages
            String indexTemplateName = System.getProperty(BallerinaDocConstants.PACKAGE_TEMPLATE_NAME_KEY, "index");
            String indexFilePath = output + File.separator + "index" + HTML;
            Writer.writeHtmlDocument(packageNameList, indexTemplateName, indexFilePath);
            if (BallerinaDocUtils.isDebugEnabled()) {
                out.println("Copying HTML theme...");
            }
            BallerinaDocUtils.copyResources("docerina-theme", output);
        } catch (IOException e) {
            out.println(String.format("docerina: API documentation generation failed for %s: %s", source, e.getMessage()));
            log.error(String.format("API documentation generation failed for %s", source), e);
        }
    }
    try {
        String zipPath = System.getProperty(BallerinaDocConstants.OUTPUT_ZIP_PATH);
        if (zipPath != null) {
            BallerinaDocUtils.packageToZipFile(output, zipPath);
        }
    } catch (IOException e) {
        out.println(String.format("docerina: API documentation zip packaging failed for %s: %s", output, e.getMessage()));
        log.error(String.format("API documentation zip packaging failed for %s", output), e);
    }
}
Also used : Path(java.nio.file.Path) ProjectDirConstants(org.wso2.ballerinalang.compiler.util.ProjectDirConstants) Arrays(java.util.Arrays) Link(org.ballerinalang.docgen.model.Link) Generator(org.ballerinalang.docgen.Generator) LoggerFactory(org.slf4j.LoggerFactory) Compiler(org.wso2.ballerinalang.compiler.Compiler) PackageLoader(org.wso2.ballerinalang.compiler.PackageLoader) ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) Writer(org.ballerinalang.docgen.Writer) Map(java.util.Map) Names(org.wso2.ballerinalang.compiler.util.Names) Path(java.nio.file.Path) LauncherUtils(org.ballerinalang.launcher.LauncherUtils) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) PrintStream(java.io.PrintStream) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Files(java.nio.file.Files) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) CodeAnalyzer(org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer) File(java.io.File) SemanticAnalyzer(org.wso2.ballerinalang.compiler.semantics.analyzer.SemanticAnalyzer) FileVisitResult(java.nio.file.FileVisitResult) BallerinaDocUtils(org.ballerinalang.docgen.docs.utils.BallerinaDocUtils) StaticCaption(org.ballerinalang.docgen.model.StaticCaption) List(java.util.List) Paths(java.nio.file.Paths) StringJoiner(java.util.StringJoiner) PackageName(org.ballerinalang.docgen.model.PackageName) CompilerOptionName(org.ballerinalang.compiler.CompilerOptionName) Comparator(java.util.Comparator) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) Collections(java.util.Collections) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) IOException(java.io.IOException) StaticCaption(org.ballerinalang.docgen.model.StaticCaption) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Link(org.ballerinalang.docgen.model.Link)

Example 93 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class WorkspaceUtils method getBallerinaFileForContent.

public static BLangPackage getBallerinaFileForContent(String fileName, String source) {
    CompilerContext context = prepareCompilerContext(fileName, source);
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(CompilerOptionName.COMPILER_PHASE, CompilerPhase.DEFINE.toString());
    options.put(CompilerOptionName.PRESERVE_WHITESPACE, Boolean.TRUE.toString());
    return getBallerinaPackage(fileName, context);
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions)

Example 94 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class DocumentationTest method testDeprecatedTransformer.

@Test(description = "Test doc deprecated Transformer.")
public void testDeprecatedTransformer() {
    CompileResult compileResult = BCompileUtil.compile("test-src/documentation/deprecated_transformer.bal");
    Assert.assertEquals(0, compileResult.getWarnCount());
    PackageNode packageNode = compileResult.getAST();
    List<BLangDeprecatedNode> dNodes = ((BLangTransformer) packageNode.getTransformers().get(0)).deprecatedAttachments;
    BLangDeprecatedNode dNode = dNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + "  This Transformer is deprecated use\n" + "  `transformer <Person p, Employee e> Bar(any defaultAddress) { e.name = p.firstName; }\n" + "  ` instead.\n");
    List<BLangDocumentation> docNodes = ((BLangTransformer) packageNode.getTransformers().get(0)).docAttachments;
    BLangDocumentation docNode = docNodes.get(0);
    Assert.assertNotNull(docNode);
    Assert.assertEquals(docNode.documentationText, "\n Transformer Foo Person -> Employee\n ");
    Assert.assertEquals(docNode.getAttributes().size(), 3);
    Assert.assertEquals(docNode.getAttributes().get(0).documentationField.getValue(), "p");
    Assert.assertEquals(docNode.getAttributes().get(0).documentationText, " input struct Person source used for transformation\n ");
    Assert.assertEquals(docNode.getAttributes().get(1).documentationField.getValue(), "e");
    Assert.assertEquals(docNode.getAttributes().get(1).documentationText, " output struct Employee struct which Person transformed to\n ");
    Assert.assertEquals(docNode.getAttributes().get(2).documentationField.getValue(), "defaultAddress");
    Assert.assertEquals(docNode.getAttributes().get(2).documentationText, " address which serves Eg: `POSTCODE 112`\n");
}
Also used : BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) CompileResult(org.ballerinalang.launcher.util.CompileResult) BLangDeprecatedNode(org.wso2.ballerinalang.compiler.tree.BLangDeprecatedNode) PackageNode(org.ballerinalang.model.tree.PackageNode) Test(org.testng.annotations.Test)

Example 95 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class DocumentationTest method testDocFunction.

@Test(description = "Test doc function.")
public void testDocFunction() {
    CompileResult compileResult = BCompileUtil.compile("test-src/documentation/function.bal");
    Assert.assertEquals(0, compileResult.getWarnCount());
    PackageNode packageNode = compileResult.getAST();
    List<BLangDocumentation> docNodes = ((BLangFunction) packageNode.getFunctions().get(0)).docAttachments;
    BLangDocumentation dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + "Gets a access parameter value (`true` or `false`) for a given key. " + "Please note that #foo will always be bigger than #bar.\n" + "Example:\n" + "``SymbolEnv pkgEnv = symbolEnter.packageEnvs.get(pkgNode.symbol);``\n");
    Assert.assertEquals(dNode.getAttributes().size(), 2);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "file");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " file path ``C:\\users\\OddThinking\\Documents\\My Source\\Widget\\foo.src``\n");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "accessMode");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " read or write mode\n");
    docNodes = ((BLangStruct) packageNode.getStructs().get(0)).docAttachments;
    dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, " Documentation for File struct\n");
    Assert.assertEquals(dNode.getAttributes().size(), 1);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "path");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " struct `field path` documentation\n");
}
Also used : BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) CompileResult(org.ballerinalang.launcher.util.CompileResult) PackageNode(org.ballerinalang.model.tree.PackageNode) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)22 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)15 ArrayList (java.util.ArrayList)11 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)11 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)11 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)11 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)8 ParseTree (org.antlr.v4.runtime.tree.ParseTree)8 OMElement (org.apache.axiom.om.OMElement)8 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)8 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)8 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)8 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)8 Event (org.wso2.siddhi.core.event.Event)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Compiler (org.wso2.ballerinalang.compiler.Compiler)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 SiddhiQLBaseVisitorImpl (org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl)6