Comparing version 0.1.12 to 0.1.13
var U = require('../../utils'), | ||
colors = require('colors'), | ||
path = require('path'), | ||
fs = require('fs'), | ||
alloyUniqueIdPrefix = '__alloyId', | ||
@@ -18,2 +21,31 @@ alloyUniqueIdCounter = 0, | ||
exports.loadStyle = function(p) { | ||
if (path.existsSync(p)) { | ||
var f = fs.readFileSync(p, 'utf8'); | ||
// skip empty files | ||
if (/^\s*$/.test(f)) { | ||
return {}; | ||
} | ||
f = f.replace(/Titanium\./g,"Ti."); | ||
// fixup constants so we can use them in JSON but then we do magic conversions | ||
f = f.replace(/Ti\.UI\.FILL/g,'"TI_UI_FILL"'); | ||
f = f.replace(/Ti\.UI\.SIZE/g,'"TI_UI_SIZE"'); | ||
f = f.replace(/Ti\.UI\.TEXT_ALIGNMENT_LEFT/g,'"TI_UI_TEXT_ALIGNMENT_LEFT"') | ||
f = f.replace(/Ti\.UI\.TEXT_ALIGNMENT_RIGHT/g,'"TI_UI_TEXT_ALIGNMENT_RIGHT"') | ||
f = f.replace(/Ti\.UI\.TEXT_ALIGNMENT_CENTER/g,'"TI_UI_TEXT_ALIGNMENT_CENTER"') | ||
try | ||
{ | ||
return JSON.parse(f); | ||
} | ||
catch(E) | ||
{ | ||
U.die("Error parsing style at "+p.yellow+". Error was: "+String(E).red); | ||
} | ||
} | ||
return {}; | ||
} | ||
exports.addStyleById = function(styles, id, key, value) { | ||
@@ -20,0 +52,0 @@ var idStr = '#' + id; |
@@ -126,37 +126,2 @@ var path = require('path'), | ||
function loadStyle(p) | ||
{ | ||
if (path.existsSync(p)) | ||
{ | ||
var f = fs.readFileSync(p, 'utf8'); | ||
// skip empty files | ||
if (/^\s*$/.test(f)) { | ||
return {}; | ||
} | ||
f = f.replace(/Titanium\./g,"Ti."); | ||
// fixup constants so we can use them in JSON but then we do magic conversions | ||
f = f.replace(/Ti\.UI\.FILL/g,'"TI_UI_FILL"'); | ||
f = f.replace(/Ti\.UI\.SIZE/g,'"TI_UI_SIZE"'); | ||
f = f.replace(/Ti\.UI\.TEXT_ALIGNMENT_LEFT/g,'"TI_UI_TEXT_ALIGNMENT_LEFT"') | ||
f = f.replace(/Ti\.UI\.TEXT_ALIGNMENT_RIGHT/g,'"TI_UI_TEXT_ALIGNMENT_RIGHT"') | ||
f = f.replace(/Ti\.UI\.TEXT_ALIGNMENT_CENTER/g,'"TI_UI_TEXT_ALIGNMENT_CENTER"') | ||
try | ||
{ | ||
return JSON.parse(f); | ||
} | ||
catch(E) | ||
{ | ||
U.die("Error parsing style at "+p.yellow+". Error was: "+String(E).red); | ||
} | ||
} | ||
return {}; | ||
} | ||
function generateVarName(id) | ||
{ | ||
return '$.'+id; | ||
} | ||
function copyAssets() | ||
@@ -288,68 +253,8 @@ { | ||
var JSON_NULL = JSON.parse('null'); | ||
function mergeStyles(from,to) | ||
function generateController(name, dir, id) | ||
{ | ||
if (from) | ||
{ | ||
for (var k in from) | ||
{ | ||
var v = from[k]; | ||
// for optimization, remove null or undefined values | ||
if (v == JSON_NULL || typeof(v)==='undefined' || typeof(v)==='null') | ||
{ | ||
delete to[k]; | ||
} | ||
else | ||
{ | ||
to[k] = from[k]; | ||
} | ||
} | ||
} | ||
} | ||
function generateStyleParams(styles,classes,id,className) | ||
{ | ||
var s = {}; | ||
mergeStyles(styles['View'],s); | ||
mergeStyles(styles[U.properCase(className)],s); | ||
for (var c=0;c<classes.length;c++) | ||
{ | ||
var clsn = classes[c]; | ||
mergeStyles(styles['.'+clsn],s); | ||
} | ||
mergeStyles(styles['#'+id],s); | ||
if (id) s['id'] = id; | ||
var str = []; | ||
var constants = { | ||
'TI_UI_FILL':'Ti.UI.FILL', | ||
'TI_UI_SIZE':'Ti.UI.SIZE', | ||
'TI_UI_TEXT_ALIGNMENT_LEFT':'Ti.UI.TEXT_ALIGNMENT_LEFT', | ||
'TI_UI_TEXT_ALIGNMENT_CENTER':'Ti.UI.TEXT_ALIGNMENT_CENTER', | ||
'TI_UI_TEXT_ALIGNMENT_RIGHT':'Ti.UI.TEXT_ALIGNMENT_RIGHT' | ||
}; | ||
for (var sn in s) | ||
{ | ||
var v = s[sn]; | ||
var q = typeof(v) === 'string'; | ||
var cf = constants[v]; | ||
if (cf) { | ||
str.push("\t\t"+sn+':'+cf); | ||
} else if (q) { | ||
str.push("\t\t"+sn+':'+'"'+v+'"'); | ||
} else { | ||
str.push("\t\t"+sn+':'+ JSON.stringify(v)); | ||
} | ||
} | ||
return str.join(",\n"); | ||
} | ||
function generateController(name, dir, state, id) | ||
{ | ||
var code = ''; | ||
var cd = dir ? path.join(dir,'controllers') : controllersDir; | ||
var p = path.join(cd,name+'.js'); | ||
var symbol = generateVarName(id); | ||
var symbol = CU.generateVarName(id); | ||
@@ -364,8 +269,3 @@ if (path.existsSync(p)) { | ||
function generateUniqueId() { | ||
return alloyUniqueIdPrefix + alloyUniqueIdCounter++; | ||
}; | ||
function parseNode(node, state, defaultId) { | ||
function generateNode(node, state, defaultId) { | ||
if (node.nodeType != 1) return ''; | ||
@@ -377,3 +277,3 @@ | ||
req = node.getAttribute('require'), | ||
id = node.getAttribute('id') || defaultId || req || generateUniqueId(), | ||
id = node.getAttribute('id') || defaultId || req || CU.generateUniqueId(), | ||
code = ''; | ||
@@ -409,3 +309,3 @@ | ||
for (var i = 0, l = newParent.childNodes.length; i < l; i++) { | ||
code += parseNode(newParent.childNodes.item(i), state); | ||
code += generateNode(newParent.childNodes.item(i), state); | ||
} | ||
@@ -416,86 +316,4 @@ } | ||
} | ||
function generateNode(ischild,viewFile,node,state,defId) | ||
{ | ||
if (node.nodeType != 1) return ''; | ||
var code = ''; | ||
var req = node.getAttribute('require'); | ||
// TODO: may need to rethink including "req" here. It simplifies usage, | ||
// but will cause complications when views/widgets are used more than | ||
// once in a view. | ||
var id = node.getAttribute('id') || defId || req || generateUniqueId(); | ||
var symbol = generateVarName(id); | ||
var nodename = node.nodeName; | ||
var classes = node.getAttribute('class').split(' '); | ||
if (req) { | ||
var commonjs = "alloy/components/" + req; | ||
if (nodename === 'Widget') { | ||
commonjs = "alloy/widgets/" + req + "/components/widget"; | ||
} | ||
code += symbol + " = (require('" + commonjs + "')).create();\n"; | ||
if (!ischild) { | ||
code += "root$ = " + symbol + ";\n"; | ||
} | ||
if (state.parentNode) { | ||
code += symbol + '.setParent(' + state.parentNode + ');\n'; | ||
} | ||
} else { | ||
var ns = node.getAttribute('ns') || "Ti.UI"; | ||
var fn = "create" + nodename; | ||
if (node.childNodes.length > 0) | ||
{ | ||
var processors = | ||
[ | ||
['Label','text'], | ||
['Button','title'] | ||
]; | ||
_.every(processors, function(el) | ||
{ | ||
if (nodename == el[0]) | ||
{ | ||
var k = el[1]; | ||
var str = U.XML.getNodeText(node); | ||
if (!state.styles['#'+id]) | ||
{ | ||
state.styles['#'+id]={}; | ||
} | ||
state.styles['#'+id][k]=str; | ||
return false; | ||
} | ||
return true; | ||
}); | ||
} | ||
code += '\t' + symbol + " = A$(" + ns + "." + fn + "({\n"; | ||
code += generateStyleParams(state.styles,classes,id,node.nodeName); | ||
code += "\n\t}),'" + node.nodeName + "', " + (state.parentNode || 'null') + ");\n\t"; | ||
if (!ischild) { | ||
code += "root$ = " + symbol + ";\n"; | ||
} | ||
if (state.parentNode) { | ||
code += state.parentNode+".add("+symbol+");\n"; | ||
} | ||
} | ||
var childstate = { | ||
parentNode: symbol, | ||
styles: state.styles | ||
}; | ||
for (var c=0;c<node.childNodes.length;c++) | ||
{ | ||
var child = node.childNodes[c]; | ||
code += generateNode(true,viewFile,child,childstate); | ||
} | ||
return code; | ||
} | ||
function findModelMigrations(state,name) | ||
function findModelMigrations(name) | ||
{ | ||
@@ -537,3 +355,3 @@ try | ||
function findAndLoadModels(state) { | ||
function findAndLoadModels() { | ||
var f = modelsDir; | ||
@@ -564,7 +382,7 @@ var code = ''; | ||
var migrations = findModelMigrations(state,part); | ||
var migrations = findModelMigrations(part); | ||
var theid = U.properCase(part), theidc = U.properCase(part)+'Collection'; | ||
var symbol1 = generateVarName(theid); | ||
var symbol2 = generateVarName(theidc); | ||
var symbol1 = CU.generateVarName(theid); | ||
var symbol2 = CU.generateVarName(theidc); | ||
var codegen = symbol1 + " = M$('"+ part +"',\n" + | ||
@@ -589,3 +407,3 @@ jm + "\n" + | ||
function parseView(viewName,state,dir,viewid,isWidget,wJSon) | ||
function parseView(viewName,dir,viewid,manifest) | ||
{ | ||
@@ -598,3 +416,3 @@ var template = { | ||
}; | ||
state = { parent: {} }; | ||
var state = { parent: {} }; | ||
var vd = dir ? path.join(dir,'views') : viewsDir; | ||
@@ -609,3 +427,3 @@ var sd = dir ? path.join(dir,'styles') : stylesDir; | ||
var styleFile = path.join(sd,viewName+".json"); | ||
var styles = loadStyle(styleFile); | ||
var styles = CU.loadStyle(styleFile); | ||
state.styles = styles; | ||
@@ -627,10 +445,10 @@ | ||
if (viewName === 'index') { | ||
template.viewCode += findAndLoadModels(state); | ||
template.viewCode += findAndLoadModels(); | ||
} | ||
// Generate Titanium code from the markup | ||
for (var i = 0, l = docRoot.childNodes.length; i < l; i++) { | ||
// template.viewCode += generateNode(false,viewFile,docRoot.childNodes.item(i),state,viewid||viewname); | ||
template.viewCode += parseNode(docRoot.childNodes.item(i),state,viewid||viewname); | ||
template.viewCode += generateNode(docRoot.childNodes.item(i),state,viewid||viewname); | ||
} | ||
template.controllerCode += generateController(viewName,dir,state,id); | ||
template.controllerCode += generateController(viewName,dir,id); | ||
@@ -640,5 +458,5 @@ // create commonjs module for this view/controller | ||
code = U.processSourceCode(code, alloyConfig); | ||
if (isWidget) { | ||
wrench.mkdirSyncRecursive(path.join(outputPath, 'Resources', 'alloy', 'widgets', wJSon.id, 'components'), 0777); | ||
fs.writeFileSync(path.join(outputPath, 'Resources', 'alloy', 'widgets', wJSon.id, 'components', viewName + '.js'), code); | ||
if (manifest) { | ||
wrench.mkdirSyncRecursive(path.join(outputPath, 'Resources', 'alloy', 'widgets', manifest.id, 'components'), 0777); | ||
fs.writeFileSync(path.join(outputPath, 'Resources', 'alloy', 'widgets', manifest.id, 'components', viewName + '.js'), code); | ||
} else { | ||
@@ -648,8 +466,2 @@ fs.writeFileSync(path.join(outputPath, 'Resources', 'alloy', 'components', viewName + '.js'), code); | ||
} | ||
var state = { | ||
//parentNode: null, | ||
//styles: null | ||
parent: {} | ||
}; | ||
@@ -661,40 +473,15 @@ // create components directory for view/controller components | ||
// TODO: Clean up this iteration mess! | ||
// loop through all widgets | ||
var widgetPath = path.join(outputPath,'app','widgets'); | ||
if (path.existsSync(widgetPath)) { | ||
var wFiles = fs.readdirSync(widgetPath); | ||
for (var i = 0; i < wFiles.length; i++) { | ||
var wDir = wFiles[i]; | ||
// TODO: make sure wDir is a directory | ||
var wDirFiles = fs.readdirSync(path.join(widgetPath,wDir)); | ||
for (var j = 0; j < wDirFiles.length; j++) { | ||
if (_.indexOf(wDirFiles,'widget.json') === -1) { | ||
break; | ||
} | ||
// Process all views, including all those belonging to widgets | ||
var viewCollection = U.getWidgetDirectories(outputPath); | ||
viewCollection.push({ dir: path.join(outputPath,'app') }); | ||
_.each(viewCollection, function(collection) { | ||
_.each(fs.readdirSync(path.join(collection.dir,'views')), function(view) { | ||
if (/\.xml$/.test(view)) { | ||
var basename = path.basename(view, '.xml'); | ||
parseView(basename, collection.dir, basename, collection.manifest); | ||
} | ||
}); | ||
}); | ||
var wReq = JSON.parse(fs.readFileSync(path.join(widgetPath,wDir,'widget.json'),'utf8')); | ||
// need to loop through all views | ||
var vFiles = fs.readdirSync(path.join(widgetPath, wDir,'views')); | ||
for (var k = 0; k < vFiles.length; k++) { | ||
if (/\.xml$/.test(vFiles[k])) { | ||
var basename = path.basename(vFiles[k], '.xml'); | ||
parseView(basename,state,path.join(widgetPath,wDir),basename,true,wReq); | ||
} | ||
} | ||
} | ||
} | ||
// need to loop through all views | ||
var vFiles = fs.readdirSync(path.join(outputPath,'app','views')); | ||
for (var i = 0; i < vFiles.length; i++) { | ||
if (/\.xml$/.test(vFiles[i])) { | ||
var basename = path.basename(vFiles[i], '.xml'); | ||
parseView(basename,state,null,basename,false); | ||
} | ||
} | ||
// copy assets and libs | ||
copyAssets(); | ||
@@ -723,5 +510,5 @@ copyLibs(); | ||
// trigger our custom compiler makefile | ||
compilerMakeFile.trigger("post:compile",_.extend(_.clone(compileConfig), {state:state})); | ||
compilerMakeFile.trigger("post:compile",_.clone(compileConfig)); | ||
}; | ||
module.exports = compile; |
@@ -27,2 +27,21 @@ // The island of misfit toys... for functions | ||
exports.getWidgetDirectories = function(outputPath) { | ||
var dirs = []; | ||
var widgetPath = path.join(outputPath,'app','widgets'); | ||
if (path.existsSync(widgetPath)) { | ||
var wFiles = fs.readdirSync(widgetPath); | ||
for (var i = 0; i < wFiles.length; i++) { | ||
var wDir = path.join(widgetPath,wFiles[i]); | ||
if (fs.statSync(wDir).isDirectory() && | ||
_.indexOf(fs.readdirSync(wDir), 'widget.json') !== -1) { | ||
dirs.push({ | ||
dir: wDir, | ||
manifest: JSON.parse(fs.readFileSync(path.join(wDir,'widget.json'),'utf8')) | ||
}); | ||
} | ||
} | ||
} | ||
return dirs; | ||
}; | ||
exports.properCase = function(n) { | ||
@@ -29,0 +48,0 @@ return n.charAt(0).toUpperCase() + n.substring(1); |
@@ -16,3 +16,3 @@ { | ||
], | ||
"version": "0.1.12", | ||
"version": "0.1.13", | ||
"author": "Appcelerator, Inc. <info@appcelerator.com>", | ||
@@ -19,0 +19,0 @@ "maintainers": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
863624
14304
32