-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
217 lines (187 loc) · 4.58 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
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
'use strict'
const dir = require('node-dir')
const xml2js = require('xml2js')
const pathUtil = require('path')
const fs = require('fs')
// Will be filled with names and types
// and used to generate public.xml
const publicValues = {}
// Values resource types whose children should be added to public.xml
const recursiveTypes = [
'declare-styleable'
]
// Values resource types that shouldn't be added to public.xml
const dontAdd = [
'declare-styleable'
]
function generatePublicXml (rootDir) {
return new Promise((resolve, reject) => {
dir.subdirs(rootDir, function (err, subdirs) {
if (err) {
reject(err)
return
}
let finished = 0
for (let i = 0; i < subdirs.length; i++) {
processFolder(subdirs[i], function () {
finished++
if (finished === subdirs.length) {
writePublicXml(rootDir).then(resolve)
}
})
}
})
})
}
/**
* Gets the file or dir name from a path
*/
function getLastPathComponent (path) {
const pathComponents = path.split(pathUtil.sep)
return pathComponents[pathComponents.length - 1]
}
function addToPublicObj (type, name) {
if (!publicValues[type]) {
publicValues[type] = []
}
// Check if name was already added
if (publicValues[type].indexOf(name) !== -1) {
return
}
publicValues[type].push(name)
}
/**
* Will process folder
*/
function processFolder (path, callback) {
const dirName = getLastPathComponent(path)
if (dirName.match(/^values/i)) {
processValuesFolder(path, callback)
} else {
processResourceFolder(path, callback)
}
}
/**
* Process values folder containing resource xml files
*/
function processValuesFolder (path, callback) {
dir.readFiles(path,
{
match: /\.xml$/
},
function (err, content, filename, next) {
if (err) throw err
if (getLastPathComponent(filename) === 'public.xml') {
next()
return
}
xml2js.parseString(content, { explicitArray: true }, function (err, result) {
if (err) throw err
if (!result || !result.resources) {
return
}
processResourceXmlObj(result.resources)
})
next()
},
function (err, files) {
if (err) throw err
callback()
})
}
/**
* Uses folder name root as resource type, and
* filenames as names
*/
function processResourceFolder (path, callback) {
const dirName = getLastPathComponent(path)
const resType = dirName.split('-')[0]
dir.files(path, function (err, files) {
if (err) throw err
for (let i = 0; i < files.length; i++) {
if (files[i].match(/\.xml$/i)) {
const name = getLastPathComponent(files[i]).split('.xml')[0]
addToPublicObj(resType, name)
}
}
callback()
})
}
/**
* Parse the resources object created by xml2js
*/
function processResourceXmlObj (resources) {
const resourceTypes = Object.keys(resources)
for (let i = 0; i < resourceTypes.length; i++) {
const type = resourceTypes[i]
if (type === '$') {
continue
}
const resType = resources[type]
processResourceTypeArray(type, resType)
}
}
/**
* Parse resource type array and add to public obj
*/
function processResourceTypeArray (type, resType) {
for (let i = 0; i < resType.length; i++) {
const res = resType[i]
if (!res.$) {
continue
}
if (dontAdd.indexOf(type) < 0) {
addToPublicObj(type, res.$.name)
}
if (recursiveTypes.indexOf(type) >= 0) {
processResourceXmlObj(res)
}
}
}
function formatPublicObject () {
// Reformat public array to match xml2js format
const formattedPublic = {
resources: {
public: []
}
}
const types = Object.keys(publicValues)
types.sort()
types.forEach(type => {
publicValues[type].sort()
publicValues[type].forEach(name => {
formattedPublic.resources.public.push({
$: {
type: type,
name: name
}
})
})
})
return formattedPublic
}
function buildXml (object) {
const builder = new xml2js.Builder()
return builder.buildObject(object)
}
function writePublicXml (rootDir) {
const formatted = formatPublicObject()
const xml = buildXml(formatted)
const outDir = pathUtil.resolve(rootDir, 'values')
const outFile = pathUtil.resolve(outDir, 'public.xml')
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir)
}
return new Promise((resolve, reject) => {
fs.writeFile(outFile, xml, function (err) {
if (err) {
reject(err)
return
}
resolve(outFile)
})
})
}
module.exports = {
generatePublicXml: generatePublicXml
}