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

com.mobgen.halo.android.plugin.sdk.tasks.HaloConfigurationTask.groovy Maven / Gradle / Ivy

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

import com.mobgen.halo.android.plugin.sdk.HaloExtension
import com.mobgen.halo.android.plugin.sdk.HaloModule
import groovy.json.JsonBuilder
import org.apache.commons.codec.binary.Hex
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec

/**
 * Task to write the halo configuration file to the system.
 */
public class HaloConfigurationTask extends DefaultTask{

    private static final String CONFIGURATION_FILE_NAME = "haloConfiguration.json"
    private static final String ASSETS_DIR = "src/main/assets"
    private static final String CRYPT_ALGORITHM = "Blowfish"

    private HaloExtension extension

    def setExtension(HaloExtension extension){
        this.extension = extension
    }

    @TaskAction
    void generateConfiguration() {
        File configurationFile = new File("${getProject().projectDir}/${ASSETS_DIR}/${CONFIGURATION_FILE_NAME}")

        //Ensure the asssets dir exists
        if (!configurationFile.parentFile.exists()) {
            configurationFile.parentFile.mkdirs()
        }
        //Ensure the file dit exists
        if (!configurationFile.exists()) {
            configurationFile.createNewFile()
        }

        //Generate the json configuration file
        JsonBuilder builder = new JsonBuilder()
        List modules = extension.getModules()
        builder {
            modules.each{ module ->
                "${module.moduleName()}" module.buildConfig(new JsonBuilder()).content
            }
        }

        //Cypher the configuration if it is needed
        Object writtableData = builder.content
        writtableData = crypt(builder.toString())

        //Build the final version
        JsonBuilder finalBuilder = new JsonBuilder()
        finalBuilder {
            disclaimer "DO NOT MODIFY. This file is generated based on your HALO config"
            halo writtableData
        }
        configurationFile.withWriter { it << finalBuilder.toPrettyString()}
    }

    private static String crypt(String data) {
        byte[] keyData = "HaloSDK".getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyData, CRYPT_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CRYPT_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] cipheredText = cipher.doFinal(data.getBytes("UTF-8"));
        return Hex.encodeHexString(cipheredText)
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy