Skip to content

Commit

Permalink
Introduced a Gulpfile for the generation of derived files.
Browse files Browse the repository at this point in the history
SVG/PNG sprites, as well as Iconjar ijmaps can now be generated by
running `gulp`.
  • Loading branch information
Scott Hyndman committed Nov 13, 2015
1 parent abde048 commit acce5bf
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
Empty file modified .gitignore
100644 → 100755
Empty file.
165 changes: 165 additions & 0 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
'use strict';

import _ from 'lodash';
import File from 'vinyl';
import gulp from 'gulp';
import merge from 'merge-stream';
import sprity from 'sprity';
import svgSprite from 'gulp-svg-sprite';
import through2 from 'through2';
import { humanize, titleize } from 'underscore.string';


/** Names of directories containing icons. */
const ICON_CATEGORIES = [
'action',
'alert',
'av',
'communication',
'content',
'editor',
'file',
'hardware',
'image',
'maps',
'navigation',
'notification',
'places',
'social',
'toggle',
];


/** Standard PNG colors. */
const PNG_COLORS = [
'black',
'white',
];


/**
* Generates PNG sprites and their corresponding CSS files for each category of
* icon, and places them in `sprites/css-sprite`.
*
* TODO(shyndman): Add support for double density sprites.
*/
gulp.task('png-sprites', () =>
_(getCategoryColorPairs())
.map(([ category, color ]) =>
sprity.src({
src: `./${ category }/1x_web/*_${ color }_24dp.png`,
style: `sprite-${ category }-${ color }.css`,
name: `sprite-${ category }-${ color }`,
engine: 'sprity-gm',
orientation: 'left-right'
}))
.thru(merge)
.value()
.pipe(gulp.dest('./sprites/css-sprite/')));


/**
* Generates CSS and Symbol-based SVG sprites for each category, and places
* them in `sprites/svg-sprite`.
*/
gulp.task('svg-sprites', () =>
_(ICON_CATEGORIES)
.map((category) =>
gulp.src(`./${ category }/svg/production/*_24px.svg`)
.pipe(svgSprite(getSvgSpriteConfig(category))))
.thru(merge)
.value()
.pipe(gulp.dest('./sprites/svg-sprite')));


/**
* Generates a file to allow the consumption of the icon font by Iconjar
* (http://geticonjar.com/).
*/
gulp.task('iconjar', () =>
gulp.src('./iconfont/codepoints')
.pipe(generateIjmap('MaterialIcons-Regular.ijmap'))
.pipe(gulp.dest('./iconfont/')));


/** Runs all tasks. */
gulp.task('default', ['png-sprites', 'svg-sprites', 'iconjar']);


/**
* Returns a stream that transforms between our icon font's codepoint file
* and an Iconjar ijmap.
*/
function generateIjmap(ijmapPath) {
return through2.obj((codepointsFile, encoding, callback) => {
const ijmap = {
icons: codepointsToIjmap(codepointsFile.contents.toString())
};

callback(null, new File({
path: ijmapPath,
contents: new Buffer(JSON.stringify(ijmap), 'utf8')
}));

function codepointsToIjmap(codepoints) {
return _(codepoints)
.split('\n') // split into lines
.reject(_.isEmpty) // remove empty lines
.reduce((codepointMap, line) => { // build up the codepoint map
let [ name, codepoint ] = line.split(' ');
codepointMap[codepoint] = { name: titleize(humanize(name)) };
return codepointMap;
}, {});
}
});
}


/**
* Returns the SVG sprite configuration for the specified category.
*/
function getSvgSpriteConfig(category) {
return {
shape: {
dimension: {
maxWidth: 24,
maxHeight: 24
},
},
mode: {
css : {
bust: false,
dest: './',
sprite: `./svg-sprite-${ category }.svg`,
example: {
dest: `./svg-sprite-${ category }.html`
},
render: {
css: {
dest: `./svg-sprite-${ category }.css`
}
}
},
symbol : {
bust: false,
dest: './',
sprite: `./svg-sprite-${ category }-symbol.svg`,
example: {
dest: `./svg-sprite-${ category }-symbol.html`
}
}
}
};
}


/**
* Returns the catesian product of categories and colors.
*/
function getCategoryColorPairs() {
return _(ICON_CATEGORIES)
.map((category) =>
_.zip(_.times(PNG_COLORS.length, () => category), PNG_COLORS))
.flatten() // flattens 1 level
.value();
}
38 changes: 0 additions & 38 deletions iconfont/iconjar-map.js

This file was deleted.

15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,18 @@
"bugs": {
"url": "https://github.com/google/material-design-icons/issues"
},
"homepage": "https://github.com/google/material-design-icons"
"homepage": "https://github.com/google/material-design-icons",
"devDependencies": {
"babel-core": "^6.1.2",
"babel-preset-es2015": "^6.1.2",
"gulp": "^3.9.0",
"gulp-if": "^2.0.0",
"gulp-svg-sprite": "^1.2.13",
"lodash": "^3.10.1",
"sprity": "^1.0.7",
"sprity-gm": "^1.0.2",
"through2": "^2.0.0",
"underscore.string": "^3.2.2",
"vinyl": "^1.1.0"
}
}

0 comments on commit acce5bf

Please sign in to comment.