-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
291 lines (242 loc) · 7.72 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
/**
* Generates all SVG and PNG logo variations from a single SVG template.
*
* The generated files are stored at 'assets' (or whatever value of '*_dist' variables) folder.
* There are 4 types of output that can be used (copied to desired location):
*
* (1) SVG files located at 'assets/svg' and generated directly from 'src/template.svg'
* (2) PNG files located at 'assets/png' and generated from all the SVG files from previous step
* (3) ICO files located at 'assets/ico' and generated from 'assets/png/[email protected]'
* (4) SVG SPRITE files located at 'assets/sprite' in two format, symbol and css,
*
* @link http://pyrocms.com/
* @author PyroCMS, Inc. <[email protected]>
* @author Adnan M.Sagar, PhD. <[email protected]>
*/
var gulp = require("gulp");
var template = require("gulp-nunjucks-render");
var rename = require("gulp-rename");
var minify = require("gulp-svgmin");
var sprite = require("gulp-svg-sprite");
var svg2png = require("gulp-svg2png");
var favicons = require("gulp-favicons");
var svg_template = 'src/template.svg';
var index_template = 'src/template.html';
var index_dist = 'assets/';
var png_dist = 'assets/png/';
var svg_dist = 'assets/svg/';
var ico_dist = 'assets/ico/';
var sprite_dist = 'assets/sprite/';
var white_fill = '#ffffff';
var brand_fill = '#61269E';
/* Preview index.html */
var html_data = {svg:[],png:[],gh_pages: 'https://raw.githubusercontent.com/pyrocms/branding/gh-pages'};
/*
List of all required PNG files and sizes for both, logo and logo+text
key : file name
value : 'svg' and image configs
*/
var logo_variants = {
'logo-inverted' : {
svg : {
symbol_fill: white_fill,
background_fill: brand_fill,
scale: 0.1
},
img : {
height : [512,128,32,16]
}
},
'logo' : {
svg : {
symbol_fill: brand_fill,
scale: 0.1
},
img : {
height : [512,128,32,16]
}
},
'logo-full' : {
svg : {
symbol_fill: brand_fill
},
img : {
height :[512,128,32,16]
}
},
'logo-text' : {
svg : {
show_text: true,
symbol_fill: brand_fill,
text_fill: brand_fill,
scale: 0.5
},
img : {
height :[512,128,32,16]
}
},
'logo-text-full' : {
svg : {
show_text: true,
symbol_fill: brand_fill,
text_fill: brand_fill,
},
img : {
height :[512,128,32,16]
}
},
'logo-text-inverted' : {
svg : {
show_text: true,
symbol_fill: white_fill,
text_fill: white_fill,
background_fill: brand_fill,
scale: 0.5
},
img : {
height :[512,128,32,16]
}
}
};
/*
Info used to generate ICO files
*/
var fav_icon = {
appName: "PyroCMS",
appDescription: "Built for everyone",
developerName: "Ryan Thompson",
developerURL: "https://github.com/RyanThompson",
background: "#fff",
path: ico_dist,
url: "http://pyrocms.com/",
display: "standalone",
orientation: "portrait",
version: 1.0,
logging: false,
online: false,
replace: true
};
/*
Config params for the SVG Sprite Generator
*/
var svg_sprite_config = {
mode : {
view : {
bust : false,
render : {
css : true
}
},
symbol : true
}
};
/**
* Generate SVG files from the logo template
*
* @param {filename} string, the output filename
* @param {data} array, the template data
* @return {stream}.
*/
function svg(filename, data)
{
data = JSON.parse(JSON.stringify(data)); /* clone */
/* Oddly behaving scale factor, range 1 (full size) to 0.1, smallest )*/
var scale = data['scale'] || 1;
data['text_fill'] = data['text_fill'] || '';
data['width'] = data['width'] || (data['show_text'] ? 1405 : 512);
data['height'] = data['height'] || 512;
data['show_text'] = data['show_text'] || false;
data['background_fill'] = data['background_fill'] || 'none';
data['transform_symbol'] = data['show_text'] ? 'translate(950)' : 'translate(0)'; ;
data['viewbox_x'] = data['viewbox_x'] || 0;
data['viewbox_y'] = data['viewbox_y'] || 0;
data['viewbox_width'] = data['viewbox_width'] || data['width'];
data['viewbox_height'] = data['viewbox_height'] || data['height'];
/* Apply the scale via the document viewbox */
data['viewbox_x'] = data['viewbox_x'] - (1-scale) * data['width'] / 2;
data['viewbox_y'] = data['viewbox_y'] - (1-scale) * data['height'] / 2;
data['viewbox_width'] = data['width'] + data['width'] * (1-scale);
data['viewbox_height'] = data['height'] + data['height'] * (1-scale);
/* Append file */ html_data.svg.push(filename);
return gulp.src(svg_template)
.pipe(template({ext:'.svg', data: data}))
.pipe(minify())
.pipe(rename(filename + '.svg'))
.pipe(gulp.dest(svg_dist));
}
/**
* Generate PNG file from an svg image
*
* @param {filename} string, the svg filename without extension
* @param {height} int, the height in pixels
* @return {void}.
*/
function png(filename, height)
{
var scale = height / 512;
/* Append file */ html_data.png.push({name: filename + '_'+ height , height: height });
/* Generate normal image */
gulp.src(svg_dist+filename + '.svg')
.pipe(svg2png([scale]))
.pipe(rename(filename + '_'+ height + '.png'))
.pipe(gulp.dest(png_dist));
/* Append file */ html_data.png.push({name: filename + '_'+ height +'@2x', height: height });
/* Generate retina image */
return gulp.src(svg_dist+filename + '.svg')
.pipe(svg2png([scale * 2]))
.pipe(rename(filename + '_' + height +'@2x.png'))
.pipe(gulp.dest(png_dist));
};
// ---------------------------------------------------
// TASK : default - Generate index.html
// ---------------------------------------------------
gulp.task('default', ['process-sprites'], function() {
return gulp.src(index_template)
.pipe(template({ext:'.html', data: html_data}))
.pipe(rename('index.html'))
.pipe(gulp.dest(index_dist));
});
// ---------------------------------------------------
// TASK : process SVG sprites
// ---------------------------------------------------
gulp.task('process-sprites', ['process-favicons'], function() {
return gulp
.src(svg_dist+'*.svg')
.pipe(sprite(svg_sprite_config ))
.pipe(gulp.dest(sprite_dist));
});
// ---------------------------------------------------
// TASK : process-favicons
// ---------------------------------------------------
gulp.task('process-favicons', ['process-png'], function() {
return gulp
.src(png_dist+'[email protected]') /* or [email protected] */
.pipe(favicons(fav_icon))
.on('error', function(){})
.pipe(gulp.dest(ico_dist));
});
// ---------------------------------------------------
// TASK : process-png
// ---------------------------------------------------
gulp.task('process-png', ['process-svg'], function() {
var stream = gulp;
/* Generate all PNG files */
for (var filename in logo_variants) {
var img = logo_variants[filename].img;
/* Generate all icons */
for (var i in img.height) {
stream = png(filename, img.height[i]);
}
}
return stream;
});
// ---------------------------------------------------
// TASK : process-svg
// ---------------------------------------------------
gulp.task('process-svg', function() {
var stream = gulp;
for (var filename in logo_variants) {
stream = svg(filename , logo_variants[filename].svg);
}
return stream;
});