2017-06-12 19:49:50 +02:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as gulp from 'gulp';
|
|
|
|
import * as gutil from 'gulp-util';
|
|
|
|
import * as path from 'path';
|
|
|
|
|
|
|
|
import { MESSAGE_GENERATED, MESSAGE_ICON_ACCENTS_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 { IDefaults } from "../../extensions/interfaces/idefaults";
|
2017-06-12 19:49:50 +02:00
|
|
|
import { IThemeIconsAccents } from "../interfaces/itheme-icons-accents";
|
2017-06-12 20:11:51 +02:00
|
|
|
import PATHS from '../../extensions/consts/paths'
|
2017-10-24 00:11:48 +02:00
|
|
|
import { IThemeIconsItem } from '../interfaces/itheme-icons-item';
|
|
|
|
import { getAccentableIcons } from '../../extensions/helpers/fs';
|
2017-06-17 16:52:27 +02:00
|
|
|
|
2017-06-12 19:49:50 +02:00
|
|
|
const BASE_ICON_THEME_PATH: string = path.join(process.cwd(), PATHS.THEMES, './Material-Theme-Icons.json');
|
2017-06-23 13:55:52 +02:00
|
|
|
const DEFAULTS: IDefaults = require('../../extensions/defaults.json');
|
2017-06-12 19:49:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalizes icon path
|
|
|
|
* @param {string} iconPath
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function normalizeIconPath(iconPath: string): string {
|
|
|
|
return path.join(process.cwd(), PATHS.ICONS, iconPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces a file name with the accented filename
|
|
|
|
* @param {string} name
|
|
|
|
* @param {string} accentName
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function replaceNameWithAccent(name: string, accentName: string): string {
|
|
|
|
return name.replace('.svg', `.accent.${ accentName }.svg`);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces a SVG colour
|
|
|
|
*
|
|
|
|
* @param {string} filecontent
|
|
|
|
* @param {string} colour
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-06-16 20:44:10 +02:00
|
|
|
export function replaceSVGColour(filecontent: string, colour: string): string {
|
2017-11-01 14:33:47 +01:00
|
|
|
return filecontent.replace(new RegExp('.st0\{fill:\s{0,}#([a-zA-Z0-9]{6})|path fill="#([a-zA-Z0-9]{6})"'), ($0, $1, $2) => {
|
2017-06-12 23:16:17 +02:00
|
|
|
|
|
|
|
colour = colour.replace('#', '');
|
|
|
|
|
|
|
|
if (!$2) {
|
2017-11-01 14:33:47 +01:00
|
|
|
console.log(`Replacing colour ${ $1 } with ${ colour }`)
|
|
|
|
|
2017-06-12 23:16:17 +02:00
|
|
|
return $0.replace($1, colour);
|
|
|
|
} else {
|
2017-11-01 14:33:47 +01:00
|
|
|
console.log(`Replacing colour ${ $2 } with ${ colour }`)
|
|
|
|
|
2017-06-12 23:16:17 +02:00
|
|
|
return $0.replace($2, colour);
|
|
|
|
}
|
|
|
|
});
|
2017-06-12 19:49:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces white spaces in accents' names
|
|
|
|
* @param {string} input
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function replaceWhiteSpaces(input: string): string {
|
|
|
|
return input.replace(/\s+/g, '-');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a new svg file
|
|
|
|
* @param {string} fromFile
|
|
|
|
* @param {string} toFile
|
|
|
|
* @param {string} accentColour
|
|
|
|
*/
|
|
|
|
function writeSVGIcon(fromFile: string, toFile: string, accent: string): void {
|
|
|
|
let fileContent: string = fs.readFileSync(normalizeIconPath(fromFile), CHARSET);
|
2017-06-23 13:55:52 +02:00
|
|
|
let content: string = replaceSVGColour(fileContent, DEFAULTS.accents[accent]);
|
2017-06-12 19:49:50 +02:00
|
|
|
toFile = normalizeIconPath(toFile);
|
|
|
|
|
2017-11-01 14:15:58 +01:00
|
|
|
gutil.log(gutil.colors.gray(`Accented icon ${toFile} created with colour ${ accent } (${ DEFAULTS.accents[accent] })`));
|
|
|
|
|
2017-06-12 19:49:50 +02:00
|
|
|
fs.writeFileSync(toFile, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exports task to index.ts
|
|
|
|
export default gulp.task('build:icons.accents', cb => {
|
|
|
|
let basetheme: IThemeIconsAccents;
|
|
|
|
|
|
|
|
try {
|
|
|
|
basetheme = require(BASE_ICON_THEME_PATH);
|
|
|
|
|
2017-06-23 13:55:52 +02:00
|
|
|
Object.keys(DEFAULTS.accents).forEach(key => {
|
2017-06-12 19:49:50 +02:00
|
|
|
let iconName = replaceWhiteSpaces(key);
|
|
|
|
let themecopy: IThemeIconsAccents = JSON.parse(JSON.stringify(basetheme));
|
2017-06-12 23:16:17 +02:00
|
|
|
let themePath: string = path.join(PATHS.THEMES, `./Material-Theme-Icons-${ key }.json`);
|
2017-06-12 20:11:51 +02:00
|
|
|
|
2017-10-24 00:11:48 +02:00
|
|
|
getAccentableIcons().forEach(accentableIconName => {
|
|
|
|
gutil.log(gutil.colors.gray(`Preparing ${ accentableIconName } accented icon`));
|
|
|
|
|
|
|
|
let iconOriginDefinition: IThemeIconsItem = (basetheme.iconDefinitions as any)[accentableIconName];
|
|
|
|
let iconCopyDefinition: IThemeIconsItem = (themecopy.iconDefinitions as any)[accentableIconName];
|
|
|
|
|
|
|
|
if (iconOriginDefinition !== undefined && typeof iconOriginDefinition.iconPath === 'string' && iconCopyDefinition !== undefined && typeof iconCopyDefinition.iconPath === 'string') {
|
|
|
|
iconCopyDefinition.iconPath = replaceNameWithAccent(iconOriginDefinition.iconPath, iconName);
|
|
|
|
writeSVGIcon(iconOriginDefinition.iconPath, iconCopyDefinition.iconPath, key);
|
|
|
|
} else {
|
|
|
|
gutil.log(gutil.colors.yellow(`Icon ${ accentableIconName } not found`))
|
|
|
|
}
|
|
|
|
});
|
2017-06-12 19:49:50 +02:00
|
|
|
|
2017-10-24 00:11:48 +02:00
|
|
|
// themecopy.iconDefinitions._folder_open.iconPath = replaceNameWithAccent(basetheme.iconDefinitions._folder_open.iconPath, iconName);
|
|
|
|
// themecopy.iconDefinitions._folder_open_build.iconPath = replaceNameWithAccent(basetheme.iconDefinitions._folder_open_build.iconPath, iconName);
|
2017-06-12 19:49:50 +02:00
|
|
|
|
2017-10-24 00:11:48 +02:00
|
|
|
// writeSVGIcon(basetheme.iconDefinitions._folder_open.iconPath, themecopy.iconDefinitions._folder_open.iconPath, key);
|
|
|
|
// writeSVGIcon(basetheme.iconDefinitions._folder_open_build.iconPath, themecopy.iconDefinitions._folder_open_build.iconPath, key);
|
2017-06-12 19:49:50 +02:00
|
|
|
|
2017-06-17 16:52:27 +02:00
|
|
|
// fs.writeFileSync(themePath, JSON.stringify(themecopy));
|
2017-06-12 19:49:50 +02:00
|
|
|
|
2017-06-17 16:52:27 +02:00
|
|
|
// addContributeIconTheme(id, label, themepathJSON, PACKAGE_JSON);
|
2017-06-12 19:49:50 +02:00
|
|
|
|
|
|
|
gutil.log(gutil.colors.green(MESSAGE_GENERATED, themePath));
|
|
|
|
});
|
|
|
|
|
2017-06-17 16:52:27 +02:00
|
|
|
// writePackageJSON(PACKAGE_JSON);
|
2017-06-12 19:49:50 +02:00
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
// http://ragefaces.memesoftware.com/faces/large/misc-le-fu-l.png
|
|
|
|
gutil.log(gutil.colors.red(MESSAGE_ICON_ACCENTS_ERROR));
|
|
|
|
cb(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb();
|
|
|
|
});
|