use of org.wso2.ballerinalang.compiler.util.CompilerContext in project ballerina by ballerina-lang.
the class ParserUtils method prepareCompilerContext.
/**
* Returns a CompilerContext for the provided fileName and Ballerina source content.
*
* @param fileName - File name. This can be any arbitrary name as as we haven't save the file yet.
* @param source - Ballerina source content that needs to be parsed.
* @return CompilerContext
*/
private static CompilerContext prepareCompilerContext(String fileName, String source) {
CompilerContext context = new CompilerContext();
List<Name> names = new ArrayList<>();
names.add(new org.wso2.ballerinalang.compiler.util.Name("."));
// Registering custom PackageRepository to provide ballerina content without a file in file-system
context.put(PackageRepository.class, new InMemoryPackageRepository(PackageID.DEFAULT, "", fileName, source.getBytes(StandardCharsets.UTF_8)));
return context;
}
use of org.wso2.ballerinalang.compiler.util.CompilerContext 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;
}
}
use of org.wso2.ballerinalang.compiler.util.CompilerContext 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();
}
use of org.wso2.ballerinalang.compiler.util.CompilerContext 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);
}
}
use of org.wso2.ballerinalang.compiler.util.CompilerContext 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;
}
Aggregations