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;
}
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());
}
}
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);
}
Aggregations