/
ubuntu_parser.js
151 lines (122 loc) · 5.16 KB
/
ubuntu_parser.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
/*
*
* Copyright 2013 Canonical 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.
*
*/
var fs = require('fs');
var path = require('path');
var util = require('../util');
var shell = require('shelljs');
var Q = require('q');
var Parser = require('./parser');
var os = require('os');
var ConfigParser = require('cordova-common').ConfigParser;
function ubuntu_parser (project) {
// Call the base class constructor
Parser.call(this, 'ubuntu', project);
this.path = project;
this.config = new ConfigParser(this.config_xml());
this.update_manifest();
}
function sanitize (str) {
return str.replace(/\n/g, ' ').replace(/^\s+|\s+$/g, '');
}
require('util').inherits(ubuntu_parser, Parser);
module.exports = ubuntu_parser;
ubuntu_parser.prototype.update_from_config = function (config) {
if (config instanceof ConfigParser) {
} else {
return Q.reject(new Error('update_from_config requires a ConfigParser object'));
}
this.config = new ConfigParser(this.config_xml());
this.config.setName(config.name());
this.config.setVersion(config.version());
this.config.setPackageName(config.packageName());
this.config.setDescription(config.description());
this.config.write();
return this.update_manifest();
};
ubuntu_parser.prototype.cordovajs_path = function (libDir) {
var jsPath = path.join(libDir, 'www', 'cordova.js');
return path.resolve(jsPath);
};
ubuntu_parser.prototype.cordovajs_src_path = function (libDir) {
var jsPath = path.join(libDir, 'cordova-js-src');
return path.resolve(jsPath);
};
ubuntu_parser.prototype.update_manifest = function () {
var nodearch2debarch = { 'arm': 'armhf',
'ia32': 'i386',
'x64': 'amd64'};
var arch;
if (os.arch() in nodearch2debarch) { arch = nodearch2debarch[os.arch()]; } else { return Q.reject(new Error('unknown cpu arch')); }
if (!this.config.author()) { return Q.reject(new Error('config.xml should contain author')); }
var manifest = { name: this.config.packageName(),
version: this.config.version(),
title: this.config.name(),
hooks: { cordova: { desktop: 'cordova.desktop',
apparmor: 'apparmor.json' } },
framework: 'ubuntu-sdk-13.10',
maintainer: sanitize(this.config.author()) + ' <' + this.config.doc.find('author').attrib.email + '>',
architecture: arch,
description: sanitize(this.config.description()) };
var name = sanitize(this.config.name()); // FIXME: escaping
var content = '[Desktop Entry]\nName=' + name + '\nExec=./cordova-ubuntu www/\nTerminal=false\nType=Application\nX-Ubuntu-Touch=true';
if (this.config.doc.find('icon') && this.config.doc.find('icon').attrib.src) {
var iconPath = path.join(this.path, '../..', this.config.doc.find('icon').attrib.src);
if (fs.existsSync(iconPath)) { content += '\nIcon=' + this.config.doc.find('icon').attrib.src; } else { return Q.reject(new Error('icon does not exist: ' + iconPath)); }
} else {
content += '\nIcon=qmlscene';
console.warn('missing icon element in config.xml');
}
fs.writeFileSync(path.join(this.path, 'manifest.json'), JSON.stringify(manifest));
fs.writeFileSync(path.join(this.path, 'cordova.desktop'), content);
var policy = { policy_groups: ['networking', 'audio'], policy_version: 1 };
this.config.doc.getroot().findall('./feature/param').forEach(function (element) {
if (element.attrib.policy_group && policy.policy_groups.indexOf(element.attrib.policy_group) === -1) { policy.policy_groups.push(element.attrib.policy_group); }
});
fs.writeFileSync(path.join(this.path, 'apparmor.json'), JSON.stringify(policy));
return Q();
};
ubuntu_parser.prototype.config_xml = function () {
return path.join(this.path, 'config.xml');
};
ubuntu_parser.prototype.www_dir = function () {
return path.join(this.path, 'www');
};
ubuntu_parser.prototype.update_www = function () {
var projectRoot = util.isCordova(this.path);
var www = util.projectWww(projectRoot);
shell.rm('-rf', this.www_dir());
shell.cp('-rf', www, this.path);
};
ubuntu_parser.prototype.update_overrides = function () {
var projectRoot = util.isCordova(this.path);
var mergesPath = path.join(util.appDir(projectRoot), 'merges', 'ubuntu');
if (fs.existsSync(mergesPath)) {
var overrides = path.join(mergesPath, '*');
shell.cp('-rf', overrides, this.www_dir());
}
};
// Returns a promise.
ubuntu_parser.prototype.update_project = function (cfg) {
var self = this;
return this.update_from_config(cfg)
.then(function () {
self.update_overrides();
util.deleteSvnFolders(self.www_dir());
});
};