use of org.ballerinalang.compiler.CompilerOptionName.PRESERVE_WHITESPACE 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);
}
}
Aggregations