-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsectv-tizen.js
More file actions
372 lines (333 loc) · 13.6 KB
/
Copy pathsectv-tizen.js
File metadata and controls
372 lines (333 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Copyright 2015 Samsung Electronics Co., Ltd.
*
* 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.
*/
'use strict';
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var inquirer = require('inquirer');
var utils = require('../lib/utils');
var shelljs = require('shelljs');
var mustache = require('mustache');
var child = require('child_process');
var js2xmlparser = require('js2xmlparser');
function saveUserConfFile(configPath, tizenConf) {
var userConfData = {};
if (fs.existsSync(configPath)) {
userConfData = JSON.parse(fs.readFileSync(configPath));
}
userConfData.tizen = tizenConf;
fs.writeFileSync(configPath, JSON.stringify(userConfData, null, 2), {
encoding: 'utf8'
});
}
function getNextSemVersion(curver) { // just increase revision
var tmp = curver.split('.');
var major = parseInt(tmp[0], 10);
var minor = parseInt(tmp[1], 10);
var revision = (parseInt(tmp[2], 10) || 0) + 1;
return major + '.' + minor + '.' + revision;
}
function generateTizenPackageID() {
return Math.random().toString(36).substr(2, 10);
}
function getValidTizenConfData(configPath) {
console.log(configPath);
if (!(fs.existsSync(configPath))) {
// userconf.json is not exists
return null;
}
// userconf.json is already exists
var userConfData = JSON.parse(fs.readFileSync(configPath));
if (!(userConfData.hasOwnProperty('tizen'))) {
// userconf.json doesn't have tizen data
return null;
}
var tizenData = userConfData.tizen;
if (typeof tizenData.name !== 'string' || tizenData.name.length <= 0) {
// name is invalid
return null;
}
if (typeof tizenData.packageid !== 'string' || tizenData.packageid.length !== 10) {
// packageid is invalid
return null;
}
if (typeof tizenData.iconpath !== 'string' || tizenData.iconpath.length <= 0) {
// iconpath is invalid
return null;
}
if (!validateTizenVersion(tizenData.version)) {
// version is invalid
return null;
}
// description is not mendatory
return tizenData;
}
function validateTizenVersion(version) {
var tmp = version.split('.'),
i;
if (tmp.length !== 3) {
return false;
}
for (i = 0; i < tmp.length; i++) {
if (isNaN(tmp[i])) {
return false;
}
}
return i === tmp.length;
}
function confirmUseExistingData(userData, callback) {
console.log('');
console.log(' > [ Stored Information ]');
console.log(' > name : ' + userData.name);
console.log(' > package ID : ' + userData.packageid);
console.log(' > version : ' + userData.version);
console.log(' > description : ' + userData.description);
var ask = [{
type: 'confirm',
name: 'useExisting',
message: 'Already have \'userconf.json\', Do you want to use this data?'
}];
inquirer.prompt(ask).then(function(answers) {
callback(!!answers.useExisting);
});
}
function askUserData(cordovaConf, callback, versionOnly, userData) {
var ask;
if(versionOnly) {
var curVer = userData.version;
var updateVer = getNextSemVersion(userData.version);
ask = [{
type: 'input',
name: 'version',
message: 'Current version is ' + curVer + '. Application version: ',
default: updateVer,
validate: function(input) {
return /\d.\d.\d/.test(input) ? true : 'invalid version string for tizen platform';
}
}];
inquirer.prompt(ask).then(function(answers) {
callback(answers);
});
}
else {
ask = [{
type: 'input',
name: 'name',
message: 'What\'s the application\'s name?',
default: cordovaConf.name,
validate: function(input) {
return /^[a-zA-Z][^~!\.\;\\\/\|\"\'@\#$%<>^&*\()\-=+_\’\n\t\s]*$/.test(input) ? true : 'invalid name for tizen platform';
}
}, {
type: 'input',
name: 'packageid',
message: 'Package Id (Valid RegExp: [0-9a-zA-Z]{10})',
default: generateTizenPackageID(),
validate: function(input) {
return /[0-9a-zA-Z]{10}/.test(input) ? true : 'invalid id string for tizen platform';
}
}, {
type: 'input',
name: 'version',
message: 'Application Version (Valid RegExp: /\d./\d./\d)',
default: cordovaConf.version,
validate: function(input) {
return /\d.\d.\d/.test(input) ? true : 'invalid version string for tizen platform';
}
}, {
type: 'input',
name: 'iconpath',
message: 'Icon path (default is \'img/logo.png\')',
default: 'img/logo.png',
validate: function(input) {
return typeof(input) === 'string' ? true : 'invalid iconpath';
}
}, {
type: 'input',
name: 'description',
message: 'Application Description',
default: utils.trim(cordovaConf.description)
}];
inquirer.prompt(ask).then(function(answers) {
callback(answers);
});
}
}
function prepareDir(dir) {
mkdirp.sync(dir);
}
function getManualTizenConfData(platformsData){
var i,
manualTizenConfData = null;
if (platformsData) {
for (i=0; i < platformsData.length; i++) {
if (platformsData[i].$.name === 'sectv-tizen') {
delete platformsData[i].$;
manualTizenConfData = utils.trim(js2xmlparser('platform',platformsData[i],{declaration : {include : false},attributeString : '$'}).replace(/<(\/?platform)>/igm,''));
}
}
}
return manualTizenConfData;
}
module.exports = {
prepare: function(successCallback, errorCallback, platformName, data) {
console.log('\nStart preparing codes for Samsung Tizen Platform......');
// destination folder to paste necessary files for toast project
var dest = data.dest || path.join('platforms', platformName, 'www');
dest = path.resolve(dest);
// target repository
var platformRepos = data.platformRepos || ('../cordova-' + platformName);
platformRepos = path.resolve(platformRepos);
// original source
var wwwSrc = path.resolve('www');
// necessary files for toast project
var scripts = data.scripts;
// get data from cordova config.xml
var cordovaConf = utils.getCordovaConfig();
// get data from userconf.json
var userConfPath = data.userConfPath || path.join('platforms', 'userconf.json');
var userData = getValidTizenConfData(userConfPath);
if(userData) {
// exist userconf for tizen
confirmUseExistingData(userData, function (useExisting) {
if(useExisting) {
// if user select useExisting: Y
askUserData(cordovaConf, function (data) {
userData.version = data.version;
userData.manualConfData = getManualTizenConfData(cordovaConf.platform);
buildProject();
}, true, userData);
}
else {
// if user select useExisting: N
askUserData(cordovaConf, function (data) {
userData = data;
userData.manualConfData = getManualTizenConfData(cordovaConf.platform);
buildProject();
});
}
});
}
else {
// not exist userconf for tizen
askUserData(cordovaConf, function (data) {
userData = data;
userData.manualConfData = getManualTizenConfData(cordovaConf.platform);
buildProject();
});
}
function buildProject() {
copySrcToDest() || (errorCallback && errorCallback());
buildPlatformAdditions() || (errorCallback && errorCallback());
replaceTemplates() || (errorCallback && errorCallback());
console.log('Prepared at ' + dest);
// warning for existing meta tag for csp setting
var targetFile = path.join(wwwSrc, 'index.html');
var target = fs.readFileSync(targetFile, {
encoding: 'utf8'
});
if(target.match(/(<meta.*)(http-equiv)(.*=*.)("Content-Security-Policy"|'Content-Security-Policy')/)) {
console.log('\nWARNING!!!!! REMOVE meta tag for csp setting in the "index.html"');
console.log('It would be caused abnormal operation. It is recommended to add csp setting in config.xml.');
console.log('Please confirm your file : ' + targetFile);
}
saveUserConfFile(userConfPath, userData);
successCallback && successCallback();
}
function copySrcToDest() {
prepareDir(dest);
for (var key in scripts) {
if (scripts.hasOwnProperty(key)) {
var to = path.join(dest, key);
var from = path.resolve(scripts[key]);
shelljs.cp('-f', from, to);
if (!fs.existsSync(from)) {
throw Error('cordova.js, toast.js file are not exist.');
}
}
}
shelljs.cp('-rf', path.join(wwwSrc, '*'), dest);
return true;
}
function buildPlatformAdditions() {
shelljs.cp('-rf', path.join(platformRepos, 'www', '*'), dest);
// hack: copying hidden files(starts with dot) at root('www') to the dest directory.
// if the dest is not exist, we can copy the platform files without this hack by using this command: cp -rf <PLATFORMREPOS>/www <destDir>
// But the dest directory has the application files at this moment. So we need to use this hack.
shelljs.cp('-rf', path.join(platformRepos, 'www', '.*'), dest);
return true;
}
function replaceTemplates() {
var files = fs.readdirSync(dest);
for(var i=0; i<files.length; i++) {
var fileName = files[i];
if(fileName.match(/\.tmpl$/)) {
var tmplFilePath = path.join(dest, fileName);
var destFilePath = path.join(dest, fileName.replace(/\.tmpl$/, ''));
var template = fs.readFileSync(tmplFilePath, {
encoding: 'utf8'
});
var rendered = mustache.render(template, userData);
fs.writeFileSync(destFilePath, rendered, {
encoding: 'utf8'
});
shelljs.rm(path.join(dest, fileName));
}
}
return true;
}
},
build: function(successCallback, errorCallback, data) {
console.log('\nStart packaging Samsung Tizen TV Platform......');
var www = data.www || path.join('platforms', 'sectv-tizen', 'www');
var dest = data.dest || path.join('platforms', 'sectv-tizen', 'build');
var TEMPORARY_BUILD_DIR = '.buildResult';
www = path.resolve(www);
dest = path.resolve(dest);
child.exec('tizen version', function(err, stdout, stderr) {
if(err) {
console.log(stderr);
throw Error('The command \"tizen\" failed. Make sure you have the latest Tizen SDK installed, and the \"tizen\" command (inside the tools/ide/bin folder) is added to your path.');
}
console.log(stdout);
// reference url : https://developer.tizen.org/development/tools/web-tools/command-line-interface#mw_package
var result = shelljs.exec('tizen cli-config "default.profiles.path=' + path.resolve(data.profilePath) + '"');
if(result.code) {
throw Error(result.output);
}
result = shelljs.exec('tizen build-web -out ' + TEMPORARY_BUILD_DIR + ' -- "' + path.resolve(www) + '"');
if(result.code) {
throw Error(result.output);
}
result = shelljs.exec('tizen package --type wgt --sign ' + data.profileName + ' -- ' + path.resolve(path.join(www, TEMPORARY_BUILD_DIR)));
if(result.code) {
throw Error(result.output);
}
else {
var packagePath = result.stdout.match(/Package File Location\:\s*(.*)/);
if(packagePath && packagePath[1]) {
prepareDir(dest);
shelljs.mv('-f', packagePath[1], path.resolve(dest));
shelljs.rm('-rf', path.resolve(path.join(www, TEMPORARY_BUILD_DIR)));
console.log('Package created at ' + path.join(dest, path.basename(packagePath[1])));
}
else {
throw Error('Fail to retrieve Package File Location.');
}
}
});
}
};