xb - 前后端打包构建工具
xb(PyPI: xb-init)是一个基于 uv 又类似 uv 的项目管理工具,专为快速初始化 UV + FastAPI + Vue3 + Electron 桌面应用而生。一条命令拉起完整工程脚手架,开发、构建、版本管理一站式搞定。
核心特性
- 一键初始化完整项目结构(
xb init demo) - UV + FastAPI + Vue3 + Electron 开箱即用
- 自动安装前端和 Electron 依赖(
npm install,内置国内镜像源) - 自动
git init并提交首个 commit(含 lock 文件) - 自动生成
AGENTS.md(AI 编码助手协作约定) - 内置全局亮色/暗色主题切换
- 可选 sudo 免密配置(
--sudoers) - 可选内置终端(
--terminal) - 可选自定义应用图标(
--icon) - 内置开发、构建、版本管理命令
- 自动版本检查与一键升级(
xb --upgrade) - 环境诊断(
xb doctor) - Linux DEB 与 Windows NSIS 安装支持
安装
# 安装 uv(若已有可跳过)
curl -LsSf https://astral.sh/uv/install.sh | sh
# 方式一:从 PyPI 安装(推荐)
uv tool install xb-init
# 方式二:从源码安装
git clone -b release_v0 https://github.com/xuebli/xb.git
cd xb
uv sync
uv tool install .
# 验证
xb --help
快速开始
# 创建项目
xb init demo
# 带 sudo 免密配置
xb init demo --sudoers
# 带内置终端
xb init demo --terminal
# 同时启用终端和 sudo 免密配置
xb init demo --terminal --sudoers
# 带自定义图标
xb init demo --icon ~/icons/app.png
cd demo
# 启动开发环境
xb dev
# 查看状态
xb dev status
# 停止
xb dev stop
Windows PowerShell 也可以直接运行生成项目脚本:
Set-Location demo
python .\dev.py start
python .\dev.py status
python .\dev.py stop
使用 --terminal 时,Windows 终端使用 PowerShell + ConPTY,支持上下箭头
命令历史、复制粘贴、Tab 补全和 Ctrl+C;Ubuntu/Debian 使用 bash + Unix PTY。
Electron 二进制镜像通过 ELECTRON_MIRROR 环境变量注入,不向 .npmrc
写 npm 不识别的自定义键。若安装过程被网络中断,生成项目的
electron/prepare.js 会在 npm start 或 npm run build 前自动检查并补装
缺失的 Electron 二进制。
xb init自动行为:
- 执行
npm install(frontend + electron),生成package-lock.json
- 生成的
.npmrc已配好华为云镜像;electron 约 200MB 二进制也走镜像下载- 若安装失败或超时,会打印提示但不中断项目创建,可稍后手动重试
- 执行
git init并提交首个 commit(包含所有文件和 lock 文件)- 若检测到 PyPI 有新版 xb,会询问是否先升级再创建项目
- 若系统未安装 git 或未配置
user.name/user.email,打印警告但不阻塞
命令一览
| 命令 | 说明 |
|---|---|
xb init <name> [--sudoers] [--terminal] [--icon PATH] |
初始化项目 |
xb dev [start|stop|status] |
启动/停止/查看开发环境 |
xb build [all|frontend|backend|electron] |
构建项目 |
xb build -f / -b / -e / -a |
构建快捷 flag |
xb version [patch|minor|major] |
读取或更新版本号 |
xb doctor |
检查开发环境 |
xb --upgrade |
升级 xb 到 PyPI 最新版本 |
应用图标
# 显式指定图标
xb init demo --icon ./my-icon.png
# 不指定时自动查找以下约定路径:
# ./app-icon.png, ./icon.png, ./<package>.png,
# ./assets/app-icon.png, ./assets/icon.png, ./resources/icon.png
生成的项目结构
demo/
├── pyproject.toml # Python 依赖(uv 管理)
├── AGENTS.md # AI 编码助手协作约定
├── README.md # 项目说明(含三种启动方式详解)
├── .gitignore
├── backend/ # FastAPI 后端
│ ├── main.py # FastAPI 入口(lifespan / CORS / SPA 兜底)
│ ├── backend_build.py # PyInstaller 打包脚本
│ ├── api/ # API 路由层
│ └── managers/ # 基础设施单例
├── frontend/ # Vue 3 前端
│ ├── package.json
│ ├── package-lock.json
│ ├── .npmrc # npm 镜像源(华为云)
│ ├── vite.config.js
│ └── src/
├── electron/ # Electron 主进程
│ ├── package.json
│ ├── package-lock.json
│ ├── .npmrc # npm + electron 二进制镜像源
│ ├── main.js
│ └── resources/ # icon + Linux DEB hooks
├── version/ # 版本管理(pre-commit hook)
├── configs/ # 配置文件
├── datas/ # 运行时数据(gitignore)
├── dev.py # 开发脚本(Windows/Linux)
├── build.py # 打包脚本(Windows/Linux)
└── .venv # Python 虚拟环境
xb 工具自身结构
xb/
├── cli.py # CLI 入口(Click + Rich)
├── __init__.py # 版本号
├── commands/ # 子命令实现
│ ├── init.py # 初始化(升级检查、图标、npm install、git init)
│ ├── dev.py # 开发环境管理
│ ├── build.py # 构建
│ ├── version.py # 版本管理
│ ├── doctor.py # 环境诊断
│ └── upgrade.py # 升级 xb
├── templates/ # Jinja2 项目模板
│ ├── backend/
│ ├── frontend/
│ ├── electron/
│ ├── configs/
│ ├── scripts/
│ ├── version/
│ └── root/
└── utils/
├── template_engine.py # 模板渲染引擎
├── validators.py # 包名校验
├── click_helpers.py # 中文化 Help 输出
└── version_check.py # PyPI 版本检查(24h 缓存)
环境要求
- Python: 3.12+
- Node.js: 16+
- npm: 8+
- uv: 已安装
- OS: Windows 10/11 或 Ubuntu/Debian
- Windows 终端: 使用
--terminal时自动安装 Windows 专用pywinpty
Windows 可使用 Python、Node.js、npm 和 uv 的官方安装方式或 winget 安装。
Ubuntu/Debian 可使用系统包管理器或官方安装方式。
使用 xb doctor 可一键检查所有环境依赖。
许可证
MIT
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xb_init-2.0.3.tar.gz.
File metadata
- Download URL: xb_init-2.0.3.tar.gz
- Upload date:
- Size: 4.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78e120c402f85c07e23166e78d8518393b4b5e8cb7cce1f5384ddd0427c5d536
|
|
| MD5 |
ef85baa0b81da8462fef8b5b7aa8b49d
|
|
| BLAKE2b-256 |
7277157a4f25bfba60ac0f4b5067b788669e7c69d1a08f16b48a655872fe5f9b
|
File details
Details for the file xb_init-2.0.3-py3-none-any.whl.
File metadata
- Download URL: xb_init-2.0.3-py3-none-any.whl
- Upload date:
- Size: 124.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7e6b6c8c7757bb6fff96cf13a97fae149c283df8667d3a02e98b3555183797e
|
|
| MD5 |
bad63581efbc3511539eb400017f28b4
|
|
| BLAKE2b-256 |
5d129b0cff3e4e7dce5f725a983e0fe45ae22717675973f3fc6af5b3809b25ff
|