Skip to content

Commit 6eeb13a

Browse files
Update GitHub Actions script and create a modules updating script
1 parent 9e4ca69 commit 6eeb13a

File tree

4 files changed

+437
-52
lines changed

4 files changed

+437
-52
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: Update Module Exports
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'httplib.h'
7+
push:
8+
branches:
9+
- master
10+
- main
11+
paths:
12+
- 'httplib.h'
13+
14+
jobs:
15+
update-exports:
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0 # Fetch all history for proper diff
23+
24+
- name: Set up Python
25+
uses: actions/setup-python@v5
26+
with:
27+
python-version: '3.11'
28+
29+
- name: Check for changes in httplib.h
30+
id: check_changes
31+
run: |
32+
if git diff --name-only HEAD~1 HEAD | grep -q "httplib.h"; then
33+
echo "changes=true" >> $GITHUB_OUTPUT
34+
else
35+
echo "changes=false" >> $GITHUB_OUTPUT
36+
fi
37+
38+
- name: Update module exports
39+
if: steps.check_changes.outputs.changes == 'true'
40+
run: |
41+
python3 update_modules.py
42+
43+
- name: Check if module file was modified
44+
if: steps.check_changes.outputs.changes == 'true'
45+
id: check_module_changes
46+
run: |
47+
if git diff --quiet modules/httplib.cppm; then
48+
echo "modified=false" >> $GITHUB_OUTPUT
49+
else
50+
echo "modified=true" >> $GITHUB_OUTPUT
51+
fi
52+
53+
- name: Commit changes
54+
if: steps.check_module_changes.outputs.modified == 'true'
55+
run: |
56+
git config user.name "github-actions[bot]"
57+
git config user.email "github-actions[bot]@users.noreply.github.com"
58+
git add modules/httplib.cppm
59+
git commit -m "chore: update module exports for httplib.h changes"
60+
61+
- name: Push changes (for push events)
62+
if: steps.check_module_changes.outputs.modified == 'true' && github.event_name == 'push'
63+
run: |
64+
git push
65+
66+
- name: Push changes (for pull requests)
67+
if: steps.check_module_changes.outputs.modified == 'true' && github.event_name == 'pull_request'
68+
run: |
69+
git push origin HEAD:${{ github.head_ref }}
70+
71+
- name: Add comment to PR
72+
if: steps.check_module_changes.outputs.modified == 'true' && github.event_name == 'pull_request'
73+
uses: actions/github-script@v7
74+
with:
75+
script: |
76+
github.rest.issues.createComment({
77+
issue_number: context.issue.number,
78+
owner: context.repo.owner,
79+
repo: context.repo.repo,
80+
body: '✅ Module exports have been automatically updated based on changes to `httplib.h`.'
81+
})

modules/httplib.cppm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// httplib.h
2+
// httplib.cppm
33
//
44
// Copyright (c) 2025 Yuji Hirose. All rights reserved.
55
// MIT License

split.py

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,74 @@
22

33
"""This script splits httplib.h into .h and .cc parts."""
44

5-
import argparse
65
import os
76
import sys
7+
from argparse import ArgumentParser, Namespace
8+
from typing import List
89

9-
border = '// ----------------------------------------------------------------------------'
1010

11-
args_parser = argparse.ArgumentParser(description=__doc__)
12-
args_parser.add_argument(
13-
"-e", "--extension", help="extension of the implementation file (default: cc)",
14-
default="cc"
15-
)
16-
args_parser.add_argument(
17-
"-o", "--out", help="where to write the files (default: out)", default="out"
18-
)
19-
args = args_parser.parse_args()
11+
def main() -> None:
12+
"""Main entry point for the script."""
13+
BORDER: str = '// ----------------------------------------------------------------------------'
2014

21-
cur_dir = os.path.dirname(sys.argv[0])
22-
lib_name = 'httplib'
23-
header_name = '/' + lib_name + '.h'
24-
source_name = '/' + lib_name + '.' + args.extension
25-
# get the input file
26-
in_file = cur_dir + header_name
27-
# get the output file
28-
h_out = args.out + header_name
29-
cc_out = args.out + source_name
15+
args_parser: ArgumentParser = ArgumentParser(description=__doc__)
16+
args_parser.add_argument(
17+
"-e", "--extension", help="extension of the implementation file (default: cc)",
18+
default="cc"
19+
)
20+
args_parser.add_argument(
21+
"-o", "--out", help="where to write the files (default: out)", default="out"
22+
)
23+
args: Namespace = args_parser.parse_args()
3024

31-
# if the modification time of the out file is after the in file,
32-
# don't split (as it is already finished)
33-
do_split = True
25+
cur_dir: str = os.path.dirname(sys.argv[0])
26+
lib_name: str = 'httplib'
27+
header_name: str = f"/{lib_name}.h"
28+
source_name: str = f"/{lib_name}.{args.extension}"
29+
# get the input file
30+
in_file: str = cur_dir + header_name
31+
# get the output file
32+
h_out: str = args.out + header_name
33+
cc_out: str = args.out + source_name
3434

35-
if os.path.exists(h_out):
36-
in_time = os.path.getmtime(in_file)
37-
out_time = os.path.getmtime(h_out)
38-
do_split = in_time > out_time
35+
# if the modification time of the out file is after the in file,
36+
# don't split (as it is already finished)
37+
do_split: bool = True
3938

40-
if do_split:
41-
with open(in_file) as f:
42-
lines = f.readlines()
39+
if os.path.exists(h_out):
40+
in_time: float = os.path.getmtime(in_file)
41+
out_time: float = os.path.getmtime(h_out)
42+
do_split: bool = in_time > out_time
4343

44-
python_version = sys.version_info[0]
45-
if python_version < 3:
46-
os.makedirs(args.out)
44+
if do_split:
45+
with open(in_file) as f:
46+
lines: List[str] = f.readlines()
47+
48+
python_version: int = sys.version_info[0]
49+
if python_version < 3:
50+
os.makedirs(args.out)
51+
else:
52+
os.makedirs(args.out, exist_ok=True)
53+
54+
in_implementation: bool = False
55+
cc_out: str = args.out + source_name
56+
with open(h_out, 'w') as fh, open(cc_out, 'w') as fc:
57+
fc.write('#include "httplib.h"\n')
58+
fc.write('namespace httplib {\n')
59+
for line in lines:
60+
is_border_line: bool = BORDER in line
61+
if is_border_line:
62+
in_implementation: bool = not in_implementation
63+
elif in_implementation:
64+
fc.write(line.replace('inline ', ''))
65+
else:
66+
fh.write(line)
67+
fc.write('} // namespace httplib\n')
68+
69+
print(f"Wrote {h_out} and {cc_out}")
4770
else:
48-
os.makedirs(args.out, exist_ok=True)
71+
print(f"{h_out} and {cc_out} are up to date")
4972

50-
in_implementation = False
51-
cc_out = args.out + source_name
52-
with open(h_out, 'w') as fh, open(cc_out, 'w') as fc:
53-
fc.write('#include "httplib.h"\n')
54-
fc.write('namespace httplib {\n')
55-
for line in lines:
56-
is_border_line = border in line
57-
if is_border_line:
58-
in_implementation = not in_implementation
59-
elif in_implementation:
60-
fc.write(line.replace('inline ', ''))
61-
else:
62-
fh.write(line)
63-
fc.write('} // namespace httplib\n')
6473

65-
print("Wrote {} and {}".format(h_out, cc_out))
66-
else:
67-
print("{} and {} are up to date".format(h_out, cc_out))
74+
if __name__ == "__main__":
75+
main()

0 commit comments

Comments
 (0)