Search in sources :

Example 1 with BLangProgram

use of org.ballerinalang.model.BLangProgram in project carbon-apimgt by wso2.

the class GenerateArchives method execute.

/**
 * Generate archives
 *
 * @param type        type of the bal file
 * @param packagePath package path to the bal
 * @param targetName  target file name
 */
public void execute(String type, String srcDir, String packagePath, String targetName) {
    Path sourcePath = Paths.get(packagePath);
    try {
        Path realPath = Paths.get(srcDir + File.separator + packagePath).toRealPath(LinkOption.NOFOLLOW_LINKS);
        if (!Files.isDirectory(realPath, LinkOption.NOFOLLOW_LINKS) && !realPath.toString().endsWith(BLangPrograms.BSOURCE_FILE_EXT)) {
            log.error("invalid file or package '" + sourcePath + "'");
            throw new IllegalArgumentException("invalid file or package '" + sourcePath + "'");
        }
    } catch (IOException e) {
        log.error("error reading from file: " + sourcePath + " reason: " + e.getMessage(), e);
        throw new RuntimeException("error reading from file: " + sourcePath + " reason: " + e.getMessage(), e);
    }
    Path programDirPath = Paths.get(srcDir);
    BLangProgram bLangProgram = null;
    if (MAIN_TYPE.equals(type)) {
        bLangProgram = new BLangProgramLoader().loadMain(programDirPath, sourcePath);
    } else if (SERVICE_TYPE.equals(type)) {
        bLangProgram = new BLangProgramLoader().loadService(programDirPath, sourcePath);
    } else {
        log.error("source type '" + type + "' not supported");
        throw new RuntimeException("source type '" + type + "' not supported");
    }
    new BLangProgramArchiveBuilder().build(bLangProgram, targetName.trim());
}
Also used : Path(java.nio.file.Path) BLangProgramArchiveBuilder(org.ballerinalang.BLangProgramArchiveBuilder) BLangProgramLoader(org.ballerinalang.BLangProgramLoader) BLangProgram(org.ballerinalang.model.BLangProgram) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 Path (java.nio.file.Path)1 BLangProgramArchiveBuilder (org.ballerinalang.BLangProgramArchiveBuilder)1 BLangProgramLoader (org.ballerinalang.BLangProgramLoader)1 BLangProgram (org.ballerinalang.model.BLangProgram)1