Skip to content

Commit

Permalink
moved csscheck to script folder
Browse files Browse the repository at this point in the history
  • Loading branch information
IITI-tushar committed Jan 15, 2025
1 parent 0681a9a commit 1e5934b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
exit 1
fi
chmod +x ./.github/workflows/css_check.py
./.github/workflows/css_check.py --directory ./src --allowed_css_patterns app.module.css . || {
./.github/workflows/css_check.py --directory --allowed_css_patterns app.module.cs . || {
echo "Error: CSS check failed"
exit 1
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""Check TypeScript files for CSS violations and embedded CSS."""
import argparse
import os
Expand All @@ -6,10 +8,13 @@
from collections import namedtuple

# Define namedtuples for storing results
Violation = namedtuple('Violation', ['file_path', 'css_file', 'reason'])
CorrectImport = namedtuple('CorrectImport', ['file_path', 'css_file'])
EmbeddedViolation = namedtuple('EmbeddedViolation', ['file_path', 'css_codes'])
CSSCheckResult = namedtuple('CSSCheckResult', ['violations', 'correct_imports', 'embedded_violations'])
Violation = namedtuple("Violation", ["file_path", "css_file", "reason"])
CorrectImport = namedtuple("CorrectImport", ["file_path", "css_file"])
EmbeddedViolation = namedtuple("EmbeddedViolation", ["file_path", "css_codes"])
CSSCheckResult = namedtuple(
"CSSCheckResult", ["violations", "correct_imports", "embedded_violations"]
)


def check_embedded_css(content: str) -> list:
"""
Expand All @@ -24,8 +29,12 @@ def check_embedded_css(content: str) -> list:
embedded_css_pattern = r"#([0-9a-fA-F]{3}){1,2}" # Matches CSS color codes
return re.findall(embedded_css_pattern, content)


def check_files(
directory: str, exclude_files: list, exclude_directories: list, allowed_css_patterns: list
directory: str,
exclude_files: list,
exclude_directories: list,
allowed_css_patterns: list,
) -> CSSCheckResult:
"""
Check TypeScript files for CSS violations and correct CSS imports.
Expand Down Expand Up @@ -72,28 +81,38 @@ def check_files(
css_imports = re.findall(r'import\s+.*?["\'](.+?\.css)["\']', content)
for css_file in css_imports:
# Try to find the CSS file
css_file_path = os.path.join(os.path.dirname(file_path), css_file)
base_path = os.path.dirname(file_path)
css_file_path = os.path.normpath(os.path.join(base_path, css_file))
if not os.path.exists(css_file_path):
# If not found, try to find it relative to the src directory
src_dir = os.path.abspath(directory)
css_file_path = os.path.join(src_dir, css_file)

# Check if the CSS file exists
if not os.path.exists(css_file_path):
violations.append(Violation(file_path, css_file, "File not found"))
violations.append(
Violation(file_path, css_file, "File not found")
)
# Check if the CSS import matches the allowed patterns
elif any(css_file.endswith(pattern) for pattern in allowed_css_patterns):
elif any(
css_file.endswith(pattern) for pattern in allowed_css_patterns
):
correct_css_imports.append(CorrectImport(file_path, css_file))
else:
violations.append(Violation(file_path, css_file, "Invalid import"))
violations.append(
Violation(file_path, css_file, "Invalid import")
)

# Check for embedded CSS
embedded_css = check_embedded_css(content)
if embedded_css:
embedded_css_violations.append(EmbeddedViolation(file_path, embedded_css))
embedded_css_violations.append(
EmbeddedViolation(file_path, embedded_css)
)

return CSSCheckResult(violations, correct_css_imports, embedded_css_violations)


def main():
"""Run the CSS check script."""
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -137,31 +156,35 @@ def main():
if result.violations:
output.append("CSS Import Violations:")
for violation in result.violations:
output.append(f"- {violation.file_path}: {violation.css_file} ({violation.reason})")
output.append(
f"- {violation.file_path}: {violation.css_file} ({violation.reason})"
)
exit_code = 1

if result.embedded_violations:
output.append("\nEmbedded CSS Violations:")
for violation in result.embedded_violations:
output.append(f"- {violation.file_path}: {', '.join(violation.css_codes)}")
exit_code = 1
exit_code = 1

if output:
print("\n".join(output))
print("""
print(
"""
Please address the above CSS violations:
1. For invalid CSS imports, ensure you're using the correct import syntax and file paths.
2. For embedded CSS, move the CSS to appropriate stylesheet files and import them correctly.
3. Make sure to use only the allowed CSS patterns as specified in the script arguments.
4. Check that all imported CSS files exist in the specified locations.
""")
"""
)
if args.show_success and result.correct_imports:
print("\nCorrect CSS Imports:")
for import_ in result.correct_imports:
print(f"- {import_.file_path}: {import_.css_file}")

sys.exit(exit_code)


if __name__ == "__main__":
main()

0 comments on commit 1e5934b

Please sign in to comment.