Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
com.vipera.de.foundation.apprating / hooks / removeEmbeddedWithCli.js
Size: Mime:
'use strict';
module.exports = function(context) {
  const xcode = context.requireCordovaModule('vipera-de-cli').xcode,
      fs = require('fs'),
      path = require('path');


    if (process.length >= 5 && process.argv[1].indexOf('cordova') == -1) {
        if (process.argv[4] != 'ios') {
            return; // plugin only meant to work for ios platform.
        }
    }

    var cordovaProjectPath =  context.opts.projectRoot;
    var pluginId = context.opts.plugin.id;
    var removeTargetPlugin = context.opts.plugins[0];

    if (removeTargetPlugin!=pluginId){
        return;
    }


    console.log("==============================================================================================================================");
    console.log("'Remove Embedded Frameworks' task is starting...");
    console.log("==============================================================================================================================");


    console.log("cordovaProjectPath='" + cordovaProjectPath +"'");
    console.log("pluginId='" + pluginId +"'");


    var cordova_util = context.requireCordovaModule("cordova-lib/src/cordova/util");
    var ConfigParser = context.requireCordovaModule("cordova-common").ConfigParser;
    var platforms;
    try {
         platforms = context.requireCordovaModule('cordova-lib/src/cordova/platforms');
    } catch(e) {
         platforms = context.requireCordovaModule('cordova-lib/src/platforms/platforms');
    }
    var projectRoot = cordova_util.isCordova();
    var xml = cordova_util.projectConfig(projectRoot);
    var cfg = new ConfigParser(xml);
    var appName = cfg.name();


  	var platform_path = path.join(projectRoot, 'platforms', 'ios');
	  console.log("platform_path='" + platform_path + "'");

    const xcodeProjectFileName = fromDir(platform_path, '.xcodeproj', false)[0];
    console.log("xcodeProjectFileName='" + xcodeProjectFileName + "'");

    process.chdir(cordovaProjectPath);
    console.log('Current working directory: ' + process.cwd());

    // Get the iOS Xcode Project Paths and structure
    const xcodeProjectPath = xcodeProjectFileName; //fromDir(cordovaProjectPath, projectName + '.xcodeproj', true);
    const projectPath = xcodeProjectPath + '/project.pbxproj';
    const myProj = xcode.project(projectPath);
    myProj.parseSync();

    var xcodeProjetcFileName = path.basename(xcodeProjectPath);
    var xcodeProjectRoot = path.dirname(xcodeProjectPath);
    console.log("Xcode project found at: " + xcodeProjectRoot + " Project File Name:" + xcodeProjetcFileName);


    // Get frameworks to embed
    var pathForPlugins = projectRoot +"/plugins/" + pluginId;// xcodeProjectRoot +"/" + appName +"/Plugins/" +pluginId +"/";
    console.log("Searching frameworks on plugin path: "+ pathForPlugins +"...");
    var frameworksFilesToRemoveAbsolute = fromDir(pathForPlugins, '.framework', true, true);
    console.log("frameworksFilesToRemoveAbsolute='" + frameworksFilesToRemoveAbsolute +"' ", frameworksFilesToRemoveAbsolute);
    var frameworksFilesToRemoveRelative = toRelative(xcodeProjectRoot, frameworksFilesToRemoveAbsolute);
    console.log("frameworksFilesToRemoveRelative='" + frameworksFilesToRemoveRelative +"' ", frameworksFilesToRemoveRelative);
    
    removeFrameworkOrLibrary(myProj, frameworksFilesToRemoveAbsolute);

    //Write the Xcode project
    fs.writeFileSync(projectPath, myProj.writeSync());

    //Done!
    console.log('Framework removed successfully.');

    function removeFrameworkOrLibrary(myProj, frameworksFilesToRemove){
          //Add the framework
          var opt = {
                      embed:true,
                      sign:true,
                      link:true,
                      customFramework:true,
                      settings: {
                          ATTRIBUTES: ["CodeSignOnCopy", "RemoveHeadersOnCopy"]
                      }
          };
          for (var i=0;i<frameworksFilesToRemove.length;i++){
              console.log(" ------> removing framework at: ", frameworksFilesToRemove[i])
            myProj.removeFramework(frameworksFilesToRemove[i], opt);
          }
    }

    function isEmbedFrameworkObjectPresentById(projForCheck, id){
        var idCompare = '"'+ id +'"';
        var copyFilesObj = projForCheck.getPBXObject('PBXCopyFilesBuildPhase');
        for (var uuid in copyFilesObj){
            var item = copyFilesObj[uuid];
            if (item && item.name && (item.name==idCompare)){
              //ora devo andare a prendere il PBXBuildFile con id corrispondente e vedere se c'è dentro qualcosa, perchè potrebbe essere una phase vuota
              if (item.files && item.files.length>0){
                  uuid = item.files[0].value;
                  var test = projForCheck.pbxBuildFileSection()[uuid]
                  if (test){
                      return true;
                  }
              }
            }
        }
        return false;
    }

    function isFrameworkPresentByName(projForCheck, id, opt){
        var pbxFile = context.requireCordovaModule('vipera-de-cli').xcodePbfile;
        var file = new pbxFile(id, opt);
        var sources = projForCheck.pbxFrameworksBuildPhaseObj(file.target);
        for (var i in sources.files) {
            if (sources.files[i].comment.indexOf(id)===0) {
                return true;
            }
        }
        return false;
    }


    function toRelative(xcodeProjectRoot, frameworksFilesToEmbedAbsolute){
      var ret = new Array();
      for (var i=0;i<frameworksFilesToEmbedAbsolute.length;i++){
        ret.push(path.relative(xcodeProjectRoot, frameworksFilesToEmbedAbsolute[i]));
      }
      return ret;
    }

    function fromDir(startPath, filter, rec, multiple, multipleData, resultFiles) {
        //console.log("Scanning folder: " + startPath);
        //check if path exists
        if (!fs.existsSync(startPath)) {
            console.log("no dir ", startPath);
            return null;
        }

        const files = fs.readdirSync(startPath);
        if (resultFiles==undefined){
          resultFiles = new Array();
        }
        //console.log(">>>> files found: ", files);
        
        for (var i = 0; i < files.length; i++) {
            var filename = path.join(startPath, files[i]);
            var stat = fs.lstatSync(filename);
            if (filename.indexOf(filter) >= 0) {
                resultFiles.push(filename);
                //console.log(">>>> found " + filename +"");
                //console.log(">>>> now resultFiles are =  " + resultFiles.length +"");
            } else {
                if (stat.isDirectory() && rec) {
                    fromDir(filename, filter, rec, multiple, multipleData, resultFiles); //recurse
                }
            }
        }

        return resultFiles;

    }





};