返回

CLI-Anything 教程 8:第七阶段 Publish(PyPI 发布与迭代优化)

CLI-Anything 第七阶段 Publish 详解:如何打包发布到 PyPI、使用 refine 命令迭代优化 CLI、版本管理和持续改进策略。

教程概述

系列目录:CLI-Anything 教程系列索引

Publish 是 CLI-Anything 7 阶段流程的第七阶段。这一阶段的核心任务是发布 CLI 包,并建立持续优化的流程。

发布准备

setup.py 配置

# setup.py

from setuptools import setup, find_packages

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

setup(
    name="cli-anything-gimp",
    version="1.0.0",
    author="Your Name",
    author_email="[email protected]",
    description="GIMP CLI for AI Agents",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/yourusername/cli-anything-gimp",
    packages=find_packages(),
    classifiers=[
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Programming Language :: Python :: 3.12",
    ],
    python_requires=">=3.10",
    install_requires=[
        "click>=8.0",
        "pydantic>=2.0",
    ],
    entry_points={
        "console_scripts": [
            "cli-anything-gimp=cli_anything.gimp.cli:main",
        ],
    },
    include_package_data=True,
    package_data={
        "cli_anything.gimp": ["templates/*", "data/*"],
    },
)

pyproject.toml 配置

# pyproject.toml

[build-system]
requires = ["setuptools>=45", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "cli-anything-gimp"
version = "1.0.0"
description = "GIMP CLI for AI Agents"
readme = "README.md"
license = {text = "MIT"}
authors = [
    {name = "Your Name", email = "[email protected]"}
]
classifiers = [
    "Development Status :: 4 - Beta",
    "License :: OSI Approved :: MIT License",
    "Programming Language :: Python :: 3.10",
]
requires-python = ">=3.10"
dependencies = [
    "click>=8.0",
    "pydantic>=2.0",
]

[project.scripts]
cli-anything-gimp = "cli_anything.gimp.cli:main"

[project.optional-dependencies]
dev = [
    "pytest>=7.0",
    "pytest-cov>=4.0",
    "black>=22.0",
    "flake8>=5.0",
]

发布流程

本地测试安装

# 创建虚拟环境
python3 -m venv venv
source venv/bin/activate

# 本地安装测试
pip install -e .

# 验证安装
which cli-anything-gimp
cli-anything-gimp --help

# 运行测试
pytest

构建分发包

# 安装构建工具
pip install build twine

# 构建包
python -m build

# 检查包
twine check dist/*

# 上传到 Test PyPI
twine upload --repository testpypi dist/*

# 测试从 Test PyPI 安装
pip install --index-url https://test.pypi.org/simple/ cli-anything-gimp

# 正式发布到 PyPI
twine upload dist/*

Refine 迭代优化

为什么需要 Refine?

flowchart TD
    A[初始生成] --> B[功能覆盖 60%]
    B --> C[Refine 迭代]
    C --> D[功能覆盖 80%]
    D --> E[Refine 迭代]
    E --> F[功能覆盖 95%]

    style A fill:#ffcccc
    style B fill:#fff3e0
    style D fill:#e3f2fd
    style F fill:#e8f5e9

Refine 命令使用

# 广度优化 - 分析所有未覆盖功能
/cli-anything:refine ./gimp

# 深度优化 - 针对特定功能
/cli-anything:refine ./gimp "focus on layer masking and blend modes"

# 针对特定命令组
/cli-anything:refine ./gimp "add more filter commands"

# 优化测试覆盖
/cli-anything:refine ./gimp "improve test coverage for export module"

Refine 工作流程

flowchart TD
    A[Refine 启动] --> B[Gap Analysis]
    B --> C[识别缺失功能]
    C --> D[生成新命令]
    D --> E[编写测试]
    E --> F[更新文档]
    F --> G[验证完成]

    style B fill:#e3f2fd
    style C fill:#e3f2fd
    style D fill:#e8f5e9
    style E fill:#fff3e0
    style F fill:#f3e5f5

版本管理

语义化版本

版本变化 说明 示例
MAJOR 不兼容的 API 变更 1.0.0 → 2.0.0
MINOR 向下兼容的功能添加 1.0.0 → 1.1.0
PATCH 向下兼容的问题修复 1.0.0 → 1.0.1

版本迭代示例

v1.0.0 - 初始发布
  ├── v1.0.1 - Bug 修复
  ├── v1.1.0 - 新增批处理功能
  │     ├── v1.1.1 - 修复批处理内存泄漏
  │     └── v1.2.0 - 新增滤镜预设
  └── v2.0.0 - 重构 API(不兼容变更)

版本更新流程

# 1. 更新版本号
# 编辑 setup.py 或 pyproject.toml

# 2. 更新 CHANGELOG.md
# 记录变更内容

# 3. 创建 Git 标签
git add .
git commit -m "Release v1.1.0"
git tag v1.1.0
git push origin v1.1.0

# 4. 重新构建发布
python -m build
twine upload dist/*

持续优化策略

用户反馈收集

# 反馈收集清单

## 功能请求
- [ ] 用户请求的缺失功能
- [ ] 常见使用模式

## Bug 报告
- [ ] 崩溃报告
- [ ] 错误处理改进

## 性能优化
- [ ] 大文件处理
- [ ] 内存使用优化

覆盖率改进

# 检查当前覆盖率
pytest --cov=cli_anything --cov-report=term-missing

# 识别未覆盖代码
# 编写针对性测试

# 设定覆盖率目标
# 单元测试: 80%
# 核心模块: 90%

文档维护

# 检查文档链接
# 更新使用示例
# 同步 API 变更

小结

Publish 阶段完成 CLI 的发布和持续优化:

  1. PyPI 发布 - 打包安装,便于分发
  2. Refine 优化 - 迭代改进功能覆盖
  3. 版本管理 - 语义化版本控制
  4. 持续优化 - 根据反馈不断改进

系列导航