2017-05-23 18:44:31 +02:00
|
|
|
import * as fs from 'fs';
|
2017-05-24 00:46:34 +02:00
|
|
|
import * as gulp from 'gulp';
|
2017-05-23 18:44:31 +02:00
|
|
|
import * as gutil from 'gulp-util';
|
2017-05-24 00:46:34 +02:00
|
|
|
import * as mustache from 'mustache';
|
2017-05-23 18:44:31 +02:00
|
|
|
import * as path from 'path';
|
|
|
|
|
2017-05-24 00:46:34 +02:00
|
|
|
import { HR, MESSAGE_GENERATED, MESSAGE_ICON_ERROR } from './../consts/log';
|
|
|
|
|
2017-06-17 16:52:27 +02:00
|
|
|
import { CHARSET } from "../../extensions/consts/files";
|
2017-06-23 13:55:52 +02:00
|
|
|
import { IGenericObject } from "../../extensions/interfaces/igeneric-object";
|
2017-05-24 00:46:34 +02:00
|
|
|
import { IIcon } from './../interfaces/iicon';
|
2017-06-12 20:11:51 +02:00
|
|
|
import paths from '../../extensions/consts/paths';
|
2017-05-24 00:46:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an object implementing the IIcon interface
|
|
|
|
* @param {string} fileName
|
|
|
|
* @returns {IIcon}
|
|
|
|
*/
|
|
|
|
function iconFactory(fileName: string): IIcon {
|
2017-10-24 00:11:48 +02:00
|
|
|
gutil.log(gutil.colors.gray(`Processing icon ${ fileName }`))
|
2017-05-24 00:46:34 +02:00
|
|
|
let name: string = path.basename(fileName, path.extname(fileName));
|
2017-10-24 00:11:48 +02:00
|
|
|
let filename: string = name;
|
2017-05-24 00:46:34 +02:00
|
|
|
let last: boolean = false;
|
2017-05-23 18:44:31 +02:00
|
|
|
|
2017-10-24 00:11:48 +02:00
|
|
|
// renaming icon for vscode
|
|
|
|
// if the icon filename starts with a folder prefix,
|
|
|
|
// the resulting name will be prefixed only by an underscore,
|
|
|
|
// otherwise the icon will be prefixed by a _file_ prefix
|
|
|
|
if (name.indexOf('folder')) {
|
|
|
|
name = name.indexOf('file') ? `_file_${ name }` : `_${ name }`;
|
|
|
|
} else {
|
|
|
|
name = `_${ name }`;
|
|
|
|
}
|
|
|
|
|
|
|
|
gutil.log(gutil.colors.gray(`VSCode icon name ${ name } with filename ${ filename }`));
|
|
|
|
|
|
|
|
return { filename, name, last } as IIcon;
|
2017-05-24 00:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* > Build Icons
|
|
|
|
* @returns {gulp.Gulp}
|
|
|
|
*/
|
|
|
|
export default gulp.task('build:icons', cb => {
|
|
|
|
let contents: string;
|
|
|
|
let fileNames: string[] = fs.readdirSync(path.join(paths.SRC, `./icons/svgs`));
|
|
|
|
let icons: IIcon[] = fileNames.map(fileName => iconFactory(fileName));
|
|
|
|
let partials: string[] = fs.readdirSync(path.join(paths.SRC, `./icons/partials`));
|
2017-06-23 13:55:52 +02:00
|
|
|
let partialsData: IGenericObject<any> = {};
|
2017-05-24 00:46:34 +02:00
|
|
|
let pathTemp: string = './themes/.material-theme-icons.tmp';
|
2017-05-23 18:44:31 +02:00
|
|
|
|
|
|
|
icons[icons.length - 1].last = true;
|
|
|
|
|
|
|
|
partials.forEach(partial => {
|
2017-05-24 00:46:34 +02:00
|
|
|
partialsData[path.basename(partial, path.extname(partial))] = fs.readFileSync(path.join(paths.SRC, `./icons/partials`, `./${partial}`), CHARSET);
|
2017-05-23 18:44:31 +02:00
|
|
|
});
|
|
|
|
|
2017-05-24 00:46:34 +02:00
|
|
|
contents = mustache.render(
|
|
|
|
fs.readFileSync(path.join(paths.SRC, `./icons/icons-theme.json`), CHARSET)
|
|
|
|
, { icons }
|
|
|
|
, partialsData
|
2017-05-23 18:44:31 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
contents = JSON.stringify(JSON.parse(contents), null, 2);
|
2017-05-24 00:46:34 +02:00
|
|
|
} catch (error) {
|
|
|
|
gutil.log(gutil.colors.red(MESSAGE_ICON_ERROR), error);
|
|
|
|
cb(error);
|
2017-05-23 18:44:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-18 14:27:08 +02:00
|
|
|
fs.writeFileSync(pathTemp, contents, { encoding: CHARSET });
|
2017-05-23 18:44:31 +02:00
|
|
|
|
2017-05-24 00:46:34 +02:00
|
|
|
gutil.log(gutil.colors.gray(HR));
|
|
|
|
gutil.log(MESSAGE_GENERATED, gutil.colors.green(pathTemp));
|
|
|
|
gutil.log(gutil.colors.gray(HR));
|
2017-05-23 18:44:31 +02:00
|
|
|
|
|
|
|
cb();
|
|
|
|
});
|