-
Notifications
You must be signed in to change notification settings - Fork 3.7k
WIP: Allow generating iconset for HASS icons #1100
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
66b80a0
Allow generating iconset for HASS icons
balloob 343cca8
Lint
balloob fd73167
Load mdi on demand
balloob 1d29291
mdi -> hass
balloob 892e33a
Lint
balloob 39dfdfb
Hack reschedule
balloob 7d1dc1e
Revert mdi -> hass change for customize
balloob 24e8df9
Clarify on top of ha-icon what has changed
balloob a9026f7
Convert latest mdi icon references
balloob 004ef4c
Convert python script to JS
balloob 4458501
BUILD_DEV=0 all the things
balloob 69f22d5
Embed built-in panel icons
balloob 408cede
service worker hass icons fix
balloob 7e46d4e
Lint
balloob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // From https://www.tomas-dvorak.cz/posts/nodejs-request-without-dependencies/ | ||
| const http = require('http'); | ||
| const https = require('https'); | ||
|
|
||
| module.exports.getContent = function getContent(url) { | ||
| // return new pending promise | ||
| return new Promise((resolve, reject) => { | ||
| // select http or https module, depending on reqested url | ||
| const lib = url.startsWith('https') ? https : http; | ||
| const request = lib.get(url, (response) => { | ||
| // handle http errors | ||
| if (response.statusCode < 200 || response.statusCode > 299) { | ||
| reject(new Error('Failed to load page, status code: ' + response.statusCode)); | ||
| } | ||
| // temporary data holder | ||
| const body = []; | ||
| // on every content chunk, push it to the data array | ||
| response.on('data', chunk => body.push(chunk)); | ||
| // we are done, resolve promise with those joined chunks | ||
| response.on('end', () => resolve(body.join(''))); | ||
| }); | ||
| // handle connection errors of the request | ||
| request.on('error', err => reject(err)); | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| const gulp = require('gulp'); | ||
| const path = require('path'); | ||
| const fs = require('fs'); | ||
| const parse5 = require('parse5'); | ||
| const getContent = require('../common/http').getContent; | ||
|
|
||
| const outputDir = 'hass_frontend'; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| const iconRegEx = /hass:[\w-]+/g; | ||
|
|
||
| const BUILT_IN_PANEL_ICONS = [ | ||
| 'settings', // Config | ||
| 'home-assistant', // Hass.io | ||
| 'poll-box', // History panel | ||
| 'format-list-bulleted-type', // Logbook | ||
| 'mailbox', // Mailbox | ||
| 'account-location', // Map | ||
| 'cart', // Shopping List | ||
| ]; | ||
|
|
||
| function mapFiles(startPath, filter, mapFunc) { | ||
| const files = fs.readdirSync(startPath); | ||
| for (let i = 0; i < files.length; i++) { | ||
| const filename = path.join(startPath, files[i]); | ||
| const stat = fs.lstatSync(filename); | ||
| if (stat.isDirectory()) { | ||
| mapFiles(filename, filter, mapFunc); | ||
| } else if (filename.indexOf(filter) >= 0) { | ||
| mapFunc(filename); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| function findIcons() { | ||
| const icons = new Set(BUILT_IN_PANEL_ICONS); | ||
| function processFile(filename) { | ||
| const content = fs.readFileSync(filename); | ||
| let match; | ||
| // eslint-disable-next-line | ||
| while (match = iconRegEx.exec(content)) { | ||
| // strip off "hass:" and add to set | ||
| icons.add(match[0].substr(5)); | ||
| } | ||
| } | ||
| mapFiles('src', '.html', processFile); | ||
| mapFiles('panels', '.html', processFile); | ||
| mapFiles('hassio', '.html', processFile); | ||
| mapFiles('js', '.js', processFile); | ||
| return icons; | ||
| } | ||
|
|
||
| function generateHassIcons() { | ||
| const icons = findIcons(); | ||
|
|
||
| const iconDoc = parse5.parseFragment(fs.readFileSync(`${outputDir}/mdi.html`, { encoding: 'utf-8' })); | ||
|
|
||
| const ironIconset = iconDoc.childNodes[0]; | ||
| ironIconset.attrs.forEach((attr) => { | ||
| if (attr.name === 'name') { | ||
| attr.value = 'hass'; | ||
| } | ||
| }); | ||
|
|
||
| const defs = ironIconset.childNodes[0].childNodes[0]; | ||
| defs.childNodes = defs.childNodes.filter(icon => icons.has(icon.attrs[0].value)); | ||
|
|
||
| fs.writeFileSync(`${outputDir}/hass_icons.html`, parse5.serialize(iconDoc)); | ||
| // eslint-disable-next-line | ||
| console.log(`Home Assistant has ${icons.size} icons.`); | ||
| } | ||
|
|
||
| async function downloadMDIIcons() { | ||
| // Fetch website of MaterialDesignIcons.com | ||
| const content = await getContent('https://raw.githubusercontent.com/Templarian/MaterialDesign/master/site/getting-started.savvy'); | ||
| // Find link to Polymer v1 download | ||
| const downloadMatch = content.match('(/api/download/polymer/v1/([A-Z0-9-]{36}))'); | ||
|
|
||
| if (!downloadMatch) { | ||
| // eslint-disable-next-line | ||
| console.error('Unable to find Polymer v1 download link'); | ||
| return null; | ||
| } | ||
|
|
||
| // Download iconset | ||
| let iconSet = await getContent(`https://materialdesignicons.com${downloadMatch[0]}`); | ||
|
|
||
| // Strip out the import statement | ||
| iconSet = iconSet.substr(iconSet.indexOf('<iron-iconset-svg')); | ||
|
|
||
| return iconSet; | ||
| } | ||
|
|
||
| async function main() { | ||
| const iconSet = await downloadMDIIcons(); | ||
|
|
||
| if (iconSet === null) throw new Error('Download MDI failed!'); | ||
|
|
||
| fs.writeFileSync(`${outputDir}/mdi.html`, iconSet); | ||
| generateHassIcons(); | ||
| } | ||
|
|
||
| // gulp.task('gen-hass-icons', generateHassIcons); | ||
| gulp.task('gen-icons', main); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we still have to fingerprint mdi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. It's only going to be loaded from inside
ha-iconand that will reference to it without fingerprint.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a new service worker is installed, it will check the fingerprint and download a new version if it has changed.