Search in sources :

Example 1 with BLangCompilerException

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

the class PushUtils method pushPackages.

/**
 * Push/Uploads packages to the central repository.
 *
 * @param packageName   path of the package folder to be pushed
 * @param installToRepo if it should be pushed to central or home
 */
public static void pushPackages(String packageName, String installToRepo) {
    String accessToken = getAccessTokenOfCLI();
    Manifest manifest = readManifestConfigurations();
    if (manifest.getName() == null && manifest.getVersion() == null) {
        throw new BLangCompilerException("An org-name and package version is required when pushing. " + "This is not specified in Ballerina.toml inside the project");
    }
    String orgName = manifest.getName();
    String version = manifest.getVersion();
    PackageID packageID = new PackageID(new Name(orgName), new Name(packageName), new Name(version));
    Path prjDirPath = Paths.get(".").toAbsolutePath().normalize().resolve(ProjectDirConstants.DOT_BALLERINA_DIR_NAME);
    // Get package path from project directory path
    Path pkgPathFromPrjtDir = Paths.get(prjDirPath.toString(), "repo", Names.ANON_ORG.getValue(), packageName, Names.DEFAULT_VERSION.getValue(), packageName + ".zip");
    if (installToRepo == null) {
        if (accessToken == null) {
            // TODO: get bal home location dynamically
            throw new BLangCompilerException("Access token is missing in ~/ballerina_home/Settings.toml file.\n" + "Please visit https://central.ballerina.io/cli-token");
        }
        // Push package to central
        String resourcePath = resolvePkgPathInRemoteRepo(packageID);
        URI balxPath = URI.create(String.valueOf(PushUtils.class.getClassLoader().getResource("ballerina.push.balx")));
        String msg = orgName + "/" + packageName + ":" + version + " [project repo -> central]";
        ExecutorUtils.execute(balxPath, accessToken, resourcePath, pkgPathFromPrjtDir.toString(), msg);
    } else {
        if (!installToRepo.equals("home")) {
            throw new BLangCompilerException("Unknown repository provided to push the package");
        }
        Path balHomeDir = HomeRepoUtils.createAndGetHomeReposPath();
        Path targetDirectoryPath = Paths.get(balHomeDir.toString(), "repo", orgName, packageName, version, packageName + ".zip");
        if (Files.exists(targetDirectoryPath)) {
            throw new BLangCompilerException("Ballerina package exists in the home repository");
        } else {
            try {
                Files.createDirectories(targetDirectoryPath);
                Files.copy(pkgPathFromPrjtDir, targetDirectoryPath, StandardCopyOption.REPLACE_EXISTING);
                outStream.println(orgName + "/" + packageName + ":" + version + " [project repo -> home repo]");
            } catch (IOException e) {
                throw new BLangCompilerException("Error occurred when creating directories in the home repository");
            }
        }
    }
}
Also used : Path(java.nio.file.Path) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) PackageID(org.ballerinalang.model.elements.PackageID) IOException(java.io.IOException) Manifest(org.ballerinalang.toml.model.Manifest) URI(java.net.URI) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 2 with BLangCompilerException

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

the class PullCommand method execute.

@Override
public void execute() {
    if (helpFlag) {
        String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(parentCmdParser, "pull");
        outStream.println(commandUsageInfo);
        return;
    }
    if (argList == null || argList.size() == 0) {
        throw new BLangCompilerException("no package given");
    }
    if (argList.size() > 1) {
        throw new BLangCompilerException("too many arguments");
    }
    // Enable remote debugging
    if (null != debugPort) {
        System.setProperty(SYSTEM_PROP_BAL_DEBUG, debugPort);
    }
    String resourceName = argList.get(0);
    String orgName;
    String packageName;
    String version;
    // Get org-name
    int orgNameIndex = resourceName.indexOf("/");
    if (orgNameIndex != -1) {
        orgName = resourceName.substring(0, orgNameIndex);
    } else {
        throw new BLangCompilerException("no package-name provided");
    }
    // Get package name
    int packageNameIndex = resourceName.indexOf(":");
    if (packageNameIndex != -1) {
        // version is provided
        packageName = resourceName.substring(orgNameIndex + 1, packageNameIndex);
        version = resourceName.substring(packageNameIndex + 1, resourceName.length());
    } else {
        packageName = resourceName.substring(orgNameIndex + 1, resourceName.length());
        version = "*";
    }
    URI baseURI = URI.create("https://staging.central.ballerina.io:9090/");
    Repo remoteRepo = new RemoteRepo(baseURI);
    PackageID packageID = new PackageID(new Name(orgName), new Name(packageName), new Name(version));
    Patten patten = remoteRepo.calculate(packageID);
    if (patten != Patten.NULL) {
        Converter converter = remoteRepo.getConverterInstance();
        patten.convertToSources(converter, packageID).collect(Collectors.toList());
    } else {
        outStream.println("couldn't find package " + patten);
    }
    Runtime.getRuntime().exit(0);
}
Also used : RemoteRepo(org.wso2.ballerinalang.compiler.packaging.repo.RemoteRepo) Repo(org.wso2.ballerinalang.compiler.packaging.repo.Repo) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) Converter(org.wso2.ballerinalang.compiler.packaging.converters.Converter) RemoteRepo(org.wso2.ballerinalang.compiler.packaging.repo.RemoteRepo) PackageID(org.ballerinalang.model.elements.PackageID) Patten(org.wso2.ballerinalang.compiler.packaging.Patten) URI(java.net.URI) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 3 with BLangCompilerException

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

the class BinaryFileWriter method writeLibraryPackage.

public void writeLibraryPackage(BLangPackage packageNode) {
    String fileName = getOutputFileName(packageNode, BLANG_COMPILED_PACKAGE_FILE_SUFFIX);
    CompiledBinaryFile.PackageFile packageFile = this.codeGenerator.generateBALO(packageNode);
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    try {
        PackageFileWriter.writePackage(packageFile, byteArrayOS);
    } catch (IOException e) {
        throw new BLangCompilerException("error writing package file '" + fileName + "'", e);
    }
    this.sourceDirectory.saveCompiledPackage(new ByteArrayInputStream(byteArrayOS.toByteArray()), fileName);
}
Also used : CompiledBinaryFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile) ByteArrayInputStream(java.io.ByteArrayInputStream) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 4 with BLangCompilerException

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

the class BinaryFileWriter method writeExecutableBinary.

public void writeExecutableBinary(BLangPackage packageNode, String fileName) {
    if (fileName == null || fileName.isEmpty()) {
        throw new IllegalArgumentException("invalid target file name");
    }
    if (!fileName.endsWith(BLANG_EXEC_FILE_SUFFIX)) {
        fileName += BLANG_EXEC_FILE_SUFFIX;
    }
    // Generate code for the given executable
    ProgramFile programFile = this.codeGenerator.generateBALX(packageNode);
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    try {
        ProgramFileWriter.writeProgram(programFile, byteArrayOS);
    } catch (IOException e) {
        throw new BLangCompilerException("error writing program file '" + fileName + "'", e);
    }
    final Path execFilePath = this.sourceDirectory.saveCompiledProgram(new ByteArrayInputStream(byteArrayOS.toByteArray()), fileName);
    ServiceLoader<CompilerPlugin> processorServiceLoader = ServiceLoader.load(CompilerPlugin.class);
    processorServiceLoader.forEach(plugin -> {
        plugin.codeGenerated(execFilePath);
    });
}
Also used : Path(java.nio.file.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) CompilerPlugin(org.ballerinalang.compiler.plugins.CompilerPlugin) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ProgramFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile.ProgramFile)

Example 5 with BLangCompilerException

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

the class PackageLoader method updateVersionFromToml.

private void updateVersionFromToml(PackageID pkgId) {
    String orgName = pkgId.orgName.value;
    String pkgName = pkgId.name.value;
    String pkgAlias = orgName + "/" + pkgName;
    // TODO: make getDependencies return a map
    Optional<Dependency> dependency = manifest.getDependencies().stream().filter(d -> d.getPackageName().equals(pkgAlias)).findFirst();
    if (dependency.isPresent()) {
        if (pkgId.version.value.isEmpty()) {
            pkgId.version = new Name(dependency.get().getVersion());
        } else {
            throw new BLangCompilerException("dependency version in Ballerina.toml mismatches" + " with the version in the source for package " + pkgAlias);
        }
    }
}
Also used : SystemPackageRepositoryProvider(org.ballerinalang.spi.SystemPackageRepositoryProvider) Arrays(java.util.Arrays) PackageSource(org.ballerinalang.repository.PackageSource) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) PROJECT_DIR(org.ballerinalang.compiler.CompilerOptionName.PROJECT_DIR) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) Converter(org.wso2.ballerinalang.compiler.packaging.converters.Converter) Names(org.wso2.ballerinalang.compiler.util.Names) RepoHierarchy(org.wso2.ballerinalang.compiler.packaging.RepoHierarchy) URI(java.net.URI) Path(java.nio.file.Path) ProjectSourceRepo(org.wso2.ballerinalang.compiler.packaging.repo.ProjectSourceRepo) PackageID(org.ballerinalang.model.elements.PackageID) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Manifest(org.ballerinalang.toml.model.Manifest) OFFLINE(org.ballerinalang.compiler.CompilerOptionName.OFFLINE) Resolution(org.wso2.ballerinalang.compiler.packaging.Resolution) RepoHierarchyBuilder(org.wso2.ballerinalang.compiler.packaging.RepoHierarchyBuilder) Optional(java.util.Optional) SymbolEnter(org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolEnter) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) ManifestProcessor(org.ballerinalang.toml.parser.ManifestProcessor) RemoteRepo(org.wso2.ballerinalang.compiler.packaging.repo.RemoteRepo) ArrayList(java.util.ArrayList) Dependency(org.ballerinalang.toml.model.Dependency) RepoHierarchyBuilder.node(org.wso2.ballerinalang.compiler.packaging.RepoHierarchyBuilder.node) HomeRepoUtils(org.wso2.ballerinalang.util.HomeRepoUtils) PackageRepository(org.ballerinalang.repository.PackageRepository) Repo(org.wso2.ballerinalang.compiler.packaging.repo.Repo) TreeBuilder(org.ballerinalang.model.TreeBuilder) ProgramingSourceRepo(org.wso2.ballerinalang.compiler.packaging.repo.ProgramingSourceRepo) ProjectDirs(org.wso2.ballerinalang.compiler.util.ProjectDirs) StreamSupport(java.util.stream.StreamSupport) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) Name(org.wso2.ballerinalang.compiler.util.Name) ZipRepo(org.wso2.ballerinalang.compiler.packaging.repo.ZipRepo) PackageEntity(org.ballerinalang.repository.PackageEntity) Parser(org.wso2.ballerinalang.compiler.parser.Parser) Paths(java.nio.file.Paths) GenericPackageSource(org.wso2.ballerinalang.compiler.packaging.GenericPackageSource) CacheRepo(org.wso2.ballerinalang.compiler.packaging.repo.CacheRepo) Collections(java.util.Collections) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) Dependency(org.ballerinalang.toml.model.Dependency) Name(org.wso2.ballerinalang.compiler.util.Name)

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