mirror of
https://github.com/RGBCube/GCCPreprocessHTML
synced 2025-05-14 13:04:59 +00:00
Scripts: Rewrite build script in python
This commit is contained in:
parent
6ec445545b
commit
858fe35c1b
4 changed files with 102 additions and 21 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -1,9 +1,12 @@
|
||||||
*
|
*
|
||||||
|
|
||||||
|
!build/
|
||||||
|
!build/example/
|
||||||
|
|
||||||
!example/
|
!example/
|
||||||
|
|
||||||
!.gitignore
|
!.gitignore
|
||||||
|
|
||||||
!*.html
|
!*.html
|
||||||
!*.md
|
!*.md
|
||||||
!*.sh
|
!*.py
|
||||||
|
|
|
@ -5,7 +5,7 @@ Preprocess HTML files with the C processor.
|
||||||
Simply run the build script:
|
Simply run the build script:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
./build.sh
|
./build.py
|
||||||
```
|
```
|
||||||
|
|
||||||
And you will get your compiled HTML files in the `build` directory.
|
And you will get your compiled HTML files in the `build` directory.
|
||||||
|
|
97
build.py
Executable file
97
build.py
Executable file
|
@ -0,0 +1,97 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import subprocess as spc
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
PWD = Path(".")
|
||||||
|
BUILD_DIR = Path("build")
|
||||||
|
|
||||||
|
IGNORE_DIRS = [
|
||||||
|
".idea",
|
||||||
|
"build",
|
||||||
|
"venv",
|
||||||
|
]
|
||||||
|
|
||||||
|
# If a file extension is here and the key value is True, it will get processed.
|
||||||
|
# If it is here and the key value is False, it will get copied.
|
||||||
|
TO_PROCESS = {
|
||||||
|
"html": True,
|
||||||
|
"css": False,
|
||||||
|
"ico": False,
|
||||||
|
"js": False,
|
||||||
|
"png": False,
|
||||||
|
"svg": False,
|
||||||
|
"webmanifest": False,
|
||||||
|
"xml": False,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def ext_of(file: Path) -> str:
|
||||||
|
"""Returns the extension of the given file."""
|
||||||
|
return file.name.split(".")[-1]
|
||||||
|
|
||||||
|
|
||||||
|
def should_process(file: Path) -> bool:
|
||||||
|
"""Returns whether if the file should be processed."""
|
||||||
|
return should_copy(file) and TO_PROCESS.get(ext_of(file), False)
|
||||||
|
|
||||||
|
|
||||||
|
def should_copy(file: Path, /) -> bool:
|
||||||
|
"""Returns whether if the file should be copied (i.e. included in the build/ dir)."""
|
||||||
|
return ext_of(file) in TO_PROCESS and not file.name.startswith("_")
|
||||||
|
|
||||||
|
|
||||||
|
def process_file(in_: Path, out: Path, /) -> None:
|
||||||
|
"""
|
||||||
|
Processes the give file with the C preprocessor.
|
||||||
|
Writes the output to the given file.
|
||||||
|
Filters lines that start with `#`.
|
||||||
|
"""
|
||||||
|
process = spc.Popen(
|
||||||
|
f"cc -E - < {in_}", stderr=spc.DEVNULL, stdout=spc.PIPE, shell=True
|
||||||
|
)
|
||||||
|
|
||||||
|
with out.open("wb") as out_file:
|
||||||
|
for line in process.stdout:
|
||||||
|
if line.startswith(b"#"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
out_file.write(line)
|
||||||
|
|
||||||
|
|
||||||
|
def build(directory: Path, /) -> None:
|
||||||
|
for path in directory.iterdir():
|
||||||
|
if any(
|
||||||
|
part in IGNORE_DIRS
|
||||||
|
for part in (path.parts[:-1] if path.is_file() else path.parts)
|
||||||
|
):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if path.is_dir():
|
||||||
|
build(path)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not should_copy(path):
|
||||||
|
continue
|
||||||
|
|
||||||
|
path_build_path = BUILD_DIR / path.relative_to(PWD)
|
||||||
|
path_build_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
if should_process(path):
|
||||||
|
print("Building", end="")
|
||||||
|
process_file(path, path_build_path)
|
||||||
|
else:
|
||||||
|
print("Copying ", end="")
|
||||||
|
shutil.copyfile(path, path_build_path)
|
||||||
|
|
||||||
|
print(f" {path}...")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
shutil.rmtree("build", ignore_errors=True)
|
||||||
|
os.makedirs("build")
|
||||||
|
|
||||||
|
build(PWD)
|
||||||
|
print()
|
||||||
|
print("Building finished!")
|
19
build.sh
19
build.sh
|
@ -1,19 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
|
|
||||||
function process {
|
|
||||||
cc -E - < "$1"
|
|
||||||
}
|
|
||||||
|
|
||||||
rm -rf ./build
|
|
||||||
mkdir ./build
|
|
||||||
|
|
||||||
for html_file in $(find -name "*.html"); do
|
|
||||||
path=./build/${html_file#./}
|
|
||||||
|
|
||||||
# Preprocess if filename doesn't start with _.
|
|
||||||
if [[ $(basename $html_file) != _* ]]; then
|
|
||||||
echo "Building $html_file..."
|
|
||||||
mkdir $(dirname $path) 2> /dev/null
|
|
||||||
sed "/^#/d" <(process $html_file) > $path
|
|
||||||
fi
|
|
||||||
done
|
|
Loading…
Add table
Add a link
Reference in a new issue