-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (93 loc) · 3.52 KB
/
index.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
var fs = require('fs');
var path = require('path');
var XML = require('xmlobject');
var parseVersion = require('parse-version');
var androidns = "http://schemas.android.com/apk/res/android";
class Manifest{
constructor(){
this._xml = null;
this._dir = ".";
}
get isXamarinProject(){
var dir = path.join(this._dir, "..");
var files = fs.readdirSync(dir);
var csproj = files.find(function(f) { return path.extname(f) === ".csproj"; });
return csproj;
}
get resourceFolder(){
if(this.isXamarinProject)
return path.join(this._dir, "..", "Resources");
return path.join(this._dir, "res");
}
/**
* Find all resource files from "@mimap/name" or "@drawable/name" format.$
* @param {*} res The resource link
*/
resolveResources(res){
if(res && res.startsWith("@")) {
var parts = res.substring(1).split("/");
var subfoldername = parts[0];
var resname = parts[1];
var resfolder = this.resourceFolder;
var ressubfolders = fs.readdirSync(resfolder);
var typesubfolders = ressubfolders.filter(f => path.basename(f).startsWith(subfoldername))
.map(d => path.join(resfolder, d));
var result = [];
typesubfolders.forEach(folder => {
var files = fs.readdirSync(folder);
var file = files.find(f => path.basename(f).slice(0, - path.extname(f).length) == resname)
if(file && path.extname(file) === ".png") result.push(path.join(folder, file));
}, this);
return result;
}
return res;
}
// #region Properties
get version(){
var v = this._xml.getAttribute(androidns,"versionName");
v += "." + this._xml.getAttribute(androidns,"versionCode");
return parseVersion(v);
}
set version(v){
var version = parseVersion(v);
this._xml.setAttribute(androidns,"versionName", version.major + "." + version.minor + "." + version.patch);
this._xml.setAttribute(androidns,"versionCode", version.build + "");
}
get bundleIdentifier() { return this._xml.getAttribute("package"); }
set bundleIdentifier(v) { this._xml.setAttribute("package", v); }
get displayName() { return this._xml.firstChild("application").getAttribute(androidns,"label"); }
set displayName(v) { this._xml.firstChild("application").setAttribute(androidns, "label", v); }
get icons(){
var icon = this._xml.firstChild("application").getAttribute(androidns,"icon");
return this.resolveResources(icon);
}
// # region Loading
load(args, cb){
var manifest = "AndroidManifest.xml";
if(args.file){
manifest = args.file;
this._dir = path.dirname(manifest);
}
var manifestContent = args.content || fs.readFileSync(manifest, 'utf8');
var _this = this;
XML.deserialize(manifestContent, function(err, plist) {
if (err) return cb(err);
_this._xml = plist;
cb(null,_this);
});
}
save(args, cb){
var output = args.file;
var _this = this;
this._xml.serialize(function(err,xml){
if (err) return cb(err);
try {
fs.writeFileSync(output, xml, 'utf8');
cb(null,_this);
} catch (error) {
cb(err);
}
})
}
}
module.exports = Manifest;