Search in sources :

Example 1 with FileSystemProjectDirectory

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

the class ParserUtils method getBallerinaFile.

/**
 * Returns an object which contains Ballerina model and Diagnostic information.
 *
 * @param fileName - File name
 * @param context  - CompilerContext
 * @return BallerinaFile - Object which contains Ballerina model and Diagnostic information
 */
private static BallerinaFile getBallerinaFile(Path packagePath, String fileName, CompilerContext context) {
    List<Diagnostic> diagnostics = new ArrayList<>();
    ComposerDiagnosticListener composerDiagnosticListener = new ComposerDiagnosticListener(diagnostics);
    context.put(DiagnosticListener.class, composerDiagnosticListener);
    BallerinaFile ballerinaFile = new BallerinaFile();
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, packagePath.toString());
    options.put(COMPILER_PHASE, CompilerPhase.DEFINE.toString());
    options.put(PRESERVE_WHITESPACE, "true");
    context.put(SourceDirectory.class, new FileSystemProjectDirectory(packagePath));
    Compiler compiler = Compiler.getInstance(context);
    // compile
    try {
        ballerinaFile.setBLangPackage(compiler.compile(fileName));
    } catch (Exception ex) {
        BDiagnostic catastrophic = new BDiagnostic();
        catastrophic.msg = "Failed in the runtime parse/analyze. " + ex.getMessage();
        diagnostics.add(catastrophic);
    }
    ballerinaFile.setDiagnostics(diagnostics);
    return ballerinaFile;
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) FileSystemProjectDirectory(org.wso2.ballerinalang.compiler.FileSystemProjectDirectory) BallerinaFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile) ArrayList(java.util.ArrayList) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) BDiagnostic(org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnostic) BDiagnostic(org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnostic) IOException(java.io.IOException)

Example 2 with FileSystemProjectDirectory

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

the class BCompileUtil method compile.

/**
 * Compile and return the semantic errors.
 *
 * @param obj this is to find the original callers location.
 * @param sourceRoot  root path of the source packages
 * @param packageName name of the package to compile
 * @return Semantic errors
 */
public static CompileResult compile(Object obj, String sourceRoot, String packageName) {
    try {
        String effectiveSource;
        CodeSource codeSource = obj.getClass().getProtectionDomain().getCodeSource();
        URL location = codeSource.getLocation();
        URI locationUri = location.toURI();
        Path pathLocation = Paths.get(locationUri);
        String filePath = concatFileName(sourceRoot, pathLocation);
        Path rootPath = Paths.get(filePath);
        Path packagePath = Paths.get(packageName);
        if (Files.isDirectory(packagePath)) {
            String[] pkgParts = packageName.split("\\/");
            List<Name> pkgNameComps = Arrays.stream(pkgParts).map(part -> {
                if (part.equals("")) {
                    return Names.EMPTY;
                } else if (part.equals("_")) {
                    return Names.EMPTY;
                }
                return new Name(part);
            }).collect(Collectors.toList());
            // TODO: orgName is anon, fix it.
            PackageID pkgId = new PackageID(Names.ANON_ORG, pkgNameComps, Names.DEFAULT_VERSION);
            effectiveSource = pkgId.getName().getValue();
            return compile(rootPath.toString(), effectiveSource, CompilerPhase.CODE_GEN);
        } else {
            effectiveSource = packageName;
            return compile(rootPath.toString(), effectiveSource, CompilerPhase.CODE_GEN, new FileSystemProjectDirectory(rootPath));
        }
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("error while running test: " + e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) CompilerUtils(org.wso2.ballerinalang.compiler.util.CompilerUtils) ProgramFile(org.ballerinalang.util.codegen.ProgramFile) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) Compiler(org.wso2.ballerinalang.compiler.Compiler) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) PROJECT_DIR(org.ballerinalang.compiler.CompilerOptionName.PROJECT_DIR) SourceDirectory(org.wso2.ballerinalang.compiler.SourceDirectory) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) Names(org.wso2.ballerinalang.compiler.util.Names) BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) FileSystemProjectDirectory(org.wso2.ballerinalang.compiler.FileSystemProjectDirectory) URI(java.net.URI) PRESERVE_WHITESPACE(org.ballerinalang.compiler.CompilerOptionName.PRESERVE_WHITESPACE) Path(java.nio.file.Path) LauncherUtils(org.ballerinalang.launcher.LauncherUtils) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener) Files(java.nio.file.Files) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) PackageID(org.ballerinalang.model.elements.PackageID) COMPILER_PHASE(org.ballerinalang.compiler.CompilerOptionName.COMPILER_PHASE) IOException(java.io.IOException) BStructType(org.ballerinalang.model.types.BStructType) CompiledBinaryFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) Name(org.wso2.ballerinalang.compiler.util.Name) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Paths(java.nio.file.Paths) BufferedReader(java.io.BufferedReader) CodeSource(java.security.CodeSource) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) InputStream(java.io.InputStream) FileSystemProjectDirectory(org.wso2.ballerinalang.compiler.FileSystemProjectDirectory) URISyntaxException(java.net.URISyntaxException) CodeSource(java.security.CodeSource) URI(java.net.URI) URL(java.net.URL) Name(org.wso2.ballerinalang.compiler.util.Name) PackageID(org.ballerinalang.model.elements.PackageID)

Example 3 with FileSystemProjectDirectory

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

the class TestCmd method execute.

public void execute() {
    if (helpFlag) {
        printCommandUsageInfo(parentCmdParser, "test");
        return;
    }
    if (sourceFileList == null || sourceFileList.isEmpty()) {
        Path userDir = Paths.get(System.getProperty("user.dir"));
        SourceDirectory srcDirectory = new FileSystemProjectDirectory(userDir);
        sourceFileList = srcDirectory.getSourcePackageNames();
    }
    if (groupList != null && disableGroupList != null) {
        throw LauncherUtils.createUsageException("Cannot specify both --groups and --disable-groups flags at the same time");
    }
    Path sourceRootPath = LauncherUtils.getSourceRootPath(sourceRoot);
    Path ballerinaConfPath = sourceRootPath.resolve("ballerina.conf");
    try {
        ConfigRegistry.getInstance().initRegistry(configRuntimeParams, null, ballerinaConfPath);
        ((BLogManager) LogManager.getLogManager()).loadUserProvidedLogConfiguration();
    } catch (IOException e) {
        throw new RuntimeException("failed to read the specified configuration file: " + ballerinaConfPath.toString(), e);
    }
    Path[] paths = sourceFileList.stream().map(Paths::get).toArray(Path[]::new);
    BTestRunner testRunner = new BTestRunner();
    if (disableGroupList != null) {
        testRunner.runTest(sourceRoot, paths, disableGroupList, false);
    } else {
        testRunner.runTest(sourceRoot, paths, groupList, true);
    }
    if (testRunner.getTesterinaReport().isFailure()) {
        Runtime.getRuntime().exit(1);
    }
    Runtime.getRuntime().exit(0);
}
Also used : Path(java.nio.file.Path) BLogManager(org.ballerinalang.logging.BLogManager) FileSystemProjectDirectory(org.wso2.ballerinalang.compiler.FileSystemProjectDirectory) SourceDirectory(org.wso2.ballerinalang.compiler.SourceDirectory) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)3 FileSystemProjectDirectory (org.wso2.ballerinalang.compiler.FileSystemProjectDirectory)3 Path (java.nio.file.Path)2 Diagnostic (org.ballerinalang.util.diagnostic.Diagnostic)2 Compiler (org.wso2.ballerinalang.compiler.Compiler)2 SourceDirectory (org.wso2.ballerinalang.compiler.SourceDirectory)2 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)2 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 CodeSource (java.security.CodeSource)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1