Remove the old bash based include fixer with a better python version
ci/woodpecker/push/build Pipeline was successful Details
ci/woodpecker/push/docs Pipeline was successful Details

This commit is contained in:
Sameer Rahmani 2023-07-25 22:24:26 +01:00
parent 7fb2125c4b
commit 48e97095c7
Signed by: lxsameer
GPG Key ID: B0A4AF28AB9FD90B
3 changed files with 56 additions and 54 deletions

View File

@ -18,13 +18,6 @@ repos:
- id: trailing-whitespace
- id: mixed-line-ending
# - repo: local
# hooks:
# - id: include-fixer
# name: Fixing 'serene' includes
# language: script
# entry: ./scripts/include-fixer.sh
# files: ".*.(h|cpp)"
- repo: local
hooks:
- id: include-fixer py

56
scripts/include-fixer.py Executable file
View File

@ -0,0 +1,56 @@
#! /usr/bin/env python
import sys
import re
import fileinput
from pathlib import Path
target_header_pattern = re.compile(r"\#include [<\"](.+)[\">](.*)$", re.M)
if __name__ == "__main__":
# In the `.pre-commit-config.yaml` configuration we have set:
# files: ".*.(h|cpp)"
# for this file. So it will only receive cpp and h files. No need to
# fiter.
files = sys.argv[1:]
src_dir = Path(__file__).parent.parent / "serene" / "src"
headers_files = (src_dir).rglob("*.h")
headers = []
for h in headers_files:
headers.append(str(h.relative_to(src_dir)))
# Loop over every line in every input file and write lines
# one by one and modify any file that needs to be rewritten
for line in fileinput.input(files=files, inplace=True):
m = re.match(target_header_pattern, line)
if m:
header = m.group(1)
rest = m.group(2)
if header in headers or header.startswith("serene/"):
print(f"#include \"{header}\"{rest}")
else:
print(f"#include <{header}>{rest}")
else:
print(line, end='')
# # For debugging purposes I leave this here.
# for f in files:
# with open(f, "r") as fd:
# lines = fd.readlines()
# for line in lines:
# m = re.match(target_header_pattern, line)
# if m:
# header = m.group(1)
# print("header: ", header)
# rest = m.group(2)
# if header in headers or header == "serene/config.h":
# print(f"#include \"{header}\"{rest}")
# else:
# print(f"#include <{header}>{rest}")
# else:
# print(line, end='')
# raise TypeError()

View File

@ -1,47 +0,0 @@
#! /bin/bash
set -e
# function get_changed_files() {
# local add_modified
# local renamed
# add_modified=$(git diff --cached --name-status |egrep '*\.(cpp|h|hpp|cpp.inc|h.inc)'|awk '($1 != "D" && $1 !~ /R.+/) { print $2 }')
# renamed=$(git diff --cached --name-status |egrep '*\.(cpp|h|hpp|cpp.inc|h.inc)'|awk '$1 ~ /^R.+/ { print $3 }')
# echo $add_modified | sed '/^[[:space:]]*$/d'
# echo $renamed | sed '/^[[:space:]]*$/d'
# }
function fix_includes(){
#local files=$(get_changed_files)
echo "Fixing '#include' syntax in '$1'"
sed -i -E '/^#include "(serene|\.)/!s/^.include \"(.*)\"/#include <\1>/g' "$1"
}
# function clang_format_staged() {
# local files=$(get_changed_files)
# if [[ "$files" ]];
# then
# for file in $(get_changed_files)
# do
# echo "Reformatting $file..."
# clang-format -i $file
# done
# fi
# }
# function git_add_changes() {
# local files=$(get_changed_files)
# if [[ "$files" ]];
# then
# git add $files
# fi
# }
fix_includes "$1"
# clang_format_staged
# git_add_changes