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.connector / hooks / addEmbeddedWithCli.js
Size: Mime:
'use strict';
module.exports = function(context) {
  const xcode = context.requireCordovaModule('vipera-de-cli').xcode,
      fs = context.requireCordovaModule('vipera-de-cli').fsx,
      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.
        }
    }

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

    var cordovaProjectPath =  context.opts.projectRoot;
    var pluginId = context.opts.plugin.id;

    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; 
    const projectPath = xcodeProjectPath + '/project.pbxproj';

    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;
    console.log("Searching frameworks on plugin path: "+ pathForPlugins +"...");
    var frameworksFilesToEmbedAbsolute = fromDir(pathForPlugins, '.framework', true, true);
    console.log("frameworksFilesToEmbedAbsolute='" + frameworksFilesToEmbedAbsolute +"' ", frameworksFilesToEmbedAbsolute);
    embedFrameworkOrLibrary(xcodeProjectPath, frameworksFilesToEmbedAbsolute);

    //Done!
    console.log('Framework embedding done.');

    function embedFrameworkOrLibrary(xcodeProject, frameworksFilesToEmbed){
          for (var i=0;i<frameworksFilesToEmbed.length;i++){
              console.log("Adding framework ='" + frameworksFilesToEmbed[i] +"'...");
              embedFramework(xcodeProject, frameworksFilesToEmbed[i]);
          }
    }

    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;

    }


    function embedFramework(xcodeProjectPath, frameworkPath) {

        console.log("embedFramework called for xcodeProjectPath="+xcodeProjectPath+" frameworkPath="+frameworkPath);

        // Load Xcode project
        var projectName = path.basename(xcodeProjectPath); //the name of the Xcode project file
        var projectRoot = path.dirname(xcodeProjectPath); //the root of the xcode project
        var projectPath = path.join(xcodeProjectPath, "project.pbxproj"); //the path of the pbxproj file
        const currProj = xcode.project(projectPath);
        currProj.parseSync();
    
    
        // copy the framework locally
        var projectFrameworksPath = path.join(projectRoot, "Frameworks"); //local framework folder
        if (!fs.existsSync(projectFrameworksPath)){
            console.log("Creating Frameworks folder for project")
            fs.mkdirSync(projectFrameworksPath);
        }
        var frameworkName = path.basename(frameworkPath); //the framework filename (ex: FooFramework.framework)
        var localFrameworkPath = path.join(projectFrameworksPath, frameworkName);
        if (!fs.existsSync(localFrameworkPath)){
            //copy the framework locally
            console.log("Copying framework locally to the project...");
            fs.copySync(frameworkPath, localFrameworkPath);
        }
    
        var relativeFrameworkPath = path.relative(projectRoot, localFrameworkPath);
    
        console.log("relativeFrameworkPath = " + relativeFrameworkPath);
    
    
        // Add framework
        var opt = {
            embed:true,
            sign:true,
            link:true,
            customFramework:true,
            settings: {
                ATTRIBUTES: ["CodeSignOnCopy", "RemoveHeadersOnCopy"]
            }
        };
        currProj.addFramework(relativeFrameworkPath, opt);
    
        fs.writeFileSync(projectPath, currProj.writeSync());
    }


};