1. #1
    Sencha User
    Join Date
    Mar 2014
    Posts
    6
    Vote Rating
    1
    mgreystone is on a distinguished road

      0  

    Default Custom Build Parameters

    Custom Build Parameters


    Apologies if this has been answered before. I could not find an answer in my research.

    I am looking for a way to pass variables to the application on build. I am building a Sencha Touch app that will be hosted in a few different environments, & the app needs different configuration variables for all of those environments.

    For example, i have a class with all my configurable variables.

    PHP Code:
    Ext.define("Foo.Config", {    
      
    statics: {
        
    API_BASE_URL"http://myapiserver1"
      
    }
    }); 
    On build, I want to somehow pass a parameter to replace these parameters. Something like:

    Code:
    sencha app build --API_BASE_URL=http://myapiserver2
    What is the best way to do this?

    Many thanks for your time.

  2. #2
    Sencha Premium Member
    Join Date
    May 2011
    Posts
    27
    Vote Rating
    5
    kpiland is on a distinguished road

      0  

    Default


    I have a somewhat similar need, so I did some experimentation and think I've figured out what you can do.

    The key is to use the x-generate Ant task as described in the Ant Integration Guide.

    Forgive me, if you will in my example for just using JS files instead of a PHP file.
    In your example, you'd create a template file like this:
    Code:
    Ext.define("Foo.Config", {    
      statics: {
        API_BASE_URL: "{baseurl}"
      }
    });
    Name the file with both the 'tpl' and 'merge' extensions (e.g. 'Config.js.tpl.merge').

    Add the x-generate task to the Ant build. It's not clear to me exactly which task you should do this in as there are several customization points that happen at different points in the build process. In my example, I just put it in the before init phase.

    The task tells the build process to generate Config.js using Config.js.merge as a template and merge any user changes from an existing Config.js into the final file. The merge uses a JSON file named 'appcodegen.json' as a store for managing merges. You don't have to create the file yourself. I'm not sure if this is the ideal location either. When Cmd generates things for your app, it has its own JSON file called 'codegen.json' in that same directory. I just set this up to use my own JSON file in the same location.
    Code:
    <target name="-before-init">
        <x-generate file="./app/Config.js.tpl.merge" tofile="./app/Config.js" 
              store="${args.path}/${senchadir}/app/appcodegen.json"
              basedir="${args.path}">
            <param name="baseurl" value="${baseurl}"/>
        </x-generate>
    </target>
    Add an Ant property to local.properties that gets changed to set the desired URL - or if there is some more complex way you wish to set the variable, then knock yourself out .
    Code:
    baseurl=http://myapiserver1
    When you run the build, it will re-generate Config.js and merge the template change into your file.

    One catch that I found to this is that the JSON file used for the merge needs to be initialized. The build complains if you run this with nothing in the JSON file and with an existing Config.js file. You basically need to make the build generate a 'clean' version first. What I did was:
    1. Copy off my customized JS file
    2. Run the build to generate a new JS file from the template. This puts something into the JSON merge file.
    3. Manually put back my customizations to the JS file.
    After doing that, I could change the ant variable and regenerate to get template to modify my customized file.

    It does seem to be a tad sensitive when trying to do the merge and figure out what you've changed and where to apply the template change. I found that by pushing the template code to the top and making sure that it was followed by an empty line it was able to reliably find the right place to update the file.

    The example class you provided is very simplistic. I'm assuming that you just cut out a bunch of other stuff that is unaffected by what you're trying to do. If you really only have a very simple class with nothing that isn't generated, then you could simplify this by skipping all the merge stuff. You could just name the template file Config.js.tpl, remove the store property from the Ant task and then the build would generate a new file without bothering to merge in any manual changes.

  3. #3
    Sencha User
    Join Date
    Mar 2014
    Posts
    6
    Vote Rating
    1
    mgreystone is on a distinguished road

      0  

    Default


    Yes! This is exactly what i needed. Thank you for such a complete answer.

Thread Participants: 1

Tags for this Thread