Friday 6 September 2013

Using Grunt-phonegap-build: depricating grunt-zipstream and buiding on other platforms

If you use Grunt and want to automate builds in build.phonegap.com you may have noticed the Grunt-Phonegap-Build plugin. However the documentation is a little bit outdated. For one, Grunt-zipstream doesn't work with newer versions of Node.js (as of the writing here), so we will substitute in Grunt-contrib-compress. Then there is the fact that iOS uses certs now, and there is a key/password example in the documentation? It turns out that you can simply omit this line and build with iOS no problem! Finally there are the missing examples for Blackberry and Windows Phones builds. Yes, you can do them, and I will show you how:

Replacing Grunt-zipstream

We will use Grunt-contrib-compress instead with the following configuration:
var compressConfig = {
        main: {
            options: {
                archive: 'app.zip'
            },
            expand: true,
            src: ['*.html', 'styles/**/*.css', 'srcipts/**/*.js', 'favicon.ico', 'images/**/*'],
        }
    };
and with a sub-directory using a variable for Yeoman
var compressConfig = {
        main: {
            options: {
                archive: 'app.zip'
            },
            expand: true,
            cwd: '<%= yeoman.app %>/',
            src: ['*.html', 'styles/**/*.css', 'srcipts/**/*.js', 'favicon.ico', 'images/**/*'],
        }
    };
 You can replace "zip" with "compress" in the documentation, or make a separate task like I did:
grunt.registerTask('remote_build', [
        'compress',
        'phonegap-build:debug'
    ]);

Blackberry and Windows Phone builds

Wondering how to make Windows Phone Builds with Grunt-phonegap-build? To build out for iOS, Android, Blackberry, and Windows Phone you can do your setup this way:

var phonegapBuildConfig = {
        debug: {
            options: {
                archive: 'app.zip',
                'appId': 'XXXXXX',
                'user': {
                    'email': 'myemail@server.com',
                    'password': 'password1234'
                },
                keys: {
                    //ios: { 'password': password1234' },
                    android: { 'key_pw': 'password1234', 'keystore_pw': 'password1234' },
                    blackberry: { 'password': 'password1234'}
                },
                download: {
                    ios: 'dist/ios.ipa',
                    android: 'dist/android.apk',
                    blackberry: 'dist/blackberry.ota',
                    winphone: 'dist/winphone.xap'
                }
            }
        }

};

No need for the iOS password anymore, see how to specify the key for Blackberry, and the attribute name for Windows Phone is "winphone"!

Here is everything in total:
module.exports = function (grunt) {
    // show elapsed time at the end
    require('time-grunt')(grunt);
    // load all grunt tasks
    require('load-grunt-tasks')(grunt);

    //REMOTE (build.phonegap.com) phonegap (for devices) builds
    grunt.loadNpmTasks('grunt-contrib-compress');
    grunt.loadNpmTasks('grunt-phonegap-build');

    // configurable paths
    var yeomanConfig = {
        app: 'app',
        dist: 'dist'
    };

    var phonegapBuildConfig = {
        debug: {
            options: {
                archive: 'app.zip',
                'appId': 'XXXXXX',
                'user': {
                    'email': 'myemail@server.com',
                    'password': 'password1234'
                },
                keys: {
                    //ios: { 'password': password1234' },
                    android: { 'key_pw': 'password1234', 'keystore_pw': 'password1234' },
                    blackberry: { 'password': 'password1234'}
                },
                download: {
                    ios: 'dist/ios.ipa',
                    android: 'dist/android.apk',
                    blackberry: 'dist/blackberry.ota',
                    winphone: 'dist/winphone.xap'
                }
            }
        };

    var compressConfig = {
        main: {
            options: {
                archive: 'app.zip'
            },
            expand: true,
            cwd: '<%= yeoman.app %>/',
            src: ['*.html', 'styles/**/*.css', 'srcipts/**/*.js', 'favicon.ico', 'images/**/*'],
        }
    };

    grunt.initConfig({
        'phonegap-build': phonegapBuildConfig,
        compress: compressConfig
    });

    grunt.registerTask('remote_build', [
        'compress',
        'phonegap-build:debug'
    ]);
};

No comments:

Post a Comment