Vero
Vero

Reputation: 1750

Get cordova package name from javascript hook

I am writing a cordova plugin with a node hook to run after_prepare .

This is for Android only.

From within this hook I need to get the cordova package name, so I can copy a file to the src/com/example/myproject folder (if the package is com.example.myproject).

If I know the package name I can make this path. I have it working hardcoded now but I need this to work with two different package names.

Is there a way to get the package name from within this code in the plugin hook?

module.exports = function(ctx){
   var fs = ctx.requireCordovaModule('fs');
   var path = ctx.requireCordovaModule('path');
   var deferral = ctx.requireCordovaModule('q').defer();

   //get package name here

   //do other stuff
}

I have done a lot of research but have not been able to find this.

Thanks.

Upvotes: 4

Views: 1482

Answers (2)

ArMouReR
ArMouReR

Reputation: 33

Here my compilation from different answers that works in 2021. I use it to update some parameters in Xcode project for plugins compilation.

You can see that I am getting here app id and name from config.xml

And you can add it to after_prepare hook:

<hook src="scripts/addBuildSettingsToXcode.js" type="after_prepare" />

#!/usr/bin/env node

let fs    = require('fs');
let xcode = require('xcode');
let path = require('path');
let et = require('elementtree');


module.exports = function (context) {
    //console.log(context);

    function addBuildPropertyToDebugAndRelease(prop, value) {
        console.log('Xcode Adding   ' + prop + '=' + value);
        myProj.addBuildProperty(prop, value, 'Debug');
        myProj.addBuildProperty(prop, value, 'Release');
    }

    function updateBuildPropertyToDebugAndRelease(prop, value) {
        console.log('Xcode Updating ' + prop + '=' + value );
        myProj.updateBuildProperty(prop, value, 'Debug');
        myProj.updateBuildProperty(prop, value, 'Release');
    }


    // Getting app id and name from config.xml
    let config_xml = path.join(context.opts.projectRoot, 'config.xml');
    let data = fs.readFileSync(config_xml).toString();
    let etree = et.parse(data);
    let appId = etree.getroot().attrib.id ;
    let appName = etree.getroot().find('name')['text'];

    // Building project path
    let projectPath = 'platforms/ios/' + appName + '.xcodeproj/project.pbxproj';

    // Opening Xcode project and parsing it
    myProj = xcode.project(projectPath);
    myProj = myProj.parseSync();

    // Common properties
    addBuildPropertyToDebugAndRelease('DEVELOPMENT_TEAM', 'CGXXXXXXX');
    addBuildPropertyToDebugAndRelease('CODE_SIGN_IDENTITY', '"Apple Development"');

    // Compilation properties
    addBuildPropertyToDebugAndRelease('ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES', 'YES');

    // Save project file
    fs.writeFileSync(projectPath, myProj.writeSync());

};

Upvotes: 0

Connor
Connor

Reputation: 64674

It doesn't look like it is available off of the context object, but you could try to parse the config.xml.

module.exports = function(context) {
    var fs = require('fs');
    var path = require('path');
    var config_xml = path.join(context.opts.projectRoot, 'config.xml');
    var et = context.requireCordovaModule('elementtree');

    var data = fs.readFileSync(config_xml).toString();
    var etree = et.parse(data);

    console.log(etree.getroot().attrib.id);
};

The local-webserver plugin uses a similar strategy for reading config properties.

Upvotes: 3

Related Questions