Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Icons update #834

Merged
merged 22 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
141 changes: 141 additions & 0 deletions assets/build/generate-icon-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* Pimcore
*
* This source file is available under two different licenses:
* - Pimcore Open Core License (POCL)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL
*
* HOW TO USE:
* 1. Place all the SVG files in the folder `./js/src/core/assets/icons`
* 2. Please remove any duplicates and add SVG file names that use a protected word to the `protectedNames` array,
* the script will append 'Icon' to the variable name.
* 3. Run the script using `npm run generate-icons`
* 4. The script will generate/replace the index file at `./js/src/core/modules/icon-library/index.ts`
* 5. The script will also update the SVG files to use `currentColor` as the stroke color or fill color (if stroke does not exist in the file).
*/

import * as fs from 'fs';
import * as path from 'path';

const SVG_FOLDER = path.resolve('./js/src/core/assets/icons');
const OUTPUT_FILE = path.resolve('./js/src/core/modules/icon-library/index.ts');
const protectedNames = new Set(['new', 'package', 'import', 'export']);

if (!fs.existsSync(SVG_FOLDER)) {
console.error(`Error: Directory ${SVG_FOLDER} does not exist.`);
process.exit(1);
}

const files: string[] = fs.readdirSync(SVG_FOLDER as string).filter(file => file.endsWith('.inline.svg'));

if (files.length === 0) {
console.log(`No SVG files found in ${SVG_FOLDER}`);
process.exit(0);
}

const generateVariableName = (fileName: string): string => {
const baseName = fileName.replace('.inline.svg', '');
let variableName = baseName
.replace(/[-_\s]+(.)?/g, (_, letter) => letter ? letter.toUpperCase() : '')
.replace(/^./, str => str.toLowerCase());

if (protectedNames.has(variableName)) {
variableName += 'Icon';
}

return variableName;
};

const generateIconEntry = (fileName: string): string => {
const iconName = fileName.replace('.inline.svg', '');

const variableName = generateVariableName(fileName)
return `
iconLibrary.register({
name: '${iconName}',
component: ${variableName}
});`;
};

const modifySvgAttributes = (filePath: string): void => {
let svgContent: string = fs.readFileSync(filePath, 'utf-8');
const hasStroke = /stroke="[^"]*"/.test(svgContent);

if (!hasStroke) {
svgContent = svgContent.replace(/fill="[^"]*"/g, 'fill="currentColor"');
}

svgContent = svgContent.replace(/stroke="[^"]*"/g, 'stroke="currentColor"');

fs.writeFileSync(filePath, svgContent, 'utf-8');
};

const variableNameSet = new Set<string>();
files.forEach(file => {
const variableName = generateVariableName(file);

if (variableNameSet.has(variableName)) {
console.error(`Error: Duplicate SVG file detected with variable name '${variableName}'. File: '${file}'`);
process.exit(1);
}

variableNameSet.add(variableName);
});

let content = `
/**
* Pimcore
*
* This source file is available under two different licenses:
* - Pimcore Open Core License (POCL)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL
*/

/* eslint-disable max-lines */

import { container } from '@Pimcore/app/depency-injection';
import { moduleSystem } from '@Pimcore/app/module-system/module-system';
import { serviceIds } from '@Pimcore/app/config/services/service-ids';
import { type IconLibrary } from './services/icon-library';
`;

files.forEach((file: string) => {
const filePath: string = path.join(SVG_FOLDER as string, file);
modifySvgAttributes(filePath);

const variableName: string = generateVariableName(file);
content += `
import ${variableName} from '@Pimcore/assets/icons/${file}';`;
});

content += `

moduleSystem.registerModule({
onInit: () => {
const iconLibrary = container.get<IconLibrary>(serviceIds.iconLibrary);`;

files.forEach(file => {
content += generateIconEntry(file);
});

content += `
}
});
`;

try {
fs.writeFileSync(OUTPUT_FILE, content.trim());
console.log(`Index file generated successfully at: ${OUTPUT_FILE}`);
} catch (error) {
console.error('Error generating the index file:', error);
}
3 changes: 0 additions & 3 deletions assets/js/src/core/assets/icons/AppstoreOutlined.inline.svg

This file was deleted.

5 changes: 0 additions & 5 deletions assets/js/src/core/assets/icons/DeleteOutlined.inline.svg

This file was deleted.

5 changes: 0 additions & 5 deletions assets/js/src/core/assets/icons/FileOutlined.inline.svg

This file was deleted.

8 changes: 0 additions & 8 deletions assets/js/src/core/assets/icons/PlusOutlined.inline.svg

This file was deleted.

5 changes: 0 additions & 5 deletions assets/js/src/core/assets/icons/ShopOutlined.inline.svg

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions assets/js/src/core/assets/icons/ToolOutlined.inline.svg

This file was deleted.

3 changes: 3 additions & 0 deletions assets/js/src/core/assets/icons/accessory.inline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/js/src/core/assets/icons/add-find.inline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/js/src/core/assets/icons/add-folder.inline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/js/src/core/assets/icons/add-image.inline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/js/src/core/assets/icons/add-package.inline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading