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

com.mobgen.halo.android.gradle.tasks.HaloResourceGenerationTask.groovy Maven / Gradle / Ivy

There is a newer version: 2.9.3
Show newest version
package com.mobgen.halo.android.gradle.tasks

import com.android.build.gradle.api.BaseVariant
import com.google.common.base.Charsets
import com.google.common.io.Files
import com.mobgen.halo.android.gradle.HaloConfiguration
import com.mobgen.halo.android.gradle.HaloExtensionV2
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction

/**
 * Resource generation task to generate some resources with the ids of halo.
 */
public class HaloResourceGenerationTask extends DefaultTask {

    @Input
    public HaloExtensionV2 extension

    @Input
    public BaseVariant androidVariant

    @OutputDirectory
    public File intermediateDir

    @TaskAction
    public void action() {
        deleteFolder(intermediateDir)
        if (!intermediateDir.mkdirs()) {
            throw new GradleException("Failed to create folder: " + intermediateDir);
        }

        Map valuesInjected = new TreeMap();
        Map> resAttributes = new TreeMap>();
        insertVariables(valuesInjected)
        File values = new File(intermediateDir, "values");
        if (!values.exists() && !values.mkdirs()) {
            throw new GradleException("Failed to create folder: " + values);
        }
        Files.write(getValuesContent(valuesInjected, resAttributes), new File(values, "values.xml"), Charsets.UTF_8);
    }

    void insertVariables(TreeMap data) {
        HaloConfiguration config = extension.getConfigurationForName(androidVariant.getName())
        data.put("halo_client_id", config.clientId)
        data.put("halo_secret_id", config.clientSecret)
        data.put("halo_client_id_debug", config.clientIdDebug)
        data.put("halo_secret_id_debug", config.clientSecretDebug)
        if(config.haloServices){
            data.put("halo_notifications_enabled", String.valueOf(config.haloServices.notificationsEnabled))
            data.put("halo_analytics_enabled", String.valueOf(config.haloServices.analyticsEnabled))
            data.put("halo_presenter_enabled", String.valueOf(config.haloServices.presenterEnabled))
            data.put("halo_translations_enabled", String.valueOf(config.haloServices.translationsEnabled))
            data.put("halo_content_enabled", String.valueOf(config.haloServices.contentEnabled))
            if(config.haloServices.auth){
                data.put("halo_social_google_client", config.haloServices.auth.googleClient)
                data.put("halo_social_facebook_client", config.haloServices.auth.facebookClient)
            }
        }
    }

    private static void deleteFolder(final File folder) {
        if (!folder.exists()) {
            return;
        }
        File[] files = folder.listFiles();
        if (files != null) {
            for (final File file : files) {
                if (file.isDirectory()) {
                    deleteFolder(file);
                } else {
                    if (!file.delete()) {
                        throw new GradleException("Failed to delete: " + file);
                    }
                }
            }
        }
        if (!folder.delete()) {
            throw new GradleException("Failed to delete: " + folder);
        }
    }

    private static String getValuesContent(Map values,
                                           Map> attributes) {
        StringBuilder sb = new StringBuilder(256);

        sb.append("\n" +
                "\n");

        for (Map.Entry entry : values.entrySet()) {
            String name = entry.getKey();
            sb.append("     attr : attributes.get(name).entrySet()) {
                    sb.append(" ").append(attr.getKey()).append("=\"")
                            .append(attr.getValue()).append("\"");
                }
            }
            sb.append(">").append(entry.getValue()).append("\n");
        }

        sb.append("\n");

        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy