Search in sources :

Example 1 with SourceDirectory

use of org.wso2.ballerinalang.compiler.SourceDirectory in project carbon-apimgt by wso2.

the class APIFileUtils method archiveDirectory.

/**
 * Creates a zip archive from of a directory
 *
 * @param sourceDirectory directory to create zip archive from
 * @param archiveLocation path to the archive location, excluding archive name
 * @param archiveName     name of the archive to create
 * @throws APIMgtDAOException if an error occurs while creating the archive
 */
public static void archiveDirectory(String sourceDirectory, String archiveLocation, String archiveName) throws APIMgtDAOException {
    File directoryToZip = new File(sourceDirectory);
    List<File> fileList = new ArrayList<>();
    getAllFiles(directoryToZip, fileList);
    try {
        writeArchiveFile(directoryToZip, fileList, archiveLocation, archiveName);
    } catch (IOException e) {
        String errorMsg = "Error while writing archive file " + directoryToZip.getPath() + " to archive " + archiveLocation;
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
    if (log.isDebugEnabled()) {
        log.debug("Archived API generated successfully" + archiveName);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with SourceDirectory

use of org.wso2.ballerinalang.compiler.SourceDirectory in project carbon-apimgt by wso2.

the class FileBasedApplicationImportExportManager method createArchiveFromExportedAppArtifacts.

/**
 * Creates an archive of the contained application details.
 *
 * @param sourceDirectory Directory which contains source file
 * @param archiveLocation Directory to generate the zip archive
 * @param archiveName     Name of the zip archive
 * @return path to the created archive file
 * @throws APIMgtEntityImportExportException
 */
public String createArchiveFromExportedAppArtifacts(String sourceDirectory, String archiveLocation, String archiveName) throws APIMgtEntityImportExportException {
    String archivedFilePath = null;
    try {
        APIFileUtils.archiveDirectory(sourceDirectory, archiveLocation, archiveName);
    } catch (APIManagementException e) {
        // cleanup the archive root directory
        try {
            FileUtils.deleteDirectory(new File(path));
        } catch (IOException e1) {
            log.warn("Unable to remove directory " + path);
        }
        String errorMsg = "Error while archiving directory " + sourceDirectory;
        throw new APIMgtEntityImportExportException(errorMsg, e, ExceptionCodes.APPLICATION_EXPORT_ERROR);
    }
    archivedFilePath = archiveLocation + File.separator + archiveName + ".zip";
    return archivedFilePath;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) IOException(java.io.IOException) File(java.io.File) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 3 with SourceDirectory

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

the class BCompileUtil method compile.

public static CompileResult compile(String sourceRoot, String packageName, CompilerPhase compilerPhase, SourceDirectory sourceDirectory) {
    CompilerContext context = new CompilerContext();
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, sourceRoot);
    options.put(COMPILER_PHASE, compilerPhase.toString());
    options.put(PRESERVE_WHITESPACE, "false");
    context.put(SourceDirectory.class, sourceDirectory);
    CompileResult comResult = new CompileResult();
    // catch errors
    DiagnosticListener listener = comResult::addDiagnostic;
    context.put(DiagnosticListener.class, listener);
    // compile
    Compiler compiler = Compiler.getInstance(context);
    BLangPackage packageNode = compiler.compile(packageName);
    comResult.setAST(packageNode);
    CompiledBinaryFile.ProgramFile programFile = compiler.getExecutableProgram(packageNode);
    if (programFile != null) {
        comResult.setProgFile(LauncherUtils.getExecutableProgram(programFile));
    }
    return comResult;
}
Also used : Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) CompiledBinaryFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) DiagnosticListener(org.ballerinalang.util.diagnostic.DiagnosticListener)

Example 4 with SourceDirectory

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

the class TestCmd method execute.

public void execute() {
    if (helpFlag) {
        printCommandUsageInfo(parentCmdParser, "test");
        return;
    }
    if (sourceFileList == null || sourceFileList.isEmpty()) {
        Path userDir = Paths.get(System.getProperty("user.dir"));
        SourceDirectory srcDirectory = new FileSystemProjectDirectory(userDir);
        sourceFileList = srcDirectory.getSourcePackageNames();
    }
    if (groupList != null && disableGroupList != null) {
        throw LauncherUtils.createUsageException("Cannot specify both --groups and --disable-groups flags at the same time");
    }
    Path sourceRootPath = LauncherUtils.getSourceRootPath(sourceRoot);
    Path ballerinaConfPath = sourceRootPath.resolve("ballerina.conf");
    try {
        ConfigRegistry.getInstance().initRegistry(configRuntimeParams, null, ballerinaConfPath);
        ((BLogManager) LogManager.getLogManager()).loadUserProvidedLogConfiguration();
    } catch (IOException e) {
        throw new RuntimeException("failed to read the specified configuration file: " + ballerinaConfPath.toString(), e);
    }
    Path[] paths = sourceFileList.stream().map(Paths::get).toArray(Path[]::new);
    BTestRunner testRunner = new BTestRunner();
    if (disableGroupList != null) {
        testRunner.runTest(sourceRoot, paths, disableGroupList, false);
    } else {
        testRunner.runTest(sourceRoot, paths, groupList, true);
    }
    if (testRunner.getTesterinaReport().isFailure()) {
        Runtime.getRuntime().exit(1);
    }
    Runtime.getRuntime().exit(0);
}
Also used : Path(java.nio.file.Path) BLogManager(org.ballerinalang.logging.BLogManager) FileSystemProjectDirectory(org.wso2.ballerinalang.compiler.FileSystemProjectDirectory) SourceDirectory(org.wso2.ballerinalang.compiler.SourceDirectory) IOException(java.io.IOException)

Example 5 with SourceDirectory

use of org.wso2.ballerinalang.compiler.SourceDirectory in project carbon-apimgt by wso2.

the class FileBasedServicesImportExportManager method createArchiveFromExportedServices.

/**
 * Creates an archive of the contained service details.
 *
 * @param sourceDirectory Directory which contains source file
 * @param archiveLocation Directory to generate the zip archive
 * @param archiveName     Name of the zip archive
 * @return path to the created archive file
 * @throws APIManagementException if an error occurs while creating an archive from app details
 */
public ExportArchive createArchiveFromExportedServices(String sourceDirectory, String archiveLocation, String archiveName) throws APIManagementException {
    String archivedFilePath;
    ExportArchive exportArchive = new ExportArchive();
    try {
        archiveDirectory(sourceDirectory, archiveLocation, archiveName);
    } catch (IOException e) {
        // cleanup the archive root directory
        try {
            FileUtils.deleteDirectory(new File(path));
        } catch (IOException e1) {
            log.warn("Unable to remove directory " + path);
        }
        String errorMsg = "Error while archiving directory " + sourceDirectory;
        throw new APIManagementException(errorMsg);
    }
    archivedFilePath = archiveLocation + File.separator + archiveName + APIConstants.ZIP_FILE_EXTENSION;
    exportArchive.setArchiveName(archivedFilePath);
    return exportArchive;
}
Also used : ExportArchive(org.wso2.carbon.apimgt.rest.api.service.catalog.model.ExportArchive) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ZipFile(java.util.zip.ZipFile)

Aggregations

IOException (java.io.IOException)3 File (java.io.File)2 ZipFile (java.util.zip.ZipFile)2 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 BLogManager (org.ballerinalang.logging.BLogManager)1 DiagnosticListener (org.ballerinalang.util.diagnostic.DiagnosticListener)1 Compiler (org.wso2.ballerinalang.compiler.Compiler)1 FileSystemProjectDirectory (org.wso2.ballerinalang.compiler.FileSystemProjectDirectory)1 SourceDirectory (org.wso2.ballerinalang.compiler.SourceDirectory)1 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)1 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)1 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)1 CompiledBinaryFile (org.wso2.ballerinalang.programfile.CompiledBinaryFile)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 APIMgtEntityImportExportException (org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)1 ExportArchive (org.wso2.carbon.apimgt.rest.api.service.catalog.model.ExportArchive)1