Periphery终极部署指南:Docker和Bazel构建的完整说明
Periphery终极部署指南Docker和Bazel构建的完整说明【免费下载链接】peripheryA tool to identify unused code in Swift projects.项目地址: https://gitcode.com/gh_mirrors/pe/peripheryPeriphery是一款强大的Swift代码分析工具专门用于识别Swift项目中的未使用代码。作为Swift开发者的代码清理利器它能够帮助开发者发现冗余的类、协议、函数和属性从而优化代码库并提升应用性能。本文将详细介绍如何使用Docker容器化部署和Bazel构建系统来高效运行Periphery让您的Swift项目代码保持整洁高效。为什么选择Periphery进行代码分析在Swift开发过程中随着项目不断迭代代码库中往往会积累大量未使用的代码。这些僵尸代码不仅增加了编译时间还降低了代码可维护性。Periphery通过构建项目的索引存储index store来分析代码间的引用关系能够准确识别以下类型的未使用代码未使用的类、结构体和协议冗余的函数参数从未被调用的枚举case仅赋值但未使用的属性不必要的public访问修饰符未使用的import语句Docker部署快速启动PeripheryPeriphery提供了两种Docker镜像分别支持不同的构建系统1. Linux基础镜像Swift Package Manager最简单的部署方式是通过官方Docker镜像运行Periphery# 拉取最新镜像 docker pull peripheryapp/periphery:latest # 运行扫描 docker run -v $(pwd):/workspace peripheryapp/periphery:latest scan --project /workspace项目中的Dockerfile-Linux展示了基础镜像的构建过程FROM swift:latest WORKDIR /workspace COPY . /workspace RUN swift build --product periphery ENTRYPOINT [.build/debug/periphery]2. Bazel集成镜像对于使用Bazel构建系统的项目Periphery提供了专门的Bazel镜像# 使用Bazel镜像扫描 docker run -v $(pwd):/workspace peripheryapp/periphery:bazel scan --bazel查看Dockerfile-Bazel可以看到完整的构建配置FROM swift:latest RUN apt-get update apt-get install -y curl RUN curl -Lo /usr/local/bin/bazelisk https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-arm64 WORKDIR /workspace COPY . /workspace ENV CCclang RUN bazel build //:peripheryBazel构建系统深度集成Periphery与Bazel的集成非常紧密提供了原生的构建支持Bazel规则定义在bazel/rules.bzl中定义了Periphery的Bazel规则scan rule( doc Scans the top-level deps and their transitive deps for unused code., attrs { deps: attr.label_list( cfg force_indexstore, mandatory True, aspects [scan_inputs_aspect], doc Top-level project targets to scan., ), config: attr.string(doc Path to the periphery.yml configuration file.), global_indexstore: attr.string(doc Path to a global index store.), periphery: attr.label( doc The periphery executable target., default periphery//:periphery, ), }, executable True, )使用Bazel扫描项目在您的Bazel项目中可以这样配置Periphery扫描# WORKSPACE文件配置 load(bazel_tools//tools/build_defs/repo:http.bzl, http_archive) http_archive( name periphery, sha256 ..., urls [https://github.com/peripheryapp/periphery/releases/download/v2.0.0/periphery-v2.0.0.tar.gz], ) load(periphery//bazel:generated.bzl, generated) use_extension(generated, periphery_generated)# BUILD.bazel文件配置 load(periphery//bazel:rules.bzl, scan) scan( name unused_code_scan, deps [ //src:main_target, //src:library_target, ], config periphery.yml, )执行扫描命令使用Bazel运行扫描非常简单# 基本扫描 bazel run periphery -- scan --bazel # 带过滤条件的扫描 bazel run periphery -- scan --bazel --bazel-filter //src/... # 查看详细日志 bazel run periphery -- scan --bazel --verboseXcode项目集成指南对于Xcode项目Periphery提供了完整的集成方案。以下是在Xcode中设置Periphery的完整步骤创建聚合目标首先在Xcode中创建聚合目标Aggregate Target这是集成外部工具的标准方法选择Other分类然后选择Aggregate点击Next继续。配置目标名称为目标命名并选择所属项目输入目标名称如Periphery并选择对应的项目。添加运行脚本阶段在Build Phases中添加Run Script Phase点击按钮选择New Run Script Phase。配置扫描脚本在脚本区域输入Periphery扫描命令脚本示例xcodebuild scan --project Example.xcodeproj --schemes Example --format xcode --destination generic/platformiOS-Simulator禁用脚本沙箱为了允许脚本访问项目文件需要禁用用户脚本沙箱在Build Settings中将ENABLE_USER_SCRIPT_SANDBOXING设置为No。运行扫描选择新建的Periphery方案并运行在Scheme菜单中选择Periphery目标点击运行按钮开始扫描。配置选项详解Periphery提供了丰富的配置选项可以通过YAML文件进行持久化配置基本配置示例创建.periphery.yml文件project: YourProject.xcodeproj schemes: - YourScheme targets: - YourTarget output_format: xcode retain_public: false retain_objc_accessible: true exclude: - **/Generated/*.swift - **/*.generated.swiftDocker环境配置在Docker环境中运行时需要考虑以下特殊配置# Docker专用配置 skip_build: true index_store_path: /workspace/.build/debug/index/store strict: true quiet: trueBazel专用选项使用Bazel构建系统时的额外配置# Bazel配置 bazel: true bazel_check_visibility: false bazel_filter: //src/...持续集成(CI)集成将Periphery集成到CI/CD流程中可以确保代码质量GitHub Actions示例name: Code Quality on: [push, pull_request] jobs: scan: runs-on: ubuntu-latest steps: - uses: actions/checkoutv3 - name: Setup Swift uses: swift-actions/setup-swiftv1 with: swift-version: 5.9 - name: Run Periphery run: | docker run --rm -v ${{ github.workspace }}:/workspace \ peripheryapp/periphery:latest \ scan --project /workspace/YourProject.xcodeproj \ --schemes YourScheme \ --format github-actionsBazel CI配置name: Bazel Build and Scan on: [push, pull_request] jobs: bazel-scan: runs-on: ubuntu-latest steps: - uses: actions/checkoutv3 - name: Setup Bazel uses: bazelbuild/setup-bazeliskv2 - name: Run Periphery with Bazel run: | bazel run periphery -- scan --bazel \ --format checkstyle \ --output scan-results.xml最佳实践和性能优化1. 增量扫描优化对于大型项目可以使用增量扫描来提高性能# 使用缓存目录 periphery scan --cache-path .periphery-cache --project YourProject.xcodeproj # 只扫描变更文件 periphery scan --changed-files-only --project YourProject.xcodeproj2. 结果过滤策略根据项目需求定制扫描结果# 高级过滤配置 retain_files: - **/Protocols/*.swift - **/Models/*.swift report_exclude: - **/ThirdParty/** - **/Generated/** retain_assign_only_property_types: - String - [String] - SetString3. Docker镜像优化创建自定义Docker镜像以获得最佳性能# 自定义Periphery镜像 FROM swift:5.9-focal # 安装依赖 RUN apt-get update apt-get install -y \ clang \ libsqlite3-dev \ rm -rf /var/lib/apt/lists/* # 预构建Periphery WORKDIR /app COPY . . RUN swift build -c release --product periphery # 设置入口点 ENTRYPOINT [.build/release/periphery]故障排除和常见问题Docker环境问题问题Docker容器中无法访问Xcode项目文件解决方案确保正确挂载卷并使用绝对路径# 正确挂载方式 docker run -v /absolute/path/to/project:/workspace \ peripheryapp/periphery:latest \ scan --project /workspace/Project.xcodeprojBazel构建问题问题Bazel扫描时权限错误解决方案检查Bazel缓存权限# 清理Bazel缓存 bazel clean --expunge # 重新运行扫描 bazel run periphery -- scan --bazel --clean-build索引存储问题问题扫描结果不准确解决方案强制重新构建索引# 清理并重新构建 periphery scan --clean-build --project YourProject.xcodeproj # 或者手动删除索引 rm -rf ~/Library/Developer/Xcode/DerivedData/YourProject-*/Index.noindex总结Periphery作为Swift项目的代码质量守护者通过Docker和Bazel的现代化部署方式为团队提供了灵活高效的代码分析解决方案。无论是本地开发环境还是CI/CD流水线Periphery都能无缝集成帮助您自动化代码清理- 定期扫描并清除未使用代码提升代码质量- 发现潜在的设计问题和冗余代码优化构建性能- 减少编译时间和二进制大小统一团队标准- 通过CI/CD确保代码规范一致性通过本文介绍的Docker容器化部署和Bazel构建集成方案您可以轻松地将Periphery集成到现有的开发工作流中让代码质量维护变得简单高效。官方文档docs/official.md核心源码Sources/Frontend/Docker配置docker/Bazel规则bazel/rules.bzl开始使用Periphery让您的Swift项目代码始终保持整洁和高效【免费下载链接】peripheryA tool to identify unused code in Swift projects.项目地址: https://gitcode.com/gh_mirrors/pe/periphery创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2452508.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!