Skip to content

Commit f2e8fc4

Browse files
Merge pull request #4 from CornerstoneCode/Yaml-Correction
Correct action.yml
2 parents 378a15b + 990c5a8 commit f2e8fc4

3 files changed

Lines changed: 50 additions & 50 deletions

File tree

Dockerfile

Lines changed: 0 additions & 20 deletions
This file was deleted.

action.yml

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: 'DeploySlim'
2-
description: 'Compress web assets (HTML, CSS, JS, etc.) with Brotli & Gzip in GitHub Actions. Works on Linux/Windows.'
2+
description: 'Compress web assets (HTML, CSS, JS, etc.) with Brotli & Gzip in GitHub Actions.'
33
author: 'CornerstoneCode'
44

55
inputs:
@@ -21,10 +21,24 @@ inputs:
2121
default: '6'
2222

2323
runs:
24-
using: 'docker'
25-
image: 'Dockerfile' # Uses the Dockerfile in the root of your repository
26-
env:
27-
INPUT_DIRECTORY: ${{ inputs.directory }}
28-
INPUT_ALGORITHMS: ${{ inputs.algorithms }}
29-
INPUT_BROTLI_LEVEL: ${{ inputs.brotli-level }}
30-
INPUT_GZIP_LEVEL: ${{ inputs.gzip-level }}
24+
using: 'composite'
25+
steps:
26+
- name: Set up Python 3.x
27+
uses: actions/setup-python@v3
28+
with:
29+
python-version: '3.x'
30+
31+
- name: Install Python dependencies
32+
run: |
33+
python -m pip install --upgrade pip
34+
pip install brotli
35+
shell: bash
36+
37+
- name: Run compression script
38+
run: python main.py
39+
shell: bash
40+
env:
41+
INPUT_DIRECTORY: ${{ inputs.directory }}
42+
INPUT_ALGORITHMS: ${{ inputs.algorithms }}
43+
INPUT_BROTLI_LEVEL: ${{ inputs.brotli-level }}
44+
INPUT_GZIP_LEVEL: ${{ inputs.gzip-level }}

src/main.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,36 @@
44
import mimetypes
55

66
def compress_file(filepath, algorithms=['br', 'gz'], brotli_level=6, gzip_level=6):
7-
"""Compresses a file using specified algorithms."""
7+
"""Compresses a file using specified algorithms and prints file size changes."""
88
content_type, _ = mimetypes.guess_type(filepath)
99
if content_type and content_type.startswith(('text/', 'application/javascript', 'application/json', 'application/xml', 'image/svg+xml', 'application/wasm')):
10-
with open(filepath, 'rb') as f_in:
11-
content = f_in.read()
12-
if 'br' in algorithms:
13-
compressed_filepath_br = filepath + '.br'
14-
try:
15-
compressed_content_br = brotli.compress(content, quality=brotli_level)
16-
with open(compressed_filepath_br, 'wb') as f_out:
17-
f_out.write(compressed_content_br)
18-
print(f"Compressed '{filepath}' to '{compressed_filepath_br}' using Brotli.")
19-
except Exception as e:
20-
print(f"Error compressing '{filepath}' with Brotli: {e}")
21-
if 'gz' in algorithms:
22-
compressed_filepath_gz = filepath + '.gz'
23-
try:
24-
with gzip.open(compressed_filepath_gz, 'wb', compresslevel=gzip_level) as f_out:
25-
f_out.write(content)
26-
print(f"Compressed '{filepath}' to '{compressed_filepath_gz}' using Gzip.")
27-
except Exception as e:
28-
print(f"Error compressing '{filepath}' with Gzip: {e}")
29-
else:
30-
print(f"Skipping '{filepath}' - Content type '{content_type}' not suitable for compression.")
10+
try:
11+
original_size = os.path.getsize(filepath)
12+
with open(filepath, 'rb') as f_in:
13+
content = f_in.read()
14+
if 'br' in algorithms:
15+
compressed_filepath_br = filepath + '.br'
16+
try:
17+
compressed_content_br = brotli.compress(content, quality=brotli_level)
18+
with open(compressed_filepath_br, 'wb') as f_out:
19+
f_out.write(compressed_content_br)
20+
compressed_size_br = os.path.getsize(compressed_filepath_br)
21+
print(f"Compressed '{filepath}' ({original_size} bytes) to '{compressed_filepath_br}' ({compressed_size_br} bytes) using Brotli.")
22+
except Exception as e:
23+
print(f"Error compressing '{filepath}' with Brotli: {e}")
24+
if 'gz' in algorithms:
25+
compressed_filepath_gz = filepath + '.gz'
26+
try:
27+
with gzip.open(compressed_filepath_gz, 'wb', compresslevel=gzip_level) as f_out:
28+
f_out.write(content)
29+
compressed_size_gz = os.path.getsize(compressed_filepath_gz)
30+
print(f"Compressed '{filepath}' ({original_size} bytes) to ({compressed_size_gz} bytes) using Gzip.")
31+
except Exception as e:
32+
print(f"Error compressing '{filepath}' with Gzip: {e}")
33+
except FileNotFoundError:
34+
print(f"Error: File '{filepath}' not found.")
35+
except Exception as e:
36+
print(f"An error occurred while processing '{filepath}': {e}")
3137

3238
def process_directory(directory, algorithms, brotli_level, gzip_level):
3339
"""Processes all files in a directory."""

0 commit comments

Comments
 (0)