-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsectv-orsay.js
497 lines (440 loc) · 17 KB
/
sectv-orsay.js
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* 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 grunt = require('grunt');
var zipdir = require('zip-dir');
var js2xmlparser = require('js2xmlparser');
var revLen = 3;
function saveUserConfFile(configPath, orsayConf) {
var userConfData = {};
if (fs.existsSync(configPath)) {
userConfData = JSON.parse(fs.readFileSync(configPath));
}
userConfData.orsay = orsayConf;
fs.writeFileSync(configPath, JSON.stringify(userConfData, null, 2), {
encoding: 'utf8'
});
}
function getNextVersion(curver){
var tmp = curver.split('.');
var nextVersion = '';
if(tmp.length == 2){
// orsay version
var major = tmp[0];
var minor = tmp[1];
var minorLen = minor.length;
var minorInt = parseInt(minor) + 1;
var minorStr = minorInt.toString();
var minorStrLen = minorStr.length;
if(minorLen < minorStrLen){
var majorInt = parseInt(major) + 1;
var majorStr = majorInt.toString();
return majorStr + '.' + '00000';
}
var makeZero = minorLen - minorStrLen;
var zeroStr = '';
while(makeZero--){
zeroStr += '0';
}
nextVersion = zeroStr + minorStr;
return major + '.' + nextVersion;
}else if(tmp.length == 3){
//semantic version
var majorsem = tmp[0];
var minorsem = tmp[1];
var revision = tmp[2];
var revisionInt = parseInt(revision) + 1;
var revisionStr = revisionInt.toString();
var revisionStrLen = revisionStr.length;
if(revision.length < revisionStrLen){
var minorsemInt = parseInt(minorsem) + 1;
var minorsemStr = minorsemInt.toString();
return majorsem + '.' + minorsemStr + '.' + '0';
}
var beforeZero = revision.length - revisionStrLen;
var zerosemStr = '';
while(beforeZero--){
zerosemStr += '0';
}
nextVersion = zerosemStr + revisionStr;
return semVer2OrsayVer(majorsem + '.' + minorsem + '.' + nextVersion);
}
}
function getValidOrsayConfData(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('orsay'))) {
// userconf.json doesn't have orsay data
return null;
}
var orsayData = userConfData.orsay;
if (typeof orsayData.name !== 'string' || orsayData.name.length <= 0) {
// name is invalid
return null;
}
if (typeof orsayData.thumbicon !== 'string' || orsayData.thumbicon.length <= 0) {
// thumbicon is invalid
return null;
}
if (typeof orsayData.bigthumbicon !== 'string' || orsayData.bigthumbicon.length <= 0) {
// bigthumbicon is invalid
return null;
}
if (typeof orsayData.listicon !== 'string' || orsayData.listicon.length <= 0) {
// listicon is invalid
return null;
}
if (typeof orsayData.biglisticon !== 'string' || orsayData.biglisticon.length <= 0) {
// biglisticon is invalid
return null;
}
if (!validateOrsayVersion(orsayData.version)) {
// version is invalid
return null;
}
// description is not mendatory
return orsayData;
}
function validateOrsayVersion(version) {
var tmp = version.split('.'),
i;
if (tmp.length !== 2) {
return false;
}
for (i = 0; i < tmp.length; i++) {
if (isNaN(tmp[i])) {
return false;
}
}
return i === tmp.length;
}
function semVer2OrsayVer(semver) {
var LEN_MINOR = 2;
var LEN_REV = 3;
var tmp = semver.split('.');
if(tmp.length < 3){
return semver;
}
var major = tmp[0];
var minor = tmp[1];
var rev = tmp[2];
minor = '000000' + tmp[1];
rev = '000000' + tmp[2];
minor = tmp[1].length > LEN_MINOR ? tmp[1] : minor.substr(Math.max(minor.length-LEN_MINOR,LEN_MINOR));
rev = tmp[2].length > LEN_REV ? tmp[2] : rev.substr(Math.max(rev.length-LEN_REV,LEN_REV));
revLen = rev.length;
return major + '.' + minor + rev;
}
function confirmUseExistingData(userData, callback) {
console.log('');
console.log(' > [ Stored Information ]');
console.log(' > name : ' + userData.name);
console.log(' > resolution : ' + userData.resolution);
console.log(' > category : ' + userData.category);
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 = getNextVersion(userData.version);
ask = [{
type: 'input',
name: 'version',
message: 'current version is '+curVer+ '. Application version: ',
default: updateVer,
validate: function(input) {
return /^[0-9]+\.[0-9]+$/.test(input) ? true : 'invalid version string for orsay 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 orsay platform';
}
}, {
type: 'list',
name: 'resolution',
message: 'Which resolution does your application developed for?',
default: '960x540',
choices: [
'960x540',
'1280x720',
'1920x1080'
]
}, {
type: 'list',
name: 'category',
message: 'What\'s the application\'s category?',
choices: [
'VOD', 'sports', 'game', 'lifestyle', 'information', 'education'
]
}, {
type: 'input',
name: 'version',
message: 'Application Version(Valid RegExp: ^[0-9]+\.[0-9]+$)',
default: semVer2OrsayVer(cordovaConf.version),
validate: function(input) {
return /^[0-9]+\.[0-9]+$/.test(input) ? true : 'invalid version string for orsay platform';
}
}, {
type: 'input',
name: 'thumbicon',
message: 'ThumbIcon path (default is \'img/logo.png\')',
default: 'img/logo.png',
validate: function(input) {
return typeof(input) === 'string' ? true : 'invalid iconpath';
}
}, {
type: 'input',
name: 'bigthumbicon',
message: 'BigThumbIcon path (default is \'img/logo.png\')',
default: 'img/logo.png',
validate: function(input) {
return typeof(input) === 'string' ? true : 'invalid iconpath';
}
}, {
type: 'input',
name: 'listicon',
message: 'ListIcon path (default is \'img/logo.png\')',
default: 'img/logo.png',
validate: function(input) {
return typeof(input) === 'string' ? true : 'invalid iconpath';
}
}, {
type: 'input',
name: 'biglisticon',
message: 'BigListIcon 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)
}, {
type: 'input',
name: 'authorName',
message: 'Author\'s name',
default: cordovaConf.authorName
}, {
type: 'input',
name: 'authorEmail',
message: 'Author\'s email',
default: cordovaConf.authorEmail
}, {
type: 'input',
name: 'authorHref',
message: 'Author\'s IRI(href)',
default: cordovaConf.authorHref
}];
inquirer.prompt(ask).then(function(answers) {
var config = answers;
var tmp = config.resolution.split('x');
config.resWidth = parseInt(tmp[0], 10);
config.resHeight = parseInt(tmp[1], 10);
answers = config;
callback(answers);
});
}
}
function prepareDir(dir) {
mkdirp.sync(dir);
}
function getManualOrsayConfData(platformsData){
var i,
manualOrsayConfData = null;
if (platformsData) {
for (i=0; i < platformsData.length; i++) {
if (platformsData[i].$.name === 'sectv-orsay') {
delete platformsData[i].$;
manualOrsayConfData = utils.trim(js2xmlparser('platform',platformsData[i],{declaration : {include : false},attributeString : '$'}).replace(/<(\/?platform)>/igm,''));
}
}
}
return manualOrsayConfData;
}
module.exports = {
prepare: function (successCallback, errorCallback, platformName, data) {
console.log('\nStart preparing codes for Legacy Samsung Smart TV 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 = path.join('platforms', 'userconf.json');
var userData = getValidOrsayConfData(userConfPath);
if(userData) {
// exist userconf for orsay
confirmUseExistingData(userData, function (useExisting) {
if(useExisting) {
// if user select useExisting: Y
askUserData(cordovaConf, function (data) {
userData.version = data.version;
userData.manualConfData = getManualOrsayConfData(cordovaConf.platform);
buildProject();
}, true, userData);
}
else {
// if user select useExisting: N
askUserData(cordovaConf, function (data) {
userData = data;
userData.manualConfData = getManualOrsayConfData(cordovaConf.platform);
buildProject();
});
}
});
}
else {
// not exist userconf for orsay
askUserData(cordovaConf, function (data) {
userData = data;
userData.manualConfData = getManualOrsayConfData(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 is not supported in this platform. It would be caused abnormal operation.');
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);
if(cordovaConf.contentSrc !== 'index.html') {
if(fs.existsSync(path.join(dest, 'index.html'))) {
grunt.log.error('Initial content, which is pointed by \'content\' tag in the \'config.xml\'), is not \'index.html\', but another \'index.html\' is already exist in the source!');
return false;
}
shelljs.mv(path.join(dest, cordovaConf.contentSrc), path.join(dest, 'index.html'));
}
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 Legacy Samsung Smart TV Platform......');
var www = data.www || path.join('platforms', 'sectv-orsay', 'www');
var dest = data.dest || path.join('platforms', 'sectv-orsay', 'build');
www = path.resolve(www);
dest = path.resolve(dest);
var userConfPath = path.join('platforms', 'userconf.json');
var projectName = 'package';
if(fs.existsSync(userConfPath)){
var userData = JSON.parse(fs.readFileSync(userConfPath));
if(userData.hasOwnProperty('orsay')){
projectName = userData.orsay.name;
}
}
else {
grunt.log.error('Prepare the project first.');
}
fs.mkdir(dest, function(){
zipdir(www, {saveTo: path.join(dest, projectName + '.zip')}, function(){
console.log('Package created at ' + path.join(dest, projectName));
successCallback && successCallback();
});
});
}
};