Skip to content

Commit 3a2249a

Browse files
committed
feat(module): 自动为新模块生成 GitHub Actions CI 工作流.
1 parent 25c673c commit 3a2249a

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "xrobot"
7-
version = "0.2.0"
7+
version = "0.2.1"
88
description = "A lightweight application framework for robot module management, lifecycle control, and hardware abstraction."
99
requires-python = ">=3.8"
1010
authors = [

src/xrobot/ModuleCreator.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,130 @@ class {class_name} : public LibXR::Application {{
4949
{hardware}
5050
"""
5151

52+
GITHUB_ACTIONS_WORKFLOW = """name: XRobot Module Build Test
53+
54+
on:
55+
push:
56+
pull_request:
57+
schedule:
58+
- cron: '0 3 1 * *' # 每月1日凌晨3点(UTC)自动触发
59+
60+
jobs:
61+
build:
62+
runs-on: ubuntu-latest
63+
container:
64+
image: ghcr.io/xrobot-org/docker-image-linux:main
65+
options: --user 0
66+
67+
env:
68+
XR_MODULE_NAME: ${{ github.event.repository.name }}
69+
70+
steps:
71+
- name: Checkout current module repo to ./Modules/${{ env.XR_MODULE_NAME }}
72+
uses: actions/checkout@v4
73+
with:
74+
path: Modules/${{ env.XR_MODULE_NAME }}
75+
76+
- name: Create main.cpp
77+
run: |
78+
cat > main.cpp <<'EOF'
79+
#include <iostream>
80+
#include "xrobot_main.hpp"
81+
#include "libxr.hpp"
82+
83+
int main() {
84+
LibXR::PlatformInit();
85+
LibXR::STDIO::Printf("This is XRobot Module Template Test\\n");
86+
LibXR::HardwareContainer hw;
87+
XRobotMain(hw);
88+
return 0;
89+
}
90+
EOF
91+
92+
- name: Create minimal CMakeLists.txt
93+
run: |
94+
cat > CMakeLists.txt <<'EOF'
95+
project(xrobot_mod_test CXX)
96+
set(CMAKE_CXX_STANDARD 17)
97+
add_executable(xr_test main.cpp)
98+
add_subdirectory(libxr)
99+
target_include_directories(xr_test PUBLIC $<TARGET_PROPERTY:xr,INTERFACE_INCLUDE_DIRECTORIES> ${CMAKE_SOURCE_DIR}/User)
100+
target_link_libraries(xr_test PUBLIC xr)
101+
include(Modules/CMakeLists.txt)
102+
EOF
103+
104+
- name: Pull libxr to ./libxr
105+
run: git clone --depth=1 https://github.com/Jiu-xiao/libxr ./libxr
106+
107+
- name: Setup Python & Install deps
108+
run: |
109+
python3 -m pip install --upgrade pip
110+
pip3 install pyyaml requests
111+
112+
- name: Add XRobot tools to PATH
113+
run: |
114+
echo "$HOME/.local/bin" >> $GITHUB_PATH
115+
116+
- name: Install xrobot toolchain (assumes pyproject/tar.gz/pip install .)
117+
run: |
118+
pip3 install xrobot libxr
119+
120+
- name: Run xrobot_setup
121+
run: |
122+
xrobot_setup || true
123+
124+
- name: Run xrobot_init_mod
125+
run: |
126+
xrobot_init_mod
127+
128+
- name: Add BlinkLED module
129+
run: |
130+
xrobot_add_mod BlinkLED --instance-id BlinkLED_0
131+
132+
- name: Add this repo module
133+
run: |
134+
xrobot_add_mod ${{ env.XR_MODULE_NAME }} && cat User/xrobot.yaml
135+
136+
- name: Generate main again
137+
run: |
138+
xrobot_setup
139+
140+
- name: Build
141+
run: |
142+
mkdir -p build
143+
cd build
144+
cmake ..
145+
make
146+
147+
- name: Create Tag via GitHub API
148+
if: ${{ success() && github.event_name != 'pull_request' }}
149+
uses: actions/github-script@v7
150+
with:
151+
github-token: ${{ secrets.GITHUB_TOKEN }}
152+
script: |
153+
const tagPrefix = 'auto-'
154+
const date = new Date();
155+
const yyyy = date.getUTCFullYear();
156+
const mm = String(date.getUTCMonth() + 1).padStart(2, '0');
157+
const dd = String(date.getUTCDate()).padStart(2, '0');
158+
const HH = String(date.getUTCHours()).padStart(2, '0');
159+
const MM = String(date.getUTCMinutes()).padStart(2, '0');
160+
const SS = String(date.getUTCSeconds()).padStart(2, '0');
161+
const tagName = `${tagPrefix}${yyyy}${mm}${dd}-${HH}${MM}${SS}`;
162+
163+
// 获取当前 commit sha
164+
const sha = process.env.GITHUB_SHA;
165+
166+
// 创建 tag reference
167+
await github.rest.git.createRef({
168+
owner: context.repo.owner,
169+
repo: context.repo.repo,
170+
ref: `refs/tags/${tagName}`,
171+
sha: sha
172+
});
173+
console.log(`Created tag: ${tagName} on sha: ${sha}`);
174+
"""
175+
52176
def yaml_block(obj: Any, key: str, indent: int = 2) -> str:
53177
"""
54178
Output a compliant YAML field block:
@@ -145,6 +269,11 @@ def create_module(
145269
"""
146270
(mod_dir / "CMakeLists.txt").write_text(cmake_code, encoding="utf-8")
147271

272+
# Generate GitHub Actions workflow
273+
workflow_dir = mod_dir / ".github" / "workflows"
274+
workflow_dir.mkdir(parents=True, exist_ok=True)
275+
(workflow_dir / "build.yml").write_text(GITHUB_ACTIONS_WORKFLOW, encoding="utf-8")
276+
148277
print(f"[OK] Module {class_name} generated at {mod_dir}")
149278

150279
def main():

0 commit comments

Comments
 (0)