Search in sources :

Example 6 with Compiler

use of org.wso2.ballerinalang.compiler.Compiler 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 7 with Compiler

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

the class LSPackageCache method findPackage.

/**
 * Find the package by Package ID.
 * @param compilerContext       compiler context
 * @param pkgId                 Package Id to lookup
 * @return {@link BLangPackage} BLang Package resolved
 */
public BLangPackage findPackage(CompilerContext compilerContext, PackageID pkgId) {
    if (containsPackage(pkgId.getName().getValue())) {
        return packageMap.get(pkgId.getName().getValue());
    } else {
        BLangPackage bLangPackage = LSPackageLoader.getPackageById(compilerContext, pkgId);
        addPackage(bLangPackage);
        return bLangPackage;
    }
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage)

Example 8 with Compiler

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

the class BuilderUtils method compileAndWrite.

public static void compileAndWrite(Path sourceRootPath, Path packagePath, Path targetPath, boolean buildCompiledPkg, boolean offline) {
    CompilerContext context = new CompilerContext();
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, sourceRootPath.toString());
    options.put(OFFLINE, Boolean.toString(offline));
    options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString());
    options.put(PRESERVE_WHITESPACE, "false");
    options.put(BUILD_COMPILED_PACKAGE, Boolean.toString(buildCompiledPkg));
    Compiler compiler = Compiler.getInstance(context);
    compiler.build();
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions)

Example 9 with Compiler

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

the class UserRepositoryUtils method installSourcePackage.

public static void installSourcePackage(Path sourceRootPath, String packageStr) {
    // First, we need to validate what is in their.
    // Let's try to compile and see.
    // If it won't compile, we won't install the package
    Path packagePath = Paths.get(packageStr);
    CompilerContext context = new CompilerContext();
    CompilerOptions cOptions = CompilerOptions.getInstance(context);
    cOptions.put(PROJECT_DIR, sourceRootPath.toString());
    cOptions.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString());
    cOptions.put(PRESERVE_WHITESPACE, "false");
    // compile
    Compiler compiler = Compiler.getInstance(context);
    compiler.compile(packagePath.toString());
    Path srcDirectoryPath = BLangPrograms.validateAndResolveSourcePath(sourceRootPath, packagePath);
    Path targetDirectoryPath = initializeUserRepository().resolve(USER_REPO_ARTIFACTS_DIRNAME).resolve(USER_REPO_SRC_DIRNAME).resolve(packagePath);
    try {
        Files.list(srcDirectoryPath).filter(filePath -> !Files.isDirectory(filePath, LinkOption.NOFOLLOW_LINKS)).filter(filePath -> filePath.toString().endsWith(BLANG_SRC_FILE_SUFFIX)).forEach(filePath -> {
            // Here we get the absolute path.
            // Just get the file name and copy.
            Path srcNamePath = filePath.getFileName();
            Path targetFilePath = targetDirectoryPath.resolve(srcNamePath);
            CopyOption[] options = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES, LinkOption.NOFOLLOW_LINKS };
            try {
                if (Files.exists(targetDirectoryPath, LinkOption.NOFOLLOW_LINKS) && !Files.isDirectory(targetDirectoryPath, LinkOption.NOFOLLOW_LINKS)) {
                    throw new RuntimeException("a file exists with the same name as the package name: " + targetDirectoryPath.toString());
                } else if (!Files.exists(targetDirectoryPath, LinkOption.NOFOLLOW_LINKS)) {
                    Files.createDirectories(targetDirectoryPath);
                }
                Files.copy(filePath, targetFilePath, options);
            } catch (IOException e) {
                throw new RuntimeException("error installing package: " + packageStr + ": " + e.getMessage(), e);
            }
        });
    } catch (IOException e) {
        throw new RuntimeException("error installing package: " + packageStr + ": " + e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) Files(java.nio.file.Files) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) COMPILER_PHASE(org.ballerinalang.compiler.CompilerOptionName.COMPILER_PHASE) IOException(java.io.IOException) BLangPrograms.createDirectory(org.ballerinalang.util.program.BLangPrograms.createDirectory) Compiler(org.wso2.ballerinalang.compiler.Compiler) BLANG_SRC_FILE_SUFFIX(org.ballerinalang.util.BLangConstants.BLANG_SRC_FILE_SUFFIX) StandardCopyOption(java.nio.file.StandardCopyOption) PROJECT_DIR(org.ballerinalang.compiler.CompilerOptionName.PROJECT_DIR) LinkOption(java.nio.file.LinkOption) USER_REPO_DEFAULT_DIRNAME(org.ballerinalang.util.BLangConstants.USER_REPO_DEFAULT_DIRNAME) USER_REPO_ARTIFACTS_DIRNAME(org.ballerinalang.util.BLangConstants.USER_REPO_ARTIFACTS_DIRNAME) Paths(java.nio.file.Paths) USER_REPO_SRC_DIRNAME(org.ballerinalang.util.BLangConstants.USER_REPO_SRC_DIRNAME) BLangPrograms(org.ballerinalang.util.program.BLangPrograms) USER_REPO_ENV_KEY(org.ballerinalang.util.BLangConstants.USER_REPO_ENV_KEY) PRESERVE_WHITESPACE(org.ballerinalang.compiler.CompilerOptionName.PRESERVE_WHITESPACE) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) Path(java.nio.file.Path) USER_HOME(org.ballerinalang.util.BLangConstants.USER_HOME) USER_REPO_OBJ_DIRNAME(org.ballerinalang.util.BLangConstants.USER_REPO_OBJ_DIRNAME) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CopyOption(java.nio.file.CopyOption) Compiler(org.wso2.ballerinalang.compiler.Compiler) StandardCopyOption(java.nio.file.StandardCopyOption) CopyOption(java.nio.file.CopyOption) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) IOException(java.io.IOException)

Example 10 with Compiler

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

the class BCompileUtil method compile.

/**
 * Compile and return the semantic errors.
 *
 * @param sourceRoot    root path of the source packages
 * @param packageName   name of the package to compile
 * @param compilerPhase Compiler phase
 * @return Semantic errors
 */
public static CompileResult compile(String sourceRoot, String packageName, CompilerPhase compilerPhase) {
    CompilerContext context = new CompilerContext();
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, sourceRoot);
    options.put(COMPILER_PHASE, compilerPhase.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);
    BLangPackage packageNode = compiler.compile(packageName);
    comResult.setAST(packageNode);
    if (comResult.getErrorCount() > 0 || CompilerPhase.CODE_GEN.compareTo(compilerPhase) > 0) {
        return comResult;
    }
    CompiledBinaryFile.ProgramFile programFile = compiler.getExecutableProgram(packageNode);
    if (programFile != null) {
        ProgramFile pFile = LauncherUtils.getExecutableProgram(programFile);
        comResult.setProgFile(pFile);
        if (pFile != null) {
            boolean distributedTxEnabled = CompilerUtils.isDistributedTransactionsEnabled();
            pFile.setDistributedTransactionEnabled(distributedTxEnabled);
        }
    }
    return comResult;
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) CompiledBinaryFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Aggregations

CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)15 Compiler (org.wso2.ballerinalang.compiler.Compiler)14 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)13 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)12 ArrayList (java.util.ArrayList)8 Path (java.nio.file.Path)7 DiagnosticListener (org.ballerinalang.util.diagnostic.DiagnosticListener)4 CompiledBinaryFile (org.wso2.ballerinalang.programfile.CompiledBinaryFile)4 IOException (java.io.IOException)3 LSDocument (org.ballerinalang.langserver.common.LSDocument)3 WorkspacePackageRepository (org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository)3 Diagnostic (org.ballerinalang.util.diagnostic.Diagnostic)3 BDiagnostic (org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnostic)3 CompilerPhase (org.ballerinalang.compiler.CompilerPhase)2 CompilerPlugin (org.ballerinalang.compiler.plugins.CompilerPlugin)2 SupportedAnnotationPackages (org.ballerinalang.compiler.plugins.SupportedAnnotationPackages)2 BallerinaFile (org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile)2 PackageRepository (org.ballerinalang.repository.PackageRepository)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 File (java.io.File)1