All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.swagger.swaggerhub.plugin.services.DefinitionFileFinder Maven / Gradle / Ivy

Go to download

A maven plugin for downloading and uploading Swagger/OAS definitions from/to SwaggerHub as part of a build process.

There is a newer version: 1.0.4
Show newest version
package io.swagger.swaggerhub.plugin.services;

import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Service to search for and load definition files contained within downstream projects directory
 */
public class DefinitionFileFinder {

    /**
     * Given a directory and the optional regex pattern, find API definitions.
     * Definitions should be contained in a file with one of the following extensions: .json, .yml, .yaml.
     * @param directory
     * @param fileNameRegexPattern
     * @return
     * @throws IOException - Returned when the directory specified cannot be found
     */
    public static List findDefinitionFiles(String directory, Optional fileNameRegexPattern) throws IOException {


        File directoryFile = new File(modifyPathForOperatingFileSystem(directory));
        if(!directoryFile.exists()){
            throw new IOException(String.format("The given directory [%s] cannot be found", directory));
        }

        ArrayList files = new ArrayList<>();
        File[] childFiles = directoryFile.listFiles();

        for (File childFile:childFiles){

            String fileExtension = FilenameUtils.getExtension(childFile.getName());
            String fileNameWithoutExtension = FilenameUtils.removeExtension(childFile.getName());

            boolean fileExtensionTypeIsSupported = DefinitionFileFormat.getByFileExtensionType(fileExtension).isPresent();

            if(!fileExtensionTypeIsSupported){
                continue;
            }

            boolean addFile = fileNameRegexPattern
                    .map(pattern -> fileNameWithoutExtension.matches(pattern))
                    .orElse(!fileNameRegexPattern.isPresent());

            if(addFile){
                files.add(childFile);
            }

        }

        return files;
    }

    /**
     * Utility function that modifies the path given to match the operating system that is being used to run the plugin.
     * @param path
     * @return
     */
    public static String modifyPathForOperatingFileSystem(String path){
        return path.replace("/",File.separator).replace("\\", File.separator);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy