/**
 * @license
 * Copyright 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

foam.CLASS({
  package: 'foam.android',
  name: 'GenResources',
  imports: [
    'arequire',
  ],
  properties: [
    {
      class: 'StringArray',
      name: 'models',
    },
    {
      class: 'String',
      name: 'outfile',
    },
    {
      name: 'fs',
      factory: function() { return require('fs'); }
    },
  ],
  methods: [
    function execute() {
      var self = this;
      if (!this.outfile) {
        console.log('ERROR: outfile not specified');
        process.exit(1);
      }
      var p = Promise.resolve();
      for (var i = 0; i < this.models.length; i++) {
        p = p.then(this.arequire(this.models[i])) 
      }
      p.then(function() {
        var resources = [];
        for (var i = 0; i < self.models.length; i++) {
          var model = self.lookup(self.models[i], self.__subContext__);
          resources = resources.concat(self.modelToResources(model));
        }
        self.fs.writeFileSync(
            self.outfile,
            self.genResource(resources));
      })
      p = p.catch(function(err) {
        console.log('Error', err);
      });
    },
    function modelToResources(model) { foam.assert(false, 'Implement') },
  ],
  templates: [
    {
      name: 'genResource',
      args: ['resources'],
      template: '<% foam.assert(false, "Implement") %>',
    }
  ],
});

@@ -1136,28 +1136,20 @@
 ar p
- = P
 romise
-.resolve()
+s = %5B%5D
 ;%0A  
@@ -1212,17 +1212,20 @@
    p
- = p.then
+romises.push
 (thi
@@ -1255,17 +1255,54 @@
 i%5D))
- %0A      %7D
+;%0A      %7D%0A      var p = Promise.all(promises);
 %0A   
@@ -1465,23 +1465,8 @@
 self
-.__subContext__
 );%0A 

Make GenResources use an array of promises instead of chaining thens.