Tony Lukasavage

Caffeine. Whiskey. Code. Mostly the last one.

Automating Appcelerator: grunt-titanium and grunt-alloy

| Comments

If you follow me on twitter, my current love affair with task management via the node.js module grunt is no secret. Long story short, it is a deliciously simple way to automate development tasks, with a multitude of those tasks (like linting, minification, file watching, etc…) already done for you. I could babble on about it here, but I think a tweet of mine best encapsulates my experience with it.


In a natural fusion of my current technological entanglements, I took to creating grunt task plugins for Appcelerator’s core cross-platform mobile development tools. As a result, we now have grunt-titanium for the Titanium CLI and grunt-alloy for the Alloy MVC framework. With these plugins you can now automate all functionality involved by these 2 tools, in turn letting you shift your focus onto your mobile app development, where it should be.

For a crash course in…

  • task automation with grunt, check out their docs. Seriously, learn grunt.
  • Titanium and Alloy, check out Appcelerator’s guides and get to building top-of-the-line, cross-platform, native mobile apps quickly and easily.

In the meantime, though, check out these few examples of how you can use grunt-titanium and grunt-alloy to super-charge your development workflow. Bear in mind that these are excerpts from a Gruntfile.js implementation, so again, read up on grunt and check out the grunt-titanium and grunt-alloy repos to fully understand how to use these examples.



Create and build a Titanium app

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module.exports = function(grunt) {

  // configure the plugins
  grunt.initConfig({
      titanium: {
          should_create: {
              options: {
                  command: 'create',
                  name: 'app_name',
                  workspaceDir: '.'
              }
          },
          should_build: {
              options: {
                  command: 'build',
                  projectDir: 'app_name',
                  buildOnly: true
              }
          }
      }
  });

  // Actually load this plugin's task(s).
  grunt.loadTasks('tasks');

  // These plugins provide necessary tasks.
  grunt.loadNpmTasks('grunt-titanium');

  grunt.registerTask('default', ['titanium']);
};

Create and compile a Alloy app

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module.exports = function(grunt) {

  // configure the plugins
  grunt.initConfig({
      titanium: {
          all: {
              options: {
                  command: 'create',
                  name: 'app_name',
                  workspaceDir: '.'
              }
          }
      },
      alloy: {
          new_app: {
              options: {
                  command: 'new',
                  args: ['app_name']
              }
          },
          compile: {
              options: {
                  command: 'compile',
                  platform: 'ios',
                  outputPath: 'app_name'
              }
          }
      }
  });

  // Actually load this plugin's task(s).
  grunt.loadTasks('tasks');

  // These plugins provide necessary tasks.
  grunt.loadNpmTasks('grunt-titanium');
  grunt.loadNpmTasks('grunt-alloy');

  grunt.registerTask('default', ['titanium', 'alloy']);
};

Stay tuned. I’ll soon be posting about how you can expand this workflow with ti-mocha to start automating the runtime testing of your Titanium and Alloy apps!

PS - grunt-clean is a great plugin to use for cleaning up after yourself when creating temporary Titanium/Alloy apps for test automation.

Resources & Links

Comments