Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Automatic build number generation

Is there a way to make Titanium auto-generate a build number every time a project is built? It would be nice if we could have this build number built into the "project data" like the version string, but have it be incremented by Titanium for each build.

Vadtec

— asked March 25th 2010 by Nicholas Davey
  • build
  • development
  • titanium
0 Comments

5 Answers

  • I've never worked with Python before, so there's probably a better way to do this, but this is what I've managed to cobble together:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #
    # example compiler plugin
    # the config object is a set of properties
    # that are passed (dependent on platform)
    # that will allow you to hook into the compiler tooling
    # 
    
    import os
    #import re
    
    def compile(config):
        print "[INFO] Compiler plugin: ti.version loaded and working for %s" % config['platform']
    
        project_dir = config['project_dir']
        resource_dir = os.path.abspath(os.path.join(project_dir, 'Resources'))
    
        version_file = os.path.join(resource_dir, 'version.js')
    
        vl = ''
        if os.path.exists(version_file):
            vf = open(version_file)
            vl = vf.readline().strip()
            vf.close()
    
          if len(vl) > 0:
              _key, _value = vl.split("=")
              _value = _value.replace(";", "").strip()
              vl_value = int(_value) + 1
        else:
            vl_value = 1
    
        _build = str(vl_value)
        data = "var _build = %s;" % _build
    
        #print "[DEBUG] [ti.version] data: %s" % data
    
        vf = open(version_file, "w")
        vf.write(data)
        vf.close()
    

    Stick that in a file called <plugin.py> in a folder called <ti.version>. The <ti.version> folder should be in the <plugins> folder for your application.

    Edit tiapp.xml, and add:

    <plugins>
        <plugin version="0.1">ti.version</plugin>
    </plugins>
    

    That should create / update a file <version.js> in your <Resources> folder. All that needs to be done is to include <version.js> in your code, and use the variable.

    Hope this helps.

    — answered August 17th 2011 by Heinrich Steenberg
    permalink
    1 Comment
    • There may be a better way but hey, it works. Thanks for this.

      — commented November 15th 2011 by Aaron Francis
  • There is a documentation to create plugins in python for Titanium?

    Because I try to add the library PIL, but I have the following error : "[ERROR] ImportError: No module named Image"

    — answered August 8th 2012 by Laroche Marine
    permalink
    0 Comments
  • Added JSON support (package.json style)

    I took Heinrich's excellent script and modified it a bit for JSON support:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #
    # example compiler plugin
    # the config object is a set of properties
    # that are passed (dependent on platform)
    # that will allow you to hook into the compiler tooling
    # 
    
    import os
    import json
    
    def compile(config):
        print "[INFO] Compiler plugin: ti.version loaded and working for %s" % config['platform']
    
        project_dir = config['project_dir']
        resource_dir = os.path.abspath(os.path.join(project_dir, 'Resources'))
    
        version_file = os.path.join(resource_dir, 'package.json')
    
        if os.path.exists(version_file):
            vf = open(version_file, 'r').read()
    
            try:
                data = json.loads(vf)
    
                _oldVersion = data["version"]
                _version = data["version"].split(".")
                _major = _version[0]
                _minor = _version[1]
                _patch = _version[2].split("-")[0]
                _build = _version[2].split("-")[1]
    
                # increment build number
                _build = int(_build) + 1
    
                # reconstruct json
                data["version"] = str(_major) + '.' + str(_minor) + '.' + str(_patch) + '-' + str(_build)
    
                print "[INFO] [ti.version]",_oldVersion,">>",data["version"]
    
            except:
                print "[ERROR] Invalid package.json file. Version number unable to auto-increment."
    
        else:
            print "[ERROR] No package.json file found in Resources. Creating a new one..."
            data = {"name": "new-application", "version": "0.0.1-0"}
    
        # write changes back to file
        vf = open(version_file, "w")
        vf.write(json.dumps(data, sort_keys=True, indent=2))
        vf.close()
    

    Notes

    • I have NO python experience, just pieced together from web
    • Only increments build number at the moment, not sure if more is needed, just manually change the json file for major/minor/patch releases
    — answered September 19th 2012 by Daniel Mahon
    permalink
    2 Comments
    • You can read the package.json file in app using:

      var pjson = JSON.parse(Ti.Filesystem.getFile('./package.json').read());
      Ti.API.info(pjson);
      

      — commented September 19th 2012 by Daniel Mahon
    • View on GitHub

      — commented September 19th 2012 by Daniel Mahon
  • Is this still working for Aptana studio 3.0.2> ?

    I added the python script, edited the tiap.xml but on build, nothing happens, no file is created.
    If i set a break point in the python script, it doesn't get triggered.

    — answered March 1st 2013 by peet klomp
    permalink
    0 Comments
  • Do you have answer for your question? I get the same problem.

    — answered December 22nd 2010 by Pasha Tsipinio
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.