Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/devicons/devicon
Browse files Browse the repository at this point in the history
  • Loading branch information
EnisMulic committed Mar 16, 2021
2 parents 259ce31 + 2809b56 commit 21bf571
Show file tree
Hide file tree
Showing 55 changed files with 3,294 additions and 227 deletions.
File renamed without changes.
26 changes: 26 additions & 0 deletions .github/drafts/check_svgs_monthly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import traceback
import sys

# pycharm complains that build_assets is an unresolved ref
# don't worry about it, the script still runs
from build_assets import filehandler, arg_getters
from build_assets import util


def main():
"""
Check the quality of the svgs of the whole icons folder.
"""
args = arg_getters.get_check_svgs_monthly_args()

try:
devicon_json = filehandler.get_json_file_content(args.devicon_json_path)
svgs = filehandler.get_svgs_paths(devicon_json, args.icons_folder_path)
util.check_svgs(svgs)
print("All SVGs found were good. Task completed.")
except Exception as e:
util.exit_with_err(e)


if __name__ == "__main__":
main()
42 changes: 42 additions & 0 deletions .github/drafts/check_svgs_monthly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Check SVGs Monthly
on: workflow_dispatch
# schedule:
# - cron: '0 0 1 * *'
jobs:
check_develop:
name: Check the SVGs' quality in the `develop` branch
runs-on: ubuntu-18.04
steps:

- uses: actions/checkout@v2
with:
ref: develop

- uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Install dependencies
run: python -m pip install --upgrade pip

- name: Run the check_svg script
run: >
python ./.github/scripts/check_svgs_monthly.py ./devicon.json ./icons
check_master:
name: Check the SVGs' quality in the `master` branch
runs-on: ubuntu-18.04
steps:

- uses: actions/checkout@v2 # check out default branch, which is master

- uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Install dependencies
run: python -m pip install --upgrade pip

- name: Run the check_svg script
run: >
python ./.github/scripts/check_svgs_monthly.py ./icomoon.json ./devicon.json ./icons
File renamed without changes.
21 changes: 16 additions & 5 deletions .github/scripts/build_assets/arg_getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,26 @@ def get_selenium_runner_args(peek_mode=False):
return parser.parse_args()


def get_check_svgs_args():
def get_check_svgs_on_pr_args():
"""
Get the commandline arguments for the chec_svgs.py.
Get the commandline arguments for the check_svgs_on_pr.py.
"""
parser = ArgumentParser(description="Check the SVGs to ensure their attributes are correct.")
parser.add_argument("icomoon_json_path",
help="The path to the icomoon.json aka the selection.json created by Icomoon",
parser = ArgumentParser(description="Check the SVGs to ensure their attributes are correct. Run whenever a PR is opened")
parser.add_argument("files_added_json_path",
help="The path to the files_added.json created by the gh-action-get-changed-files@2.1.4",
action=PathResolverAction)

parser.add_argument("files_modified_json_path",
help="The path to the files_modified.json created by the gh-action-get-changed-files@2.1.4",
action=PathResolverAction)
return parser.parse_args()


def get_check_svgs_monthly_args():
"""
Get the commandline arguments for the check_svgs_monthly.py.
"""
parser = ArgumentParser(description="Check the SVGs to ensure their attributes are correct. Run monthly.")
parser.add_argument("devicon_json_path",
help="The path to the devicon.json",
action=PathResolverAction)
Expand Down
55 changes: 47 additions & 8 deletions .github/scripts/build_assets/filehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
import re


def find_new_icons(devicon_json_path: str, icomoon_json_path: str) -> List[dict]:
def find_new_icons(devicon_json_path: str, icomoon_json_path: str):
"""
Find the newly added icons by finding the difference between
the devicon.json and the icomoon.json.
:param devicon_json_path, the path to the devicon.json.
:param icomoon_json_path: a path to the iconmoon.json.
:return: a list of the new icons as JSON objects.
"""
with open(devicon_json_path) as json_file:
devicon_json = json.load(json_file)

with open(icomoon_json_path) as json_file:
icomoon_json = json.load(json_file)
devicon_json = get_json_file_content(devicon_json_path)
icomoon_json = get_json_file_content(icomoon_json_path)

new_icons = []
for icon in devicon_json:
Expand All @@ -28,7 +25,17 @@ def find_new_icons(devicon_json_path: str, icomoon_json_path: str) -> List[dict]
return new_icons


def is_not_in_icomoon_json(icon, icomoon_json) -> bool:
def get_json_file_content(json_path: str):
"""
Get the json content of the json_path.
:param: json_path, the path to the json file.
:return: a dict representing the file content.
"""
with open(json_path) as json_file:
return json.load(json_file)


def is_not_in_icomoon_json(icon, icomoon_json):
"""
Checks whether the icon's name is not in the icomoon_json.
:param icon: the icon object we are searching for.
Expand All @@ -45,7 +52,7 @@ def is_not_in_icomoon_json(icon, icomoon_json) -> bool:


def get_svgs_paths(new_icons: List[dict], icons_folder_path: str,
icon_versions_only: bool=False, as_str: bool=True) -> List[str] or List[Path]:
icon_versions_only: bool=False, as_str: bool=True):
"""
Get all the suitable svgs file path listed in the devicon.json.
:param new_icons, a list containing the info on the new icons.
Expand Down Expand Up @@ -199,3 +206,35 @@ def create_screenshot_folder(dir, screenshot_name: str="screenshots/"):
print(f"{screenshot_folder} already exist. Script will do nothing.")
finally:
return str(screenshot_folder)

def get_added_modified_svgs(files_added_json_path: str,
files_modified_json_path: str):
"""
Get the svgs added and modified from the files_changed_json_path.
:param: files_added_json_path, the path to the files_added.json created by the gh-action-get-changed-files@2.1.4
:param: files_modified_json_path, the path to the files_modified.json created by the gh-action-get-changed-files@2.1.4
:return: a list of the svg file paths that were added/modified in this pr as Path. It will only return icons in /icons path (see https://github.com/devicons/devicon/issues/505)
"""
files_added = get_json_file_content(files_added_json_path)
files_modified = get_json_file_content(files_modified_json_path)

svgs = []
for file in files_added:
path = Path(file)
if path.suffix.lower() == ".svg" and path.as_posix().lower().startswith('icons/'):
svgs.append(path)

for file in files_modified:
path = Path(file)
if path.suffix.lower() == ".svg" and path.as_posix().lower().startswith('icons/'):
svgs.append(path)

return svgs


def write_to_file(path: str, value: any):
"""
Write the value to a JSON file.
"""
with open(path, "w") as file:
file.write(value)
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import os
import platform
import sys
import traceback


def exit_with_err(err: Exception):
"""
Exit the current step and display the err.
:param: err, the error/exception encountered.
"""
traceback.print_exc()
sys.exit(1)


def set_env_var(key: str, value: str, delimiter: str='~'):
Expand Down
18 changes: 0 additions & 18 deletions .github/scripts/check_all_icons.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
from enum import Enum
from typing import List
import sys
import xml.etree.ElementTree as et
import time
from pathlib import Path


# pycharm complains that build_assets is an unresolved ref
# don't worry about it, the script still runs
from build_assets import filehandler, arg_getters
from build_assets import github_env
from build_assets import util


def main():
class SVG_STATUS_CODE(Enum):
"""
Check the quality of the svgs.
If any error is found, set an environmental variable called ERR_MSGS
that will contains the error messages.
The status codes to check for in post_check_svgs_comment.yml
"""
args = arg_getters.get_check_svgs_args()
new_icons = filehandler.find_new_icons(args.devicon_json_path, args.icomoon_json_path)
NO_SVG = 0 # action: do nothing
SVG_OK = 1 # action: let user know their svgs are fine

if len(new_icons) == 0:
sys.exit("No files need to be uploaded. Ending script...")

# print list of new icons
print("SVGs being checked:", *new_icons, sep = "\n", end='\n\n')

time.sleep(1) # do this so the logs stay clean
def main():
"""
Check the quality of the svgs.
If any svg error is found, create a json file called 'svg_err_messages.json'
in the root folder that will contains the error messages.
"""
args = arg_getters.get_check_svgs_on_pr_args()
try:
# check the svgs
svgs = filehandler.get_svgs_paths(new_icons, args.icons_folder_path, as_str=False)
check_svgs(svgs)
print("All SVGs found were good.\nTask completed.")
svgs = filehandler.get_added_modified_svgs(args.files_added_json_path,
args.files_modified_json_path)
print("SVGs to check: ", *svgs, sep='\n')

if len(svgs) == 0:
print("No SVGs to check, ending script.")
err_messages = SVG_STATUS_CODE.NO_SVG.value
else:
err_messages = check_svgs(svgs)

filehandler.write_to_file("./svg_err_messages.txt", str(err_messages))
print("Task completed.")
except Exception as e:
github_env.set_env_var("ERR_MSGS", str(e))
sys.exit(str(e))
util.exit_with_err(e)


def check_svgs(svg_file_paths: List[Path]):
Expand All @@ -45,14 +51,16 @@ def check_svgs(svg_file_paths: List[Path]):
The style must not contain any 'fill' declarations.
If any error is found, they will be thrown.
:param: svg_file_paths, the file paths to the svg to check for.
:return: None if there no errors. If there is, return a JSON.stringified
list with the error messages in it.
"""
# batch err messages together so user can fix everything at once
err_msgs = []
for svg_path in svg_file_paths:
tree = et.parse(svg_path)
root = tree.getroot()
namespace = "{http://www.w3.org/2000/svg}"
err_msg = [f"{svg_path.name}:"]
err_msg = [f"{svg_path}:"]

if root.tag != f"{namespace}svg":
err_msg.append(f"-root is '{root.tag}'. Root must be an 'svg' element")
Expand Down Expand Up @@ -84,7 +92,8 @@ def check_svgs(svg_file_paths: List[Path]):
err_msgs.append("\n".join(err_msg))

if len(err_msgs) > 0:
raise Exception("Errors found in these files:\n" + "\n\n".join(err_msgs))
return "\n\n".join(err_msgs)
return SVG_STATUS_CODE.SVG_OK.value


if __name__ == "__main__":
Expand Down
5 changes: 3 additions & 2 deletions .github/scripts/icomoon_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# don't worry about it, the script still runs
from build_assets.SeleniumRunner import SeleniumRunner
from build_assets import filehandler, arg_getters
from build_assets import util


def main():
Expand All @@ -32,9 +33,9 @@ def main():
filehandler.rename_extracted_files(args.download_path)
print("Task completed.")
except TimeoutException as e:
sys.exit("Selenium Time Out Error: \n" + str(e))
util.exit_with_err("Selenium Time Out Error: \n" + str(e))
except Exception as e:
sys.exit(str(e))
util.exit_with_err(e)
finally:
runner.close()

Expand Down
5 changes: 3 additions & 2 deletions .github/scripts/icomoon_peek.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# don't worry about it, the script still runs
from build_assets.SeleniumRunner import SeleniumRunner
from build_assets import filehandler, arg_getters
from build_assets import util


def main():
Expand Down Expand Up @@ -39,9 +40,9 @@ def main():
runner.upload_svgs(svgs, screenshot_folder)
print("Task completed.")
except TimeoutException as e:
sys.exit("Selenium Time Out Error: \n" + str(e))
util.exit_with_err("Selenium Time Out Error: \n" + str(e))
except Exception as e:
sys.exit(str(e))
util.exit_with_err(e)
finally:
runner.close()

Expand Down
Loading

0 comments on commit 21bf571

Please sign in to comment.