Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-24198] Improve code generator class usage detection (2_0_X) #136

Merged
merged 1 commit into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions iphone/plugin/hyperloop.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,12 +625,24 @@ HyperloopiOSBuilder.prototype.generateStubs = function generateStubs(callback) {

// now generate the stubs
this.logger.debug('Generating stubs');
var started = Date.now();
hm.generate.generateFromJSON(
this.builder.tiapp.name,
this.hyperloopJSDir,
this.metabase,
this.parserState,
callback,
function (err, sourceSet, modules) {
if (err) {
return callback(err);
}

var codeGenerator = new hm.generate.CodeGenerator(sourceSet, this.metabase, this.parserState, modules, this.references);
codeGenerator.generate(this.hyperloopJSDir);

var duration = Date.now() - started;
this.logger.info('Generation took ' + duration + ' ms');

callback();
}.bind(this),
this.frameworks
);
};
Expand Down
4 changes: 2 additions & 2 deletions metabase/ios/lib/generate/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ function isClassProperty(propertyMetadata) {
}

/**
* generate a class file
* Generates the source template data for a class file
*/
function generate (dir, json, cls, state) {
function generate (json, cls, state) {
return makeClass(json, cls, state);
}

Expand Down
19 changes: 14 additions & 5 deletions metabase/ios/lib/generate/code-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var chalk = require('chalk');
var fs = require('fs');
var path = require('path');
var util = require('./util');
var wrench = require('wrench');

/**
* A generator for Hyperloop source code files
Expand All @@ -19,12 +20,14 @@ class CodeGenerator {
* @param {Object} json Metabase object
* @param {Object} state State from the metabase parser
* @param {Object} modules Map of module info objects
* @param {Object} userRequires Map of explicit requires of Hyperloop files made by the user
*/
constructor(sourceSet, json, state, modules) {
constructor(sourceSet, json, state, modules, userRequires) {
this.sourceSet = sourceSet;
this.json = json;
this.state = state;
this.modules = modules;
this.userRequires = userRequires;
}

/**
Expand All @@ -39,6 +42,9 @@ class CodeGenerator {
// TODO: Only do this for production builds to support liveview
this.sourceSet.classes = this.stripUnusedClasses();

if (!fs.existsSync(outputPath)) {
wrench.mkdirSyncRecursive(outputPath);
}
this.generateClasses(outputPath);
this.generateStructs(outputPath);
this.generateModules(outputPath);
Expand Down Expand Up @@ -87,14 +93,17 @@ class CodeGenerator {
var hasClassMembers = classMemberLists.some((memberListName) => {
return classInfo.class[memberListName].length > 0;
});

var isSuperclass = classInfo.class.name in superclassMap;

var isClassUsed = hasClassMembers || isSuperclass;
if (isClassUsed) {

var fqcn = classInfo.framework + '/' + classInfo.class.name;
var isExplicitlyRequired = Object.keys(this.userRequires).some((requirePath) => {
return requirePath.indexOf(fqcn.toLowerCase()) !== -1;
});

if (isClassUsed || isExplicitlyRequired) {
usedClasses[className] = this.sourceSet.classes[className];
} else {
var fqcn = classInfo.framework + '/' + classInfo.class.name;
util.logger.trace(chalk.gray('Excluding class ') + chalk.green(fqcn));
}
});
Expand Down
4 changes: 2 additions & 2 deletions metabase/ios/lib/generate/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ function generateMethod (state, metabase, imports, cls, classDef, selector, enco
return code.join('\n');
}

Parser.generate = function (dir, state, metabase) {
Parser.generate = function (state, metabase) {
if (Object.keys(state.getClassNames())) {
var code = state.gencode || [], imports = state.imports || {};
state.getClassNames().forEach(function (name) {
Expand Down Expand Up @@ -413,7 +413,7 @@ Parser.generate = function (dir, state, metabase) {
return 'Hyperloop.addMethod(this, arguments[0]);';
}
};
customClasses[cls.name] = classgen.generate(dir, metabase, cls, state);
customClasses[cls.name] = classgen.generate(metabase, cls, state);
mappings[cls.name] = '/hyperloop/' + (cls.framework + '/' + cls.name).toLowerCase();
});
}
Expand Down
27 changes: 7 additions & 20 deletions metabase/ios/lib/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
var fs = require('fs'),
path = require('path'),
async = require('async'),
wrench = require('wrench'),
genclass = require('./class'),
genmodule = require('./module'),
genstruct = require('./struct'),
Expand Down Expand Up @@ -136,20 +135,14 @@ function generateBuiltins (json, callback) {
});
}

function generateFromJSON (name, dir, json, state, callback, includes) {
var started = Date.now();

function generateFromJSON (name, json, state, callback, includes) {
// set the name of the app in the state object
state.appName = name;

if (!json) { return callback(); }

json.classes = json.classes || {};

if (!fs.existsSync(dir)) {
wrench.mkdirSyncRecursive(dir);
}

generateBuiltins(json, function (err) {
if (err) { return callback(err); }

Expand Down Expand Up @@ -253,7 +246,7 @@ function generateFromJSON (name, dir, json, state, callback, includes) {
cls.framework = custom_frameworks[cls.filename] || cls.framework;
}
// TODO: add categories
sourceSet.classes[k] = genclass.generate(dir, json, cls, state);
sourceSet.classes[k] = genclass.generate(json, cls, state);
});

// structs
Expand All @@ -263,7 +256,7 @@ function generateFromJSON (name, dir, json, state, callback, includes) {
// if we have leading underscores for struct names, trim them
struct.name = struct.name.replace(/^(_)+/g,'').trim();
}
sourceSet.structs[k] = genstruct.generate(dir, json, struct);
sourceSet.structs[k] = genstruct.generate(json, struct);
});

// modules
Expand Down Expand Up @@ -301,23 +294,16 @@ function generateFromJSON (name, dir, json, state, callback, includes) {

// generate the modules
modules && Object.keys(modules).forEach(function (k) {
var moduleInfo = genmodule.generate(dir, json, modules[k], state);
var moduleInfo = genmodule.generate(json, modules[k], state);
if (moduleInfo) {
sourceSet.modules[k] = moduleInfo;
}
});

// generate any custom classes
sourceSet.customs = gencustom.generate(dir, state, json);

var codeGenerator = new CodeGenerator(sourceSet, json, state, modules);
codeGenerator.generate(dir);

var duration = Date.now() - started;

util.logger.info('Generation took ' + duration + ' ms');
sourceSet.customs = gencustom.generate(state, json);

callback();
callback(null, sourceSet, modules);
});
}

Expand Down Expand Up @@ -362,3 +348,4 @@ exports.generateFromJSON = generateFromJSON;
exports.parse = parse;
exports.parseFromBuffer = parseBuffer;
exports.generateState = generateState;
exports.CodeGenerator = CodeGenerator;
5 changes: 3 additions & 2 deletions metabase/ios/lib/generate/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ function makeModule (json, module, state) {
}

/**
* generate a module file and it's module objective-c class
* Generates the source template data for a module file and it's module
* objective-c class
*/
function generate (dir, json, mod, state) {
function generate (json, mod, state) {
// for now, skip non frameworks
if (mod.framework.indexOf('/') >= 0) { return; }
// generate the objective-c module
Expand Down
4 changes: 2 additions & 2 deletions metabase/ios/lib/generate/struct.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ function makeStruct (json, struct) {
}

/**
* generate a struct file
* Generates the source template data for a struct file
*/
function generate (dir, json, struct) {
function generate (json, struct) {
return makeStruct(json, struct);
}

Expand Down