'use strict';
const LOG_TAG = "[DE-PROGUARD]"
const path = require('path');
const fs = require('fs');
const ConfigUtils = require("./ConfigUtils");
var fsx = undefined;
const DEFAULT_FILE_NAME = "proguard_conf.pro"
function deployProguardFile(sourceFilePath, androidPlatformPath) {
if (!fs.existsSync(sourceFilePath)) {
console.error(LOG_TAG, "File not found: ", sourceFilePath);
throw new Error("File not found: " + sourceFilePath);
}
var newLibFileName = path.join(androidPlatformPath, DEFAULT_FILE_NAME);
console.log(LOG_TAG, "start file deploy at ", newLibFileName);
fsx.copySync(sourceFilePath, newLibFileName);
console.log(LOG_TAG, "Deploy done");
}
function deleteProguardFile(androidPlatformPath) {
var target = path.join(androidPlatformPath, DEFAULT_FILE_NAME);
if (!fs.existsSync(target)) {
console.log(LOG_TAG, "Installed proguard file not found");
return;
}
console.log(LOG_TAG, "Delete ", target);
fsx.removeSync(target);
console.log(LOG_TAG, "Delete done");
}
module.exports = function(ctx) {
console.log(LOG_TAG, "Hook Start");
linkViperaCli();
try{
fsx = ctx.requireCordovaModule('vipera-de-cli').fsx;
} catch(ex) {
fsx = require('vipera-de-cli').fsx;
}
const projectRoot = ctx.opts.projectRoot;
console.log(LOG_TAG, "project root:",projectRoot);
if (ctx.opts.platforms && ctx.opts.platforms.indexOf('android') < 0) {
console.log(LOG_TAG, "Android platform not found: skyp this hook");
return;
}
ConfigUtils.init(ctx);
var isRelease = false;
if(ctx.opts && ctx.opts.options){
isRelease = ctx.opts.options.release == true;
}
console.log(LOG_TAG,"isRelease: ",isRelease);
const xmlFilePath = ConfigUtils.getConfigXMLFilePath(projectRoot,"android");
console.log(LOG_TAG, "android platform xmlFilePath :",xmlFilePath);
const config = ConfigUtils.loadConfigXML(xmlFilePath);
if(!config){
console.error(LOG_TAG, "unable to read config: skyp this hook");
return;
}
var androidGradlePath = ConfigUtils.getAndroidPlatformGradlePath(projectRoot);
var enabled = config.getGlobalPreference("proguard.enabled");
if(enabled !== "true") {
console.log(LOG_TAG, "Proguard not enabled (proguard.enable not set to true): purge installed proguard file");
deleteProguardFile(androidGradlePath);
console.log(LOG_TAG, "Purge done: end hook");
return;
}
var preferenceName = "proguard.file.debug";
if(isRelease){
preferenceName = "proguard.file.release";
}
var proguardFilePath = config.getGlobalPreference(preferenceName,"android");
if(!proguardFilePath){
console.error(LOG_TAG, "Setting for proguard file not found: set ",preferenceName,"preference in your config.xml file");
throw new Error("Setting for "+ preferenceName+ " not found... Add " + preferenceName + " preference in your config.xml file")
}
var sourceFilePath = path.resolve(projectRoot,proguardFilePath);
deployProguardFile(sourceFilePath,androidGradlePath);
console.log(LOG_TAG,"Hook end")
}
function linkViperaCli(){
try{
require('child_process').execSync(
'npm link vipera-de-cli'
);
}catch(ex){
console.warn("linkViperaCli fail");
}
}