Search in sources :

Example 1 with BLANG_SRC_FILE_SUFFIX

use of org.ballerinalang.util.BLangConstants.BLANG_SRC_FILE_SUFFIX 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 2 with BLANG_SRC_FILE_SUFFIX

use of org.ballerinalang.util.BLangConstants.BLANG_SRC_FILE_SUFFIX in project ballerina by ballerina-lang.

the class UserRepositoryUtils method uninstallSourcePackage.

public static void uninstallSourcePackage(String packageStr) {
    Path packagePath = Paths.get(packageStr);
    Path userRepoSrcPath = initializeUserRepository().resolve(USER_REPO_ARTIFACTS_DIRNAME).resolve(USER_REPO_SRC_DIRNAME);
    Path dirPathToDelete = userRepoSrcPath.resolve(packagePath);
    if (Files.exists(dirPathToDelete, LinkOption.NOFOLLOW_LINKS) && !Files.isDirectory(dirPathToDelete, LinkOption.NOFOLLOW_LINKS)) {
        throw new RuntimeException("a file exists with the same name as the package name: " + dirPathToDelete.toString());
    } else if (!Files.exists(dirPathToDelete, LinkOption.NOFOLLOW_LINKS)) {
        // File doesn't exists
        throw new RuntimeException("package does not exist: " + dirPathToDelete.toString());
    }
    try {
        Files.list(dirPathToDelete).filter(filePath -> !Files.isDirectory(filePath, LinkOption.NOFOLLOW_LINKS)).filter(filePath -> filePath.toString().endsWith(BLANG_SRC_FILE_SUFFIX)).forEach(filePath -> {
            try {
                Files.delete(filePath);
            } catch (IOException e) {
                throw new RuntimeException("error uninstalling package: " + packageStr + ": " + e.getMessage(), e);
            }
        });
        deleteEmptyDirsUpTo(dirPathToDelete, userRepoSrcPath);
    } catch (DirectoryNotEmptyException e) {
        throw new RuntimeException("error uninstalling package: " + packageStr + ": directory not empty: " + dirPathToDelete.toString(), e);
    } catch (IOException e) {
        throw new RuntimeException("error uninstalling 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) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 CopyOption (java.nio.file.CopyOption)2 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)2 Files (java.nio.file.Files)2 LinkOption (java.nio.file.LinkOption)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2 StandardCopyOption (java.nio.file.StandardCopyOption)2 COMPILER_PHASE (org.ballerinalang.compiler.CompilerOptionName.COMPILER_PHASE)2 PRESERVE_WHITESPACE (org.ballerinalang.compiler.CompilerOptionName.PRESERVE_WHITESPACE)2 PROJECT_DIR (org.ballerinalang.compiler.CompilerOptionName.PROJECT_DIR)2 CompilerPhase (org.ballerinalang.compiler.CompilerPhase)2 BLANG_SRC_FILE_SUFFIX (org.ballerinalang.util.BLangConstants.BLANG_SRC_FILE_SUFFIX)2 USER_HOME (org.ballerinalang.util.BLangConstants.USER_HOME)2 USER_REPO_ARTIFACTS_DIRNAME (org.ballerinalang.util.BLangConstants.USER_REPO_ARTIFACTS_DIRNAME)2 USER_REPO_DEFAULT_DIRNAME (org.ballerinalang.util.BLangConstants.USER_REPO_DEFAULT_DIRNAME)2 USER_REPO_ENV_KEY (org.ballerinalang.util.BLangConstants.USER_REPO_ENV_KEY)2 USER_REPO_OBJ_DIRNAME (org.ballerinalang.util.BLangConstants.USER_REPO_OBJ_DIRNAME)2 USER_REPO_SRC_DIRNAME (org.ballerinalang.util.BLangConstants.USER_REPO_SRC_DIRNAME)2 BLangPrograms (org.ballerinalang.util.program.BLangPrograms)2