Search in sources :

Example 6 with BLangCompilerException

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

the class ZipUtils method addFileToArchive.

/**
 * Add file to archive.
 *
 * @param filesToBeArchived files to be archived
 * @param zipFS             Zip file system
 * @param outFileName       output archive name
 */
private static void addFileToArchive(Stream<Path> filesToBeArchived, FileSystem zipFS, String outFileName) {
    filesToBeArchived.forEach((path) -> {
        Path root = zipFS.getPath(SRC_DIR);
        String fileName = path.getFileName().toString();
        Path dest = zipFS.getPath(root.toString(), fileName);
        if (path.getFileName().toString().equals("Ballerina.md")) {
            dest = zipFS.getPath(File.separator, fileName);
        }
        try {
            if (Files.exists(path)) {
                copyFileToArchive(new FileInputStream(path.toString()), dest);
            }
        } catch (IOException e) {
            throw new BLangCompilerException("error generating artifact " + outFileName);
        }
    });
}
Also used : Path(java.nio.file.Path) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 7 with BLangCompilerException

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

the class ZipUtils method createArchive.

/**
 * Create archive when creating the balo.
 *
 * @param filesToBeArchived files to be archived
 * @param outFileName       output archive name
 */
private static void createArchive(Stream<Path> filesToBeArchived, String outFileName) {
    Map<String, String> zipFSEnv = new HashMap<>();
    zipFSEnv.put("create", "true");
    URI filepath = Paths.get(outFileName).toUri();
    URI zipFileURI;
    try {
        zipFileURI = new URI("jar:" + filepath.getScheme(), filepath.getUserInfo(), filepath.getHost(), filepath.getPort(), filepath.getPath() + "!/", filepath.getQuery(), filepath.getFragment());
    } catch (URISyntaxException ignore) {
        throw new BLangCompilerException("error creating artifact" + outFileName);
    }
    try (FileSystem zipFS = FileSystems.newFileSystem(zipFileURI, zipFSEnv)) {
        addFileToArchive(filesToBeArchived, zipFS, outFileName);
    } catch (IOException e) {
        throw new BLangCompilerException("error creating artifact" + outFileName);
    }
}
Also used : HashMap(java.util.HashMap) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) FileSystem(java.nio.file.FileSystem) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI)

Example 8 with BLangCompilerException

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

the class ZipUtils method generateBalo.

/**
 * Generates the balo/zip of the package.
 *
 * @param bLangPackage bLangPackage node
 * @param projectPath  project path
 * @param paths        paths of bal files inside the package
 */
static void generateBalo(BLangPackage bLangPackage, String projectPath, Stream<Path> paths) {
    PackageID packageID = bLangPackage.packageID;
    Path destPath = Paths.get(projectPath).resolve(".ballerina").resolve("repo").resolve(packageID.getOrgName().getValue()).resolve(packageID.getName().getValue()).resolve(packageID.getPackageVersion().getValue());
    if (!Files.exists(destPath)) {
        try {
            Files.createDirectories(destPath);
        } catch (IOException e) {
            throw new BLangCompilerException("Error occurred when creating directories in " + "./ballerina/repo/ to save the generated balo");
        }
    }
    String fileName = packageID.getName() + ".zip";
    String baloDirPath = destPath.resolve(fileName).toString();
    createArchive(paths, baloDirPath);
}
Also used : Path(java.nio.file.Path) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) PackageID(org.ballerinalang.model.elements.PackageID) IOException(java.io.IOException)

Example 9 with BLangCompilerException

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

the class ZipConverter method initFS.

private static void initFS(URI uri) {
    Map<String, String> env = new HashMap<>();
    env.put("create", "true");
    try {
        FileSystems.newFileSystem(uri, env);
    } catch (FileSystemAlreadyExistsException ignore) {
    } catch (IOException e) {
        throw new BLangCompilerException("Error loading balo " + uri.getPath(), e);
    }
}
Also used : HashMap(java.util.HashMap) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) FileSystemAlreadyExistsException(java.nio.file.FileSystemAlreadyExistsException) IOException(java.io.IOException)

Example 10 with BLangCompilerException

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

the class PushUtils method resolvePkgPathInRemoteRepo.

/**
 * Get URI of the package from the remote repo.
 *
 * @param packageID packageID object
 * @return full URI path of the package relative to the remote repo
 */
private static String resolvePkgPathInRemoteRepo(PackageID packageID) {
    Repo<URI> remoteRepo = new RemoteRepo(URI.create("https://staging.central.ballerina.io:9090/"));
    Patten patten = remoteRepo.calculate(packageID);
    if (patten == Patten.NULL) {
        throw new BLangCompilerException("Couldn't find package " + packageID.toString());
    }
    Converter<URI> converter = remoteRepo.getConverterInstance();
    List<URI> uris = patten.convert(converter).collect(Collectors.toList());
    if (uris.isEmpty()) {
        throw new BLangCompilerException("Couldn't find package " + packageID.toString());
    }
    return uris.get(0).toString();
}
Also used : BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) RemoteRepo(org.wso2.ballerinalang.compiler.packaging.repo.RemoteRepo) Patten(org.wso2.ballerinalang.compiler.packaging.Patten) URI(java.net.URI)

Aggregations

BLangCompilerException (org.ballerinalang.compiler.BLangCompilerException)11 IOException (java.io.IOException)7 Path (java.nio.file.Path)6 URI (java.net.URI)5 PackageID (org.ballerinalang.model.elements.PackageID)4 RemoteRepo (org.wso2.ballerinalang.compiler.packaging.repo.RemoteRepo)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HashMap (java.util.HashMap)2 Manifest (org.ballerinalang.toml.model.Manifest)2 Patten (org.wso2.ballerinalang.compiler.packaging.Patten)2 Name (org.wso2.ballerinalang.compiler.util.Name)2 FileInputStream (java.io.FileInputStream)1 URISyntaxException (java.net.URISyntaxException)1 FileSystem (java.nio.file.FileSystem)1 FileSystemAlreadyExistsException (java.nio.file.FileSystemAlreadyExistsException)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1