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

hu.ponte.respresso.plugin.gradle.RespressoPluginExtension.groovy Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package hu.ponte.respresso.plugin.gradle

import groovy.transform.PackageScope
import hu.ponte.respresso.client.RespressoLogger
import org.gradle.api.Project

enum RespressoResourceTarget {
    build,
    resources
}

class RespressoPluginExtension {
    transient Project gradleProject
    String server = "https://app.respresso.io"
    String lockFile = "respresso.lock"
    String projectToken
    String target = "resources"
    private String rootProjectDirectory
    Resources resources
    boolean strictMode = false
    String logger = "basic"

    RespressoPluginExtension(Project gradleProject) {
        this.gradleProject = gradleProject
        resources = new Resources(gradleProject)
    }

    Resources resources(Closure closure) {
        gradleProject.configure(resources, closure)
        return resources
    }

    public String getProject() {
        return rootProjectDirectory
    }

    @PackageScope
    void setProject(String rootProjectDirectory) {
        this.rootProjectDirectory = rootProjectDirectory;
    }
}

class Resources {
    transient Project gradleProject
    ResourceConfig localization = new ResourceConfig()
    ResourceConfig color = new ResourceConfig()
    ResourceConfig image = new ResourceConfig()
    ResourceConfig font = new ResourceConfig()
    ResourceConfig appIcon = new ResourceConfig()

    private Map resources = new HashMap<>()

    Resources(Project gradleProject) {
        this.gradleProject = gradleProject
    }

    ResourceConfig localization(Closure closure) {
        gradleProject.configure(localization, closure)
        return localization
    }

    ResourceConfig vectorImages(Closure closure) {
        gradleProject.configure(vectorImages, closure)
        return vectorImages
    }

    ResourceConfig require(String resource) {
        if (resource.contains(":")) {
            ResourceConfig config = new ResourceConfig()
            String[] parts = resource.split(":")
            if (parts.length == 2) {
                config.version(parts[1])
                resources.put(parts[0], config)
                return config
            }
        }
        GoogleAnalyticsHandler.sendError("request syntax must be the following: : eg.: localization:1.0+", true)
        throw new RuntimeException("request syntax must be the following: : eg.: localization:1.0+")
    }

    ResourceConfig require(String category, Closure closure) {
        ResourceConfig config = new ResourceConfig()
        gradleProject.configure(config, closure)
        resources.put(category, config)
        return config
    }

    List getSyncResources() {
        List configs = new ArrayList<>()
        for (String category : resources.keySet()) {
            ResourceConfig config = resources.get(category)
            if (config != null) {
                configs.add(new hu.ponte.respresso.client.ResourceConfig(category, config.versionCode, config.group))
            }
        }
        overrideCategory(configs, localization, "localization")
        overrideCategory(configs, color, "color")
        overrideCategory(configs, image, "image")
        overrideCategory(configs, font, "font")
        overrideCategory(configs, appIcon, "appIcon")
        return configs
    }


    private
    static void overrideCategory(List configs, ResourceConfig config, String category) {
        if (config.versionCode != null && config.versionCode.length() > 0) {
            configs.removeIf({ (it.category == category) })

            configs.add(new hu.ponte.respresso.client.ResourceConfig(category, config.versionCode, config.group))
        }
    }


}

class ResourceConfig {
    private String versionCode
    private String group = "android"

    public String getVersionCode() {
        return versionCode
    }

    public ResourceConfig version(String versionCode) {
        this.versionCode = versionCode
        return this
    }

    public String getGroup() {
        return group
    }

    public ResourceConfig group(String group) {
        this.group = group
        return this
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy