com.mobgen.halo.android.plugin.sdk.tasks.HaloConfigurationTask.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of halo-plugin Show documentation
Show all versions of halo-plugin Show documentation
This plugin helps on the configuration of the SDK.
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
boolean isCyphered = !extension.development()
if (isCyphered) {
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"
cyphered isCyphered
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)
}
}