Skip to content

Commit

Permalink
✨ add recompile option
Browse files Browse the repository at this point in the history
  • Loading branch information
t4k committed Oct 24, 2023
1 parent 0b73f25 commit f51ef1d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 57 deletions.
101 changes: 50 additions & 51 deletions .github/workflows/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,61 @@
import os
import shutil
import subprocess
import sys

from pathlib import Path

import plac

def compile_css(extent, github_commit):
# NOTE avoid redundant artifact creation
if Path(f"artifacts/{extent}.css").is_file():
print(f"⚠️ file exists: artifacts/{extent}.css")
return
# NOTE requires `sass` command
subprocess.run(
[
"sass",
"--no-charset",
"--no-source-map",
f"{extent}.scss",
f"artifacts/{extent}.css",
],
check=True,
)
with open(f"artifacts/{extent}.css", "r") as f:
css = f.read()
with open(f"artifacts/{extent}.css", "w") as f:
if github_commit:
f.write(
f"/* see https://github.com/{github_commit[:len(github_commit) - 33]} */\n\n"
)
f.write(css)


def parse_nested_includes(wrapper, variant=None):
html = ""
wrapper.seek(0)
for line in wrapper:
if line.strip().startswith("<!--#include"):
included_file = line.split("'")[1]
if included_file.split(".")[0].endswith("-GROUP"):
fo = open(included_file.replace("GROUP", variant))
html += parse_nested_includes(fo, variant)
fo.close()
else:
fo = open(included_file)
html += parse_nested_includes(fo, variant)
fo.close()
else:
html += line
return html

def main(
file: "modified file from which to build artifacts", # type: ignore
github_commit: ("optional github commit path", "option", "g"), # type: ignore
):

def main(file):
print(f"🐞 file: {file}")
github_commit = (
f'{os.environ.get("GITHUB_REPOSITORY")}/commit/{os.environ.get("GITHUB_SHA")}'
)

if file.endswith(".scss"):
# NOTE primary scss files do not have named parent directories
Expand Down Expand Up @@ -98,50 +142,5 @@ def main(
print(f"🐞 artifacts:", os.listdir("artifacts"))


def compile_css(extent, github_commit):
# NOTE avoid redundant artifact creation
if Path(f"artifacts/{extent}.css").is_file():
print(f"⚠️ file exists: artifacts/{extent}.css")
return
# NOTE requires `sass` command
subprocess.run(
[
"sass",
"--no-charset",
"--no-source-map",
f"{extent}.scss",
f"artifacts/{extent}.css",
],
check=True,
)
with open(f"artifacts/{extent}.css", "r") as f:
css = f.read()
with open(f"artifacts/{extent}.css", "w") as f:
if github_commit:
f.write(
f"/* see https://github.com/{github_commit[:len(github_commit) - 33]} */\n\n"
)
f.write(css)


def parse_nested_includes(wrapper, variant=None):
html = ""
wrapper.seek(0)
for line in wrapper:
if line.strip().startswith("<!--#include"):
included_file = line.split("'")[1]
if included_file.split(".")[0].endswith("-GROUP"):
fo = open(included_file.replace("GROUP", variant))
html += parse_nested_includes(fo, variant)
fo.close()
else:
fo = open(included_file)
html += parse_nested_includes(fo, variant)
fo.close()
else:
html += line
return html


if __name__ == "__main__":
plac.call(main)
main(sys.argv[1])
33 changes: 28 additions & 5 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
name: Compile Artifacts

on:
workflow_call:
workflow_call:
inputs:
recompile:
required: false
type: string

jobs:
compile:
Expand All @@ -15,21 +19,40 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: ${{ vars.PYTHON_VERSION }}
- name: Install dependencies
- name: Check changed files for SCSS
id: compile_scss
if: ${{ ! inputs.recompile }}
run: |
for f in $(git diff-tree --no-commit-id --name-only -r ${{ github.sha }}); do
if [[ "$f" == *".scss" ]]; then
echo "SCSS=true" >> "$GITHUB_OUTPUT"
break
fi
done
- name: Check inputs for SCSS
id: recompile_scss
if: ${{ endsWith(inputs.recompile, '.scss') }}
run: echo "SCSS=true" >> "$GITHUB_OUTPUT"
- name: Conditionally install dart-sass
if: steps.recompile_scss.outputs.SCSS == 'true' || steps.compile_scss.outputs.SCSS == 'true'
run: |
python -m pip install --upgrade pip
pip install plac
curl --silent --show-error --location https://github.com/sass/dart-sass/releases/download/${{ vars.DART_SASS_VERSION }}/dart-sass-${{ vars.DART_SASS_VERSION }}-linux-x64.tar.gz | tar --extract --gzip
mv dart-sass/sass /usr/local/bin
- name: Create artifacts directory
run: mkdir -p artifacts
- name: Compile artifacts from changed files
if: ${{ ! inputs.recompile }}
env:
GROUPS: ${{ secrets.LIBGUIDES_GROUPS }}
run: for f in $(git diff-tree --no-commit-id --name-only -r ${{ github.sha }});
do
python .github/workflows/compile.py "$f" --github-commit ${{ github.repository }}/commit/${{ github.sha }};
python .github/workflows/compile.py "$f";
done
- name: Recompile artifact from input
if: ${{ inputs.recompile }}
env:
GROUPS: ${{ secrets.LIBGUIDES_GROUPS }}
run: python .github/workflows/compile.py ${{ inputs.recompile }};
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,4 @@ def test_deploy(page: Page):
)
except PlaywrightTimeoutError:
page.close()
sys.exit(f"PLAYWRIGHT_TIMEOUT: {item.name}", end="")
sys.exit(f"PLAYWRIGHT_TIMEOUT: {item.name}")
8 changes: 8 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ on:
- "*.js"
- "*.html"
- "*.shtm"
workflow_dispatch:
inputs:
recompile:
description: 'Source file to recompile'
required: false
type: string

jobs:
compile:
uses: ./.github/workflows/compile.yml
with:
recompile: ${{ inputs.recompile }}
secrets: inherit
deploy:
runs-on: ubuntu-latest
Expand Down

0 comments on commit f51ef1d

Please sign in to comment.