-
Notifications
You must be signed in to change notification settings - Fork 7
/
gulpfile.js
499 lines (348 loc) · 11.3 KB
/
gulpfile.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
498
499
/**
* Plane icon theme
* Copyright (C) 2023 wfpaisa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.en.html>.
*
* @author @wfpaisa
* @copyright 2023 Plane icon theme
*/
"use strict";
const version = '4.0';
const variants = [
['Plane','Gnome'],
['Plane','Dark','Gnomedark'],
['Plane','Gnome', 'Arch'],
['Plane','Dark','Gnomedark', 'Archdark'],
['Plane','Gnome', 'Magenta'],
['Plane','Dark','Gnomedark', 'Magentadark'],
['Plane','Gnome', 'Manjaro'],
['Plane','Dark','Gnomedark', 'Manjarodark'],
['Plane','Gnome', 'Ubuntu'],
['Plane','Dark','Gnomedark', 'Ubuntudark'],
['Plane','Gnome', 'Neon'],
['Plane','Dark','Gnomedark', 'Neondark'],
// ['Plane','Kde'],
// ['Plane','Dark','Kdedark']
];
var fs = require('fs'),
path = require('path'),
gulp = require('gulp'),
through2 = require('through2'),
File = require('vinyl'),
del = require('del'),
xml2js = require('xml2js'),
svgmin = require('gulp-svgmin'),
exec = require('child_process').exec,
watch = require('gulp-watch'),
foreach = require("gulp-foreach"),
zip = require("gulp-zip");
/**
* Scan all groups from svg file and return a array of icons
*
* @param {stream} file The file
* @param {Function} cb { err,[] } return a array of icons
*/
function renderIcons(file, cb) {
try {
let xml = file.contents.toString('utf8');
// Are there icon inside file?
var findIcon = false;
xml2js.parseString(xml, (err, xmlObj) => {
if (err) return cb(err);
// let layers = result.svg;
// Are there layers?
if (!xmlObj.svg.g) return cb(null, []);
let allIcons = [];
// Get layers
xmlObj.svg.g.forEach(layer => {
// Are there groups?
if (!layer.g) return;
// Check if is visible
if (layer.$.style && (layer.$.style === 'display:none')) return;
/*==================================================
= 1. Separe icon (inmutable) =
==================================================*/
let icon = JSON.parse(JSON.stringify(xmlObj)),
builder = new xml2js.Builder();
// Replace all groups for current group
icon.svg.g = [layer];
/*=============================================
= 2. Name and Size the icon =
*
* Set new size from layer name_size
=============================================*/
let haveName = false;
layer.$["inkscape:label"].replace(/(.{2,})_([0-9]{1,}|scalable)/, (match, $1, $2) => {
icon.svg.$.folder = $2;
if ($1 == 'icon') {
icon.svg.$['sodipodi:docname'] = path.basename(file.path);
} else {
icon.svg.$['sodipodi:docname'] = `${$1}.svg`;
}
// Find a name
haveName = true;
});
if(!haveName) return console.log("\x1b[31m", `Layer(${layer.$['inkscape:label']}) The name must be: "name_size", example: icon_24" or "atom_24".`);
/*===================================================================================
= 3. Move the layer to x=0 y=0 and check if have layer with name=frame =
====================================================================================*/
let haveFrame = false;
let rectx = 0;
let recty = 0;
// Check if there are a group with label iframe and if this have a rectangle
layer.g.forEach(frame => {
if ((frame.$['inkscape:label'] === 'frame') && frame.rect) {
// File sizes
icon.svg.$.width = frame.rect[0].$.width;
icon.svg.$.height = frame.rect[0].$.height;
icon.svg.$.viewBox = `0 0 ${frame.rect[0].$.width} ${frame.rect[0].$.height}`;
// Translate this elemento to position 0x 0y
let gx = -1 * frame.rect[0].$.x;
let gy = -1 * frame.rect[0].$.y;
icon.svg.g[0].$.transform = `translate(${gx},${gy})`;
haveFrame = true;
}
})
if (!haveFrame) return console.log("\x1b[31m", `Layer(${layer.$['inkscape:label']}) Has to have a layer with the name "frame" and inside this a rectangle with the measures of the icon.`);
/*========================================
= 4. JSON -> xml(svg) =
========================================*/
let strToXml = builder.buildObject(icon);
let fileDir = path.dirname(file.path);
// Complete path of icon size/name_id.svg
let filePath = `${fileDir}/${icon.svg.$.folder}/${icon.svg.$['sodipodi:docname']}`;
/*==========================================================
= 5. Create stream icon and push icon to gulp stream =
==========================================================*/
let newIcon = new File({
base: file.base,
path: filePath,
contents: new Buffer(strToXml)
});
allIcons.push(newIcon)
});
// Test, no render
// return cb(null, [])
cb(null, allIcons);
})
} catch (err) {
console.error("\x1b[31m", 'Error in: ' + file.path);
return cb(err, [])
}
}
/*================================================
= Prepare for export icons =
================================================*/
/**
* Prepare for export icons
*
* @return {stream} return a gulp stream
*/
function svg_icons_export() {
return through2.obj(function(file, enc, next) {
// Only svg files
if (path.extname(file.path) != '.svg') return next(null, file);
if(process.argv[process.argv.length - 1] == '-debug'){
console.log("\x1b[32m", `├─${file.path}`)
}
// If icon is in size-folder or scalable folder return
if (file.path.match('\/scalable\/.*\.svg|\/[0-9]{2,}\/.*\.svg')) return next(null, file)
// Render Icons
renderIcons(file, (err, icons) => {
if (err) return next(err);
if (!icons[0]) return next(null, file)
// through2.this.push new icon
icons.forEach(icon => {
if(process.argv[process.argv.length - 1] == '-debug'){
console.log("\x1b[32m", `│ ├─>${icon.path}`)
}else{
process.stdout.write(".");
}
this.push(icon);
})
if(process.argv[process.argv.length - 1] == '-debug'){
console.log(' │')
}
next();
})
});
}
/*==========================================
= Watch files =
==========================================*/
gulp.task('watch', function(cb) {
// for compare the date of modified of the file
var rere = '';
return watch('./src/variants/**/*', function(file) {
// console.log("\x1b[0m", file.path);
// when delete file
if (!fs.existsSync(file.path)) return;
let iconSet = 'PlaneGnome';
if(process.argv[process.argv.length - 1]){
iconSet = process.argv[process.argv.length - 1].replace('-', '');
}
var dir = path.dirname(file.path);
var dirTo = dir.replace(/src\/variants\/(.*?)\//, `build/variants/${iconSet}/`);
// Time for write big files
// Avoid the error: no writecb in Transform class
setTimeout(function(){
// Modified date
var fStatMtime = String(fs.statSync(file.path).mtime) + file.path;
// Sometimes the same file is passed twice, by means of the modification date control that will be rendered only once
if(rere !== fStatMtime){
rere = fStatMtime;
// console.log("\x1b[0m");
gulp.src(file.path)
.pipe(svg_icons_export())
// .pipe(svgmin({
// js2svg: {
// pretty: true
// }
// }))
.pipe(gulp.dest(dirTo))
.on('end', (cb) => {
exec(`gtk-update-icon-cache -f -t ~/.local/share/icons/${iconSet}`, () => {
console.log("\x1b[32m", `✔ Update ${iconSet} set icon`);
})
});
}// if
}, 2000)//settimeout
});
});
/*==============================
= Render =
==============================*/
gulp.task('default', (cb)=>{
// Clean folders
new Promise( (resolve, reject) =>{
del(['./.tmp', './build']).then(paths => {
console.log("\x1b[32m", "✔ Clean");
resolve()
});
})
// Prepare temp files
.then( (ret) => {
return new Promise( (resolve, reject) => {
gulp.src([
'./src/**/*.*',
'!./src/variants/**/*.svg'
])
.pipe(gulp.dest(__dirname + '/.tmp'))
.on('end', (cb) => {
console.log("\x1b[32m", "✔ Prepare temp files");
resolve()
})
});
})
// Render icons
.then( (ret) => {
console.log("\x1b[33m", "» Render icons (This may take some time)");
return new Promise( (resolve, reject) => {
gulp.src('./src/variants/**/*.svg')
.pipe(svg_icons_export())
// Opcional, minimize
.pipe(svgmin())
.pipe(gulp.dest(__dirname + '/.tmp/variants/'))
.on('end', (cb) =>{
console.log("\x1b[32m", "✔ Render icons");
resolve();
});
});
})
// Make set icons
.then( (ret) => {
return new Promise( (resolve, reject) => {
// 3 Synchronous, copy every folder in each array of variants
function copyFolder(folderFrom, folderTo){
return new Promise( (resolve) => {
gulp.src(`./.tmp/variants/${folderFrom}/**/*`)
.pipe(gulp.dest(__dirname + '/build/variants/' + folderTo))
.on('end', (cb) =>{
resolve();
});
});
}
// 2 Make a mix of foldes from each item of variants
async function mixFolder(folderArray){
var folderTo = '';
folderArray.map( (f) => { folderTo += f })
for (const folderFrom of folderArray){
await copyFolder(folderFrom, folderTo)
}
console.log("\x1b[32m", `✔ Build ${folderTo} icon set`);
}
// 1 Go through each variants
async function arrayVariants(variants){
for(const arrayFolder of variants){
await mixFolder(arrayFolder);
}
resolve();
}
arrayVariants(variants)
});
})
// Copy others files
.then( (ret) => {
return new Promise( (resolve, reject) => {
gulp.src([
'./.tmp/**/*',
'!./.tmp/variants/',
'!./.tmp/variants/**/*',
])
.pipe(gulp.dest(__dirname + '/build/'))
.on('end', (cb) =>{
console.log("\x1b[32m", "✔ Copy others files");
resolve();
});
});
})
// Zip files
.then( (ret) => {
return new Promise( (resolve, reject) => {
gulp.src("./build/variants/*")
.pipe( foreach( function(stream, file){
var fileName = file.path.substr(file.path.lastIndexOf("/")+1);
// console.log(` - ${fileName}`);
return gulp.src(`./build/variants/${fileName}/**/*`)
.pipe(zip(`${fileName}-${version}.zip`))
}))
.pipe(gulp.dest('./build/zip-variants'))
.on('end', (cb)=>{
console.log("\x1b[32m", "✔ Zip icon set");
resolve();
})
})
})
// fin
.then( (msgEnd) => {
console.log("\x1b[32m", `✔ Ready`);
cb()
}).
// Erros
catch( (err) =>{
if(err) return console.log('Error: ' + err);
});
});
/**
TODO:
Temporales:
Build
1. pasar a build todo
1. Copiar todos los archivos que no esten en Variantes
2. Combinar todas las variantes segun el array
- Minify and clear files too with https://github.com/scour-project/scour
- Multiple folder colors
- Compress each set-icons
*/