Skip to content

Commit

Permalink
added dart definitions (#54)
Browse files Browse the repository at this point in the history
* added dart definitions

* created script to regenerate test files
  • Loading branch information
mikecoomber authored Jun 17, 2024
1 parent 35e979c commit 54275f3
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 47,986 deletions.
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"ts-node": "^10.9.2",
"typescript": "^5.4.5",
"webfont": "^11.2.26",
"zeta-icon-name-checker": "^0.0.5",
"zeta-icon-name-checker": "^0.0.13",
"@types/md5": "^2.3.5",
"md5": "^2.3.0",
"oslllo-svg-fixer": "^3.0.0"
Expand All @@ -53,12 +53,12 @@
"license": "MIT",
"description": "The icon library for Zebra Technologies' Zeta Design System.",
"scripts": {
"pretest": "tsc -p tsconfig.test.json",
"test": "mocha dist/test/**/*.test.js -g fetchIcons -i --file ./test/setup.js --timeout 100000",
"test:build": "mocha dist/test/**/*.test.js -g fetchIcons --timeout 100000",
"test": "tsc -p tsconfig.test.json && mocha dist/test/**/*.test.js -g fetchIcons -i --file ./test/setup.js --timeout 100000",
"test:build": "tsc -p tsconfig.test.json && mocha dist/test/**/*.test.js -g fetchIcons --timeout 100000",
"build": "npx tsc -p tsconfig.build.json",
"build:fetch-icons": "npx tsc -p tsconfig.scripts.json",
"fetch-icons": "npm run build:fetch-icons && node --env-file=.env ./.github/fetch_icons/index.js",
"regenerate-test-files": "ts-node ./test/regenerateTestFiles.ts",
"prepack": "npm run build"
},
"repository": {
Expand Down
20 changes: 12 additions & 8 deletions scripts/generators/generateDefinitionFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ const generateDartFile = (fontData: GenerateFontResult): string => {
let dartTemplate = readFileSync("./scripts/templates/icons.dart.template").toString();
const newLine = "\n";

const roundIcons = fontData.dartRoundTypes.join(newLine);
const sharpIcons = fontData.dartSharpTypes.join(newLine);
const roundList = fontData.dartRoundList.join("," + newLine) + ",";
const sharpList = fontData.dartSharpList.join("," + newLine) + ",";
const icons = fontData.dartDefinitions.join(newLine);
const roundIcons = fontData.dartRoundDefinitions.join(newLine);
const sharpIcons = fontData.dartSharpDefinitions.join(newLine);
const iconNames =
fontData.iconNames.map((iconName) => ` '${iconName}': ZetaIcons.${iconName}`).join(`,${newLine}`) + ",";

dartTemplate = dartTemplate.replace("{{icons}}", icons);
dartTemplate = dartTemplate.replace("{{roundIcons}}", roundIcons);
dartTemplate = dartTemplate.replace("{{sharpIcons}}", sharpIcons);
dartTemplate = dartTemplate.replace("{{sharpIconList}}", sharpList);
dartTemplate = dartTemplate.replace("{{roundIconList}}", roundList);
dartTemplate = dartTemplate.replace("{{iconNames}}", iconNames);

return dartTemplate;
};
Expand All @@ -62,9 +63,12 @@ const generateDartFile = (fontData: GenerateFontResult): string => {
const generateTSFile = (fontData: GenerateFontResult): string => {
let tsTemplate = readFileSync("./scripts/templates/icon-types.ts.template").toString();

const tsList = fontData.tsTypes.map((icon) => `"${icon}"`).join(",\n ");
const nameList = fontData.iconNames.map((icon) => `"${icon}"`);
const iconNames = nameList.join(",\n ");
const unionType = nameList.join("\n | ");

tsTemplate = tsTemplate.replace("{{iconNames}}", tsList);
tsTemplate = tsTemplate.replace("{{iconNames}}", iconNames);
tsTemplate = tsTemplate.replace("{{iconTypes}}", unionType);

return tsTemplate;
};
45 changes: 18 additions & 27 deletions scripts/generators/generateFonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ export const generateFonts = async (
fontName: string
): Promise<GenerateFontResult> => {
const fontResult: GenerateFontResult = {
dartRoundTypes: [],
dartRoundList: [],
dartSharpTypes: [],
dartSharpList: [],
tsTypes: [],
} as GenerateFontResult;
dartDefinitions: [],
dartRoundDefinitions: [],
dartSharpDefinitions: [],
iconNames: [],
};

const baseFontPath = `${fontOutputDir}/${fontName}`;
createFolder(fontOutputDir);
Expand Down Expand Up @@ -77,18 +76,15 @@ const buildFontFile = async (type: FontType, fontResult: GenerateFontResult, inp
obj.unicode[0] = String.fromCharCode(unicodeAcc);
obj.name = obj.unicode[1] = obj.name.replace("_round", "").replace("_sharp", "");

const dartIconName = getIconFileName(obj.name, type);

const strUnicode = Number(unicodeAcc).toString(16);

if (type === "round") {
fontResult.dartRoundTypes.push(getDartIconDefinition(dartIconName, strUnicode, "round"));
fontResult.dartRoundList.push(getDartIconListItem(dartIconName));
fontResult.dartDefinitions.push(getDartIconDefinition(obj.name, strUnicode, undefined));
fontResult.dartRoundDefinitions.push(getDartIconDefinition(obj.name, strUnicode, "round"));

fontResult.tsTypes.push(obj.name);
fontResult.iconNames.push(obj.name);
} else if (type === "sharp") {
fontResult.dartSharpTypes.push(getDartIconDefinition(dartIconName, strUnicode, "sharp"));
fontResult.dartSharpList.push(getDartIconListItem(dartIconName));
fontResult.dartSharpDefinitions.push(getDartIconDefinition(obj.name, strUnicode, "sharp"));
}

unicodeAcc++;
Expand All @@ -109,19 +105,14 @@ const buildFontFile = async (type: FontType, fontResult: GenerateFontResult, inp
* @param {FontType} type - Round or sharp.
* @returns {string} Dart definition line used in body of `Icons.dart`.
*/
function getDartIconDefinition(iconName: string, unicode: string, type: FontType): string {
return ` static const IconData ${iconName} = IconData(0x${unicode}, fontFamily: _family${type.capitalize()}, fontPackage: _package);`;
}
function getDartIconDefinition(iconName: string, unicode: string, type: FontType | undefined): string {
if (type == undefined) {
iconName = iconName.toSnakeCase();
} else {
iconName = getIconFileName(iconName, type);
}

/**
* Generates line in dart icons list for an icon.
*
* * Does not contain new line characters, or commas.
* * Does contain spaces for formatting.
*
* @param {string} iconName - snake_case formatted name for icon.
* @returns {string} Dart map item used for list of icons.
*/
function getDartIconListItem(iconName: string): string {
return ` '${iconName}': ZetaIcons.${iconName}`;
return ` static const IconData ${iconName} = IconData(0x${unicode}, fontFamily: family${
type?.capitalize() ?? ""
}, fontPackage: package);`;
}
4 changes: 2 additions & 2 deletions scripts/templates/icon-types.ts.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const ZetaIconNameList = [
{{iconNames}}
];
type ZetaIconNameTuple = typeof ZetaIconNameList;
export type ZetaIconName = ZetaIconNameTuple[number];

export type ZetaIconName = {{iconTypes}};
27 changes: 14 additions & 13 deletions scripts/templates/icons.dart.template
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
// ignore_for_file: public_member_api_docs, constant_identifier_names
import 'package:flutter/material.dart';

// This file is automatically generated by the zeta-icons respository
// DO NOT MODIFY

const zetaIconsVersion = 'VERSION_NUM';

/// Zeta Icons. Full list of icons can be found at [Zeta Icons](https://zeta-icons.web.app/).
class ZetaIcons {
ZetaIcons._();
static const String _familyRound = 'zeta-icons-round';
static const String _familySharp = 'zeta-icons-sharp';
static const String _package = 'zeta_flutter';
abstract class ZetaIcons {
static const String family = 'zeta-icons';
static const String familyRound = 'zeta-icons-round';
static const String familySharp = 'zeta-icons-sharp';
static const String package = 'zeta_flutter';

// Icons
{{icons}}

// Round Icons
{{roundIcons}}
Expand All @@ -17,12 +23,7 @@ class ZetaIcons {
{{sharpIcons}}
}

// List of all rounded icons.
const Map<String, IconData> iconsRound = {
{{roundIconList}}
};

// List of all sharp icons.
const Map<String, IconData> iconsSharp = {
{{sharpIconList}}
// List of all rounded icons.
const Map<String, IconData> icons = {
{{iconNames}}
};
9 changes: 4 additions & 5 deletions scripts/types/customTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ export interface FontDefinition extends IconDefinition {
}

export interface GenerateFontResult {
dartRoundTypes: string[];
dartRoundList: string[];
dartSharpTypes: string[];
dartSharpList: string[];
tsTypes: string[];
dartDefinitions: string[];
dartRoundDefinitions: string[];
dartSharpDefinitions: string[];
iconNames: string[];
}

export type IconManifest = Map<string, IconDefinition>;
Expand Down
Loading

0 comments on commit 54275f3

Please sign in to comment.