UNPKG

6.12 kBJavaScriptView Raw
1var fs = require('fs'),
2 path = require('path'),
3 XMLSerializer = require('xmldom').XMLSerializer,
4 pkg = require('../package'),
5 U = require('./utils'),
6 CONST = require('./common/constants'),
7 logger = require('./logger');
8
9var DEFAULT_TIAPP = path.join('.', 'tiapp.xml');
10
11var tiapp = {},
12 tiappFile, doc;
13
14// initialize the project folder
15tiapp.init = function(file) {
16 tiappFile = file || DEFAULT_TIAPP;
17 doc = tiapp.parse(tiappFile);
18};
19
20// Return an XML document object representing the tiapp.xml file
21tiapp.parse = function(file) {
22 file = file || tiappFile;
23 if (!fs.existsSync(file)) {
24 U.die('tiapp.xml file does not exist at "' + file + '"');
25 }
26 return U.XML.parseFromFile(file);
27};
28
29// Get the Titanium SDK version as a string
30// Get the Titanium SDK version as a string
31tiapp.getSdkVersion = function() {
32 var elems = doc.documentElement.getElementsByTagName('sdk-version');
33 if (elems && elems.length > 0) {
34 return U.XML.getNodeText(elems.item(elems.length - 1));
35 } else {
36 if (process.env.sdk) {
37 return process.env.sdk;
38 } else {
39 return getSdkSelectVersion();
40 }
41 }
42};
43function getSdkSelectVersion() {
44 var homeDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'],
45 file = path.join(homeDir, '.titanium', 'config.json');
46 if (!fs.existsSync(file)) {
47 U.die('Titanium configuration file does not exist at "' + file + '"');
48 }
49 var ticonfig = JSON.parse(fs.readFileSync(file, {encoding: 'utf8'}));
50 return ticonfig.sdk.selected;
51}
52
53// Get the value of a property from the tiapp.xml
54tiapp.getProperty = function(name) {
55 var props = doc.documentElement.getElementsByTagName('property');
56 for (var i = 0; i < props.length; i++) {
57 if (props.item(i).getAttribute('name') === name) {
58 return props.item(i);
59 }
60 }
61 return null;
62};
63
64// Add a module to the tiapp.xml
65tiapp.installModule = function(opts) {
66 install('module', opts);
67};
68
69// Add a plugin to the tiapp.xml
70tiapp.installPlugin = function(opts) {
71 install('plugin', opts);
72};
73
74// make sure the target TiSDK version meets the minimum for Alloy
75tiapp.validateSdkVersion = function() {
76 var tiVersion = tiapp.getSdkVersion();
77 if (tiVersion === null) {
78 logger.warn('Unable to determine Titanium SDK version from tiapp.xml.');
79 logger.warn('Your app may have unexpected behavior. Make sure your tiapp.xml is valid.');
80 } else if (tiapp.version.lt(tiVersion, CONST.MINIMUM_TI_SDK)) {
81 logger.error('Alloy ' + pkg.version + ' requires Titanium SDK ' +
82 CONST.MINIMUM_TI_SDK + ' or higher.');
83 logger.error('"' + tiVersion + '" was found in the "sdk-version" field of your tiapp.xml.');
84 logger.error('If you are building with the legacy titanium.py script and are specifying ');
85 logger.error('an SDK version as a CLI argument that is different than the one in your ');
86 logger.error('tiapp.xml, please change the version in your tiapp.xml file.');
87 process.exit(1);
88 }
89};
90
91// version comparison functions
92tiapp.version = {
93 compare: function(v1, v2) {
94 // use the tiapp.xml version if v2 is not specified
95 if (typeof v2 === 'undefined') {
96 v2 = v1;
97 v1 = tiapp.getSdkVersion();
98 }
99
100 var parts1 = (v1 || '').split('.'),
101 parts2 = (v2 || '').split('.');
102
103 for (var i = 0; i < 3; i++) {
104 var p1 = parseInt(parts1[i] || 0, 10),
105 p2 = parseInt(parts2[i] || 0, 10);
106 if (p1 > p2) {
107 return 1;
108 } else if (p1 < p2) {
109 return -1;
110 }
111 }
112
113 return 0;
114 },
115 eq: function(v1, v2) { return tiapp.version.compare(v1, v2) === 0; },
116 gt: function(v1, v2) { return tiapp.version.compare(v1, v2) === 1; },
117 gte: function(v1, v2) { return tiapp.version.compare(v1, v2) !== -1; },
118 lt: function(v1, v2) { return tiapp.version.compare(v1, v2) === -1; },
119 lte: function(v1, v2) { return tiapp.version.compare(v1, v2) !== 1; },
120 neq: function(v1, v2) { return tiapp.version.compare(v1, v2) !== 0; }
121};
122
123tiapp.getDeploymentTargets = function() {
124 var deployment = doc.documentElement.getElementsByTagName('deployment-targets'),
125 results = [];
126
127 if (deployment.length > 0) {
128 var targets = deployment.item(0).getElementsByTagName('target');
129 for (var i = 0, j = targets.length; i < j; i++) {
130 var target = targets.item(i);
131 if (U.XML.getNodeText(target) === 'true') {
132 var platform = target.getAttribute('device');
133 if (platform === 'iphone' || platform === 'ipad') {
134 platform = 'ios';
135 }
136
137 if (results.indexOf(platform) === -1) {
138 results.push(platform);
139 }
140 }
141 }
142 }
143 return results;
144};
145
146function install(type, opts) {
147 type = type || 'module';
148 opts = opts || {};
149
150 var err = 'Project creation failed. Unable to install ' + type + ' "' +
151 (opts.name || opts.id) + '"';
152
153 // read the tiapp.xml file
154 var collection = doc.documentElement.getElementsByTagName(type + 's');
155 var found = false;
156
157 // Determine if the module or plugin is already installed
158 if (collection.length > 0) {
159 var items = collection.item(0).getElementsByTagName(type);
160 if (items.length > 0) {
161 for (var c = 0; c < items.length; c++) {
162 var theItem = items.item(c);
163 var theItemText = U.XML.getNodeText(theItem);
164 if (theItemText == opts.id) {
165 found = true;
166 break;
167 }
168 }
169 }
170 }
171
172 // install module or plugin
173 if (!found) {
174 // create the node to be inserted
175 var node = doc.createElement(type);
176 var text = doc.createTextNode(opts.id);
177 if (opts.platform) {
178 node.setAttribute('platform', opts.platform);
179 }
180 if (opts.version) {
181 node.setAttribute('version', opts.version);
182 }
183 node.appendChild(text);
184
185 // add the node into tiapp.xml
186 var pna = null;
187 if (collection.length === 0) {
188 var pn = doc.createElement(type + 's');
189 doc.documentElement.appendChild(pn);
190 doc.documentElement.appendChild(doc.createTextNode('\n'));
191 pna = pn;
192 } else {
193 pna = collection.item(0);
194 }
195 pna.appendChild(node);
196 pna.appendChild(doc.createTextNode('\n'));
197
198 // serialize the xml and write to tiapp.xml
199 var serializer = new XMLSerializer();
200 var newxml = serializer.serializeToString(doc);
201 fs.writeFileSync(tiappFile, newxml, 'utf8');
202
203 logger.info('Installed "' + opts.id + '" ' + type + ' to ' + tiappFile);
204 }
205
206}
207
208module.exports = tiapp;